Skip to content

Commit

Permalink
Integrate ip location db
Browse files Browse the repository at this point in the history
  • Loading branch information
ken.sun committed Nov 19, 2021
1 parent 763d929 commit 78e7518
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 389 deletions.
246 changes: 246 additions & 0 deletions bindata/bindata.go

Large diffs are not rendered by default.

Binary file added bindata/data/IP2LOCATION-LITE-DB1.IPV6.BIN
Binary file not shown.
2 changes: 2 additions & 0 deletions bindata/doc.go
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
73 changes: 73 additions & 0 deletions bindata/init.go
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)
}
38 changes: 38 additions & 0 deletions bindata/ip2location_test.go
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)
}
}
}
8 changes: 8 additions & 0 deletions cmd/btfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
cmds "github.com/TRON-US/go-btfs-cmds"
config "github.com/TRON-US/go-btfs-config"
cserial "github.com/TRON-US/go-btfs-config/serialize"
"github.com/TRON-US/go-btfs/bindata"
"github.com/TRON-US/go-btfs/chain"
cc "github.com/TRON-US/go-btfs/chain/config"
utilmain "github.com/TRON-US/go-btfs/cmd/btfs/util"
Expand Down Expand Up @@ -446,6 +447,13 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
return err
}

// init ip2location db
if err := bindata.Init(); err != nil {
// log init ip2location err
fmt.Println("init ip2location err: ", err)
log.Errorf("init ip2location err:%+v", err)
}

hValue, hasHval := req.Options[hValueKwd].(string)

migrated := config.MigrateConfig(cfg, inited, hasHval)
Expand Down
24 changes: 17 additions & 7 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"time"

"github.com/TRON-US/go-btfs/bindata"
commands "github.com/TRON-US/go-btfs/commands"
cmdenv "github.com/TRON-US/go-btfs/core/commands/cmdenv"
repo "github.com/TRON-US/go-btfs/repo"
Expand Down Expand Up @@ -124,6 +125,14 @@ var swarmPeersCmd = &cmds.Command{
ci.Streams = append(ci.Streams, streamInfo{Protocol: string(s)})
}
}

// fill int short country code by ip address
if code, err := bindata.CountryShortCode(c.Address()); err != nil {
fmt.Printf("get country short code err:%+v", err)
} else {
ci.CountryShort = code
}

sort.Sort(&ci)
out.Peers = append(out.Peers, ci)
}
Expand All @@ -134,7 +143,7 @@ var swarmPeersCmd = &cmds.Command{
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, ci *connInfos) error {
for _, info := range ci.Peers {
fmt.Fprintf(w, "%s/%s/%s", info.Addr, "btfs", info.Peer)
fmt.Fprintf(w, "%s/%s/%s/%s/%s", info.Addr, "btfs", info.Peer, "country_short", info.CountryShort)
if info.Latency != "" {
fmt.Fprintf(w, " %s", info.Latency)
}
Expand Down Expand Up @@ -164,12 +173,13 @@ type streamInfo struct {
}

type connInfo struct {
Addr string
Peer string
Latency string
Muxer string
Direction inet.Direction
Streams []streamInfo
Addr string
Peer string
Latency string
Muxer string
CountryShort string
Direction inet.Direction
Streams []streamInfo
}

func (ci *connInfo) Less(i, j int) bool {
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ require (
github.com/ethersphere/go-sw3-abi v0.4.0
github.com/fsnotify/fsnotify v1.4.9
github.com/gabriel-vasile/mimetype v1.1.2
github.com/go-bindata/go-bindata v3.1.2+incompatible
github.com/go-bindata/go-bindata/v3 v3.1.3
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.2.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/ip2location/ip2location-go/v9 v9.0.0
github.com/ipfs/go-bitswap v0.2.20
github.com/ipfs/go-block-format v0.0.2
github.com/ipfs/go-blockservice v0.1.3
Expand Down Expand Up @@ -119,7 +121,6 @@ require (
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.26.0
github.com/shirou/gopsutil/v3 v3.20.12
github.com/sirupsen/logrus v1.6.0
github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969
github.com/stretchr/testify v1.7.0
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954
Expand Down Expand Up @@ -175,5 +176,5 @@ replace github.com/libp2p/go-libp2p => github.com/libp2p/go-libp2p v0.11.0
replace github.com/libp2p/go-libp2p-circuit => github.com/libp2p/go-libp2p-circuit v0.3.1

replace github.com/libp2p/go-libp2p-quic-transport => github.com/libp2p/go-libp2p-quic-transport v0.8.0
go 1.14

go 1.14
Loading

0 comments on commit 78e7518

Please sign in to comment.