system.info.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.GetCPUInfo)
  65. router.HandleFunc("/system/info/ifconfig", info.Ifconfig)
  66. router.HandleFunc("/system/info/getDriveStat", info.GetDriveStat)
  67. router.HandleFunc("/system/info/usbPorts", info.GetUSB)
  68. //For low-memory mode detection
  69. authRouter.HandleFunc("/system/info/getRAMinfo", info.GetRamInfo)
  70. //Register as a system setting
  71. registerSetting(settingModule{
  72. Name: "Host Info",
  73. Desc: "System Information",
  74. IconPath: "SystemAO/info/img/host.png",
  75. Group: "Info",
  76. StartDir: "SystemAO/info/index.html",
  77. })
  78. /*
  79. CPU and RAM usage interface
  80. */
  81. registerSetting(settingModule{
  82. Name: "Performance",
  83. Desc: "System CPU and RAM usage",
  84. IconPath: "SystemAO/info/img/performance.png",
  85. Group: "Info",
  86. StartDir: "SystemAO/info/taskManagerFrame.html",
  87. })
  88. router.HandleFunc("/system/info/getUsageInfo", InfoHandleTaskInfo)
  89. } else {
  90. //Remve hardware information from the infoServer
  91. infoServer = info.NewInfoServer(info.ArOZInfo{
  92. BuildVersion: build_version + "." + internal_version,
  93. DeviceVendor: deviceVendor,
  94. DeviceModel: deviceModel,
  95. VendorIcon: imageBase64,
  96. SN: deviceUUID,
  97. HostOS: "virtualized",
  98. CPUArch: "generic",
  99. HostName: *host_name,
  100. })
  101. }
  102. //Register endpoints that do not involve hardware management
  103. authRouter.HandleFunc("/system/info/getRuntimeInfo", InfoHandleGetRuntimeInfo)
  104. authRouter.HandleFunc("/system/info/getLocaleInfo", InfoHandleGetLocaleInfo)
  105. //ArOZ Info do not need permission router
  106. http.HandleFunc("/system/info/getArOZInfo", infoServer.GetArOZInfo)
  107. //Router to handle login background image loading
  108. http.HandleFunc("/system/info/wallpaper.jpg", func(w http.ResponseWriter, r *http.Request) {
  109. imgsrc := filepath.Join(vendorResRoot, "auth_bg.jpg")
  110. if !fs.FileExists(imgsrc) {
  111. imgsrc = "./web/img/public/auth_bg.jpg"
  112. }
  113. http.ServeFile(w, r, imgsrc)
  114. })
  115. go func() {
  116. if updates.CheckLauncherPortResponsive() {
  117. //Launcher port is responsive. Assume launcher exists
  118. registerSetting(settingModule{
  119. Name: "Updates",
  120. Desc: "Perform ArozOS Updates",
  121. IconPath: "SystemAO/updates/img/update.png",
  122. Group: "Info",
  123. StartDir: "SystemAO/updates/index.html",
  124. RequireAdmin: true,
  125. })
  126. //Register Update Functions
  127. adminRouter.HandleFunc("/system/update/download", updates.HandleUpdateDownloadRequest)
  128. adminRouter.HandleFunc("/system/update/checksize", updates.HandleUpdateCheckSize)
  129. adminRouter.HandleFunc("/system/update/checkpending", updates.HandlePendingCheck)
  130. adminRouter.HandleFunc("/system/update/platform", updates.HandleGetUpdatePlatformInfo)
  131. //Special function for handling launcher restart, must be in this scope
  132. adminRouter.HandleFunc("/system/update/restart", func(w http.ResponseWriter, r *http.Request) {
  133. launcherVersion, err := updates.GetLauncherVersion()
  134. if err != nil {
  135. utils.SendErrorResponse(w, err.Error())
  136. return
  137. }
  138. execute, _ := utils.PostPara(r, "exec")
  139. if execute == "true" && r.Method == http.MethodPost {
  140. //Do the update
  141. systemWideLogger.PrintAndLog("System", "REQUESTING LAUNCHER FOR UPDATE RESTART", nil)
  142. executeShutdownSequence()
  143. utils.SendOK(w)
  144. } else if execute == "true" {
  145. //Prevent redirection attack
  146. w.WriteHeader(http.StatusMethodNotAllowed)
  147. w.Write([]byte("405 - Method Not Allowed"))
  148. } else {
  149. //Return the launcher message
  150. utils.SendTextResponse(w, string(launcherVersion))
  151. }
  152. })
  153. }
  154. }()
  155. //Log Viewer, so developers can debug inside arozos
  156. logViewer := logviewer.NewLogViewer(&logviewer.ViewerOption{
  157. RootFolder: "system/logs/",
  158. Extension: ".log",
  159. })
  160. adminRouter.HandleFunc("/system/log/list", logViewer.HandleListLog)
  161. adminRouter.HandleFunc("/system/log/read", logViewer.HandleReadLog)
  162. registerSetting(settingModule{
  163. Name: "System Log",
  164. Desc: "View ArozOS System Log",
  165. IconPath: "SystemAO/updates/img/update.png",
  166. Group: "Advance",
  167. StartDir: "SystemAO/advance/logview.html",
  168. RequireAdmin: true,
  169. })
  170. }
  171. func InfoHandleGetRuntimeInfo(w http.ResponseWriter, r *http.Request) {
  172. type RuntimeInfo struct {
  173. StartupTime int64
  174. ContinuesRuntime int64
  175. }
  176. runtimeInfo := RuntimeInfo{
  177. StartupTime: startupTime,
  178. ContinuesRuntime: time.Now().Unix() - startupTime,
  179. }
  180. js, _ := json.Marshal(runtimeInfo)
  181. utils.SendJSONResponse(w, string(js))
  182. }
  183. func InfoHandleTaskInfo(w http.ResponseWriter, r *http.Request) {
  184. type UsageInfo struct {
  185. CPU float64
  186. UsedRAM string
  187. TotalRam string
  188. RamUsage float64
  189. }
  190. cpuUsage := usage.GetCPUUsage()
  191. usedRam, totalRam, usagePercentage := usage.GetRAMUsage()
  192. info := UsageInfo{
  193. cpuUsage,
  194. usedRam,
  195. totalRam,
  196. usagePercentage,
  197. }
  198. js, _ := json.Marshal(info)
  199. utils.SendJSONResponse(w, string(js))
  200. }