system.info.go 6.7 KB

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