system.info.go 7.1 KB

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