|
| 1 | +/* |
| 2 | + * Copyright (C) 2020-2022, IrineSistiana |
| 3 | + * |
| 4 | + * This file is part of mosdns. |
| 5 | + * |
| 6 | + * mosdns is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * mosdns is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package resp_ip_mmdb |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "errors" |
| 25 | + "fmt" |
| 26 | + "github.com/IrineSistiana/mosdns/v5/pkg/query_context" |
| 27 | + "github.com/IrineSistiana/mosdns/v5/plugin/data_provider" |
| 28 | + "github.com/IrineSistiana/mosdns/v5/plugin/executable/sequence" |
| 29 | + "github.com/miekg/dns" |
| 30 | + "github.com/oschwald/geoip2-golang" |
| 31 | + "net" |
| 32 | + "strings" |
| 33 | +) |
| 34 | + |
| 35 | +const PluginType = "resp_ip_mmdb" |
| 36 | + |
| 37 | +func init() { |
| 38 | + sequence.MustRegMatchQuickSetup(PluginType, QuickSetup) |
| 39 | +} |
| 40 | + |
| 41 | +func QuickSetup(bq sequence.BQ, s string) (sequence.Matcher, error) { |
| 42 | + if len(s) == 0 { |
| 43 | + return nil, errors.New("a iso code probability is required") |
| 44 | + } |
| 45 | + |
| 46 | + args := strings.Fields(s) |
| 47 | + if len(args) != 2 { |
| 48 | + return nil, errors.New("probability error, must like: resp_ip_mmdb $plugin_name CN") |
| 49 | + } |
| 50 | + |
| 51 | + mmdbName, _ := cutPrefix(args[0], "$") |
| 52 | + |
| 53 | + p := bq.M().GetPlugin(mmdbName) |
| 54 | + provider, _ := p.(data_provider.MmdbMatcherProvider) |
| 55 | + if provider == nil { |
| 56 | + return nil, fmt.Errorf("cannot find mmdb %s", mmdbName) |
| 57 | + } |
| 58 | + m := provider.GetMmdbMatcher() |
| 59 | + |
| 60 | + return &Matcher{args[1], m}, nil |
| 61 | +} |
| 62 | + |
| 63 | +type Matcher struct { |
| 64 | + isoCode string |
| 65 | + mmdb *geoip2.Reader |
| 66 | +} |
| 67 | + |
| 68 | +func (m *Matcher) Match(_ context.Context, qCtx *query_context.Context) (bool, error) { |
| 69 | + r := qCtx.R() |
| 70 | + if r == nil { |
| 71 | + return false, nil |
| 72 | + } |
| 73 | + |
| 74 | + if m.mmdb == nil { |
| 75 | + return false, nil |
| 76 | + } |
| 77 | + |
| 78 | + for _, rr := range r.Answer { |
| 79 | + var ip net.IP |
| 80 | + switch rr := rr.(type) { |
| 81 | + case *dns.A: |
| 82 | + ip = rr.A |
| 83 | + case *dns.AAAA: |
| 84 | + ip = rr.AAAA |
| 85 | + default: |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + record, err := m.mmdb.Country(ip) |
| 90 | + if err != nil { |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + if record.Country.IsoCode == m.isoCode { |
| 95 | + return true, nil |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return false, nil |
| 100 | +} |
| 101 | + |
| 102 | +func cutPrefix(s string, p string) (string, bool) { |
| 103 | + if strings.HasPrefix(s, p) { |
| 104 | + return strings.TrimPrefix(s, p), true |
| 105 | + } |
| 106 | + return s, false |
| 107 | +} |
0 commit comments