system.info.go 5.6 KB

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