1
0

warp.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "time"
  9. "x-ui/logger"
  10. "x-ui/util/common"
  11. )
  12. type WarpService struct {
  13. SettingService
  14. }
  15. func (s *WarpService) GetWarpData() (string, error) {
  16. warp, err := s.SettingService.GetWarp()
  17. if err != nil {
  18. return "", err
  19. }
  20. return warp, nil
  21. }
  22. func (s *WarpService) DelWarpData() error {
  23. err := s.SettingService.SetWarp("")
  24. if err != nil {
  25. return err
  26. }
  27. return nil
  28. }
  29. func (s *WarpService) GetWarpConfig() (string, error) {
  30. var warpData map[string]string
  31. warp, err := s.SettingService.GetWarp()
  32. if err != nil {
  33. return "", err
  34. }
  35. err = json.Unmarshal([]byte(warp), &warpData)
  36. if err != nil {
  37. return "", err
  38. }
  39. url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s", warpData["device_id"])
  40. req, err := http.NewRequest("GET", url, nil)
  41. if err != nil {
  42. return "", err
  43. }
  44. req.Header.Set("Authorization", "Bearer "+warpData["access_token"])
  45. client := &http.Client{}
  46. resp, err := client.Do(req)
  47. if err != nil {
  48. return "", err
  49. }
  50. defer resp.Body.Close()
  51. buffer := bytes.NewBuffer(make([]byte, 8192))
  52. buffer.Reset()
  53. _, err = buffer.ReadFrom(resp.Body)
  54. if err != nil {
  55. return "", err
  56. }
  57. return buffer.String(), nil
  58. }
  59. func (s *WarpService) RegWarp(secretKey string, publicKey string) (string, error) {
  60. tos := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
  61. hostName, _ := os.Hostname()
  62. data := fmt.Sprintf(`{"key":"%s","tos":"%s","type": "PC","model": "x-ui", "name": "%s"}`, publicKey, tos, hostName)
  63. url := "https://api.cloudflareclient.com/v0a2158/reg"
  64. req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
  65. if err != nil {
  66. return "", err
  67. }
  68. req.Header.Add("CF-Client-Version", "a-7.21-0721")
  69. req.Header.Add("Content-Type", "application/json")
  70. client := &http.Client{}
  71. resp, err := client.Do(req)
  72. if err != nil {
  73. return "", err
  74. }
  75. defer resp.Body.Close()
  76. buffer := bytes.NewBuffer(make([]byte, 8192))
  77. buffer.Reset()
  78. _, err = buffer.ReadFrom(resp.Body)
  79. if err != nil {
  80. return "", err
  81. }
  82. var rspData map[string]interface{}
  83. err = json.Unmarshal(buffer.Bytes(), &rspData)
  84. if err != nil {
  85. return "", err
  86. }
  87. deviceId := rspData["id"].(string)
  88. token := rspData["token"].(string)
  89. license, ok := rspData["account"].(map[string]interface{})["license"].(string)
  90. if !ok {
  91. logger.Debug("Error accessing license value.")
  92. return "", err
  93. }
  94. warpData := fmt.Sprintf("{\n \"access_token\": \"%s\",\n \"device_id\": \"%s\",", token, deviceId)
  95. warpData += fmt.Sprintf("\n \"license_key\": \"%s\",\n \"private_key\": \"%s\"\n}", license, secretKey)
  96. s.SettingService.SetWarp(warpData)
  97. result := fmt.Sprintf("{\n \"data\": %s,\n \"config\": %s\n}", warpData, buffer.String())
  98. return result, nil
  99. }
  100. func (s *WarpService) SetWarpLicense(license string) (string, error) {
  101. var warpData map[string]string
  102. warp, err := s.SettingService.GetWarp()
  103. if err != nil {
  104. return "", err
  105. }
  106. err = json.Unmarshal([]byte(warp), &warpData)
  107. if err != nil {
  108. return "", err
  109. }
  110. url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s/account", warpData["device_id"])
  111. data := fmt.Sprintf(`{"license": "%s"}`, license)
  112. req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(data)))
  113. if err != nil {
  114. return "", err
  115. }
  116. req.Header.Set("Authorization", "Bearer "+warpData["access_token"])
  117. client := &http.Client{}
  118. resp, err := client.Do(req)
  119. if err != nil {
  120. return "", err
  121. }
  122. defer resp.Body.Close()
  123. buffer := bytes.NewBuffer(make([]byte, 8192))
  124. buffer.Reset()
  125. _, err = buffer.ReadFrom(resp.Body)
  126. if err != nil {
  127. return "", err
  128. }
  129. var response map[string]interface{}
  130. err = json.Unmarshal(buffer.Bytes(), &response)
  131. if err != nil {
  132. return "", err
  133. }
  134. if response["success"] == false {
  135. errorArr, _ := response["errors"].([]interface{})
  136. errorObj := errorArr[0].(map[string]interface{})
  137. return "", common.NewError(errorObj["code"], errorObj["message"])
  138. }
  139. warpData["license_key"] = license
  140. newWarpData, err := json.MarshalIndent(warpData, "", " ")
  141. if err != nil {
  142. return "", err
  143. }
  144. s.SettingService.SetWarp(string(newWarpData))
  145. return string(newWarpData), nil
  146. }