system.info.go 5.5 KB

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