123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package service
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "net/http"
- "os"
- "time"
- "x-ui/logger"
- )
- type WarpService struct {
- SettingService
- }
- func (s *WarpService) GetWarpData() (string, error) {
- warp, err := s.SettingService.GetWarp()
- if err != nil {
- return "", err
- }
- return warp, nil
- }
- func (s *WarpService) DelWarpData() error {
- err := s.SettingService.SetWarp("")
- if err != nil {
- return err
- }
- return nil
- }
- func (s *WarpService) GetWarpConfig() (string, error) {
- var warpData map[string]string
- warp, err := s.SettingService.GetWarp()
- if err != nil {
- return "", err
- }
- err = json.Unmarshal([]byte(warp), &warpData)
- if err != nil {
- return "", err
- }
- url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s", warpData["device_id"])
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return "", err
- }
- req.Header.Set("Authorization", "Bearer "+warpData["access_token"])
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- buffer := bytes.NewBuffer(make([]byte, 8192))
- buffer.Reset()
- _, err = buffer.ReadFrom(resp.Body)
- if err != nil {
- return "", err
- }
- return buffer.String(), nil
- }
- func (s *WarpService) RegWarp(secretKey string, publicKey string) (string, error) {
- tos := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
- hostName, _ := os.Hostname()
- data := fmt.Sprintf(`{"key":"%s","tos":"%s","type": "PC","model": "x-ui", "name": "%s"}`, publicKey, tos, hostName)
- url := "https://api.cloudflareclient.com/v0a2158/reg"
- req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
- if err != nil {
- return "", err
- }
- req.Header.Add("CF-Client-Version", "a-7.21-0721")
- req.Header.Add("Content-Type", "application/json")
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- buffer := bytes.NewBuffer(make([]byte, 8192))
- buffer.Reset()
- _, err = buffer.ReadFrom(resp.Body)
- if err != nil {
- return "", err
- }
- var rspData map[string]interface{}
- err = json.Unmarshal(buffer.Bytes(), &rspData)
- if err != nil {
- return "", err
- }
- deviceId := rspData["id"].(string)
- token := rspData["token"].(string)
- license, ok := rspData["account"].(map[string]interface{})["license"].(string)
- if !ok {
- logger.Debug("Error accessing license value.")
- return "", err
- }
- warpData := fmt.Sprintf("{\n \"access_token\": \"%s\",\n \"device_id\": \"%s\",", token, deviceId)
- warpData += fmt.Sprintf("\n \"license_key\": \"%s\",\n \"private_key\": \"%s\"\n}", license, secretKey)
- s.SettingService.SetWarp(warpData)
- result := fmt.Sprintf("{\n \"data\": %s,\n \"config\": %s\n}", warpData, buffer.String())
- return result, nil
- }
- func (s *WarpService) SetWarpLicense(license string) (string, error) {
- var warpData map[string]string
- warp, err := s.SettingService.GetWarp()
- if err != nil {
- return "", err
- }
- err = json.Unmarshal([]byte(warp), &warpData)
- if err != nil {
- return "", err
- }
- url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s/account", warpData["device_id"])
- data := fmt.Sprintf(`{"license": "%s"}`, license)
- req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(data)))
- if err != nil {
- return "", err
- }
- req.Header.Set("Authorization", "Bearer "+warpData["access_token"])
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- buffer := bytes.NewBuffer(make([]byte, 8192))
- buffer.Reset()
- _, err = buffer.ReadFrom(resp.Body)
- if err != nil {
- return "", err
- }
- warpData["license_key"] = license
- newWarpData, err := json.MarshalIndent(warpData, "", " ")
- if err != nil {
- return "", err
- }
- s.SettingService.SetWarp(string(newWarpData))
- println(string(newWarpData))
- return string(newWarpData), nil
- }
|