system.go 5.6 KB

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