echoip/http/http.go

433 lines
11 KiB
Go

package http
import (
"context"
"encoding/json"
"fmt"
"html/template"
"log"
"path/filepath"
"strings"
"net/http/pprof"
rcache "github.com/go-redis/cache/v9"
"github.com/levelsoftware/echoip/cache"
"github.com/levelsoftware/echoip/config"
parser "github.com/levelsoftware/echoip/iputil/paser"
"github.com/levelsoftware/echoip/useragent"
"net"
"net/http"
"strconv"
)
const (
jsonMediaType = "application/json"
textMediaType = "text/plain"
)
type Server struct {
IPHeaders []string
LookupAddr func(net.IP) (string, error)
LookupPort func(net.IP, uint64) error
cache cache.Cache
runConfig *config.Config
parser parser.Parser
}
type PortResponse struct {
IP net.IP `json:"ip"`
Port uint64 `json:"port"`
Reachable bool `json:"reachable"`
}
func New(parser parser.Parser, cache cache.Cache, runConfig *config.Config) *Server {
return &Server{cache: cache, parser: parser, runConfig: runConfig}
}
func ipFromForwardedForHeader(v string) string {
sep := strings.Index(v, ",")
if sep == -1 {
return v
}
return v[:sep]
}
// ipFromRequest detects the IP address for this transaction.
//
// * `headers` - the specific HTTP headers to trust
// * `r` - the incoming HTTP request
// * `customIP` - whether to allow the IP to be pulled from query parameters
func ipFromRequest(headers []string, r *http.Request, customIP bool) (net.IP, error) {
remoteIP := ""
if customIP && r.URL != nil {
if v, ok := r.URL.Query()["ip"]; ok {
remoteIP = v[0]
}
}
if remoteIP == "" {
for _, header := range headers {
remoteIP = r.Header.Get(header)
if http.CanonicalHeaderKey(header) == "X-Forwarded-For" {
remoteIP = ipFromForwardedForHeader(remoteIP)
}
if remoteIP != "" {
break
}
}
}
if remoteIP == "" {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return nil, err
}
remoteIP = host
}
ip := net.ParseIP(remoteIP)
if ip == nil {
return nil, fmt.Errorf("could not parse IP: %s", remoteIP)
}
return ip, nil
}
func userAgentFromRequest(r *http.Request) *useragent.UserAgent {
var userAgent *useragent.UserAgent
userAgentRaw := r.UserAgent()
if userAgentRaw != "" {
parsed := useragent.Parse(userAgentRaw)
userAgent = &parsed
}
return userAgent
}
func (s *Server) newResponse(r *http.Request) (parser.Response, error) {
if err := handleAuth(r, s.runConfig); err != nil {
return parser.Response{}, err
}
ctx := context.Background()
ip, err := ipFromRequest(s.IPHeaders, r, true)
if err != nil {
return parser.Response{}, err
}
var cachedResponse cache.CachedResponse
if err := s.cache.Get(ctx, ip.String(), &cachedResponse); err != nil && err != rcache.ErrCacheMiss {
return parser.Response{}, err
}
if cachedResponse.IsSet() {
log.Printf("Return cached response for %s", ip.String())
return cachedResponse.Get(), nil
}
var hostname string
if s.LookupAddr != nil {
hostname, _ = s.LookupAddr(ip)
}
var response parser.Response
response, err = s.parser.Parse(ip, hostname)
log.Printf("Caching response for %s", ip.String())
if err := s.cache.Set(ctx, ip.String(), cachedResponse.Build(response), s.runConfig.CacheTtl); err != nil {
return parser.Response{}, err
}
response.UserAgent = userAgentFromRequest(r)
return response, nil
}
func (s *Server) newPortResponse(r *http.Request) (PortResponse, error) {
lastElement := filepath.Base(r.URL.Path)
port, err := strconv.ParseUint(lastElement, 10, 16)
if err != nil || port < 1 || port > 65535 {
return PortResponse{Port: port}, fmt.Errorf("invalid port: %s", lastElement)
}
ip, err := ipFromRequest(s.IPHeaders, r, false)
if err != nil {
return PortResponse{Port: port}, err
}
err = s.LookupPort(ip, port)
return PortResponse{
IP: ip,
Port: port,
Reachable: err == nil,
}, nil
}
func (s *Server) CLIHandler(w http.ResponseWriter, r *http.Request) *appError {
if err := handleAuth(r, s.runConfig); err != nil {
return badRequest(err).WithMessage(err.Error()).AsJSON()
}
ip, err := ipFromRequest(s.IPHeaders, r, true)
if err != nil {
return badRequest(err).WithMessage(err.Error()).AsJSON()
}
fmt.Fprintln(w, ip.String())
return nil
}
func (s *Server) CLICountryHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newResponse(r)
if err != nil {
return badRequest(err).WithMessage(err.Error()).AsJSON()
}
fmt.Fprintln(w, response.Country)
return nil
}
func (s *Server) CLICountryISOHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newResponse(r)
if err != nil {
return badRequest(err).WithMessage(err.Error()).AsJSON()
}
fmt.Fprintln(w, response.CountryISO)
return nil
}
func (s *Server) CLICityHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newResponse(r)
if err != nil {
return badRequest(err).WithMessage(err.Error()).AsJSON()
}
fmt.Fprintln(w, response.City)
return nil
}
func (s *Server) CLICoordinatesHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newResponse(r)
if err != nil {
return badRequest(err).WithMessage(err.Error()).AsJSON()
}
fmt.Fprintf(w, "%s,%s\n", formatCoordinate(response.Latitude), formatCoordinate(response.Longitude))
return nil
}
func (s *Server) CLIASNHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newResponse(r)
if err != nil {
return badRequest(err).WithMessage(err.Error()).AsJSON()
}
fmt.Fprintf(w, "%s\n", response.ASN)
return nil
}
func (s *Server) CLIASNOrgHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newResponse(r)
if err != nil {
return badRequest(err).WithMessage(err.Error()).AsJSON()
}
fmt.Fprintf(w, "%s\n", response.ASNOrg)
return nil
}
func (s *Server) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newResponse(r)
if err != nil {
return badRequest(err).WithMessage(err.Error()).AsJSON()
}
b, err := json.MarshalIndent(response, "", " ")
if err != nil {
return internalServerError(err).AsJSON()
}
w.Header().Set("Content-Type", jsonMediaType)
w.Write(b)
return nil
}
func (s *Server) HealthHandler(w http.ResponseWriter, r *http.Request) *appError {
w.Header().Set("Content-Type", jsonMediaType)
w.Write([]byte(`{"status":"OK"}`))
return nil
}
func (s *Server) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newPortResponse(r)
if err != nil {
return badRequest(err).WithMessage(err.Error()).AsJSON()
}
b, err := json.MarshalIndent(response, "", " ")
if err != nil {
return internalServerError(err).AsJSON()
}
w.Header().Set("Content-Type", jsonMediaType)
w.Write(b)
return nil
}
func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newResponse(r)
if err != nil {
return badRequest(err).WithMessage(err.Error())
}
t, err := template.ParseGlob(s.runConfig.TemplateDir + "/*")
if err != nil {
return internalServerError(err)
}
json, err := json.MarshalIndent(response, "", " ")
if err != nil {
return internalServerError(err)
}
var data = struct {
parser.Response
Host string
BoxLatTop float64
BoxLatBottom float64
BoxLonLeft float64
BoxLonRight float64
JSON string
Port bool
Sponsor bool
}{
response,
r.Host,
response.Latitude + 0.05,
response.Latitude - 0.05,
response.Longitude - 0.05,
response.Longitude + 0.05,
string(json),
s.LookupPort != nil,
s.runConfig.ShowSponsor,
}
if err := t.Execute(w, &data); err != nil {
return internalServerError(err)
}
return nil
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) *appError {
err := notFound(nil).WithMessage("404 page not found")
if r.Header.Get("accept") == jsonMediaType {
err = err.AsJSON()
}
return err
}
func cliMatcher(r *http.Request) bool {
ua := useragent.Parse(r.UserAgent())
switch ua.Product {
case "curl", "HTTPie", "httpie-go", "Wget", "fetch libfetch", "Go", "Go-http-client", "ddclient", "Mikrotik", "xh":
return true
}
return false
}
type appHandler func(http.ResponseWriter, *http.Request) *appError
func wrapHandlerFunc(f http.HandlerFunc) appHandler {
return func(w http.ResponseWriter, r *http.Request) *appError {
f.ServeHTTP(w, r)
return nil
}
}
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if e := fn(w, r); e != nil { // e is *appError
if e.Code/100 == 5 {
log.Println(e.Error)
}
// When Content-Type for error is JSON, we need to marshal the response into JSON
if e.IsJSON() {
var data = struct {
Code int `json:"status"`
Error string `json:"error"`
}{e.Code, e.Message}
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
panic(err)
}
e.Message = string(b)
}
// Set Content-Type of response if set in error
if e.ContentType != "" {
w.Header().Set("Content-Type", e.ContentType)
}
w.WriteHeader(e.Code)
fmt.Fprint(w, e.Message)
}
}
func (s *Server) Handler() http.Handler {
r := NewRouter()
// Health
r.Route("GET", "/health", s.HealthHandler)
// JSON
r.Route("GET", "/", s.JSONHandler).Header("Accept", jsonMediaType)
r.Route("GET", "/json", s.JSONHandler)
// CLI
r.Route("GET", "/", s.CLIHandler).MatcherFunc(cliMatcher)
r.Route("GET", "/", s.CLIHandler).Header("Accept", textMediaType)
r.Route("GET", "/ip", s.CLIHandler)
if !s.parser.IsEmpty() {
r.Route("GET", "/country", s.CLICountryHandler)
r.Route("GET", "/country-iso", s.CLICountryISOHandler)
r.Route("GET", "/city", s.CLICityHandler)
r.Route("GET", "/coordinates", s.CLICoordinatesHandler)
r.Route("GET", "/asn", s.CLIASNHandler)
r.Route("GET", "/asn-org", s.CLIASNOrgHandler)
}
// Browser
if s.runConfig.TemplateDir != "" {
r.Route("GET", "/", s.DefaultHandler)
}
// Port testing
if s.LookupPort != nil {
r.RoutePrefix("GET", "/port/", s.PortHandler)
}
// Profiling
if s.runConfig.Profile {
r.Route("GET", "/debug/pprof/cmdline", wrapHandlerFunc(pprof.Cmdline))
r.Route("GET", "/debug/pprof/profile", wrapHandlerFunc(pprof.Profile))
r.Route("GET", "/debug/pprof/symbol", wrapHandlerFunc(pprof.Symbol))
r.Route("GET", "/debug/pprof/trace", wrapHandlerFunc(pprof.Trace))
r.RoutePrefix("GET", "/debug/pprof/", wrapHandlerFunc(pprof.Index))
}
return r.Handler()
}
func (s *Server) ListenAndServe(addr string) error {
return http.ListenAndServe(addr, s.Handler())
}
func formatCoordinate(c float64) string {
return strconv.FormatFloat(c, 'f', 6, 64)
}
type InvalidTokenError struct{}
func (m *InvalidTokenError) Error() string {
return "invalid_token"
}
func handleAuth(r *http.Request, runConfig *config.Config) error {
if !runConfig.Jwt.Enabled {
return nil
}
authorization := r.Header.Get("Authorization")
tokenString := strings.ReplaceAll(authorization, "Bearer ", "")
if err := ParseJWT(runConfig, tokenString); err != nil {
return err
}
return nil
}