seclient: add discover method

This commit is contained in:
Vladislav Yarmak
2021-03-26 22:09:37 +02:00
parent 738a5c329b
commit eb97beadca
3 changed files with 67 additions and 3 deletions
+7
View File
@@ -41,4 +41,11 @@ func main() {
log.Fatalln(err)
}
log.Printf("Geo List: %#v", geos)
log.Println("------------ GETTING IP LIST ------------")
ips, err := seclient.Discover(context.TODO(), "\"EU\",,")
if err != nil {
log.Fatalln(err)
}
log.Printf("IP List: %#v", ips)
}
+16 -3
View File
@@ -61,13 +61,26 @@ type SERegisterDeviceResponse struct {
}
type SEGeoEntry struct {
Country string `json:"country"`
Country string `json:"country,omitempty"`
CountryCode string `json:"country_code"`
}
type SEGeoListResponse struct {
Data struct {
Geos []SEGeoEntry `json:"geos"`
}
Status SEStatusPair
} `json:"data"`
Status SEStatusPair `json:"return_code"`
}
type SEIPEntry struct {
Geo SEGeoEntry `json:"geo"`
IP string `json:"ip"`
Ports []uint16 `json:"ports"`
}
type SEDiscoverResponse struct {
Data struct {
IPs []SEIPEntry `json:"ips"`
} `json:"data"`
Status SEStatusPair `json:"return_code"`
}
+44
View File
@@ -247,6 +247,50 @@ func (c *SEClient) GeoList(ctx context.Context) ([]SEGeoEntry, error) {
return geoListRes.Data.Geos, nil
}
func (c *SEClient) Discover(ctx context.Context, requestedGeo string) ([]SEIPEntry, error) {
geoListInput := url.Values{
"serial_no": {c.AssignedDeviceIDHash},
"requested_geo": {requestedGeo},
}
req, err := http.NewRequestWithContext(
ctx,
"POST",
c.Settings.Endpoints.Discover,
strings.NewReader(geoListInput.Encode()),
)
if err != nil {
return nil, err
}
c.populateRequest(req)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad http status: %s", resp.Status)
}
decoder := json.NewDecoder(resp.Body)
var discoverRes SEDiscoverResponse
err = decoder.Decode(&discoverRes)
cleanupBody(resp.Body)
if err != nil {
return nil, err
}
if discoverRes.Status.Code != SE_STATUS_OK {
return nil, fmt.Errorf("API responded with error message: code=%d, msg=\"%s\"",
discoverRes.Status.Code, discoverRes.Status.Message)
}
return discoverRes.Data.IPs, nil
}
func (c *SEClient) populateRequest(req *http.Request) {
req.Header["SE-Client-Version"] = []string{c.Settings.ClientVersion}
req.Header["SE-Operating-System"] = []string{c.Settings.OperatingSystem}