From 1c0c5f61c6a62289d0b3f5fd311423d0c3487426 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Sat, 15 Feb 2025 22:30:57 +0100 Subject: [PATCH] warp code enhancement --- service/warp.go | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/service/warp.go b/service/warp.go index 7733dd6..22f8a33 100644 --- a/service/warp.go +++ b/service/warp.go @@ -19,24 +19,18 @@ import ( type WarpService struct{} -func (s *WarpService) getWarpInfo(ep *model.Endpoint) ([]byte, error) { - var warpData map[string]string - err := json.Unmarshal(ep.Ext, &warpData) - if err != nil { - return nil, err - } - - url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s", warpData["device_id"]) +func (s *WarpService) getWarpInfo(deviceId string, accessToken string) ([]byte, error) { + url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s", deviceId) req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err } - req.Header.Set("Authorization", "Bearer "+warpData["access_token"]) + req.Header.Set("Authorization", "Bearer "+accessToken) client := &http.Client{} resp, err := client.Do(req) - if err != nil { + if err != nil || resp.StatusCode != 200 { return nil, err } defer resp.Body.Close() @@ -69,7 +63,7 @@ func (s *WarpService) RegisterWarp(ep *model.Endpoint) error { client := &http.Client{} resp, err := client.Do(req) - if err != nil { + if err != nil || resp.StatusCode != 200 { return err } defer resp.Body.Close() @@ -94,18 +88,7 @@ func (s *WarpService) RegisterWarp(ep *model.Endpoint) error { return err } - warpData := map[string]string{ - "access_token": token, - "device_id": deviceId, - "license_key": license, - } - - ep.Ext, err = json.MarshalIndent(warpData, "", " ") - if err != nil { - return err - } - - warpInfo, err := s.getWarpInfo(ep) + warpInfo, err := s.getWarpInfo(deviceId, token) if err != nil { return err } @@ -142,6 +125,17 @@ func (s *WarpService) RegisterWarp(ep *model.Endpoint) error { }, } + warpData := map[string]interface{}{ + "access_token": token, + "device_id": deviceId, + "license_key": license, + } + + ep.Ext, err = json.MarshalIndent(warpData, "", " ") + if err != nil { + return err + } + var epOptions map[string]interface{} err = json.Unmarshal(ep.Options, &epOptions) if err != nil {