echoip/api/oracle.go

140 lines
3.5 KiB
Go
Raw Normal View History

2016-04-17 11:09:56 +02:00
package api
import (
"fmt"
"net"
2016-04-17 21:34:00 +02:00
"strings"
2016-04-17 11:09:56 +02:00
"time"
"github.com/oschwald/geoip2-golang"
)
type Oracle interface {
2016-04-17 21:34:00 +02:00
LookupAddr(net.IP) ([]string, error)
2016-04-17 11:09:56 +02:00
LookupCountry(net.IP) (string, error)
2016-04-17 11:28:47 +02:00
LookupCity(net.IP) (string, error)
2016-04-17 11:09:56 +02:00
LookupPort(net.IP, uint64) error
IsLookupAddrEnabled() bool
IsLookupCountryEnabled() bool
2016-04-17 11:28:47 +02:00
IsLookupCityEnabled() bool
2016-04-17 11:09:56 +02:00
IsLookupPortEnabled() bool
}
type DefaultOracle struct {
2016-04-17 21:34:00 +02:00
lookupAddr func(net.IP) ([]string, error)
2016-04-17 11:09:56 +02:00
lookupCountry func(net.IP) (string, error)
2016-04-17 11:28:47 +02:00
lookupCity func(net.IP) (string, error)
2016-04-17 11:09:56 +02:00
lookupPort func(net.IP, uint64) error
lookupAddrEnabled bool
lookupCountryEnabled bool
2016-04-17 11:28:47 +02:00
lookupCityEnabled bool
2016-04-17 11:09:56 +02:00
lookupPortEnabled bool
}
func NewOracle() *DefaultOracle {
return &DefaultOracle{
2016-04-17 21:34:00 +02:00
lookupAddr: func(net.IP) ([]string, error) { return nil, nil },
2016-04-17 11:09:56 +02:00
lookupCountry: func(net.IP) (string, error) { return "", nil },
2016-04-17 11:28:47 +02:00
lookupCity: func(net.IP) (string, error) { return "", nil },
2016-04-17 11:09:56 +02:00
lookupPort: func(net.IP, uint64) error { return nil },
}
}
2016-04-17 21:34:00 +02:00
func (r *DefaultOracle) LookupAddr(ip net.IP) ([]string, error) {
return r.lookupAddr(ip)
2016-04-17 11:09:56 +02:00
}
func (r *DefaultOracle) LookupCountry(ip net.IP) (string, error) {
return r.lookupCountry(ip)
}
2016-04-17 11:28:47 +02:00
func (r *DefaultOracle) LookupCity(ip net.IP) (string, error) {
return r.lookupCity(ip)
}
2016-04-17 11:09:56 +02:00
func (r *DefaultOracle) LookupPort(ip net.IP, port uint64) error {
return r.lookupPort(ip, port)
}
func (r *DefaultOracle) EnableLookupAddr() {
2016-04-17 21:34:00 +02:00
r.lookupAddr = lookupAddr
2016-04-17 11:09:56 +02:00
r.lookupAddrEnabled = true
}
func (r *DefaultOracle) EnableLookupCountry(filepath string) error {
db, err := geoip2.Open(filepath)
if err != nil {
return err
}
r.lookupCountry = func(ip net.IP) (string, error) {
return lookupCountry(db, ip)
}
r.lookupCountryEnabled = true
return nil
}
2016-04-17 11:28:47 +02:00
func (r *DefaultOracle) EnableLookupCity(filepath string) error {
db, err := geoip2.Open(filepath)
if err != nil {
return err
}
r.lookupCity = func(ip net.IP) (string, error) {
return lookupCity(db, ip)
}
r.lookupCityEnabled = true
return nil
}
2016-04-17 11:09:56 +02:00
func (r *DefaultOracle) EnableLookupPort() {
r.lookupPort = lookupPort
r.lookupPortEnabled = true
}
func (r *DefaultOracle) IsLookupAddrEnabled() bool { return r.lookupAddrEnabled }
func (r *DefaultOracle) IsLookupCountryEnabled() bool { return r.lookupCountryEnabled }
2016-04-17 11:28:47 +02:00
func (r *DefaultOracle) IsLookupCityEnabled() bool { return r.lookupCityEnabled }
2016-04-17 11:09:56 +02:00
func (r *DefaultOracle) IsLookupPortEnabled() bool { return r.lookupPortEnabled }
2016-04-17 21:34:00 +02:00
func lookupAddr(ip net.IP) ([]string, error) {
names, err := net.LookupAddr(ip.String())
for i, _ := range names {
names[i] = strings.TrimRight(names[i], ".") // Always return unrooted name
}
return names, err
}
2016-04-17 11:09:56 +02:00
func lookupPort(ip net.IP, port uint64) error {
address := fmt.Sprintf("[%s]:%d", ip, port)
2016-04-17 11:09:56 +02:00
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
if err != nil {
return err
}
defer conn.Close()
return nil
}
func lookupCountry(db *geoip2.Reader, ip net.IP) (string, error) {
record, err := db.Country(ip)
if err != nil {
return "", err
}
if country, exists := record.Country.Names["en"]; exists {
return country, nil
}
if country, exists := record.RegisteredCountry.Names["en"]; exists {
return country, nil
}
return "Unknown", fmt.Errorf("could not determine country for IP: %s", ip)
}
2016-04-17 11:28:47 +02:00
func lookupCity(db *geoip2.Reader, ip net.IP) (string, error) {
record, err := db.City(ip)
if err != nil {
return "", err
}
if city, exists := record.City.Names["en"]; exists {
return city, nil
}
return "Unknown", fmt.Errorf("could not determine city for IP: %s", ip)
}