-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from SiaFoundation/add-country-code
Add country code field for hosts
- Loading branch information
Showing
12 changed files
with
137 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
[![GoDoc](https://godoc.org/go.sia.tech/explored?status.svg)](https://godoc.org/go.sia.tech/explored) | ||
|
||
`explored` is an explorer for Sia. | ||
|
||
## Required Disclosure | ||
|
||
`explored` uses the IP2Location LITE database for <a href="https://lite.ip2location.com">IP geolocation</a>. |
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
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
Binary file not shown.
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,81 @@ | ||
package geoip | ||
|
||
import ( | ||
_ "embed" // needed for geolocation database | ||
"errors" | ||
"net" | ||
"os" | ||
"sync" | ||
|
||
"github.com/ip2location/ip2location-go" | ||
) | ||
|
||
//go:embed IP2LOCATION-LITE-DB1.BIN | ||
var ip2LocationDB []byte | ||
|
||
// A Locator maps IP addresses to their location. | ||
// It is assumed that it implementations are thread-safe. | ||
type Locator interface { | ||
// Close closes the Locator. | ||
Close() error | ||
// CountryCode maps IP addresses to ISO 3166-1 A-2 country codes. | ||
CountryCode(ip *net.IPAddr) (string, error) | ||
} | ||
|
||
type ip2Location struct { | ||
mu sync.Mutex | ||
|
||
path string | ||
db *ip2location.DB | ||
} | ||
|
||
// Close implements Locator. | ||
func (ip *ip2Location) Close() error { | ||
ip.mu.Lock() | ||
defer ip.mu.Unlock() | ||
|
||
ip.db.Close() | ||
return os.Remove(ip.path) | ||
} | ||
|
||
// CountryCode implements Locator. | ||
func (ip *ip2Location) CountryCode(addr *net.IPAddr) (string, error) { | ||
if ip == nil { | ||
return "", errors.New("nil IP") | ||
} | ||
ip.mu.Lock() | ||
defer ip.mu.Unlock() | ||
|
||
loc, err := ip.db.Get_country_short(addr.String()) | ||
if err != nil { | ||
return "", err | ||
} | ||
return loc.Country_short, nil | ||
} | ||
|
||
// NewIP2LocationLocator returns a Locator that uses an underlying IP2Location | ||
// database. If no path is provided, a default embedded LITE database is used. | ||
func NewIP2LocationLocator(path string) (Locator, error) { | ||
// Unfortunately, ip2location.OpenDB only accepts a file path. So we need | ||
// to write the embedded file to a temporary file on disk, and use that | ||
// instead. | ||
if path == "" { | ||
f, err := os.CreateTemp("", "geoip") | ||
if err != nil { | ||
return nil, err | ||
} else if _, err := f.Write(ip2LocationDB); err != nil { | ||
return nil, err | ||
} else if err := f.Sync(); err != nil { | ||
return nil, err | ||
} else if err := f.Close(); err != nil { | ||
return nil, err | ||
} | ||
path = f.Name() | ||
} | ||
|
||
db, err := ip2location.OpenDB(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ip2Location{path: path, db: db}, 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
Oops, something went wrong.