1
0

warp.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.Buffer{}
  52. _, err = buffer.ReadFrom(resp.Body)
  53. if err != nil {
  54. return "", err
  55. }
  56. return buffer.String(), nil
  57. }
  58. func (s *WarpService) RegWarp(secretKey string, publicKey string) (string, error) {
  59. tos := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
  60. hostName, _ := os.Hostname()
  61. data := fmt.Sprintf(`{"key":"%s","tos":"%s","type": "PC","model": "x-ui", "name": "%s"}`, publicKey, tos, hostName)
  62. url := "https://api.cloudflareclient.com/v0a2158/reg"
  63. req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
  64. if err != nil {
  65. return "", err
  66. }
  67. req.Header.Add("CF-Client-Version", "a-7.21-0721")
  68. req.Header.Add("Content-Type", "application/json")
  69. client := &http.Client{}
  70. resp, err := client.Do(req)
  71. if err != nil {
  72. return "", err
  73. }
  74. defer resp.Body.Close()
  75. buffer := &bytes.Buffer{}
  76. _, err = buffer.ReadFrom(resp.Body)
  77. if err != nil {
  78. return "", err
  79. }
  80. var rspData map[string]any
  81. err = json.Unmarshal(buffer.Bytes(), &rspData)
  82. if err != nil {
  83. return "", err
  84. }
  85. deviceId := rspData["id"].(string)
  86. token := rspData["token"].(string)
  87. license, ok := rspData["account"].(map[string]any)["license"].(string)
  88. if !ok {
  89. logger.Debug("Error accessing license value.")
  90. return "", err
  91. }
  92. warpData := fmt.Sprintf("{\n \"access_token\": \"%s\",\n \"device_id\": \"%s\",", token, deviceId)
  93. warpData += fmt.Sprintf("\n \"license_key\": \"%s\",\n \"private_key\": \"%s\"\n}", license, secretKey)
  94. s.SettingService.SetWarp(warpData)
  95. result := fmt.Sprintf("{\n \"data\": %s,\n \"config\": %s\n}", warpData, buffer.String())
  96. return result, nil
  97. }
  98. func (s *WarpService) SetWarpLicense(license string) (string, error) {
  99. var warpData map[string]string
  100. warp, err := s.SettingService.GetWarp()
  101. if err != nil {
  102. return "", err
  103. }
  104. err = json.Unmarshal([]byte(warp), &warpData)
  105. if err != nil {
  106. return "", err
  107. }
  108. url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s/account", warpData["device_id"])
  109. data := fmt.Sprintf(`{"license": "%s"}`, license)
  110. req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(data)))
  111. if err != nil {
  112. return "", err
  113. }
  114. req.Header.Set("Authorization", "Bearer "+warpData["access_token"])
  115. client := &http.Client{}
  116. resp, err := client.Do(req)
  117. if err != nil {
  118. return "", err
  119. }
  120. defer resp.Body.Close()
  121. buffer := &bytes.Buffer{}
  122. _, err = buffer.ReadFrom(resp.Body)
  123. if err != nil {
  124. return "", err
  125. }
  126. var response map[string]any
  127. err = json.Unmarshal(buffer.Bytes(), &response)
  128. if err != nil {
  129. return "", err
  130. }
  131. if response["success"] == false {
  132. errorArr, _ := response["errors"].([]any)
  133. errorObj := errorArr[0].(map[string]any)
  134. return "", common.NewError(errorObj["code"], errorObj["message"])
  135. }
  136. warpData["license_key"] = license
  137. newWarpData, err := json.MarshalIndent(warpData, "", " ")
  138. if err != nil {
  139. return "", err
  140. }
  141. s.SettingService.SetWarp(string(newWarpData))
  142. return string(newWarpData), nil
  143. }