Use headers to check ip addresses for use behind proxy
• Break file up to multiple files • Can pass in headers through command line arguments, defaults for checking X-Real-IP and X-Forwarded-For • Add logging for requests with timing • Add 404 support for missing URLsmaster
parent
77fe2532d5
commit
ba99b61aa0
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
// IP is the net.IP with a version string to allow storing the version
|
||||
type IP struct {
|
||||
net.IP
|
||||
version string
|
||||
}
|
||||
|
||||
func (ip *IP) isV6() bool {
|
||||
return ip.To4() == nil
|
||||
}
|
||||
|
||||
// NewIP returns a new instance of an IP address
|
||||
func NewIP(addr string) (*IP, error) {
|
||||
parsedIP := net.ParseIP(addr)
|
||||
if parsedIP == nil {
|
||||
return nil, errors.New("Could not parse address " + addr)
|
||||
}
|
||||
|
||||
result := &IP{parsedIP, ""}
|
||||
if result.isV6() {
|
||||
result.version = "v6"
|
||||
} else {
|
||||
result.version = "v4"
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Logger interface
|
||||
type Logger interface {
|
||||
Println(v ...interface{})
|
||||
Printf(format string, v ...interface{})
|
||||
}
|
||||
|
||||
// LoggerHandler interface
|
||||
type LoggerHandler interface {
|
||||
Logger
|
||||
ServeHTTP(http.ResponseWriter, *http.Request, http.HandlerFunc)
|
||||
}
|
||||
|
||||
// NewLogger returns a logger interface
|
||||
func NewLogger() LoggerHandler {
|
||||
return &logger{log.New(os.Stdout, appLabel, log.Ldate|log.Lmicroseconds)}
|
||||
}
|
||||
|
||||
type logger struct {
|
||||
Logger
|
||||
}
|
||||
|
||||
func (l *logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||
start := time.Now()
|
||||
next(rw, r)
|
||||
l.Printf("Completed %s to %s in %v", r.Method, r.URL.Path, time.Since(start))
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Server that handles http responses
|
||||
type Server struct {
|
||||
headerNames []string
|
||||
logger LoggerHandler
|
||||
}
|
||||
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
for _, headerName := range s.headerNames {
|
||||
possibleIP := req.Header.Get(headerName)
|
||||
if possibleIP == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
ip, err := NewIP(possibleIP)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
s.sendResponse(w, ip)
|
||||
return
|
||||
}
|
||||
|
||||
addr, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err != nil {
|
||||
s.handleError(w, http.StatusBadRequest, err, "Could not get IP address from request")
|
||||
return
|
||||
}
|
||||
|
||||
ip, err := NewIP(addr)
|
||||
if err != nil {
|
||||
s.handleError(w, http.StatusBadRequest, err, "Could not parse IP address"+addr)
|
||||
return
|
||||
}
|
||||
|
||||
s.sendResponse(w, ip)
|
||||
}
|
||||
|
||||
func (s *Server) sendResponse(resp http.ResponseWriter, ip *IP) {
|
||||
io.WriteString(resp, ip.String()+"\n")
|
||||
s.logger.Printf("Request from %s %s\n", ip.version, ip)
|
||||
}
|
||||
|
||||
func (s *Server) handleError(resp http.ResponseWriter, status int, err error, message string) {
|
||||
resp.WriteHeader(status)
|
||||
io.WriteString(resp, message)
|
||||
s.logger.Printf("Error handling request: %s (%s)", message, err)
|
||||
}
|
Loading…
Reference in New Issue