2018-02-10 13:24:32 +01:00
|
|
|
package http
|
2015-09-17 20:57:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-04-15 20:14:16 +02:00
|
|
|
"html/template"
|
2016-08-13 20:32:19 +02:00
|
|
|
|
2018-02-10 14:35:12 +01:00
|
|
|
"github.com/mpolden/ipd/iputil"
|
|
|
|
"github.com/mpolden/ipd/iputil/db"
|
2017-05-27 15:31:50 +02:00
|
|
|
"github.com/mpolden/ipd/useragent"
|
2017-05-25 13:07:40 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-08-13 20:32:19 +02:00
|
|
|
|
2016-07-06 23:44:33 +02:00
|
|
|
"math/big"
|
2015-09-17 20:57:27 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2016-04-15 20:52:15 +02:00
|
|
|
"strconv"
|
2015-09-17 20:57:27 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2016-11-16 20:00:03 +01:00
|
|
|
const (
|
|
|
|
jsonMediaType = "application/json"
|
|
|
|
textMediaType = "text/plain"
|
|
|
|
)
|
2015-09-17 20:57:27 +02:00
|
|
|
|
2018-02-10 14:35:12 +01:00
|
|
|
type LookupAddr func(net.IP) ([]string, error)
|
|
|
|
type LookupPort func(net.IP, uint64) error
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
type Server struct {
|
2018-02-10 14:35:12 +01:00
|
|
|
Template string
|
|
|
|
IPHeader string
|
|
|
|
lookupAddr LookupAddr
|
|
|
|
lookupPort LookupPort
|
|
|
|
db db.Database
|
|
|
|
log *logrus.Logger
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
2016-04-15 20:14:16 +02:00
|
|
|
type Response struct {
|
2018-02-09 20:41:30 +01:00
|
|
|
IP net.IP `json:"ip"`
|
|
|
|
IPDecimal *big.Int `json:"ip_decimal"`
|
|
|
|
Country string `json:"country,omitempty"`
|
|
|
|
CountryISO string `json:"country_iso,omitempty"`
|
|
|
|
City string `json:"city,omitempty"`
|
|
|
|
Hostname string `json:"hostname,omitempty"`
|
2016-04-15 20:14:16 +02:00
|
|
|
}
|
|
|
|
|
2016-04-27 17:07:53 +02:00
|
|
|
type PortResponse struct {
|
2016-04-15 20:52:15 +02:00
|
|
|
IP net.IP `json:"ip"`
|
|
|
|
Port uint64 `json:"port"`
|
|
|
|
Reachable bool `json:"reachable"`
|
|
|
|
}
|
|
|
|
|
2018-02-10 14:35:12 +01:00
|
|
|
func New(db db.Database, lookupAddr LookupAddr, lookupPort LookupPort, logger *logrus.Logger) *Server {
|
|
|
|
return &Server{lookupAddr: lookupAddr, lookupPort: lookupPort, db: db, log: logger}
|
2016-07-06 23:44:33 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 15:52:06 +02:00
|
|
|
func ipFromRequest(header string, r *http.Request) (net.IP, error) {
|
|
|
|
remoteIP := r.Header.Get(header)
|
2016-04-16 10:45:43 +02:00
|
|
|
if remoteIP == "" {
|
|
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
2015-09-17 20:57:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-16 10:45:43 +02:00
|
|
|
remoteIP = host
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
2016-04-16 10:45:43 +02:00
|
|
|
ip := net.ParseIP(remoteIP)
|
2015-09-17 20:57:27 +02:00
|
|
|
if ip == nil {
|
2016-04-16 10:45:43 +02:00
|
|
|
return nil, fmt.Errorf("could not parse IP: %s", remoteIP)
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) newResponse(r *http.Request) (Response, error) {
|
|
|
|
ip, err := ipFromRequest(s.IPHeader, r)
|
2015-09-17 20:57:27 +02:00
|
|
|
if err != nil {
|
2016-04-15 20:14:16 +02:00
|
|
|
return Response{}, err
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
2018-02-10 14:35:12 +01:00
|
|
|
ipDecimal := iputil.ToDecimal(ip)
|
|
|
|
country, err := s.db.Country(ip)
|
2018-02-09 20:41:30 +01:00
|
|
|
if err != nil {
|
2018-02-10 13:24:32 +01:00
|
|
|
s.log.Debug(err)
|
2018-02-09 20:41:30 +01:00
|
|
|
}
|
2018-02-10 14:35:12 +01:00
|
|
|
city, err := s.db.City(ip)
|
2016-04-17 11:28:47 +02:00
|
|
|
if err != nil {
|
2018-02-10 13:24:32 +01:00
|
|
|
s.log.Debug(err)
|
2016-04-17 11:28:47 +02:00
|
|
|
}
|
2018-02-10 14:35:12 +01:00
|
|
|
hostnames, err := s.lookupAddr(ip)
|
2016-04-15 20:14:16 +02:00
|
|
|
if err != nil {
|
2018-02-10 13:24:32 +01:00
|
|
|
s.log.Debug(err)
|
2016-04-15 20:14:16 +02:00
|
|
|
}
|
|
|
|
return Response{
|
2018-02-09 20:41:30 +01:00
|
|
|
IP: ip,
|
|
|
|
IPDecimal: ipDecimal,
|
2018-02-10 14:35:12 +01:00
|
|
|
Country: country.Name,
|
|
|
|
CountryISO: country.ISO,
|
2018-02-09 20:41:30 +01:00
|
|
|
City: city,
|
|
|
|
Hostname: strings.Join(hostnames, " "),
|
2016-04-15 20:14:16 +02:00
|
|
|
}, nil
|
|
|
|
}
|
2015-09-17 20:57:27 +02:00
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) newPortResponse(r *http.Request) (PortResponse, error) {
|
2016-04-27 17:07:53 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
port, err := strconv.ParseUint(vars["port"], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return PortResponse{Port: port}, err
|
|
|
|
}
|
|
|
|
if port < 1 || port > 65355 {
|
|
|
|
return PortResponse{Port: port}, fmt.Errorf("invalid port: %d", port)
|
|
|
|
}
|
2018-02-10 13:24:32 +01:00
|
|
|
ip, err := ipFromRequest(s.IPHeader, r)
|
2016-04-27 17:07:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return PortResponse{Port: port}, err
|
|
|
|
}
|
2018-02-10 14:35:12 +01:00
|
|
|
err = s.lookupPort(ip, port)
|
2016-04-27 17:07:53 +02:00
|
|
|
return PortResponse{
|
|
|
|
IP: ip,
|
|
|
|
Port: port,
|
|
|
|
Reachable: err == nil,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) CLIHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|
|
|
ip, err := ipFromRequest(s.IPHeader, r)
|
2016-04-15 20:14:16 +02:00
|
|
|
if err != nil {
|
2015-09-18 17:13:14 +02:00
|
|
|
return internalServerError(err)
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
2017-05-28 19:20:02 +02:00
|
|
|
fmt.Fprintln(w, ip.String())
|
2016-04-16 09:18:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) CLICountryHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|
|
|
response, err := s.newResponse(r)
|
2016-04-16 09:18:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return internalServerError(err)
|
2016-04-15 20:14:16 +02:00
|
|
|
}
|
2017-05-28 19:20:02 +02:00
|
|
|
fmt.Fprintln(w, response.Country)
|
2015-09-18 17:13:14 +02:00
|
|
|
return nil
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) CLICountryISOHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|
|
|
response, err := s.newResponse(r)
|
2018-02-09 20:41:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return internalServerError(err)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, response.CountryISO)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) CLICityHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|
|
|
response, err := s.newResponse(r)
|
2016-04-17 11:28:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return internalServerError(err)
|
|
|
|
}
|
2017-05-28 19:20:02 +02:00
|
|
|
fmt.Fprintln(w, response.City)
|
2016-04-17 11:28:47 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|
|
|
response, err := s.newResponse(r)
|
2015-09-18 17:13:14 +02:00
|
|
|
if err != nil {
|
2016-04-15 20:14:16 +02:00
|
|
|
return internalServerError(err).AsJSON()
|
2015-09-17 22:39:12 +02:00
|
|
|
}
|
2016-04-15 20:14:16 +02:00
|
|
|
b, err := json.Marshal(response)
|
2015-09-17 20:57:27 +02:00
|
|
|
if err != nil {
|
2016-04-15 20:14:16 +02:00
|
|
|
return internalServerError(err).AsJSON()
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
2016-05-26 21:38:10 +02:00
|
|
|
w.Header().Set("Content-Type", jsonMediaType)
|
2015-09-17 20:57:27 +02:00
|
|
|
w.Write(b)
|
2015-09-18 17:13:14 +02:00
|
|
|
return nil
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|
|
|
response, err := s.newPortResponse(r)
|
2016-04-15 20:52:15 +02:00
|
|
|
if err != nil {
|
2016-04-27 17:07:53 +02:00
|
|
|
return badRequest(err).WithMessage(fmt.Sprintf("Invalid port: %d", response.Port)).AsJSON()
|
2016-04-15 20:52:15 +02:00
|
|
|
}
|
|
|
|
b, err := json.Marshal(response)
|
|
|
|
if err != nil {
|
|
|
|
return internalServerError(err).AsJSON()
|
|
|
|
}
|
2016-05-26 21:38:10 +02:00
|
|
|
w.Header().Set("Content-Type", jsonMediaType)
|
2016-04-15 20:52:15 +02:00
|
|
|
w.Write(b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|
|
|
response, err := s.newResponse(r)
|
2015-09-18 17:42:43 +02:00
|
|
|
if err != nil {
|
2016-04-15 20:14:16 +02:00
|
|
|
return internalServerError(err)
|
2015-09-18 17:42:43 +02:00
|
|
|
}
|
2018-02-10 13:24:32 +01:00
|
|
|
t, err := template.New(filepath.Base(s.Template)).ParseFiles(s.Template)
|
2015-09-18 17:13:14 +02:00
|
|
|
if err != nil {
|
2016-04-15 20:14:16 +02:00
|
|
|
return internalServerError(err)
|
2015-09-17 22:39:12 +02:00
|
|
|
}
|
2018-02-10 14:35:12 +01:00
|
|
|
json, err := json.MarshalIndent(response, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return internalServerError(err)
|
|
|
|
}
|
2016-04-15 21:23:03 +02:00
|
|
|
var data = struct {
|
|
|
|
Response
|
2018-02-10 14:35:12 +01:00
|
|
|
Host string
|
|
|
|
JSON string
|
|
|
|
Port bool
|
|
|
|
Map bool
|
|
|
|
}{
|
|
|
|
response,
|
|
|
|
r.Host,
|
|
|
|
string(json),
|
|
|
|
s.lookupPort != nil,
|
|
|
|
response.Country != "" && response.City != "",
|
|
|
|
}
|
2016-04-15 21:23:03 +02:00
|
|
|
if err := t.Execute(w, &data); err != nil {
|
2016-04-15 20:14:16 +02:00
|
|
|
return internalServerError(err)
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
2015-09-18 17:13:14 +02:00
|
|
|
return nil
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) NotFoundHandler(w http.ResponseWriter, r *http.Request) *appError {
|
2016-04-15 20:14:16 +02:00
|
|
|
err := notFound(nil).WithMessage("404 page not found")
|
2016-05-26 21:38:10 +02:00
|
|
|
if r.Header.Get("accept") == jsonMediaType {
|
2016-04-15 20:14:16 +02:00
|
|
|
err = err.AsJSON()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-17 20:57:27 +02:00
|
|
|
func cliMatcher(r *http.Request, rm *mux.RouteMatch) bool {
|
2017-05-27 15:31:50 +02:00
|
|
|
ua := useragent.Parse(r.UserAgent())
|
|
|
|
switch ua.Product {
|
|
|
|
case "curl", "HTTPie", "Wget", "fetch libfetch", "Go", "Go-http-client", "ddclient":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
2015-09-18 17:13:14 +02:00
|
|
|
type appHandler func(http.ResponseWriter, *http.Request) *appError
|
|
|
|
|
|
|
|
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if e := fn(w, r); e != nil { // e is *appError
|
2016-04-15 20:14:16 +02:00
|
|
|
// When Content-Type for error is JSON, we need to marshal the response into JSON
|
2015-09-18 17:13:14 +02:00
|
|
|
if e.IsJSON() {
|
|
|
|
var data = struct {
|
|
|
|
Error string `json:"error"`
|
2016-04-15 20:14:16 +02:00
|
|
|
}{e.Message}
|
|
|
|
b, err := json.Marshal(data)
|
2015-09-18 17:13:14 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-04-15 20:14:16 +02:00
|
|
|
e.Message = string(b)
|
|
|
|
}
|
|
|
|
// Set Content-Type of response if set in error
|
|
|
|
if e.ContentType != "" {
|
|
|
|
w.Header().Set("Content-Type", e.ContentType)
|
2015-09-18 17:13:14 +02:00
|
|
|
}
|
|
|
|
w.WriteHeader(e.Code)
|
2017-05-28 19:20:02 +02:00
|
|
|
fmt.Fprint(w, e.Message)
|
2015-09-18 17:13:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 13:24:32 +01:00
|
|
|
func (s *Server) Handler() http.Handler {
|
2015-09-17 20:57:27 +02:00
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
// JSON
|
2018-02-10 13:24:32 +01:00
|
|
|
r.Handle("/", appHandler(s.JSONHandler)).Methods("GET").Headers("Accept", jsonMediaType)
|
|
|
|
r.Handle("/json", appHandler(s.JSONHandler)).Methods("GET")
|
2015-09-17 20:57:27 +02:00
|
|
|
|
|
|
|
// CLI
|
2018-02-10 13:24:32 +01:00
|
|
|
r.Handle("/", appHandler(s.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
|
|
|
|
r.Handle("/", appHandler(s.CLIHandler)).Methods("GET").Headers("Accept", textMediaType)
|
|
|
|
r.Handle("/ip", appHandler(s.CLIHandler)).Methods("GET")
|
|
|
|
r.Handle("/country", appHandler(s.CLICountryHandler)).Methods("GET")
|
|
|
|
r.Handle("/country-iso", appHandler(s.CLICountryISOHandler)).Methods("GET")
|
|
|
|
r.Handle("/city", appHandler(s.CLICityHandler)).Methods("GET")
|
2015-09-17 20:57:27 +02:00
|
|
|
|
2016-04-15 20:14:16 +02:00
|
|
|
// Browser
|
2018-02-10 13:24:32 +01:00
|
|
|
r.Handle("/", appHandler(s.DefaultHandler)).Methods("GET")
|
2015-09-17 20:57:27 +02:00
|
|
|
|
2016-04-15 20:52:15 +02:00
|
|
|
// Port testing
|
2018-02-10 13:24:32 +01:00
|
|
|
r.Handle("/port/{port:[0-9]+}", appHandler(s.PortHandler)).Methods("GET")
|
2016-04-15 20:52:15 +02:00
|
|
|
|
2016-04-15 20:14:16 +02:00
|
|
|
// Not found handler which returns JSON when appropriate
|
2018-02-10 13:24:32 +01:00
|
|
|
r.NotFoundHandler = appHandler(s.NotFoundHandler)
|
2016-04-15 20:14:16 +02:00
|
|
|
|
2016-04-17 14:23:20 +02:00
|
|
|
return r
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
2018-02-10 13:24:32 +01:00
|
|
|
|
|
|
|
func (s *Server) ListenAndServe(addr string) error {
|
|
|
|
return http.ListenAndServe(addr, s.Handler())
|
|
|
|
}
|