system.info.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. 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. sendErrorResponse(w, "Permission Denied")
  33. },
  34. })
  35. //Create Info Server Object
  36. var infoServer *info.Server = nil
  37. if *allow_hardware_management {
  38. infoServer = info.NewInfoServer(info.ArOZInfo{
  39. BuildVersion: build_version + "." + internal_version,
  40. DeviceVendor: deviceVendor,
  41. DeviceModel: deviceModel,
  42. VendorIcon: "../../" + iconVendor,
  43. SN: deviceUUID,
  44. HostOS: runtime.GOOS,
  45. CPUArch: runtime.GOARCH,
  46. HostName: *host_name,
  47. })
  48. router.HandleFunc("/system/info/getCPUinfo", info.GetCPUInfo)
  49. router.HandleFunc("/system/info/ifconfig", info.Ifconfig)
  50. router.HandleFunc("/system/info/getDriveStat", info.GetDriveStat)
  51. router.HandleFunc("/system/info/usbPorts", info.GetUSB)
  52. router.HandleFunc("/system/info/getRAMinfo", info.GetRamInfo)
  53. //Register as a system setting
  54. registerSetting(settingModule{
  55. Name: "Host Info",
  56. Desc: "System Information",
  57. IconPath: "SystemAO/info/img/small_icon.png",
  58. Group: "Info",
  59. StartDir: "SystemAO/info/index.html",
  60. })
  61. /*
  62. CPU and RAM usage interface
  63. */
  64. registerSetting(settingModule{
  65. Name: "Performance",
  66. Desc: "System CPU and RAM usage",
  67. IconPath: "SystemAO/info/img/small_icon.png",
  68. Group: "Info",
  69. StartDir: "SystemAO/info/taskManagerFrame.html",
  70. })
  71. router.HandleFunc("/system/info/getUsageInfo", InfoHandleTaskInfo)
  72. } else {
  73. //Make a simpler page for the information of system for hardware management disabled nodes
  74. registerSetting(settingModule{
  75. Name: "Overview",
  76. Desc: "Overview for user information",
  77. IconPath: "SystemAO/info/img/small_icon.png",
  78. Group: "Info",
  79. StartDir: "SystemAO/info/overview.html",
  80. })
  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. })
  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. 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. log.Println("REQUESTING LAUNCHER FOR UPDATE RESTART")
  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. 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. sendJSONResponse(w, string(js))
  166. }