This commit is contained in:
esrrhs 2023-03-18 16:43:16 +08:00
parent aa216a7d72
commit 7af41a7723
5 changed files with 19 additions and 86 deletions

View File

@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"github.com/esrrhs/gohome/common"
"github.com/esrrhs/gohome/geoip"
"github.com/esrrhs/gohome/loggo"
"github.com/esrrhs/pingtunnel"
"net"
@ -214,7 +215,7 @@ func main() {
}
if len(*s5filter) > 0 {
err := pingtunnel.LoadGeoDB(*s5ftfile)
err := geoip.Load(*s5ftfile)
if err != nil {
loggo.Error("Load Sock5 ip file ERROR: %s", err.Error())
return
@ -230,7 +231,7 @@ func main() {
return false
}
ret, err := pingtunnel.GetCountryIsoCode(taddr.IP.String())
ret, err := geoip.GetCountryIsoCode(taddr.IP.String())
if err != nil {
return false
}

View File

@ -1,52 +0,0 @@
package pingtunnel
import (
"errors"
"github.com/esrrhs/gohome/common"
"github.com/oschwald/geoip2-golang"
"net"
)
var gdb *geoip2.Reader
func LoadGeoDB(file string) error {
if len(file) <= 0 {
file = common.GetDataDir() + "/geoip/" + "GeoLite2-Country.mmdb"
}
db, err := geoip2.Open(file)
if err != nil {
return err
}
gdb = db
return nil
}
func GetCountryIsoCode(ipaddr string) (string, error) {
ip := net.ParseIP(ipaddr)
if ip == nil {
return "", errors.New("ip " + ipaddr + " ParseIP nil")
}
record, err := gdb.City(ip)
if err != nil {
return "", err
}
return record.Country.IsoCode, nil
}
func GetCountryName(ipaddr string) (string, error) {
ip := net.ParseIP(ipaddr)
if ip == nil {
return "", errors.New("ip " + ipaddr + "ParseIP nil")
}
record, err := gdb.City(ip)
if err != nil {
return "", err
}
return record.Country.Names["en"], nil
}

View File

@ -1,16 +0,0 @@
package pingtunnel
import (
"fmt"
"testing"
)
func TestNew(t *testing.T) {
LoadGeoDB("./GeoLite2-Country.mmdb")
fmt.Println(GetCountryIsoCode("39.106.101.133"))
fmt.Println(GetCountryIsoCode(""))
fmt.Println(GetCountryIsoCode("aa"))
fmt.Println(GetCountryIsoCode("39.106.101.133:14234"))
fmt.Println(GetCountryIsoCode("192.168.1.121"))
}

12
go.mod
View File

@ -3,16 +3,16 @@ module github.com/esrrhs/pingtunnel
go 1.18
require (
github.com/esrrhs/gohome v0.0.0-20230222132228-8bb1d3e2ecc4
github.com/golang/protobuf v1.5.2
github.com/oschwald/geoip2-golang v1.8.0
golang.org/x/net v0.7.0
github.com/esrrhs/gohome v0.0.0-20230318083541-913b3bf95271
github.com/golang/protobuf v1.5.3
golang.org/x/net v0.8.0
)
require (
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/oschwald/geoip2-golang v1.8.0 // indirect
github.com/oschwald/maxminddb-golang v1.10.0 // indirect
golang.org/x/sys v0.5.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
golang.org/x/sys v0.6.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)

20
go.sum
View File

@ -1,11 +1,11 @@
github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8=
github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/esrrhs/gohome v0.0.0-20230222132228-8bb1d3e2ecc4 h1:ACKEtu7Lgqzu79p4ceZuNSCkm2Fi0GqzKmYyZBCHEXE=
github.com/esrrhs/gohome v0.0.0-20230222132228-8bb1d3e2ecc4/go.mod h1:34iMM/+BQzOgisvR1ukn1tuEwYxVvdG4UOlBPNlyNUQ=
github.com/esrrhs/gohome v0.0.0-20230318083541-913b3bf95271 h1:IyKT8av7ottkvaflceFea59ewsyOnvJyctD9DKk6I4A=
github.com/esrrhs/gohome v0.0.0-20230318083541-913b3bf95271/go.mod h1:4AMrMaduWMAu2xQ+wKY4Vr/M+3Dm3QGKobbMOdZSQSs=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
@ -16,14 +16,14 @@ github.com/oschwald/maxminddb-golang v1.10.0 h1:Xp1u0ZhqkSuopaKmk1WwHtjF0H9Hd918
github.com/oschwald/maxminddb-golang v1.10.0/go.mod h1:Y2ELenReaLAZ0b400URyGwvYxHV1dLIxBuyOsyYjHK0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=