mirror of
https://github.com/mpolden/echoip.git
synced 2025-06-25 20:27:50 +02:00
Refactor lookup methods
This commit is contained in:
76
api/api.go
76
api/api.go
@ -12,10 +12,8 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
geoip2 "github.com/oschwald/geoip2-golang"
|
||||
)
|
||||
|
||||
const APPLICATION_JSON = "application/json"
|
||||
@ -27,13 +25,8 @@ var USER_AGENT_RE = regexp.MustCompile(
|
||||
type API struct {
|
||||
CORS bool
|
||||
Template string
|
||||
lookupAddr func(string) ([]string, error)
|
||||
lookupCountry func(net.IP) (string, error)
|
||||
testPort func(net.IP, uint64) error
|
||||
oracle Oracle
|
||||
ipFromRequest func(*http.Request) (net.IP, error)
|
||||
reverseLookup bool
|
||||
countryLookup bool
|
||||
portTesting bool
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
@ -48,37 +41,13 @@ type TestPortResponse struct {
|
||||
Reachable bool `json:"reachable"`
|
||||
}
|
||||
|
||||
func New() *API {
|
||||
func New(oracle Oracle) *API {
|
||||
return &API{
|
||||
lookupAddr: func(addr string) (names []string, err error) { return nil, nil },
|
||||
lookupCountry: func(ip net.IP) (string, error) { return "", nil },
|
||||
testPort: func(ip net.IP, port uint64) error { return nil },
|
||||
oracle: oracle,
|
||||
ipFromRequest: ipFromRequest,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) EnableCountryLookup(filepath string) error {
|
||||
db, err := geoip2.Open(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.lookupCountry = func(ip net.IP) (string, error) {
|
||||
return lookupCountry(db, ip)
|
||||
}
|
||||
a.countryLookup = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *API) EnableReverseLookup() {
|
||||
a.lookupAddr = net.LookupAddr
|
||||
a.reverseLookup = true
|
||||
}
|
||||
|
||||
func (a *API) EnablePortTesting() {
|
||||
a.testPort = testPort
|
||||
a.portTesting = true
|
||||
}
|
||||
|
||||
func ipFromRequest(r *http.Request) (net.IP, error) {
|
||||
remoteIP := r.Header.Get("X-Real-IP")
|
||||
if remoteIP == "" {
|
||||
@ -95,43 +64,16 @@ func ipFromRequest(r *http.Request) (net.IP, error) {
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
func testPort(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 lookupCountry(db *geoip2.Reader, ip net.IP) (string, error) {
|
||||
if db == nil {
|
||||
return "", nil
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
func (a *API) newResponse(r *http.Request) (Response, error) {
|
||||
ip, err := a.ipFromRequest(r)
|
||||
if err != nil {
|
||||
return Response{}, err
|
||||
}
|
||||
country, err := a.lookupCountry(ip)
|
||||
country, err := a.oracle.LookupCountry(ip)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
hostnames, err := a.lookupAddr(ip.String())
|
||||
hostnames, err := a.oracle.LookupAddr(ip.String())
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
@ -187,7 +129,7 @@ func (a *API) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||
if err != nil {
|
||||
return internalServerError(err).AsJSON()
|
||||
}
|
||||
err = a.testPort(ip, port)
|
||||
err = a.oracle.LookupPort(ip, port)
|
||||
response := TestPortResponse{
|
||||
IP: ip,
|
||||
Port: port,
|
||||
@ -213,10 +155,8 @@ func (a *API) DefaultHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||
}
|
||||
var data = struct {
|
||||
Response
|
||||
ReverseLookup bool
|
||||
CountryLookup bool
|
||||
PortTesting bool
|
||||
}{response, a.reverseLookup, a.countryLookup, a.portTesting}
|
||||
Oracle
|
||||
}{response, a.oracle}
|
||||
if err := t.Execute(w, &data); err != nil {
|
||||
return internalServerError(err)
|
||||
}
|
||||
|
@ -9,20 +9,21 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type mockOracle struct{}
|
||||
|
||||
func (r *mockOracle) LookupAddr(string) ([]string, error) { return []string{"localhost"}, nil }
|
||||
func (r *mockOracle) LookupCountry(net.IP) (string, error) { return "Elbonia", nil }
|
||||
func (r *mockOracle) LookupPort(net.IP, uint64) error { return nil }
|
||||
func (r *mockOracle) IsLookupAddrEnabled() bool { return true }
|
||||
func (r *mockOracle) IsLookupCountryEnabled() bool { return true }
|
||||
func (r *mockOracle) IsLookupPortEnabled() bool { return true }
|
||||
|
||||
func newTestAPI() *API {
|
||||
return &API{
|
||||
lookupAddr: func(string) ([]string, error) {
|
||||
return []string{"localhost"}, nil
|
||||
},
|
||||
lookupCountry: func(ip net.IP) (string, error) {
|
||||
return "Elbonia", nil
|
||||
},
|
||||
oracle: &mockOracle{},
|
||||
ipFromRequest: func(*http.Request) (net.IP, error) {
|
||||
return net.ParseIP("127.0.0.1"), nil
|
||||
},
|
||||
testPort: func(net.IP, uint64) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
97
api/oracle.go
Normal file
97
api/oracle.go
Normal file
@ -0,0 +1,97 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
)
|
||||
|
||||
type Oracle interface {
|
||||
LookupAddr(string) ([]string, error)
|
||||
LookupCountry(net.IP) (string, error)
|
||||
LookupPort(net.IP, uint64) error
|
||||
IsLookupAddrEnabled() bool
|
||||
IsLookupCountryEnabled() bool
|
||||
IsLookupPortEnabled() bool
|
||||
}
|
||||
|
||||
type DefaultOracle struct {
|
||||
lookupAddr func(string) ([]string, error)
|
||||
lookupCountry func(net.IP) (string, error)
|
||||
lookupPort func(net.IP, uint64) error
|
||||
lookupAddrEnabled bool
|
||||
lookupCountryEnabled bool
|
||||
lookupPortEnabled bool
|
||||
}
|
||||
|
||||
func NewOracle() *DefaultOracle {
|
||||
return &DefaultOracle{
|
||||
lookupAddr: func(string) ([]string, error) { return nil, nil },
|
||||
lookupCountry: func(net.IP) (string, error) { return "", nil },
|
||||
lookupPort: func(net.IP, uint64) error { return nil },
|
||||
}
|
||||
}
|
||||
|
||||
func (r *DefaultOracle) LookupAddr(address string) ([]string, error) {
|
||||
return r.lookupAddr(address)
|
||||
}
|
||||
|
||||
func (r *DefaultOracle) LookupCountry(ip net.IP) (string, error) {
|
||||
return r.lookupCountry(ip)
|
||||
}
|
||||
|
||||
func (r *DefaultOracle) LookupPort(ip net.IP, port uint64) error {
|
||||
return r.lookupPort(ip, port)
|
||||
}
|
||||
|
||||
func (r *DefaultOracle) EnableLookupAddr() {
|
||||
r.lookupAddr = net.LookupAddr
|
||||
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
|
||||
}
|
||||
|
||||
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 }
|
||||
func (r *DefaultOracle) IsLookupPortEnabled() bool { return r.lookupPortEnabled }
|
||||
|
||||
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 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)
|
||||
}
|
Reference in New Issue
Block a user