refactor: WARP

This commit is contained in:
Sergei Maklagin
2025-06-08 19:51:17 +03:00
parent 5c911c97d8
commit 4e74a4108d
12 changed files with 346 additions and 253 deletions

View File

@@ -10,31 +10,26 @@ import (
"time"
"github.com/tidwall/gjson"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
type CloudeflareApi struct {
type CloudflareApi struct {
client http.Client
}
func NewCloudeflareApi(opts ...CloudflareApiOption) *CloudeflareApi {
api := &CloudeflareApi{http.Client{Timeout: 30 * time.Second}}
func NewCloudflareApi(opts ...CloudflareApiOption) *CloudflareApi {
api := &CloudflareApi{http.Client{Timeout: 30 * time.Second}}
for _, opt := range opts {
opt(api)
}
return api
}
func (api *CloudeflareApi) CreateProfile(ctx context.Context) (*CloudflareProfile, error) {
privateKey, err := wgtypes.GeneratePrivateKey()
if err != nil {
return nil, err
}
func (api *CloudflareApi) CreateProfile(ctx context.Context, publicKey string) (*CloudflareProfile, error) {
request, err := http.NewRequest("POST", "https://api.cloudflareclient.com/v0i1909051800/reg", strings.NewReader(
fmt.Sprintf(
"{\"install_id\":\"\",\"tos\":\"%s\",\"key\":\"%s\",\"fcm_token\":\"\",\"type\":\"ios\",\"locale\":\"en_US\"}",
time.Now().Format("2006-01-02T15:04:05.000Z"),
privateKey.PublicKey().String(),
publicKey,
),
))
if err != nil {
@@ -53,6 +48,27 @@ func (api *CloudeflareApi) CreateProfile(ctx context.Context) (*CloudflareProfil
return nil, err
}
profile := new(CloudflareProfile)
profile.Config.PrivateKey = privateKey.String()
return profile, json.NewDecoder(strings.NewReader(gjson.Get(string(content), "result").Raw)).Decode(profile)
}
func (api *CloudflareApi) GetProfile(ctx context.Context, authToken string, id string) (*CloudflareProfile, error) {
request, err := http.NewRequest("GET", "https://api.cloudflareclient.com/v0i1909051800/reg/"+id, nil)
if err != nil {
return nil, err
}
request.Header.Set("Authorization", "Bearer "+authToken)
response, err := api.client.Do(request.WithContext(ctx))
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return nil, fmt.Errorf("status code is not 200")
}
content, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
profile := new(CloudflareProfile)
return profile, json.NewDecoder(strings.NewReader(gjson.Get(string(content), "result").Raw)).Decode(profile)
}