system.info.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "runtime"
  6. "time"
  7. info "imuslab.com/arozos/mod/info/hardwareinfo"
  8. "imuslab.com/arozos/mod/info/logviewer"
  9. usage "imuslab.com/arozos/mod/info/usageinfo"
  10. prout "imuslab.com/arozos/mod/prouter"
  11. "imuslab.com/arozos/mod/updates"
  12. "imuslab.com/arozos/mod/utils"
  13. )
  14. // InitShowSysInformation xxx
  15. func SystemInfoInit() {
  16. systemWideLogger.PrintAndLog("System", "Operation System: "+runtime.GOOS, nil)
  17. systemWideLogger.PrintAndLog("System", "System Architecture: "+runtime.GOARCH, nil)
  18. //Updates 5 Dec 2020, Added permission router
  19. router := prout.NewModuleRouter(prout.RouterOption{
  20. ModuleName: "System Setting",
  21. AdminOnly: false,
  22. UserHandler: userHandler,
  23. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  24. utils.SendErrorResponse(w, "Permission Denied")
  25. },
  26. })
  27. //Anyone logged in can load router
  28. authRouter := prout.NewModuleRouter(prout.RouterOption{
  29. AdminOnly: false,
  30. UserHandler: userHandler,
  31. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  32. utils.SendErrorResponse(w, "Permission Denied")
  33. },
  34. })
  35. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  36. ModuleName: "System Setting",
  37. AdminOnly: true,
  38. UserHandler: userHandler,
  39. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  40. utils.SendErrorResponse(w, "Permission Denied")
  41. },
  42. })
  43. //Create Info Server Object
  44. var infoServer *info.Server = nil
  45. //Overview of account and system information
  46. registerSetting(settingModule{
  47. Name: "Overview",
  48. Desc: "Overview for user information",
  49. IconPath: "SystemAO/info/img/small_icon.png",
  50. Group: "Info",
  51. StartDir: "SystemAO/info/overview.html",
  52. })
  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: "../../" + iconVendor,
  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/small_icon.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/small_icon.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: "../../" + iconVendor,
  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. //ArOZ Info do not need permission router
  105. http.HandleFunc("/system/info/getArOZInfo", infoServer.GetArOZInfo)
  106. go func() {
  107. if updates.CheckLauncherPortResponsive() {
  108. //Launcher port is responsive. Assume launcher exists
  109. registerSetting(settingModule{
  110. Name: "Updates",
  111. Desc: "Perform ArozOS Updates",
  112. IconPath: "SystemAO/updates/img/update.png",
  113. Group: "Info",
  114. StartDir: "SystemAO/updates/index.html",
  115. RequireAdmin: true,
  116. })
  117. //Register Update Functions
  118. adminRouter.HandleFunc("/system/update/download", updates.HandleUpdateDownloadRequest)
  119. adminRouter.HandleFunc("/system/update/checksize", updates.HandleUpdateCheckSize)
  120. adminRouter.HandleFunc("/system/update/checkpending", updates.HandlePendingCheck)
  121. adminRouter.HandleFunc("/system/update/platform", updates.HandleGetUpdatePlatformInfo)
  122. //Special function for handling launcher restart, must be in this scope
  123. adminRouter.HandleFunc("/system/update/restart", func(w http.ResponseWriter, r *http.Request) {
  124. launcherVersion, err := updates.GetLauncherVersion()
  125. if err != nil {
  126. utils.SendErrorResponse(w, err.Error())
  127. return
  128. }
  129. execute, _ := utils.PostPara(r, "exec")
  130. if execute == "true" && r.Method == http.MethodPost {
  131. //Do the update
  132. systemWideLogger.PrintAndLog("System", "REQUESTING LAUNCHER FOR UPDATE RESTART", nil)
  133. executeShutdownSequence()
  134. utils.SendOK(w)
  135. } else if execute == "true" {
  136. //Prevent redirection attack
  137. w.WriteHeader(http.StatusMethodNotAllowed)
  138. w.Write([]byte("405 - Method Not Allowed"))
  139. } else {
  140. //Return the launcher message
  141. utils.SendTextResponse(w, string(launcherVersion))
  142. }
  143. })
  144. }
  145. }()
  146. //Log Viewer, so developers can debug inside arozos
  147. logViewer := logviewer.NewLogViewer(&logviewer.ViewerOption{
  148. RootFolder: "system/logs/",
  149. Extension: ".log",
  150. })
  151. adminRouter.HandleFunc("/system/log/list", logViewer.HandleListLog)
  152. adminRouter.HandleFunc("/system/log/read", logViewer.HandleReadLog)
  153. registerSetting(settingModule{
  154. Name: "System Log",
  155. Desc: "View ArozOS System Log",
  156. IconPath: "SystemAO/updates/img/update.png",
  157. Group: "Advance",
  158. StartDir: "SystemAO/advance/logview.html",
  159. RequireAdmin: true,
  160. })
  161. }
  162. func InfoHandleGetRuntimeInfo(w http.ResponseWriter, r *http.Request) {
  163. type RuntimeInfo struct {
  164. StartupTime int64
  165. ContinuesRuntime int64
  166. }
  167. runtimeInfo := RuntimeInfo{
  168. StartupTime: startupTime,
  169. ContinuesRuntime: time.Now().Unix() - startupTime,
  170. }
  171. js, _ := json.Marshal(runtimeInfo)
  172. utils.SendJSONResponse(w, string(js))
  173. }
  174. func InfoHandleTaskInfo(w http.ResponseWriter, r *http.Request) {
  175. type UsageInfo struct {
  176. CPU float64
  177. UsedRAM string
  178. TotalRam string
  179. RamUsage float64
  180. }
  181. cpuUsage := usage.GetCPUUsage()
  182. usedRam, totalRam, usagePercentage := usage.GetRAMUsage()
  183. info := UsageInfo{
  184. cpuUsage,
  185. usedRam,
  186. totalRam,
  187. usagePercentage,
  188. }
  189. js, _ := json.Marshal(info)
  190. utils.SendJSONResponse(w, string(js))
  191. }