-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgeoip2.go
More file actions
168 lines (149 loc) · 4.51 KB
/
geoip2.go
File metadata and controls
168 lines (149 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Package geoip2 provides middleware for resolving a user's IP address
// using the MaxMind GeoIP2 database.
package geoip2
import (
"errors"
"fmt"
"net"
"net/http"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/zhangjiayin/caddy-geoip2/replacer"
"go.uber.org/zap"
)
// GeoIP2 implements the http.handlers.geoip2 middleware.
// It uses GeoIP2 Data to determine the geographic location
// of a client's IP address.
type GeoIP2 struct {
Enable string `json:"enable,omitempty"`
state *GeoIP2State
ctx caddy.Context
mode mode
}
type mode string
// These are the possible values GeoIP2.Enable can have:
// - "strict" only uses remote IP address.
// - "wild" uses X-Forwarded-For if it exists.
// - "trusted_proxies" uses X-Forwarded-For if it exists when trusted_proxies is valid,
// see https://caddyserver.com/docs/caddyfile/options#trusted-proxies.
// - Lookups can also be disabled by setting it to "off", "false" or "0".
//
// The handler defaults to TrustedProxies if the variable is not set.
const (
modeStrict mode = "strict"
modeTrustedProxies mode = "trusted_proxies"
modeWild mode = "wild"
modeDisabled mode = "disabled"
)
func init() {
caddy.RegisterModule(&GeoIP2{})
httpcaddyfile.RegisterHandlerDirective("geoip2_vars", parseCaddyfile)
}
// CaddyModule implements caddy.Module.
func (m *GeoIP2) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.geoip2",
New: func() caddy.Module { return m },
}
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (m *GeoIP2) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
replacer.SetDefaultValues(repl)
if m.mode != modeDisabled {
if m.state != nil && m.state.dbReaders != nil {
clientIP, err := m.getClientIP(r)
if err != nil {
caddy.Log().Named("http.handlers.geoip2").Error(
"getting client IP address",
zap.Error(err),
)
} else {
if len(clientIP) > 0 {
repl.Set("geoip2.ip_address", clientIP.String())
}
m.state.lookup(repl, clientIP)
}
}
}
return next.ServeHTTP(w, r)
}
func (m *GeoIP2) getClientIP(r *http.Request) (net.IP, error) {
var ip string
trustedProxy := caddyhttp.GetVar(r.Context(), caddyhttp.TrustedProxyVarKey).(bool)
fwdFor := r.Header.Get("X-Forwarded-For")
if ((m.mode == modeTrustedProxies && trustedProxy) || m.mode == modeWild) && fwdFor != "" {
ips := strings.Split(fwdFor, ", ")
ip = ips[0]
} else {
// Otherwise, get the client ip from the request remote address.
var err error
ip, _, err = net.SplitHostPort(r.RemoteAddr)
if err != nil {
var addrErr *net.AddrError
if errors.As(err, &addrErr) && addrErr.Err == "missing port in address" {
// It's not critical, attempt to use RemoteAddr as-is
ip = r.RemoteAddr
} else {
return nil, err
}
}
}
// Parse the ip address string into a net.IP.
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return nil, fmt.Errorf("unable to parse address: %q", ip)
}
return parsedIP, nil
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
m := &GeoIP2{}
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (m *GeoIP2) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
if !d.Args(&m.Enable) {
return d.ArgErr()
}
}
return nil
}
// Provision implements caddy.Provisioner.
func (m *GeoIP2) Provision(ctx caddy.Context) error {
caddy.Log().Named("http.handlers.geoip2").Debug("provision")
app, err := ctx.App(moduleName)
if err != nil {
return fmt.Errorf("getting geoip2 app: %w", err)
}
m.state = app.(*GeoIP2State)
m.ctx = ctx
switch strings.ToLower(m.Enable) {
case string(modeStrict):
m.mode = modeStrict
case string(modeWild):
m.mode = modeWild
case "off", "false", "0":
m.mode = modeDisabled
default:
m.mode = modeTrustedProxies
}
return nil
}
// Validate implements caddy.Validator.
func (m *GeoIP2) Validate() error {
caddy.Log().Named("http.handlers.geoip2").Debug("validate")
return nil
}
// Interface guards.
var (
_ caddy.Module = (*GeoIP2)(nil)
_ caddy.Provisioner = (*GeoIP2)(nil)
_ caddy.Validator = (*GeoIP2)(nil)
_ caddyhttp.MiddlewareHandler = (*GeoIP2)(nil)
_ caddyfile.Unmarshaler = (*GeoIP2)(nil)
)