warp.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "time"
  9. "x-ui/logger"
  10. )
  11. type WarpService struct {
  12. SettingService
  13. }
  14. func (s *WarpService) GetWarpData() (string, error) {
  15. warp, err := s.SettingService.GetWarp()
  16. if err != nil {
  17. return "", err
  18. }
  19. return warp, nil
  20. }
  21. func (s *WarpService) DelWarpData() error {
  22. err := s.SettingService.SetWarp("")
  23. if err != nil {
  24. return err
  25. }
  26. return nil
  27. }
  28. func (s *WarpService) GetWarpConfig() (string, error) {
  29. var warpData map[string]string
  30. warp, err := s.SettingService.GetWarp()
  31. if err != nil {
  32. return "", err
  33. }
  34. err = json.Unmarshal([]byte(warp), &warpData)
  35. if err != nil {
  36. return "", err
  37. }
  38. url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s", warpData["device_id"])
  39. req, err := http.NewRequest("GET", url, nil)
  40. if err != nil {
  41. return "", err
  42. }
  43. req.Header.Set("Authorization", "Bearer "+warpData["access_token"])
  44. client := &http.Client{}
  45. resp, err := client.Do(req)
  46. if err != nil {
  47. return "", err
  48. }
  49. defer resp.Body.Close()
  50. buffer := bytes.NewBuffer(make([]byte, 8192))
  51. buffer.Reset()
  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.NewBuffer(make([]byte, 8192))
  76. buffer.Reset()
  77. _, err = buffer.ReadFrom(resp.Body)
  78. if err != nil {
  79. return "", err
  80. }
  81. var rspData map[string]interface{}
  82. err = json.Unmarshal(buffer.Bytes(), &rspData)
  83. if err != nil {
  84. return "", err
  85. }
  86. deviceId := rspData["id"].(string)
  87. token := rspData["token"].(string)
  88. license, ok := rspData["account"].(map[string]interface{})["license"].(string)
  89. if !ok {
  90. logger.Debug("Error accessing license value.")
  91. return "", err
  92. }
  93. warpData := fmt.Sprintf("{\n \"access_token\": \"%s\",\n \"device_id\": \"%s\",", token, deviceId)
  94. warpData += fmt.Sprintf("\n \"license_key\": \"%s\",\n \"private_key\": \"%s\"\n}", license, secretKey)
  95. s.SettingService.SetWarp(warpData)
  96. result := fmt.Sprintf("{\n \"data\": %s,\n \"config\": %s\n}", warpData, buffer.String())
  97. return result, nil
  98. }
  99. func (s *WarpService) SetWarpLicense(license string) (string, error) {
  100. var warpData map[string]string
  101. warp, err := s.SettingService.GetWarp()
  102. if err != nil {
  103. return "", err
  104. }
  105. err = json.Unmarshal([]byte(warp), &warpData)
  106. if err != nil {
  107. return "", err
  108. }
  109. url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s/account", warpData["device_id"])
  110. data := fmt.Sprintf(`{"license": "%s"}`, license)
  111. req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(data)))
  112. if err != nil {
  113. return "", err
  114. }
  115. req.Header.Set("Authorization", "Bearer "+warpData["access_token"])
  116. client := &http.Client{}
  117. resp, err := client.Do(req)
  118. if err != nil {
  119. return "", err
  120. }
  121. defer resp.Body.Close()
  122. buffer := bytes.NewBuffer(make([]byte, 8192))
  123. buffer.Reset()
  124. _, err = buffer.ReadFrom(resp.Body)
  125. if err != nil {
  126. return "", err
  127. }
  128. warpData["license_key"] = license
  129. newWarpData, err := json.MarshalIndent(warpData, "", " ")
  130. if err != nil {
  131. return "", err
  132. }
  133. s.SettingService.SetWarp(string(newWarpData))
  134. println(string(newWarpData))
  135. return string(newWarpData), nil
  136. }