system.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. uuid "github.com/satori/go.uuid"
  10. fs "imuslab.com/arozos/mod/filesystem"
  11. "imuslab.com/arozos/mod/utils"
  12. )
  13. /*
  14. System Identification API
  15. This module handles cross cluster scanning, responses and more that related
  16. to functions that identifiy this as a ArOZ Online device
  17. */
  18. func SystemIDInit() {
  19. //Initialize device UUID if not exists
  20. systemIdGenerateSystemUUID()
  21. //Register as a system setting
  22. registerSetting(settingModule{
  23. Name: "ArozOS",
  24. Desc: "System Information",
  25. IconPath: "SystemAO/info/img/small_icon.png",
  26. Group: "About",
  27. StartDir: "SystemAO/info/about.html",
  28. })
  29. //Handle the about page
  30. http.HandleFunc("/system/id/requestInfo", systemIdHandleRequest)
  31. //Handle ArOZ Online Beta search methods
  32. if *enable_beta_scanning_support {
  33. http.HandleFunc("/AOB/hb.php", systemIdResponseBetaScan)
  34. http.HandleFunc("/AOB/", func(w http.ResponseWriter, r *http.Request) {
  35. http.Redirect(w, r, "../index.html", 307)
  36. })
  37. http.HandleFunc("/AOB/SystemAOB/functions/info/version.inf", systemIdServeVersonNumber)
  38. http.HandleFunc("/AOB/SystemAOB/functions/system_statistic/getDriveStat.php", systemIdGetDriveStates)
  39. }
  40. //Handle license info
  41. registerSetting(settingModule{
  42. Name: "Open Source",
  43. Desc: "License from the Open Source Community",
  44. IconPath: "SystemAO/info/img/small_icon.png",
  45. Group: "About",
  46. StartDir: "SystemAO/info/license.html",
  47. })
  48. registerSetting(settingModule{
  49. Name: "License",
  50. Desc: "License of ArozOS",
  51. IconPath: "SystemAO/info/img/small_icon.png",
  52. Group: "About",
  53. StartDir: "SystemAO/info/srcLicense.html",
  54. })
  55. //Register vendor information
  56. if fs.FileExists("web/SystemAO/vendor/index.html") {
  57. registerSetting(settingModule{
  58. Name: "Vendor",
  59. Desc: "Vendor Notes",
  60. IconPath: "SystemAO/info/img/small_icon.png",
  61. Group: "About",
  62. StartDir: "SystemAO/vendor/index.html",
  63. })
  64. }
  65. http.HandleFunc("/system/info/license", systemHandleListLicense)
  66. //Handle health check ping
  67. http.HandleFunc("/system/id/ping", systemIdHandlePing)
  68. }
  69. /*
  70. Ping function. This function handles the request
  71. */
  72. func systemIdHandlePing(w http.ResponseWriter, r *http.Request) {
  73. w.Header().Set("Access-Control-Allow-Origin", "*")
  74. w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
  75. w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
  76. js, _ := json.Marshal(struct {
  77. Status string
  78. }{
  79. "OK",
  80. })
  81. utils.SendJSONResponse(w, string(js))
  82. }
  83. func systemIdGenerateSystemUUID() {
  84. if !fs.FileExists("./system/dev.uuid") {
  85. //UUID not exist. Create one
  86. thisuuid := uuid.NewV4().String()
  87. if *system_uuid != "" {
  88. //User has defined the uuid. Use user defined one instead.
  89. thisuuid = *system_uuid
  90. }
  91. err := os.WriteFile("./system/dev.uuid", []byte(thisuuid), 0755)
  92. if err != nil {
  93. log.Fatal(err)
  94. }
  95. deviceUUID = thisuuid
  96. } else {
  97. thisuuid, err := os.ReadFile("./system/dev.uuid")
  98. if err != nil {
  99. log.Fatal("Failed to read system uuid file (system/dev.uuid).")
  100. }
  101. deviceUUID = string(thisuuid)
  102. }
  103. }
  104. func systemIdGetSystemUUID() string {
  105. fileUUID, err := os.ReadFile("./system/dev.uuid")
  106. if err != nil {
  107. systemWideLogger.PrintAndLog("Storage", "Unable to read system UUID from dev.uuid file", err)
  108. log.Fatal(err)
  109. }
  110. return string(fileUUID)
  111. }
  112. func systemHandleListLicense(w http.ResponseWriter, r *http.Request) {
  113. licenses, _ := filepath.Glob("./web/SystemAO/info/license/*.txt")
  114. results := [][]string{}
  115. for _, file := range licenses {
  116. fileName := filepath.Base(file)
  117. name := strings.TrimSuffix(fileName, filepath.Ext(fileName))
  118. content, _ := os.ReadFile(file)
  119. results = append(results, []string{name, string(content)})
  120. }
  121. js, _ := json.Marshal(results)
  122. utils.SendJSONResponse(w, string(js))
  123. }
  124. func systemIdHandleRequest(w http.ResponseWriter, r *http.Request) {
  125. //Check if user has logged in
  126. if authAgent.CheckAuth(r) == false {
  127. utils.SendErrorResponse(w, "User not logged in")
  128. return
  129. }
  130. //Group everything required to show into one json string
  131. type returnStruct struct {
  132. SystemUUID string
  133. IpAddress string
  134. Vendor string
  135. Build string
  136. Version string
  137. Model string
  138. }
  139. //thisDevIP := network_info_GetOutboundIP().String()
  140. thisDevIP := ""
  141. jsonString, _ := json.Marshal(returnStruct{
  142. SystemUUID: systemIdGetSystemUUID(),
  143. IpAddress: thisDevIP,
  144. Vendor: deviceVendor,
  145. Build: build_version,
  146. Version: internal_version,
  147. Model: deviceModel,
  148. })
  149. utils.SendJSONResponse(w, string(jsonString))
  150. }
  151. func systemIdResponseBetaScan(w http.ResponseWriter, r *http.Request) {
  152. //Handle beta scanning method
  153. uuid := systemIdGetSystemUUID()
  154. IPAddress := r.Header.Get("X-Real-Ip")
  155. if IPAddress == "" {
  156. IPAddress = r.Header.Get("X-Forwarded-For")
  157. }
  158. if IPAddress == "" {
  159. IPAddress = r.RemoteAddr
  160. }
  161. IPAddress = IPAddress[:strings.LastIndex(IPAddress, ":")]
  162. resp := *host_name + ",Alive," + uuid + "," + IPAddress
  163. w.Header().Set("Access-Control-Allow-Origin", "*")
  164. w.Header().Set("Access-Control-Request-Headers", "*")
  165. w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
  166. w.Write([]byte(resp))
  167. }
  168. func systemIdServeVersonNumber(w http.ResponseWriter, r *http.Request) {
  169. if build_version == "development" {
  170. w.Write([]byte("AO-DEV_v" + internal_version))
  171. } else {
  172. w.Write([]byte("AO-REL_v" + internal_version))
  173. }
  174. }
  175. func systemIdGetDriveStates(w http.ResponseWriter, r *http.Request) {
  176. results := [][]string{}
  177. results = append(results, []string{
  178. "user",
  179. "User",
  180. "-1B/-1B",
  181. })
  182. jsonString, _ := json.Marshal(results)
  183. utils.SendJSONResponse(w, string(jsonString))
  184. }