Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 Maxmind DB 匹配器,用于匹配 response ip #643

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/miekg/dns v1.1.62
github.com/mitchellh/mapstructure v1.5.0
github.com/nadoo/ipset v0.5.0
github.com/oschwald/geoip2-golang v1.11.0
github.com/prometheus/client_golang v1.20.5
github.com/quic-go/quic-go v0.48.2
github.com/spf13/cobra v1.8.1
Expand Down Expand Up @@ -47,6 +48,7 @@ require (
github.com/mdlayher/socket v0.5.1 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.22.0 // indirect
github.com/oschwald/maxminddb-golang v1.13.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ github.com/onsi/ginkgo/v2 v2.22.0 h1:Yed107/8DjTr0lKCNt7Dn8yQ6ybuDRQoMGrNFKzMfHg
github.com/onsi/ginkgo/v2 v2.22.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
github.com/onsi/gomega v1.34.2 h1:pNCwDkzrsv7MS9kpaQvVb1aVLahQXyJ/Tv5oAZMI3i8=
github.com/onsi/gomega v1.34.2/go.mod h1:v1xfxRgk0KIsG+QOdm7p8UosrOzPYRo60fd3B/1Dukc=
github.com/oschwald/geoip2-golang v1.11.0 h1:hNENhCn1Uyzhf9PTmquXENiWS6AlxAEnBII6r8krA3w=
github.com/oschwald/geoip2-golang v1.11.0/go.mod h1:P9zG+54KPEFOliZ29i7SeYZ/GM6tfEL+rgSn03hYuUo=
github.com/oschwald/maxminddb-golang v1.13.0 h1:R8xBorY71s84yO06NgTmQvqvTvlS/bnYZrrWX1MElnU=
github.com/oschwald/maxminddb-golang v1.13.0/go.mod h1:BU0z8BfFVhi1LQaonTwwGQlsHUEu9pWNdMfmq4ztm0o=
github.com/oschwald/maxminddb-golang v1.13.1 h1:G3wwjdN9JmIK2o/ermkHM+98oX5fS+k5MbwsmL4MRQE=
github.com/oschwald/maxminddb-golang v1.13.1/go.mod h1:K4pgV9N/GcK694KSTmVSDTODk4IsCNThNdTmnaBZ/F8=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
Expand Down
5 changes: 5 additions & 0 deletions plugin/data_provider/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package data_provider
import (
"github.com/IrineSistiana/mosdns/v5/pkg/matcher/domain"
"github.com/IrineSistiana/mosdns/v5/pkg/matcher/netlist"
"github.com/oschwald/geoip2-golang"
)

type DomainMatcherProvider interface {
Expand All @@ -31,3 +32,7 @@ type DomainMatcherProvider interface {
type IPMatcherProvider interface {
GetIPMatcher() netlist.Matcher
}

type MmdbMatcherProvider interface {
GetMmdbMatcher() *geoip2.Reader
}
61 changes: 61 additions & 0 deletions plugin/data_provider/mmdb/mmdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2020-2022, IrineSistiana
*
* This file is part of mosdns.
*
* mosdns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mosdns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package mmdb

import (
"github.com/IrineSistiana/mosdns/v5/coremain"
"github.com/IrineSistiana/mosdns/v5/plugin/data_provider"
"github.com/oschwald/geoip2-golang"
)

const PluginType = "mmdb"

func init() {
coremain.RegNewPluginFunc(PluginType, Init, func() any { return new(Args) })
}

func Init(bp *coremain.BP, args any) (any, error) {
return NewMmdb(bp, args.(*Args))
}

type Args struct {
File string `yaml:"file"`
}

var _ data_provider.MmdbMatcherProvider = (*Mmdb)(nil)

type Mmdb struct {
mmdb *geoip2.Reader
}

func (m *Mmdb) GetMmdbMatcher() *geoip2.Reader {
return m.mmdb
}

func NewMmdb(bp *coremain.BP, args *Args) (*Mmdb, error) {
m := &Mmdb{}

db, err := geoip2.Open(args.File)
if err == nil {
m.mmdb = db
}

return m, nil
}
2 changes: 2 additions & 0 deletions plugin/enabled_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
// data provider
_ "github.com/IrineSistiana/mosdns/v5/plugin/data_provider/domain_set"
_ "github.com/IrineSistiana/mosdns/v5/plugin/data_provider/ip_set"
_ "github.com/IrineSistiana/mosdns/v5/plugin/data_provider/mmdb"

// matcher
_ "github.com/IrineSistiana/mosdns/v5/plugin/matcher/client_ip"
Expand All @@ -38,6 +39,7 @@ import (
_ "github.com/IrineSistiana/mosdns/v5/plugin/matcher/random"
_ "github.com/IrineSistiana/mosdns/v5/plugin/matcher/rcode"
_ "github.com/IrineSistiana/mosdns/v5/plugin/matcher/resp_ip"
_ "github.com/IrineSistiana/mosdns/v5/plugin/matcher/resp_ip_mmdb"
_ "github.com/IrineSistiana/mosdns/v5/plugin/matcher/string_exp"

// executable
Expand Down
107 changes: 107 additions & 0 deletions plugin/matcher/resp_ip_mmdb/resp_ip_mmdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (C) 2020-2022, IrineSistiana
*
* This file is part of mosdns.
*
* mosdns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mosdns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package resp_ip_mmdb

import (
"context"
"errors"
"fmt"
"github.com/IrineSistiana/mosdns/v5/pkg/query_context"
"github.com/IrineSistiana/mosdns/v5/plugin/data_provider"
"github.com/IrineSistiana/mosdns/v5/plugin/executable/sequence"
"github.com/miekg/dns"
"github.com/oschwald/geoip2-golang"
"net"
"strings"
)

const PluginType = "resp_ip_mmdb"

func init() {
sequence.MustRegMatchQuickSetup(PluginType, QuickSetup)
}

func QuickSetup(bq sequence.BQ, s string) (sequence.Matcher, error) {
if len(s) == 0 {
return nil, errors.New("a iso code probability is required")
}

args := strings.Fields(s)
if len(args) != 2 {
return nil, errors.New("probability error, must like: resp_ip_mmdb $plugin_name CN")
}

mmdbName, _ := cutPrefix(args[0], "$")

p := bq.M().GetPlugin(mmdbName)
provider, _ := p.(data_provider.MmdbMatcherProvider)
if provider == nil {
return nil, fmt.Errorf("cannot find mmdb %s", mmdbName)
}
m := provider.GetMmdbMatcher()

return &Matcher{args[1], m}, nil
}

type Matcher struct {
isoCode string
mmdb *geoip2.Reader
}

func (m *Matcher) Match(_ context.Context, qCtx *query_context.Context) (bool, error) {
r := qCtx.R()
if r == nil {
return false, nil
}

if m.mmdb == nil {
return false, nil
}

for _, rr := range r.Answer {
var ip net.IP
switch rr := rr.(type) {
case *dns.A:
ip = rr.A
case *dns.AAAA:
ip = rr.AAAA
default:
continue
}

record, err := m.mmdb.Country(ip)
if err != nil {
continue
}

if record.Country.IsoCode == m.isoCode {
return true, nil
}
}

return false, nil
}

func cutPrefix(s string, p string) (string, bool) {
if strings.HasPrefix(s, p) {
return strings.TrimPrefix(s, p), true
}
return s, false
}