system.info.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "runtime"
  7. "time"
  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. log.Println("Operation System: " + runtime.GOOS)
  16. log.Println("System Architecture: " + runtime.GOARCH)
  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. 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. sendErrorResponse(w, "Permission Denied")
  32. },
  33. })
  34. //Create Info Server Object
  35. var infoServer *info.Server = nil
  36. if *allow_hardware_management {
  37. infoServer = info.NewInfoServer(info.ArOZInfo{
  38. BuildVersion: build_version + "." + internal_version,
  39. DeviceVendor: deviceVendor,
  40. DeviceModel: deviceModel,
  41. VendorIcon: "../../" + iconVendor,
  42. SN: deviceUUID,
  43. HostOS: runtime.GOOS,
  44. CPUArch: runtime.GOARCH,
  45. HostName: *host_name,
  46. })
  47. router.HandleFunc("/system/info/getCPUinfo", info.GetCPUInfo)
  48. router.HandleFunc("/system/info/ifconfig", info.Ifconfig)
  49. router.HandleFunc("/system/info/getDriveStat", info.GetDriveStat)
  50. router.HandleFunc("/system/info/usbPorts", info.GetUSB)
  51. router.HandleFunc("/system/info/getRAMinfo", info.GetRamInfo)
  52. //Register as a system setting
  53. registerSetting(settingModule{
  54. Name: "Host Info",
  55. Desc: "System Information",
  56. IconPath: "SystemAO/info/img/small_icon.png",
  57. Group: "Info",
  58. StartDir: "SystemAO/info/index.html",
  59. })
  60. /*
  61. CPU and RAM usage interface
  62. */
  63. registerSetting(settingModule{
  64. Name: "Performance",
  65. Desc: "System CPU and RAM usage",
  66. IconPath: "SystemAO/info/img/small_icon.png",
  67. Group: "Info",
  68. StartDir: "SystemAO/info/taskManagerFrame.html",
  69. })
  70. router.HandleFunc("/system/info/getUsageInfo", InfoHandleTaskInfo)
  71. } else {
  72. //Make a simpler page for the information of system for hardware management disabled nodes
  73. registerSetting(settingModule{
  74. Name: "Overview",
  75. Desc: "Overview for user information",
  76. IconPath: "SystemAO/info/img/small_icon.png",
  77. Group: "Info",
  78. StartDir: "SystemAO/info/overview.html",
  79. })
  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. registerSetting(settingModule{
  97. Name: "Updates",
  98. Desc: "Perform ArozOS Updates",
  99. IconPath: "SystemAO/updates/img/update.png",
  100. Group: "Info",
  101. StartDir: "SystemAO/updates/index.html",
  102. })
  103. //Handle updates
  104. adminRouter.HandleFunc("/system/update/download", updates.HandleUpdateDownloadRequest)
  105. adminRouter.HandleFunc("/system/update/checksize", updates.HandleUpdateCheckSize)
  106. }
  107. func InfoHandleGetRuntimeInfo(w http.ResponseWriter, r *http.Request) {
  108. type RuntimeInfo struct {
  109. StartupTime int64
  110. ContinuesRuntime int64
  111. }
  112. runtimeInfo := RuntimeInfo{
  113. StartupTime: startupTime,
  114. ContinuesRuntime: time.Now().Unix() - startupTime,
  115. }
  116. js, _ := json.Marshal(runtimeInfo)
  117. sendJSONResponse(w, string(js))
  118. }
  119. func InfoHandleTaskInfo(w http.ResponseWriter, r *http.Request) {
  120. type UsageInfo struct {
  121. CPU float64
  122. UsedRAM string
  123. TotalRam string
  124. RamUsage float64
  125. }
  126. cpuUsage := usage.GetCPUUsage()
  127. usedRam, totalRam, usagePercentage := usage.GetRAMUsage()
  128. info := UsageInfo{
  129. cpuUsage,
  130. usedRam,
  131. totalRam,
  132. usagePercentage,
  133. }
  134. js, _ := json.Marshal(info)
  135. sendJSONResponse(w, string(js))
  136. }