seclient: add geolist method

This commit is contained in:
Vladislav Yarmak
2021-03-26 21:52:09 +02:00
parent c465aeeebd
commit 738a5c329b
3 changed files with 65 additions and 7 deletions
+9 -1
View File
@@ -25,7 +25,7 @@ func main() {
log.Fatalln(err)
}
log.Printf("seclient = %#v", seclient)
//log.Printf("jar = %#v", seclient.HttpClient.Jar)
log.Printf("jar = %#v", seclient.HttpClient.Jar)
log.Println("------------ DOING DEVICE REGISTRATION ------------")
err = seclient.RegisterDevice(context.TODO())
@@ -33,4 +33,12 @@ func main() {
log.Fatalln(err)
}
log.Printf("seclient = %#v", seclient)
log.Printf("Device Password: %s", seclient.DevicePassword)
log.Println("------------ GETTING GEO LIST ------------")
geos, err := seclient.GeoList(context.TODO())
if err != nil {
log.Fatalln(err)
}
log.Printf("Geo List: %#v", geos)
}
+12
View File
@@ -59,3 +59,15 @@ type SERegisterDeviceResponse struct {
Data SERegisterDeviceData `json:"data"`
Status SEStatusPair `json:"return_code"`
}
type SEGeoEntry struct {
Country string `json:"country"`
CountryCode string `json:"country_code"`
}
type SEGeoListResponse struct {
Data struct {
Geos []SEGeoEntry `json:"geos"`
}
Status SEStatusPair
}
+44 -6
View File
@@ -109,12 +109,7 @@ func (c *SEClient) AnonRegister(ctx context.Context) error {
}
c.SubscriberEmail = fmt.Sprintf("%s@%s.best.vpn", localPart, c.Settings.ClientType)
password, err := randomCapitalHexString(c.rng, ANON_PASSWORD_BYTES)
if err != nil {
return err
}
c.SubscriberPassword = password
c.SubscriberPassword = capitalHexSHA1(c.SubscriberEmail)
return c.Register(ctx)
}
@@ -209,6 +204,49 @@ func (c *SEClient) RegisterDevice(ctx context.Context) error {
return nil
}
func (c *SEClient) GeoList(ctx context.Context) ([]SEGeoEntry, error) {
geoListInput := url.Values{
"device_id": {c.AssignedDeviceIDHash},
}
req, err := http.NewRequestWithContext(
ctx,
"POST",
c.Settings.Endpoints.GeoList,
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 geoListRes SEGeoListResponse
err = decoder.Decode(&geoListRes)
cleanupBody(resp.Body)
if err != nil {
return nil, err
}
if geoListRes.Status.Code != SE_STATUS_OK {
return nil, fmt.Errorf("API responded with error message: code=%d, msg=\"%s\"",
geoListRes.Status.Code, geoListRes.Status.Message)
}
return geoListRes.Data.Geos, 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}