forked from bittorrent/go-btfs
-
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.
- Loading branch information
ken.sun
committed
Nov 19, 2021
1 parent
763d929
commit 78e7518
Showing
9 changed files
with
393 additions
and
389 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,2 @@ | ||
// generate bin file by go-bindata -pkg bindata ./data | ||
package bindata |
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,73 @@ | ||
package bindata | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/ip2location/ip2location-go/v9" | ||
"github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
var DB *ip2location.DB | ||
|
||
func Init() error { | ||
filename := "data/IP2LOCATION-LITE-DB1.IPV6.BIN" | ||
var err error | ||
DB, err = InitDB(filename) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// InitDB init ip2locaiton db with bin data file filename | ||
func InitDB(filename string) (*ip2location.DB, error) { | ||
data, err := Asset(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reader := bytes.NewReader(data) | ||
return ip2location.OpenDBWithReader(NopCloser(reader)) | ||
} | ||
|
||
type nopCloser struct { | ||
*bytes.Reader | ||
} | ||
|
||
func NopCloser(r *bytes.Reader) nopCloser { | ||
return nopCloser{r} | ||
} | ||
|
||
func (nc nopCloser) Close() error { | ||
return nil | ||
} | ||
|
||
// CountryShortCodeByIP4 return country short by ipv4 address | ||
func CountryShortCodeByIP(addr string) (string, error) { | ||
if DB == nil { | ||
return "", errors.New("ip2location db not initialized") | ||
} | ||
location, err := DB.Get_country_short(addr) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return location.Country_short, nil | ||
} | ||
|
||
func CountryShortCode(addr multiaddr.Multiaddr) (string, error) { | ||
ipv4, err := addr.ValueForProtocol(multiaddr.P_IP4) | ||
if err != nil { | ||
fmt.Printf("get ipv4 err:%+v", err) | ||
} else { | ||
return CountryShortCodeByIP(ipv4) | ||
} | ||
|
||
ipv6, err := addr.ValueForProtocol(multiaddr.P_IP6) | ||
if err != nil { | ||
fmt.Printf("get ipv6 err:%+v", err) | ||
return "", err | ||
} | ||
return CountryShortCodeByIP(ipv6) | ||
} |
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,38 @@ | ||
package bindata | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func TestCountryCode(t *testing.T) { | ||
Init() | ||
|
||
tests := []struct { | ||
Addr string | ||
Wanted string | ||
}{ | ||
{ | ||
"/ip4/8.8.8.8/tcp/4001", | ||
"US", | ||
}, | ||
{ | ||
"/ip4/36.112.144.130/tcp/4001", | ||
"CN", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
md, _ := multiaddr.NewMultiaddr(test.Addr) | ||
code, err := CountryShortCode(md) | ||
//fmt.Printf("code:%s\n", code) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if code != test.Wanted { | ||
t.Fatalf("test:%+v get code:%s != %s", test, code, test.Wanted) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.