system.info.go 5.3 KB

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