Skip to content

Commit 09721e0

Browse files
committed
init
0 parents  commit 09721e0

13 files changed

+37715
-0
lines changed

.editorconfig

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
root = true
2+
3+
# all files
4+
[*.go]
5+
indent_style = tab
6+
indent_size = 4
7+
insert_final_newline = true
8+
9+
[*.py]
10+
indent_style = space
11+
indent_size = 4
12+
13+
[Makefile]
14+
indent_style = tab
15+
16+
[*.js]
17+
charset = utf-8
18+
indent_style = space
19+
indent_size = 4
20+
21+
[*.json]
22+
indent_style = space
23+
indent_size = 2

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
data/geoip.data filter=lfs diff=lfs merge=lfs -text
2+
data/geosite.data filter=lfs diff=lfs merge=lfs -text

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
.vscode
3+
release
4+
__debug_bin
5+
.DS_Store
6+
build

Corefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
. {
2+
dnssrc locals.conf {
3+
expire 30s
4+
path_reload 3s
5+
max_fails 0
6+
health_check 30s
7+
to 114.114.114.114 223.5.5.5
8+
policy round_robin
9+
bootstrap 172.21.66.1
10+
debug
11+
}
12+
13+
dnssrc 172.21.66.137 192.168.0.1/24 {
14+
expire 1s
15+
path_reload 3s
16+
max_fails 0
17+
health_check 30s
18+
to json-doh://dns.google/resolve
19+
to 1.1.1.1
20+
to 9.9.9.9
21+
policy round_robin
22+
bootstrap 172.21.66.1
23+
debug
24+
}
25+
}

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# dnssrc
2+
3+
*datahub* - a CoreDNS Data Manage Plugin
4+
5+
6+
# Example
7+
8+

data/geoip.dat

10.6 MB
Binary file not shown.

data/geosite.dat

+36,645
Large diffs are not rendered by default.

go.mod

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/ca17/datahub
2+
3+
go 1.16
4+
5+
require (
6+
github.com/c-robinson/iplib v1.0.3
7+
github.com/coredns/caddy v1.1.1
8+
github.com/coredns/coredns v1.8.6
9+
github.com/m13253/dns-over-https/v2 v2.3.0
10+
github.com/mdlayher/netlink v1.4.2
11+
github.com/miekg/dns v1.1.43
12+
github.com/prometheus/client_golang v1.11.0
13+
)

main.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
_ "github.com/ca17/dnssrc/plugin/dnssrc"
5+
"github.com/coredns/coredns/core/dnsserver"
6+
_ "github.com/coredns/coredns/core/plugin"
7+
"github.com/coredns/coredns/coremain"
8+
)
9+
10+
func index(slice []string, item string) int {
11+
for i := range slice {
12+
if slice[i] == item {
13+
return i
14+
}
15+
}
16+
return -1
17+
}
18+
19+
func main() {
20+
// insert dnssrc before forward
21+
idx := index(dnsserver.Directives, "geoip")
22+
dnsserver.Directives = append(dnsserver.Directives[:idx], append([]string{"datahub"}, dnsserver.Directives[idx:]...)...)
23+
coremain.Run()
24+
}

