system.info.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "path/filepath"
  6. "runtime"
  7. "time"
  8. fs "imuslab.com/arozos/mod/filesystem"
  9. info "imuslab.com/arozos/mod/info/hardwareinfo"
  10. "imuslab.com/arozos/mod/info/logviewer"
  11. usage "imuslab.com/arozos/mod/info/usageinfo"
  12. prout "imuslab.com/arozos/mod/prouter"
  13. "imuslab.com/arozos/mod/updates"
  14. "imuslab.com/arozos/mod/utils"
  15. )
  16. // InitShowSysInformation xxx
  17. func SystemInfoInit() {
  18. systemWideLogger.PrintAndLog("System", "Operation System: "+runtime.GOOS, nil)
  19. systemWideLogger.PrintAndLog("System", "System Architecture: "+runtime.GOARCH, nil)
  20. //Updates 5 Dec 2020, Added permission router
  21. router := prout.NewModuleRouter(prout.RouterOption{
  22. ModuleName: "System Setting",
  23. AdminOnly: false,
  24. UserHandler: userHandler,
  25. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  26. utils.SendErrorResponse(w, "Permission Denied")
  27. },
  28. })
  29. //Anyone logged in can load router
  30. authRouter := prout.NewModuleRouter(prout.RouterOption{
  31. AdminOnly: false,
  32. UserHandler: userHandler,
  33. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  34. utils.SendErrorResponse(w, "Permission Denied")
  35. },
  36. })
  37. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  38. ModuleName: "System Setting",
  39. AdminOnly: true,
  40. UserHandler: userHandler,
  41. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  42. utils.SendErrorResponse(w, "Permission Denied")
  43. },
  44. })
  45. //Create Info Server Object
  46. var infoServer *info.Server = nil
  47. //Load the vendor icon
  48. vendorIconSrc := filepath.Join(vendorResRoot, "vendor_icon.png")
  49. if !fs.FileExists(vendorIconSrc) {
  50. vendorIconSrc = "./web/img/public/vendor_icon.png"
  51. }
  52. imageBase64, _ := utils.LoadImageAsBase64(vendorIconSrc)
  53. if *allow_hardware_management {
  54. infoServer = info.NewInfoServer(info.ArOZInfo{
  55. BuildVersion: build_version + "." + internal_version,
  56. DeviceVendor: deviceVendor,
  57. DeviceModel: deviceModel,
  58. VendorIcon: imageBase64,
  59. SN: deviceUUID,
  60. HostOS: runtime.GOOS,
  61. CPUArch: runtime.GOARCH,
  62. HostName: *host_name,
  63. })
  64. router.HandleFunc("/system/info/getCPUinfo", info.CachedGetCPUInfo)
  65. router.HandleFunc("/system/info/ifconfig", info.CachedIfconfig)
  66. router.HandleFunc("/system/info/getDriveStat", info.CachedGetDriveStat)
  67. router.HandleFunc("/system/info/usbPorts", info.CachedGetUSB)
  68. //For low-memory mode detection
  69. authRouter.HandleFunc("/system/info/getRAMinfo", info.CachedGetRamInfo)
  70. // Prime hardware info cache in the background.
  71. info.StartHostInfoCache()
  72. //Register as a system setting
  73. registerSetting(settingModule{
  74. Name: "Host Info",
  75. Desc: "System Information",
  76. IconPath: "SystemAO/info/img/host.png",
  77. Group: "Info",
  78. StartDir: "SystemAO/info/index.html",
  79. })
  80. /*
  81. CPU and RAM usage interface
  82. */
  83. registerSetting(settingModule{
  84. Name: "Performance",
  85. Desc: "System CPU and RAM usage",
  86. IconPath: "SystemAO/info/img/performance.png",
  87. Group: "Info",
  88. StartDir: "SystemAO/info/taskManagerFrame.html",
  89. })
  90. router.HandleFunc("/system/info/getUsageInfo", InfoHandleTaskInfo)
  91. // Sample CPU and RAM in the background so the endpoint is non-blocking.
  92. usage.StartBackgroundMonitor()
  93. } else {
  94. //Remve hardware information from the infoServer
  95. infoServer = info.NewInfoServer(info.ArOZInfo{
  96. BuildVersion: build_version + "." + internal_version,
  97. DeviceVendor: deviceVendor,
  98. DeviceModel: deviceModel,
  99. VendorIcon: imageBase64,
  100. SN: deviceUUID,
  101. HostOS: "virtualized",
  102. CPUArch: "generic",
  103. HostName: *host_name,
  104. })
  105. }
  106. //Register endpoints that do not involve hardware management
  107. authRouter.HandleFunc("/system/info/getRuntimeInfo", InfoHandleGetRuntimeInfo)
  108. authRouter.HandleFunc("/system/info/getLocaleInfo", InfoHandleGetLocaleInfo)
  109. //ArOZ Info do not need permission router
  110. http.HandleFunc("/system/info/getArOZInfo", infoServer.GetArOZInfo)
  111. //Router to handle login background image loading
  112. http.HandleFunc("/system/info/wallpaper.jpg", func(w http.ResponseWriter, r *http.Request) {
  113. imgsrc := filepath.Join(vendorResRoot, "auth_bg.jpg")
  114. if !fs.FileExists(imgsrc) {
  115. imgsrc = "./web/img/public/auth_bg.jpg"
  116. }
  117. http.ServeFile(w, r, imgsrc)
  118. })
  119. go func() {
  120. if updates.CheckLauncherPortResponsive() {
  121. //Launcher port is responsive. Assume launcher exists
  122. registerSetting(settingModule{
  123. Name: "Updates",
  124. Desc: "Perform ArozOS Updates",
  125. IconPath: "SystemAO/updates/img/update.png",
  126. Group: "Info",
  127. StartDir: "SystemAO/updates/index.html",
  128. RequireAdmin: true,
  129. })
  130. //Register Update Functions
  131. adminRouter.HandleFunc("/system/update/download", updates.HandleUpdateDownloadRequest)
  132. adminRouter.HandleFunc("/system/update/checksize", updates.HandleUpdateCheckSize)
  133. adminRouter.HandleFunc("/system/update/checkpending", updates.HandlePendingCheck)
  134. adminRouter.HandleFunc("/system/update/platform", updates.HandleGetUpdatePlatformInfo)
  135. //Special function for handling launcher restart, must be in this scope
  136. adminRouter.HandleFunc("/system/update/restart", func(w http.ResponseWriter, r *http.Request) {
  137. launcherVersion, err := updates.GetLauncherVersion()
  138. if err != nil {
  139. utils.SendErrorResponse(w, err.Error())
  140. return
  141. }
  142. execute, _ := utils.PostPara(r, "exec")
  143. if execute == "true" && r.Method == http.MethodPost {
  144. //Do the update
  145. systemWideLogger.PrintAndLog("System", "REQUESTING LAUNCHER FOR UPDATE RESTART", nil)
  146. executeShutdownSequence()
  147. utils.SendOK(w)
  148. } else if execute == "true" {
  149. //Prevent redirection attack
  150. w.WriteHeader(http.StatusMethodNotAllowed)
  151. w.Write([]byte("405 - Method Not Allowed"))
  152. } else {
  153. //Return the launcher message
  154. utils.SendTextResponse(w, string(launcherVersion))
  155. }
  156. })
  157. }
  158. }()
  159. //Log Viewer, so developers can debug inside arozos
  160. logViewer := logviewer.NewLogViewer(&logviewer.ViewerOption{
  161. RootFolder: "system/logs/",
  162. Extension: ".log",
  163. })
  164. adminRouter.HandleFunc("/system/log/list", logViewer.HandleListLog)
  165. adminRouter.HandleFunc("/system/log/read", logViewer.HandleReadLog)
  166. registerSetting(settingModule{
  167. Name: "System Log",
  168. Desc: "View ArozOS System Log",
  169. IconPath: "SystemAO/updates/img/update.png",
  170. Group: "Advance",
  171. StartDir: "SystemAO/advance/logview.html",
  172. RequireAdmin: true,
  173. })
  174. }
  175. func InfoHandleGetRuntimeInfo(w http.ResponseWriter, r *http.Request) {
  176. type RuntimeInfo struct {
  177. StartupTime int64
  178. ContinuesRuntime int64
  179. }
  180. runtimeInfo := RuntimeInfo{
  181. StartupTime: startupTime,
  182. ContinuesRuntime: time.Now().Unix() - startupTime,
  183. }
  184. js, _ := json.Marshal(runtimeInfo)
  185. utils.SendJSONResponse(w, string(js))
  186. }
  187. func InfoHandleTaskInfo(w http.ResponseWriter, r *http.Request) {
  188. type UsageInfo struct {
  189. CPU float64
  190. UsedRAM string
  191. TotalRam string
  192. RamUsage float64
  193. }
  194. cpuUsage, usedRam, totalRam, usagePercentage, _ := usage.GetCachedStats()
  195. info := UsageInfo{
  196. CPU: cpuUsage,
  197. UsedRAM: usedRam,
  198. TotalRam: totalRam,
  199. RamUsage: usagePercentage,
  200. }
  201. js, _ := json.Marshal(info)
  202. utils.SendJSONResponse(w, string(js))
  203. }