system.id.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 SystemIDInit(){
  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. }
  38. func system_id_generateSystemUUID(){
  39. if !fileExists("./system/dev.uuid"){
  40. //UUID not exist. Create one
  41. thisuuid := uuid.NewV4().String()
  42. if (*system_uuid != ""){
  43. //User has defined the uuid. Use user defined one instead.
  44. thisuuid = *system_uuid
  45. }
  46. err := ioutil.WriteFile("./system/dev.uuid", []byte(thisuuid), 0755)
  47. if (err != nil){
  48. log.Fatal(err)
  49. }
  50. deviceUUID = thisuuid
  51. }else{
  52. thisuuid, err := ioutil.ReadFile("./system/dev.uuid")
  53. if (err != nil){
  54. log.Fatal("Failed to read system uuid file (system/dev.uuid).")
  55. }
  56. deviceUUID = string(thisuuid)
  57. }
  58. }
  59. func system_id_getSystemUUID() string{
  60. fileUUID, err := ioutil.ReadFile("./system/dev.uuid")
  61. if (err != nil){
  62. log.Println("Unable to read system UUID from dev.uuid file")
  63. log.Fatal(err)
  64. }
  65. return string(fileUUID)
  66. }
  67. func system_id_handleRequest(w http.ResponseWriter, r *http.Request){
  68. //Check if user has logged in
  69. if authAgent.CheckAuth(r) == false {
  70. sendErrorResponse(w, "User not logged in")
  71. return
  72. }
  73. //Group everything required to show into one json string
  74. type returnStruct struct{
  75. SystemUUID string;
  76. IpAddress string;
  77. Vendor string;
  78. Build string;
  79. Version string;
  80. Model string;
  81. VendorIcon string;
  82. }
  83. //thisDevIP := network_info_GetOutboundIP().String()
  84. thisDevIP := ""
  85. jsonString, _ := json.Marshal(returnStruct{
  86. SystemUUID: system_id_getSystemUUID(),
  87. IpAddress: thisDevIP,
  88. Vendor: deviceVendor,
  89. Build: build_version,
  90. Version: internal_version,
  91. Model: deviceModel,
  92. VendorIcon: iconVendor,
  93. })
  94. sendJSONResponse(w, string(jsonString))
  95. }
  96. func system_id_responseBetaScan(w http.ResponseWriter, r *http.Request){
  97. //Handle beta scanning method
  98. uuid := system_id_getSystemUUID();
  99. IPAddress := r.Header.Get("X-Real-Ip")
  100. if IPAddress == "" {
  101. IPAddress = r.Header.Get("X-Forwarded-For")
  102. }
  103. if IPAddress == "" {
  104. IPAddress = r.RemoteAddr
  105. }
  106. IPAddress = IPAddress[:strings.LastIndex(IPAddress, ":")]
  107. resp := *host_name + ",Alive," + uuid + "," + IPAddress
  108. w.Header().Set("Access-Control-Allow-Origin", "*")
  109. w.Header().Set("Access-Control-Request-Headers", "*")
  110. w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
  111. w.Write([]byte(resp))
  112. }
  113. func system_id_serveVersonNumber(w http.ResponseWriter, r *http.Request){
  114. if build_version == "development"{
  115. w.Write([]byte("AO-DEV_v" + internal_version))
  116. }else{
  117. w.Write([]byte("AO-REL_v" + internal_version))
  118. }
  119. }
  120. func system_id_getDriveStates(w http.ResponseWriter, r *http.Request){
  121. results := [][]string{}
  122. results = append(results, []string{
  123. "user",
  124. "User",
  125. "-1B/-1B",
  126. })
  127. jsonString, _ := json.Marshal(results)
  128. sendJSONResponse(w, string(jsonString))
  129. }