system.id.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "net/http"
  4. "io/ioutil"
  5. "log"
  6. "strings"
  7. "github.com/satori/go.uuid"
  8. "encoding/json"
  9. )
  10. /*
  11. System Identification API
  12. This module handles cross cluster scanning, responses and more that related
  13. to functions that identifiy this as a ArOZ Online device
  14. */
  15. func system_id_init(){
  16. //Initialize device UUID if not exists
  17. system_id_generateSystemUUID();
  18. //Register as a system setting
  19. registerSetting(settingModule{
  20. Name: "ArOZ Online",
  21. Desc: "System Information",
  22. IconPath: "SystemAO/info/img/small_icon.png",
  23. Group: "About",
  24. StartDir: "SystemAO/info/about.html",
  25. })
  26. //Handle the about page
  27. http.HandleFunc("/system/id/requestInfo", system_id_handleRequest);
  28. //Handle ArOZ Online Beta search methods
  29. if *enable_beta_scanning_support{
  30. http.HandleFunc("/AOB/hb.php", system_id_responseBetaScan);
  31. http.HandleFunc("/AOB/", func(w http.ResponseWriter, r *http.Request){
  32. http.Redirect(w,r,"../index.html",307)
  33. });
  34. http.HandleFunc("/AOB/SystemAOB/functions/info/version.inf", system_id_serveVersonNumber);
  35. http.HandleFunc("/AOB/SystemAOB/functions/system_statistic/getDriveStat.php", system_id_getDriveStates);
  36. }
  37. //Handle ArOZ Online 1.0 scan methods
  38. //WIP
  39. }
  40. func system_id_generateSystemUUID(){
  41. if !fileExists("./system/dev.uuid"){
  42. //UUID not exist. Create one
  43. thisuuid := uuid.NewV4().String()
  44. if (*system_uuid != ""){
  45. //User has defined the uuid. Use user defined one instead.
  46. thisuuid = *system_uuid
  47. }
  48. err := ioutil.WriteFile("./system/dev.uuid", []byte(thisuuid), 0755)
  49. if (err != nil){
  50. log.Fatal(err)
  51. }
  52. deviceUUID = thisuuid
  53. }else{
  54. thisuuid, err := ioutil.ReadFile("./system/dev.uuid")
  55. if (err != nil){
  56. log.Fatal("Failed to read system uuid file (system/dev.uuid).")
  57. }
  58. deviceUUID = string(thisuuid)
  59. }
  60. }
  61. func system_id_getSystemUUID() string{
  62. fileUUID, err := ioutil.ReadFile("./system/dev.uuid")
  63. if (err != nil){
  64. log.Println("Unable to read system UUID from dev.uuid file")
  65. log.Fatal(err)
  66. }
  67. return string(fileUUID)
  68. }
  69. func system_id_handleRequest(w http.ResponseWriter, r *http.Request){
  70. //Check if user has logged in
  71. if system_auth_chkauth(w, r) == false {
  72. sendErrorResponse(w, "User not logged in")
  73. return
  74. }
  75. //Group everything required to show into one json string
  76. type returnStruct struct{
  77. SystemUUID string;
  78. IpAddress string;
  79. Vendor string;
  80. Build string;
  81. Version string;
  82. Model string;
  83. VendorIcon string;
  84. }
  85. thisDevIP := network_info_GetOutboundIP().String()
  86. jsonString, _ := json.Marshal(returnStruct{
  87. SystemUUID: system_id_getSystemUUID(),
  88. IpAddress: thisDevIP,
  89. Vendor: deviceVendor,
  90. Build: build_version,
  91. Version: internal_version,
  92. Model: deviceModel,
  93. VendorIcon: iconVendor,
  94. })
  95. sendJSONResponse(w, string(jsonString))
  96. }
  97. func system_id_responseBetaScan(w http.ResponseWriter, r *http.Request){
  98. //Handle beta scanning method
  99. uuid := system_id_getSystemUUID();
  100. IPAddress := r.Header.Get("X-Real-Ip")
  101. if IPAddress == "" {
  102. IPAddress = r.Header.Get("X-Forwarded-For")
  103. }
  104. if IPAddress == "" {
  105. IPAddress = r.RemoteAddr
  106. }
  107. IPAddress = IPAddress[:strings.LastIndex(IPAddress, ":")]
  108. resp := *host_name + ",Alive," + uuid + "," + IPAddress
  109. w.Header().Set("Access-Control-Allow-Origin", "*")
  110. w.Header().Set("Access-Control-Request-Headers", "*")
  111. w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
  112. w.Write([]byte(resp))
  113. }
  114. func system_id_serveVersonNumber(w http.ResponseWriter, r *http.Request){
  115. if build_version == "development"{
  116. w.Write([]byte("AO-DEV_v" + internal_version))
  117. }else{
  118. w.Write([]byte("AO-REL_v" + internal_version))
  119. }
  120. }
  121. func system_id_getDriveStates(w http.ResponseWriter, r *http.Request){
  122. results := [][]string{}
  123. for _, store := range storages{
  124. results = append(results, []string{
  125. store.Uuid,
  126. store.Name,
  127. "-1B/-1B",
  128. })
  129. }
  130. jsonString, _ := json.Marshal(results)
  131. sendJSONResponse(w, string(jsonString))
  132. }