system.info.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "runtime"
  6. "time"
  7. info "imuslab.com/arozos/mod/info/hardwareinfo"
  8. usage "imuslab.com/arozos/mod/info/usageinfo"
  9. prout "imuslab.com/arozos/mod/prouter"
  10. "imuslab.com/arozos/mod/updates"
  11. "imuslab.com/arozos/mod/utils"
  12. )
  13. //InitShowSysInformation xxx
  14. func SystemInfoInit() {
  15. systemWideLogger.PrintAndLog("System", "Operation System: "+runtime.GOOS, nil)
  16. systemWideLogger.PrintAndLog("System", "System Architecture: "+runtime.GOARCH, nil)
  17. //Updates 5 Dec 2020, Added permission router
  18. router := prout.NewModuleRouter(prout.RouterOption{
  19. ModuleName: "System Setting",
  20. AdminOnly: false,
  21. UserHandler: userHandler,
  22. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  23. utils.SendErrorResponse(w, "Permission Denied")
  24. },
  25. })
  26. //Anyone logged in can load router
  27. authRouter := prout.NewModuleRouter(prout.RouterOption{
  28. AdminOnly: false,
  29. UserHandler: userHandler,
  30. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  31. utils.SendErrorResponse(w, "Permission Denied")
  32. },
  33. })
  34. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  35. ModuleName: "System Setting",
  36. AdminOnly: true,
  37. UserHandler: userHandler,
  38. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  39. utils.SendErrorResponse(w, "Permission Denied")
  40. },
  41. })
  42. //Create Info Server Object
  43. var infoServer *info.Server = nil
  44. //Overview of account and system information
  45. registerSetting(settingModule{
  46. Name: "Overview",
  47. Desc: "Overview for user information",
  48. IconPath: "SystemAO/info/img/small_icon.png",
  49. Group: "Info",
  50. StartDir: "SystemAO/info/overview.html",
  51. })
  52. if *allow_hardware_management {
  53. infoServer = info.NewInfoServer(info.ArOZInfo{
  54. BuildVersion: build_version + "." + internal_version,
  55. DeviceVendor: deviceVendor,
  56. DeviceModel: deviceModel,
  57. VendorIcon: "../../" + iconVendor,
  58. SN: deviceUUID,
  59. HostOS: runtime.GOOS,
  60. CPUArch: runtime.GOARCH,
  61. HostName: *host_name,
  62. })
  63. router.HandleFunc("/system/info/getCPUinfo", info.GetCPUInfo)
  64. router.HandleFunc("/system/info/ifconfig", info.Ifconfig)
  65. router.HandleFunc("/system/info/getDriveStat", info.GetDriveStat)
  66. router.HandleFunc("/system/info/usbPorts", info.GetUSB)
  67. router.HandleFunc("/system/info/getRAMinfo", info.GetRamInfo)
  68. //Register as a system setting
  69. registerSetting(settingModule{
  70. Name: "Host Info",
  71. Desc: "System Information",
  72. IconPath: "SystemAO/info/img/small_icon.png",
  73. Group: "Info",
  74. StartDir: "SystemAO/info/index.html",
  75. })
  76. /*
  77. CPU and RAM usage interface
  78. */
  79. registerSetting(settingModule{
  80. Name: "Performance",
  81. Desc: "System CPU and RAM usage",
  82. IconPath: "SystemAO/info/img/small_icon.png",
  83. Group: "Info",
  84. StartDir: "SystemAO/info/taskManagerFrame.html",
  85. })
  86. router.HandleFunc("/system/info/getUsageInfo", InfoHandleTaskInfo)
  87. } else {
  88. //Remve hardware information from the infoServer
  89. infoServer = info.NewInfoServer(info.ArOZInfo{
  90. BuildVersion: build_version + "." + internal_version,
  91. DeviceVendor: deviceVendor,
  92. DeviceModel: deviceModel,
  93. VendorIcon: "../../" + iconVendor,
  94. SN: deviceUUID,
  95. HostOS: "virtualized",
  96. CPUArch: "generic",
  97. HostName: *host_name,
  98. })
  99. }
  100. //Register endpoints that do not involve hardware management
  101. authRouter.HandleFunc("/system/info/getRuntimeInfo", InfoHandleGetRuntimeInfo)
  102. //ArOZ Info do not need permission router
  103. http.HandleFunc("/system/info/getArOZInfo", infoServer.GetArOZInfo)
  104. go func() {
  105. if updates.CheckLauncherPortResponsive() {
  106. //Launcher port is responsive. Assume launcher exists
  107. registerSetting(settingModule{
  108. Name: "Updates",
  109. Desc: "Perform ArozOS Updates",
  110. IconPath: "SystemAO/updates/img/update.png",
  111. Group: "Info",
  112. StartDir: "SystemAO/updates/index.html",
  113. RequireAdmin: true,
  114. })
  115. //Register Update Functions
  116. adminRouter.HandleFunc("/system/update/download", updates.HandleUpdateDownloadRequest)
  117. adminRouter.HandleFunc("/system/update/checksize", updates.HandleUpdateCheckSize)
  118. adminRouter.HandleFunc("/system/update/checkpending", updates.HandlePendingCheck)
  119. adminRouter.HandleFunc("/system/update/platform", updates.HandleGetUpdatePlatformInfo)
  120. //Special function for handling launcher restart, must be in this scope
  121. adminRouter.HandleFunc("/system/update/restart", func(w http.ResponseWriter, r *http.Request) {
  122. launcherVersion, err := updates.GetLauncherVersion()
  123. if err != nil {
  124. utils.SendErrorResponse(w, err.Error())
  125. return
  126. }
  127. execute, _ := utils.Mv(r, "exec", true)
  128. if execute == "true" && r.Method == http.MethodPost {
  129. //Do the update
  130. systemWideLogger.PrintAndLog("System", "REQUESTING LAUNCHER FOR UPDATE RESTART", nil)
  131. executeShutdownSequence()
  132. utils.SendOK(w)
  133. } else if execute == "true" {
  134. //Prevent redirection attack
  135. w.WriteHeader(http.StatusMethodNotAllowed)
  136. w.Write([]byte("405 - Method Not Allowed"))
  137. } else {
  138. //Return the launcher message
  139. utils.SendTextResponse(w, string(launcherVersion))
  140. }
  141. })
  142. }
  143. }()
  144. }
  145. func InfoHandleGetRuntimeInfo(w http.ResponseWriter, r *http.Request) {
  146. type RuntimeInfo struct {
  147. StartupTime int64
  148. ContinuesRuntime int64
  149. }
  150. runtimeInfo := RuntimeInfo{
  151. StartupTime: startupTime,
  152. ContinuesRuntime: time.Now().Unix() - startupTime,
  153. }
  154. js, _ := json.Marshal(runtimeInfo)
  155. utils.SendJSONResponse(w, string(js))
  156. }
  157. func InfoHandleTaskInfo(w http.ResponseWriter, r *http.Request) {
  158. type UsageInfo struct {
  159. CPU float64
  160. UsedRAM string
  161. TotalRam string
  162. RamUsage float64
  163. }
  164. cpuUsage := usage.GetCPUUsage()
  165. usedRam, totalRam, usagePercentage := usage.GetRAMUsage()
  166. info := UsageInfo{
  167. cpuUsage,
  168. usedRam,
  169. totalRam,
  170. usagePercentage,
  171. }
  172. js, _ := json.Marshal(info)
  173. utils.SendJSONResponse(w, string(js))
  174. }