plugin/pkg/loader/load_helper.go

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (C) 2020-2021, IrineSistiana
2+
//
3+
// This file is part of mosdns.
4+
//
5+
// mosdns is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// mosdns is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package loader
19+
20+
import (
21+
"fmt"
22+
"strings"
23+
"time"
24+
25+
"github.com/ca17/datahub/plugin/pkg/v2data"
26+
"github.com/golang/protobuf/proto"
27+
)
28+
29+
var matcherCache = NewCache()
30+
31+
const (
32+
cacheTTL = time.Second * 30
33+
)
34+
35+
// mustHaveAttr checks if attr has all wanted attrs.
36+
func mustHaveAttr(attr, wanted []string) bool {
37+
if len(wanted) == 0 {
38+
return true
39+
}
40+
if len(attr) == 0 {
41+
return false
42+
}
43+
44+
for _, w := range wanted {
45+
ok := false
46+
for _, got := range attr {
47+
if got == w {
48+
ok = true
49+
break
50+
}
51+
}
52+
if !ok { // this attr is not in d.
53+
return false
54+
}
55+
}
56+
return true
57+
}
58+
59+
func LoadGeoSiteFromDAT(file, countryCode string) (*v2data.GeoSite, error) {
60+
geoSiteList, err := LoadGeoSiteList(file)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
countryCode = strings.ToUpper(countryCode)
66+
entry := geoSiteList.GetEntry()
67+
for i := range entry {
68+
if strings.ToUpper(entry[i].CountryCode) == countryCode {
69+
return entry[i], nil
70+
}
71+
}
72+
73+
return nil, fmt.Errorf("can not find category %s in %s", countryCode, file)
74+
}
75+
76+
func LoadGeoSiteList(file string) (*v2data.GeoSiteList, error) {
77+
data, raw, err := matcherCache.LoadFromCacheOrRawDisk(file)
78+
if err != nil {
79+
return nil, err
80+
}
81+
// load from cache
82+
if geoSiteList, ok := data.(*v2data.GeoSiteList); ok {
83+
return geoSiteList, nil
84+
}
85+
86+
// load from disk
87+
geoSiteList := new(v2data.GeoSiteList)
88+
if err := proto.Unmarshal(raw, geoSiteList); err != nil {
89+
return nil, err
90+
}
91+
92+
return geoSiteList, nil
93+
}
94+
95+
96+
func LoadGeoIPListFromDAT(file string) (*v2data.GeoIPList, error) {
97+
data, raw, err := matcherCache.LoadFromCacheOrRawDisk(file)
98+
if err != nil {
99+
return nil, err
100+
}
101+
// load from cache
102+
if geoIPList, ok := data.(*v2data.GeoIPList); ok {
103+
return geoIPList, nil
104+
}
105+
106+
// load from disk
107+
geoIPList := new(v2data.GeoIPList)
108+
if err := proto.Unmarshal(raw, geoIPList); err != nil {
109+
return nil, err
110+
}
111+
112+
// cache the file
113+
matcherCache.Put(file, geoIPList, cacheTTL)
114+
return geoIPList, nil
115+
}

plugin/pkg/loader/load_once.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright (C) 2020-2021, IrineSistiana
2+
//
3+
// This key is part of mosdns.
4+
//
5+
// mosdns is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// mosdns is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package loader
19+
20+
import (
21+
"io/ioutil"
22+
"os"
23+
"sync"
24+
"time"
25+
)
26+
27+
type LoadOnceCache struct {
28+
l sync.Mutex
29+
cache map[string]interface{}
30+
}
31+
32+
func NewCache() *LoadOnceCache {
33+
return &LoadOnceCache{
34+
cache: make(map[string]interface{}),
35+
}
36+
}
37+
38+
func (c *LoadOnceCache) Put(key string, data interface{}, ttl time.Duration) {
39+
if ttl <= 0 {
40+
return
41+
}
42+
43+
c.l.Lock()
44+
defer c.l.Unlock()
45+
46+
c.cache[key] = data
47+
48+
rm := func() { c.Remove(key) }
49+
time.AfterFunc(ttl, rm)
50+
}
51+
52+
func (c *LoadOnceCache) Remove(key string) {
53+
c.l.Lock()
54+
defer c.l.Unlock()
55+
56+
delete(c.cache, key)
57+
}
58+
59+
func (c *LoadOnceCache) Load(key string) (interface{}, bool) {
60+
c.l.Lock()
61+
defer c.l.Unlock()
62+
63+
data, ok := c.cache[key]
64+
return data, ok
65+
}
66+
67+
func (c *LoadOnceCache) LoadFromCacheOrRawDisk(file string) (interface{}, []byte, error) {
68+
// load from cache
69+
data, ok := c.Load(file)
70+
if ok {
71+
return data, nil, nil
72+
}
73+
74+
// load from disk
75+
f, err := os.Open(file)
76+
if err != nil {
77+
return nil, nil, err
78+
}
79+
defer f.Close()
80+
81+
b, err := ioutil.ReadAll(f)
82+
if err != nil {
83+
return nil, nil, err
84+
}
85+
86+
return nil, b, nil
87+
}

0 commit comments

Comments
 (0)