🦈🏠🐜 Initial Commit 🐜🦈🏠

This commit is contained in:
Jason
2022-02-14 16:45:10 -05:00
commit 964ab574ea
23 changed files with 2384 additions and 0 deletions

157
iputil/geo/geo.go Normal file
View File

@@ -0,0 +1,157 @@
package geo
import (
"math"
"net"
geoip2 "github.com/oschwald/geoip2-golang"
)
type Reader interface {
Country(net.IP) (Country, error)
City(net.IP) (City, error)
ASN(net.IP) (ASN, error)
IsEmpty() bool
}
type Country struct {
Name string
ISO string
IsEU *bool
}
type City struct {
Name string
Latitude float64
Longitude float64
PostalCode string
Timezone string
MetroCode uint
RegionName string
RegionCode string
}
type ASN struct {
AutonomousSystemNumber uint
AutonomousSystemOrganization string
}
type geoip struct {
country *geoip2.Reader
city *geoip2.Reader
asn *geoip2.Reader
}
func Open(countryDB, cityDB string, asnDB string) (Reader, error) {
var country, city, asn *geoip2.Reader
if countryDB != "" {
r, err := geoip2.Open(countryDB)
if err != nil {
return nil, err
}
country = r
}
if cityDB != "" {
r, err := geoip2.Open(cityDB)
if err != nil {
return nil, err
}
city = r
}
if asnDB != "" {
r, err := geoip2.Open(asnDB)
if err != nil {
return nil, err
}
asn = r
}
return &geoip{country: country, city: city, asn: asn}, nil
}
func (g *geoip) Country(ip net.IP) (Country, error) {
country := Country{}
if g.country == nil {
return country, nil
}
record, err := g.country.Country(ip)
if err != nil {
return country, err
}
if c, exists := record.Country.Names["en"]; exists {
country.Name = c
}
if c, exists := record.RegisteredCountry.Names["en"]; exists && country.Name == "" {
country.Name = c
}
if record.Country.IsoCode != "" {
country.ISO = record.Country.IsoCode
}
if record.RegisteredCountry.IsoCode != "" && country.ISO == "" {
country.ISO = record.RegisteredCountry.IsoCode
}
isEU := record.Country.IsInEuropeanUnion || record.RegisteredCountry.IsInEuropeanUnion
country.IsEU = &isEU
return country, nil
}
func (g *geoip) City(ip net.IP) (City, error) {
city := City{}
if g.city == nil {
return city, nil
}
record, err := g.city.City(ip)
if err != nil {
return city, err
}
if c, exists := record.City.Names["en"]; exists {
city.Name = c
}
if len(record.Subdivisions) > 0 {
if c, exists := record.Subdivisions[0].Names["en"]; exists {
city.RegionName = c
}
if record.Subdivisions[0].IsoCode != "" {
city.RegionCode = record.Subdivisions[0].IsoCode
}
}
if !math.IsNaN(record.Location.Latitude) {
city.Latitude = record.Location.Latitude
}
if !math.IsNaN(record.Location.Longitude) {
city.Longitude = record.Location.Longitude
}
// Metro code is US Only https://maxmind.github.io/GeoIP2-dotnet/doc/v2.7.1/html/P_MaxMind_GeoIP2_Model_Location_MetroCode.htm
if record.Location.MetroCode > 0 && record.Country.IsoCode == "US" {
city.MetroCode = record.Location.MetroCode
}
if record.Postal.Code != "" {
city.PostalCode = record.Postal.Code
}
if record.Location.TimeZone != "" {
city.Timezone = record.Location.TimeZone
}
return city, nil
}
func (g *geoip) ASN(ip net.IP) (ASN, error) {
asn := ASN{}
if g.asn == nil {
return asn, nil
}
record, err := g.asn.ASN(ip)
if err != nil {
return asn, err
}
if record.AutonomousSystemNumber > 0 {
asn.AutonomousSystemNumber = record.AutonomousSystemNumber
}
if record.AutonomousSystemOrganization != "" {
asn.AutonomousSystemOrganization = record.AutonomousSystemOrganization
}
return asn, nil
}
func (g *geoip) IsEmpty() bool {
return g.country == nil && g.city == nil
}

38
iputil/iputil.go Normal file
View File

@@ -0,0 +1,38 @@
package iputil
import (
"fmt"
"math/big"
"net"
"strings"
"time"
)
func LookupAddr(ip net.IP) (string, error) {
names, err := net.LookupAddr(ip.String())
if err != nil || len(names) == 0 {
return "", err
}
// Always return unrooted name
return strings.TrimRight(names[0], "."), nil
}
func LookupPort(ip net.IP, port uint64) error {
address := fmt.Sprintf("[%s]:%d", ip, port)
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
if err != nil {
return err
}
defer conn.Close()
return nil
}
func ToDecimal(ip net.IP) *big.Int {
i := big.NewInt(0)
if to4 := ip.To4(); to4 != nil {
i.SetBytes(to4)
} else {
i.SetBytes(ip)
}
return i
}

27
iputil/iputil_test.go Normal file
View File

@@ -0,0 +1,27 @@
package iputil
import (
"math/big"
"net"
"testing"
)
func TestToDecimal(t *testing.T) {
var msb = new(big.Int)
msb, _ = msb.SetString("80000000000000000000000000000000", 16)
var tests = []struct {
in string
out *big.Int
}{
{"127.0.0.1", big.NewInt(2130706433)},
{"::1", big.NewInt(1)},
{"8000::", msb},
}
for _, tt := range tests {
i := ToDecimal(net.ParseIP(tt.in))
if tt.out.Cmp(i) != 0 {
t.Errorf("Expected %d, got %d for IP %s", tt.out, i, tt.in)
}
}
}