system.info.go 6.4 KB

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