diff options
author | 2022-05-02 19:25:02 +0200 | |
---|---|---|
committer | 2022-05-02 18:25:02 +0100 | |
commit | 4ae29a449c6b89c44a95c00a8ca97b7db953793f (patch) | |
tree | 20fb73c7aa2fa23cfe7e47b74deff56f85e92098 /plugin/geoip/geoip.go | |
parent | 66f2ac7568ccb0178cc9ce6dbd7320bcd3428d64 (diff) | |
download | coredns-4ae29a449c6b89c44a95c00a8ca97b7db953793f.tar.gz coredns-4ae29a449c6b89c44a95c00a8ca97b7db953793f.tar.zst coredns-4ae29a449c6b89c44a95c00a8ca97b7db953793f.zip |
geoip: read source IP from EDNS0 subnet if provided (#5183)
* geoip: read source IP from EDNS0 subnet if provided
This patch implements EDNS backend processing (similar in powerdns: https://doc.powerdns.com/authoritative/settings.html#setting-edns-subnet-processing). This feature comes very handy to test whether your geo config is working properly.
Signed-off-by: Balazs Nagy <julsevern@gmail.com>
Diffstat (limited to 'plugin/geoip/geoip.go')
-rw-r--r-- | plugin/geoip/geoip.go | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/plugin/geoip/geoip.go b/plugin/geoip/geoip.go index 0aad35999..3451c82a0 100644 --- a/plugin/geoip/geoip.go +++ b/plugin/geoip/geoip.go @@ -20,8 +20,9 @@ var log = clog.NewWithPlugin(pluginName) // GeoIP is a plugin that add geo location data to the request context by looking up a maxmind // geoIP2 database, and which data can be later consumed by other middlewares. type GeoIP struct { - Next plugin.Handler - db db + Next plugin.Handler + db db + edns0 bool } type db struct { @@ -37,7 +38,7 @@ const ( var probingIP = net.ParseIP("127.0.0.1") -func newGeoIP(dbPath string) (*GeoIP, error) { +func newGeoIP(dbPath string, edns0 bool) (*GeoIP, error) { reader, err := geoip2.Open(dbPath) if err != nil { return nil, fmt.Errorf("failed to open database file: %v", err) @@ -66,7 +67,7 @@ func newGeoIP(dbPath string) (*GeoIP, error) { return nil, fmt.Errorf("database does not provide city schema") } - return &GeoIP{db: db}, nil + return &GeoIP{db: db, edns0: edns0}, nil } // ServeDNS implements the plugin.Handler interface. @@ -79,6 +80,17 @@ func (g GeoIP) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) ( func (g GeoIP) Metadata(ctx context.Context, state request.Request) context.Context { srcIP := net.ParseIP(state.IP()) + if g.edns0 { + if o := state.Req.IsEdns0(); o != nil { + for _, s := range o.Option { + if e, ok := s.(*dns.EDNS0_SUBNET); ok { + srcIP = e.Address + break + } + } + } + } + switch { case g.db.provides&city == city: data, err := g.db.City(srcIP) |