forked from IrineSistiana/mosdns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |