system.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. status := struct {
  75. Status string
  76. }{
  77. "OK",
  78. }
  79. js, _ := json.Marshal(status)
  80. sendJSONResponse(w, string(js))
  81. }
  82. func systemIdGenerateSystemUUID() {
  83. if !fileExists("./system/dev.uuid") {
  84. //UUID not exist. Create one
  85. thisuuid := uuid.NewV4().String()
  86. if *system_uuid != "" {
  87. //User has defined the uuid. Use user defined one instead.
  88. thisuuid = *system_uuid
  89. }
  90. err := ioutil.WriteFile("./system/dev.uuid", []byte(thisuuid), 0755)
  91. if err != nil {
  92. log.Fatal(err)
  93. }
  94. deviceUUID = thisuuid
  95. } else {
  96. thisuuid, err := ioutil.ReadFile("./system/dev.uuid")
  97. if err != nil {
  98. log.Fatal("Failed to read system uuid file (system/dev.uuid).")
  99. }
  100. deviceUUID = string(thisuuid)
  101. }
  102. }
  103. func systemIdGetSystemUUID() string {
  104. fileUUID, err := ioutil.ReadFile("./system/dev.uuid")
  105. if err != nil {
  106. log.Println("Unable to read system UUID from dev.uuid file")
  107. log.Fatal(err)
  108. }
  109. return string(fileUUID)
  110. }
  111. func systemHandleListLicense(w http.ResponseWriter, r *http.Request) {
  112. licenses, _ := filepath.Glob("./web/SystemAO/info/license/*.txt")
  113. results := [][]string{}
  114. for _, file := range licenses {
  115. fileName := filepath.Base(file)
  116. name := strings.TrimSuffix(fileName, filepath.Ext(fileName))
  117. content, _ := ioutil.ReadFile(file)
  118. results = append(results, []string{name, string(content)})
  119. }
  120. js, _ := json.Marshal(results)
  121. sendJSONResponse(w, string(js))
  122. }
  123. func systemIdHandleRequest(w http.ResponseWriter, r *http.Request) {
  124. //Check if user has logged in
  125. if authAgent.CheckAuth(r) == false {
  126. sendErrorResponse(w, "User not logged in")
  127. return
  128. }
  129. //Group everything required to show into one json string
  130. type returnStruct struct {
  131. SystemUUID string
  132. IpAddress string
  133. Vendor string
  134. Build string
  135. Version string
  136. Model string
  137. VendorIcon 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. VendorIcon: iconVendor,
  149. })
  150. sendJSONResponse(w, string(jsonString))
  151. }
  152. func systemIdResponseBetaScan(w http.ResponseWriter, r *http.Request) {
  153. //Handle beta scanning method
  154. uuid := systemIdGetSystemUUID()
  155. IPAddress := r.Header.Get("X-Real-Ip")
  156. if IPAddress == "" {
  157. IPAddress = r.Header.Get("X-Forwarded-For")
  158. }
  159. if IPAddress == "" {
  160. IPAddress = r.RemoteAddr
  161. }
  162. IPAddress = IPAddress[:strings.LastIndex(IPAddress, ":")]
  163. resp := *host_name + ",Alive," + uuid + "," + IPAddress
  164. w.Header().Set("Access-Control-Allow-Origin", "*")
  165. w.Header().Set("Access-Control-Request-Headers", "*")
  166. w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
  167. w.Write([]byte(resp))
  168. }
  169. func systemIdServeVersonNumber(w http.ResponseWriter, r *http.Request) {
  170. if build_version == "development" {
  171. w.Write([]byte("AO-DEV_v" + internal_version))
  172. } else {
  173. w.Write([]byte("AO-REL_v" + internal_version))
  174. }
  175. }
  176. func systemIdGetDriveStates(w http.ResponseWriter, r *http.Request) {
  177. results := [][]string{}
  178. results = append(results, []string{
  179. "user",
  180. "User",
  181. "-1B/-1B",
  182. })
  183. jsonString, _ := json.Marshal(results)
  184. sendJSONResponse(w, string(jsonString))
  185. }