system.id.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. //Register vendor information
  47. if fileExists("web/SystemAO/vendor/index.html") {
  48. registerSetting(settingModule{
  49. Name: "Vendor",
  50. Desc: "Vendor Information",
  51. IconPath: "SystemAO/info/img/small_icon.png",
  52. Group: "About",
  53. StartDir: "SystemAO/vendor/index.html",
  54. })
  55. }
  56. http.HandleFunc("/system/info/license", systemHandleListLicense)
  57. //Handle health check ping
  58. http.HandleFunc("/system/id/ping", systemIdHandlePing)
  59. }
  60. /*
  61. Ping function. This function handles the request
  62. */
  63. func systemIdHandlePing(w http.ResponseWriter, r *http.Request) {
  64. w.Header().Set("Access-Control-Allow-Origin", "*")
  65. w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
  66. w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
  67. status := struct {
  68. Status string
  69. }{
  70. "OK",
  71. }
  72. js, _ := json.Marshal(status)
  73. sendJSONResponse(w, string(js))
  74. }
  75. func systemIdGenerateSystemUUID() {
  76. if !fileExists("./system/dev.uuid") {
  77. //UUID not exist. Create one
  78. thisuuid := uuid.NewV4().String()
  79. if *system_uuid != "" {
  80. //User has defined the uuid. Use user defined one instead.
  81. thisuuid = *system_uuid
  82. }
  83. err := ioutil.WriteFile("./system/dev.uuid", []byte(thisuuid), 0755)
  84. if err != nil {
  85. log.Fatal(err)
  86. }
  87. deviceUUID = thisuuid
  88. } else {
  89. thisuuid, err := ioutil.ReadFile("./system/dev.uuid")
  90. if err != nil {
  91. log.Fatal("Failed to read system uuid file (system/dev.uuid).")
  92. }
  93. deviceUUID = string(thisuuid)
  94. }
  95. }
  96. func systemIdGetSystemUUID() string {
  97. fileUUID, err := ioutil.ReadFile("./system/dev.uuid")
  98. if err != nil {
  99. log.Println("Unable to read system UUID from dev.uuid file")
  100. log.Fatal(err)
  101. }
  102. return string(fileUUID)
  103. }
  104. func systemHandleListLicense(w http.ResponseWriter, r *http.Request) {
  105. licenses, _ := filepath.Glob("./web/SystemAO/info/license/*.txt")
  106. results := [][]string{}
  107. for _, file := range licenses {
  108. fileName := filepath.Base(file)
  109. name := strings.TrimSuffix(fileName, filepath.Ext(fileName))
  110. content, _ := ioutil.ReadFile(file)
  111. results = append(results, []string{name, string(content)})
  112. }
  113. js, _ := json.Marshal(results)
  114. sendJSONResponse(w, string(js))
  115. }
  116. func systemIdHandleRequest(w http.ResponseWriter, r *http.Request) {
  117. //Check if user has logged in
  118. if authAgent.CheckAuth(r) == false {
  119. sendErrorResponse(w, "User not logged in")
  120. return
  121. }
  122. //Group everything required to show into one json string
  123. type returnStruct struct {
  124. SystemUUID string
  125. IpAddress string
  126. Vendor string
  127. Build string
  128. Version string
  129. Model string
  130. VendorIcon string
  131. }
  132. //thisDevIP := network_info_GetOutboundIP().String()
  133. thisDevIP := ""
  134. jsonString, _ := json.Marshal(returnStruct{
  135. SystemUUID: systemIdGetSystemUUID(),
  136. IpAddress: thisDevIP,
  137. Vendor: deviceVendor,
  138. Build: build_version,
  139. Version: internal_version,
  140. Model: deviceModel,
  141. VendorIcon: iconVendor,
  142. })
  143. sendJSONResponse(w, string(jsonString))
  144. }
  145. func systemIdResponseBetaScan(w http.ResponseWriter, r *http.Request) {
  146. //Handle beta scanning method
  147. uuid := systemIdGetSystemUUID()
  148. IPAddress := r.Header.Get("X-Real-Ip")
  149. if IPAddress == "" {
  150. IPAddress = r.Header.Get("X-Forwarded-For")
  151. }
  152. if IPAddress == "" {
  153. IPAddress = r.RemoteAddr
  154. }
  155. IPAddress = IPAddress[:strings.LastIndex(IPAddress, ":")]
  156. resp := *host_name + ",Alive," + uuid + "," + IPAddress
  157. w.Header().Set("Access-Control-Allow-Origin", "*")
  158. w.Header().Set("Access-Control-Request-Headers", "*")
  159. w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
  160. w.Write([]byte(resp))
  161. }
  162. func systemIdServeVersonNumber(w http.ResponseWriter, r *http.Request) {
  163. if build_version == "development" {
  164. w.Write([]byte("AO-DEV_v" + internal_version))
  165. } else {
  166. w.Write([]byte("AO-REL_v" + internal_version))
  167. }
  168. }
  169. func systemIdGetDriveStates(w http.ResponseWriter, r *http.Request) {
  170. results := [][]string{}
  171. results = append(results, []string{
  172. "user",
  173. "User",
  174. "-1B/-1B",
  175. })
  176. jsonString, _ := json.Marshal(results)
  177. sendJSONResponse(w, string(jsonString))
  178. }