xray_setting.go 4.2 KB

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