-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autodetect client location if we didn't passed it
- Loading branch information
Showing
4 changed files
with
101 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2021 BlockDev AG | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
package location | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type locationProvider struct { | ||
address string | ||
user string | ||
pass string | ||
} | ||
|
||
// NewLocationProvider create a new location provider instance. | ||
func NewLocationProvider(address, user, pass string) *locationProvider { | ||
return &locationProvider{ | ||
address: address, | ||
user: user, | ||
pass: pass, | ||
} | ||
} | ||
|
||
// Country returns a country code for the provided IP-address. | ||
func (lp *locationProvider) Country(ip string) (countryCode string, err error) { | ||
url := fmt.Sprintf("%s/%s", lp.address, ip) | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
req.SetBasicAuth(lp.user, lp.pass) | ||
|
||
ctx, cancel := context.WithTimeout(req.Context(), 5*time.Second) | ||
defer cancel() | ||
|
||
req = req.WithContext(ctx) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
var country struct { | ||
Country string `json:"country"` | ||
} | ||
|
||
err = json.NewDecoder(resp.Body).Decode(&country) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return country.Country, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters