warp code enhancement
This commit is contained in:
+17
-23
@@ -19,24 +19,18 @@ import (
|
|||||||
|
|
||||||
type WarpService struct{}
|
type WarpService struct{}
|
||||||
|
|
||||||
func (s *WarpService) getWarpInfo(ep *model.Endpoint) ([]byte, error) {
|
func (s *WarpService) getWarpInfo(deviceId string, accessToken string) ([]byte, error) {
|
||||||
var warpData map[string]string
|
url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s", deviceId)
|
||||||
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"])
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("Authorization", "Bearer "+warpData["access_token"])
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@@ -69,7 +63,7 @@ func (s *WarpService) RegisterWarp(ep *model.Endpoint) error {
|
|||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@@ -94,18 +88,7 @@ func (s *WarpService) RegisterWarp(ep *model.Endpoint) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
warpData := map[string]string{
|
warpInfo, err := s.getWarpInfo(deviceId, token)
|
||||||
"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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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{}
|
var epOptions map[string]interface{}
|
||||||
err = json.Unmarshal(ep.Options, &epOptions)
|
err = json.Unmarshal(ep.Options, &epOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user