mirror of https://github.com/mpolden/echoip
Simplify
This commit is contained in:
parent
5d43f175d4
commit
d49f1f7376
|
@ -34,31 +34,26 @@ func main() {
|
||||||
}
|
}
|
||||||
log.Level = level
|
log.Level = level
|
||||||
|
|
||||||
db := database.Empty()
|
db, err := database.New(opts.CountryDBPath, opts.CityDBPath)
|
||||||
if opts.CountryDBPath != "" || opts.CityDBPath != "" {
|
if err != nil {
|
||||||
db, err = database.New(opts.CountryDBPath, opts.CityDBPath)
|
log.Fatal(err)
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var lookupAddr http.LookupAddr
|
|
||||||
var lookupPort http.LookupPort
|
server := http.New(db, log)
|
||||||
|
server.Template = opts.Template
|
||||||
|
server.IPHeader = opts.IPHeader
|
||||||
if opts.ReverseLookup {
|
if opts.ReverseLookup {
|
||||||
log.Println("Enabling reverse lookup")
|
log.Println("Enabling reverse lookup")
|
||||||
lookupAddr = iputil.LookupAddr
|
server.LookupAddr = iputil.LookupAddr
|
||||||
}
|
}
|
||||||
if opts.PortLookup {
|
if opts.PortLookup {
|
||||||
log.Println("Enabling port lookup")
|
log.Println("Enabling port lookup")
|
||||||
lookupPort = iputil.LookupPort
|
server.LookupPort = iputil.LookupPort
|
||||||
}
|
}
|
||||||
if opts.IPHeader != "" {
|
if opts.IPHeader != "" {
|
||||||
log.Printf("Trusting header %s to contain correct remote IP", opts.IPHeader)
|
log.Printf("Trusting header %s to contain correct remote IP", opts.IPHeader)
|
||||||
}
|
}
|
||||||
|
|
||||||
server := http.New(db, lookupAddr, lookupPort, log)
|
|
||||||
server.Template = opts.Template
|
|
||||||
server.IPHeader = opts.IPHeader
|
|
||||||
|
|
||||||
log.Printf("Listening on http://%s", opts.Listen)
|
log.Printf("Listening on http://%s", opts.Listen)
|
||||||
if err := server.ListenAndServe(opts.Listen); err != nil {
|
if err := server.ListenAndServe(opts.Listen); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
21
http/http.go
21
http/http.go
|
@ -23,14 +23,11 @@ const (
|
||||||
textMediaType = "text/plain"
|
textMediaType = "text/plain"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LookupAddr func(net.IP) ([]string, error)
|
|
||||||
type LookupPort func(net.IP, uint64) error
|
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Template string
|
Template string
|
||||||
IPHeader string
|
IPHeader string
|
||||||
lookupAddr LookupAddr
|
LookupAddr func(net.IP) ([]string, error)
|
||||||
lookupPort LookupPort
|
LookupPort func(net.IP, uint64) error
|
||||||
db database.Client
|
db database.Client
|
||||||
log *logrus.Logger
|
log *logrus.Logger
|
||||||
}
|
}
|
||||||
|
@ -50,8 +47,8 @@ type PortResponse struct {
|
||||||
Reachable bool `json:"reachable"`
|
Reachable bool `json:"reachable"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(db database.Client, lookupAddr LookupAddr, lookupPort LookupPort, log *logrus.Logger) *Server {
|
func New(db database.Client, log *logrus.Logger) *Server {
|
||||||
return &Server{lookupAddr: lookupAddr, lookupPort: lookupPort, db: db, log: log}
|
return &Server{db: db, log: log}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ipFromRequest(header string, r *http.Request) (net.IP, error) {
|
func ipFromRequest(header string, r *http.Request) (net.IP, error) {
|
||||||
|
@ -85,8 +82,8 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
|
||||||
s.log.Debug(err)
|
s.log.Debug(err)
|
||||||
}
|
}
|
||||||
var hostnames []string
|
var hostnames []string
|
||||||
if s.lookupAddr != nil {
|
if s.LookupAddr != nil {
|
||||||
h, err := s.lookupAddr(ip)
|
h, err := s.LookupAddr(ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log.Debug(err)
|
s.log.Debug(err)
|
||||||
}
|
}
|
||||||
|
@ -115,7 +112,7 @@ func (s *Server) newPortResponse(r *http.Request) (PortResponse, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return PortResponse{Port: port}, err
|
return PortResponse{Port: port}, err
|
||||||
}
|
}
|
||||||
err = s.lookupPort(ip, port)
|
err = s.LookupPort(ip, port)
|
||||||
return PortResponse{
|
return PortResponse{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
Port: port,
|
Port: port,
|
||||||
|
@ -210,7 +207,7 @@ func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appErro
|
||||||
response,
|
response,
|
||||||
r.Host,
|
r.Host,
|
||||||
string(json),
|
string(json),
|
||||||
s.lookupPort != nil,
|
s.LookupPort != nil,
|
||||||
response.Country != "" && response.City != "",
|
response.Country != "" && response.City != "",
|
||||||
}
|
}
|
||||||
if err := t.Execute(w, &data); err != nil {
|
if err := t.Execute(w, &data); err != nil {
|
||||||
|
@ -281,7 +278,7 @@ func (s *Server) Handler() http.Handler {
|
||||||
r.Handle("/", appHandler(s.DefaultHandler)).Methods("GET")
|
r.Handle("/", appHandler(s.DefaultHandler)).Methods("GET")
|
||||||
|
|
||||||
// Port testing
|
// Port testing
|
||||||
if s.lookupPort != nil {
|
if s.LookupPort != nil {
|
||||||
r.Handle("/port/{port:[0-9]+}", appHandler(s.PortHandler)).Methods("GET")
|
r.Handle("/port/{port:[0-9]+}", appHandler(s.PortHandler)).Methods("GET")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ func (t *testDb) City(net.IP) (string, error) { return "Bornyasherk", nil }
|
||||||
func (t *testDb) IsEmpty() bool { return false }
|
func (t *testDb) IsEmpty() bool { return false }
|
||||||
|
|
||||||
func testServer() *Server {
|
func testServer() *Server {
|
||||||
return &Server{db: &testDb{}, lookupAddr: lookupAddr, lookupPort: lookupPort}
|
return &Server{db: &testDb{}, LookupAddr: lookupAddr, LookupPort: lookupPort}
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpGet(url string, acceptMediaType string, userAgent string) (string, int, error) {
|
func httpGet(url string, acceptMediaType string, userAgent string) (string, int, error) {
|
||||||
|
@ -85,9 +85,9 @@ func TestCLIHandlers(t *testing.T) {
|
||||||
func TestDisabledHandlers(t *testing.T) {
|
func TestDisabledHandlers(t *testing.T) {
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(ioutil.Discard)
|
||||||
server := testServer()
|
server := testServer()
|
||||||
server.lookupPort = nil
|
server.LookupPort = nil
|
||||||
server.lookupAddr = nil
|
server.LookupAddr = nil
|
||||||
server.db = database.Empty()
|
server.db, _ = database.New("", "")
|
||||||
s := httptest.NewServer(server.Handler())
|
s := httptest.NewServer(server.Handler())
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
|
|
|
@ -22,14 +22,6 @@ type geoip struct {
|
||||||
city *geoip2.Reader
|
city *geoip2.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
type empty struct{}
|
|
||||||
|
|
||||||
func (d *empty) Country(ip net.IP) (Country, error) { return Country{}, nil }
|
|
||||||
func (d *empty) City(ip net.IP) (string, error) { return "", nil }
|
|
||||||
func (d *empty) IsEmpty() bool { return true }
|
|
||||||
|
|
||||||
func Empty() Client { return &empty{} }
|
|
||||||
|
|
||||||
func New(countryDB, cityDB string) (Client, error) {
|
func New(countryDB, cityDB string) (Client, error) {
|
||||||
var country, city *geoip2.Reader
|
var country, city *geoip2.Reader
|
||||||
if countryDB != "" {
|
if countryDB != "" {
|
||||||
|
|
Loading…
Reference in New Issue