system.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  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: "About this ArozOS",
  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. systemWideLogger.PrintAndLog("System", fmt.Sprint(err), nil)
  94. os.Exit(1)
  95. }
  96. deviceUUID = thisuuid
  97. } else {
  98. thisuuid, err := os.ReadFile("./system/dev.uuid")
  99. if err != nil {
  100. systemWideLogger.PrintAndLog("System", "Failed to read system uuid file (system/dev.uuid).", nil)
  101. os.Exit(1)
  102. }
  103. deviceUUID = string(thisuuid)
  104. }
  105. }
  106. func systemIdGetSystemUUID() string {
  107. fileUUID, err := os.ReadFile("./system/dev.uuid")
  108. if err != nil {
  109. systemWideLogger.PrintAndLog("Storage", "Unable to read system UUID from dev.uuid file", err)
  110. systemWideLogger.PrintAndLog("System", fmt.Sprint(err), nil)
  111. os.Exit(1)
  112. }
  113. return string(fileUUID)
  114. }
  115. func systemHandleListLicense(w http.ResponseWriter, r *http.Request) {
  116. licenses, _ := filepath.Glob("./web/SystemAO/info/license/*.txt")
  117. results := [][]string{}
  118. for _, file := range licenses {
  119. fileName := filepath.Base(file)
  120. name := strings.TrimSuffix(fileName, filepath.Ext(fileName))
  121. content, _ := os.ReadFile(file)
  122. results = append(results, []string{name, string(content)})
  123. }
  124. js, _ := json.Marshal(results)
  125. utils.SendJSONResponse(w, string(js))
  126. }
  127. func systemIdHandleRequest(w http.ResponseWriter, r *http.Request) {
  128. //Check if user has logged in
  129. if authAgent.CheckAuth(r) == false {
  130. utils.SendErrorResponse(w, "User not logged in")
  131. return
  132. }
  133. //Group everything required to show into one json string
  134. type returnStruct struct {
  135. SystemUUID string
  136. IpAddress string
  137. Vendor string
  138. Build string
  139. Version string
  140. Model string
  141. }
  142. //thisDevIP := network_info_GetOutboundIP().String()
  143. thisDevIP := ""
  144. jsonString, _ := json.Marshal(returnStruct{
  145. SystemUUID: systemIdGetSystemUUID(),
  146. IpAddress: thisDevIP,
  147. Vendor: deviceVendor,
  148. Build: build_version,
  149. Version: internal_version,
  150. Model: deviceModel,
  151. })
  152. utils.SendJSONResponse(w, string(jsonString))
  153. }
  154. func systemIdResponseBetaScan(w http.ResponseWriter, r *http.Request) {
  155. //Handle beta scanning method
  156. uuid := systemIdGetSystemUUID()
  157. IPAddress := r.Header.Get("X-Real-Ip")
  158. if IPAddress == "" {
  159. IPAddress = r.Header.Get("X-Forwarded-For")
  160. }
  161. if IPAddress == "" {
  162. IPAddress = r.RemoteAddr
  163. }
  164. IPAddress = IPAddress[:strings.LastIndex(IPAddress, ":")]
  165. resp := *host_name + ",Alive," + uuid + "," + IPAddress
  166. w.Header().Set("Access-Control-Allow-Origin", "*")
  167. w.Header().Set("Access-Control-Request-Headers", "*")
  168. w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
  169. w.Write([]byte(resp))
  170. }
  171. func systemIdServeVersonNumber(w http.ResponseWriter, r *http.Request) {
  172. if build_version == "development" {
  173. w.Write([]byte("AO-DEV_v" + internal_version))
  174. } else {
  175. w.Write([]byte("AO-REL_v" + internal_version))
  176. }
  177. }
  178. func systemIdGetDriveStates(w http.ResponseWriter, r *http.Request) {
  179. results := [][]string{}
  180. results = append(results, []string{
  181. "user",
  182. "User",
  183. "-1B/-1B",
  184. })
  185. jsonString, _ := json.Marshal(results)
  186. utils.SendJSONResponse(w, string(jsonString))
  187. }