Skip to content

Commit 50281f7

Browse files
committed
2021-12-24 23:28:58 : update datahub
1 parent c3d299a commit 50281f7

File tree

6 files changed

+243
-0
lines changed

6 files changed

+243
-0
lines changed

Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
syncwjt:
3+
@read -p "提示:同步操作尽量在完成一个完整功能特性后进行,请输入提交描述 (wjt): " cimsg; \
4+
git commit -am "$(shell date "+%F %T") : $${cimsg}" || echo "no commit"
5+
# 切换主分支并更新
6+
git checkout develop
7+
git pull origin develop
8+
# 切换开发分支变基合并提交
9+
git checkout wjt
10+
git rebase -i develop
11+
# 切换回主分支并合并开发者分支,推送主分支到远程,方便其他开发者合并
12+
git checkout develop
13+
git merge --no-ff wjt
14+
git push origin develop
15+
# 切换回自己的开发分支继续工作
16+
git checkout wjt
17+
18+

plugin/datahub/dataevent.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package datahub
2+
3+
import (
4+
"github.com/coredns/coredns/request"
5+
)
6+
7+
type DnsNotify struct {
8+
Topic string `json:"topic"`
9+
Client string `json:"client"`
10+
QName string `json:"qname"`
11+
Class string `json:"class"`
12+
QType string `json:"qtype"`
13+
}
14+
15+
func (dh *Datahub) NotifyMessage(topic string, state *request.Request) {
16+
nmsg := new(DnsNotify)
17+
nmsg.Topic = topic
18+
nmsg.Client = state.IP()
19+
nmsg.QName = state.QName()
20+
nmsg.QType = state.Type()
21+
nmsg.Class = state.Class()
22+
go dh.notifyServer.sendNotify(topic, nmsg)
23+
}

plugin/datahub/dnstap.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package datahub
2+
3+
import (
4+
"net"
5+
"strconv"
6+
"time"
7+
8+
"github.com/coredns/coredns/plugin/dnstap/msg"
9+
"github.com/coredns/coredns/request"
10+
11+
tap "github.com/dnstap/golang-dnstap"
12+
"github.com/miekg/dns"
13+
)
14+
15+
// toDnstap will send the forward and received message to the dnstap plugin.
16+
func (dh *Datahub) ToDnstap(host string, state *request.Request, reply *dns.Msg, start time.Time) {
17+
if tapPlugin == nil {
18+
return
19+
}
20+
// Query
21+
q := new(tap.Message)
22+
msg.SetQueryTime(q, start)
23+
h, p, _ := net.SplitHostPort(host) // this is preparsed and can't err here
24+
port, _ := strconv.ParseUint(p, 10, 32) // same here
25+
ip := net.ParseIP(h)
26+
27+
var ta net.Addr = &net.UDPAddr{IP: ip, Port: int(port)}
28+
t := state.Proto()
29+
if t == "tcp" {
30+
ta = &net.TCPAddr{IP: ip, Port: int(port)}
31+
}
32+
33+
// Forwarder dnstap messages are from the perspective of the downstream server
34+
// (upstream is the forward server)
35+
msg.SetQueryAddress(q, state.W.RemoteAddr())
36+
msg.SetResponseAddress(q, ta)
37+
38+
if tapPlugin.IncludeRawMessage {
39+
buf, _ := state.Req.Pack()
40+
q.QueryMessage = buf
41+
}
42+
msg.SetType(q, tap.Message_FORWARDER_QUERY)
43+
tapPlugin.TapMessage(q)
44+
45+
// Response
46+
if reply != nil {
47+
r := new(tap.Message)
48+
if tapPlugin.IncludeRawMessage {
49+
buf, _ := reply.Pack()
50+
r.ResponseMessage = buf
51+
}
52+
msg.SetQueryTime(r, start)
53+
msg.SetQueryAddress(r, state.W.RemoteAddr())
54+
msg.SetResponseAddress(r, ta)
55+
msg.SetResponseTime(r, time.Now())
56+
msg.SetType(r, tap.Message_FORWARDER_RESPONSE)
57+
tapPlugin.TapMessage(r)
58+
}
59+
}

plugin/datahub/notify.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package datahub
2+
3+
import (
4+
"github.com/ca17/datahub/plugin/pkg/httpc"
5+
"github.com/ca17/datahub/plugin/pkg/stringset"
6+
"github.com/ca17/datahub/plugin/pkg/validutil"
7+
)
8+
9+
type notifyServer struct {
10+
servers *stringset.StringSet
11+
}
12+
13+
func newNotifyServer() *notifyServer {
14+
return &notifyServer{servers: stringset.New()}
15+
}
16+
17+
func (s *notifyServer) addServer(server ...string) bool {
18+
if !validutil.IsURL(server) {
19+
return false
20+
}
21+
s.servers.Add(server...)
22+
return true
23+
}
24+
25+
func (s *notifyServer) sendNotify(topic string, nmsg *DnsNotify) {
26+
s.servers.ForEach(func(url string) {
27+
_, err := httpc.PostJson(url, nmsg)
28+
if err != nil {
29+
log.Errorf("send notify error %s", err.Error())
30+
}
31+
})
32+
}

plugin/pkg/set/stringset.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package stringset
2+
3+
//
4+
// import "fmt"
5+
//
6+
// // Set is an unsorted set of unique strings.
7+
// type Set map[string]struct{}
8+
//
9+
// // New returns an initialized Set.
10+
// func New() Set {
11+
// return Set{}
12+
// }
13+
//
14+
// // NewFromSlice returns a new set constructed from the given slice. Any
15+
// // duplicate elements will be removed.
16+
// func NewFromSlice(slice []string) Set {
17+
// s := New()
18+
// for _, v := range slice {
19+
// s.Add(v)
20+
// }
21+
// return s
22+
// }
23+
//
24+
// // Add adds each value in vs to the set. If any value is alredy in the set,
25+
// // this has no effect.
26+
// func (s Set) Add(vs ...string) {
27+
// for _, v := range vs {
28+
// s[v] = struct{}{}
29+
// }
30+
// }
31+
//
32+
// // Remove removes v from the set. If v is not in the set, this has no effect.
33+
// func (s Set) Remove(v string) {
34+
// delete(s, v)
35+
// }
36+
//
37+
// // Contains returns true if the set contains v and false otherwise.
38+
// func (s Set) Contains(v string) bool {
39+
// _, ok := s[v]
40+
// return ok
41+
// }
42+
//
43+
// // Slice returns the elements in the set as a slice of strings. It returns an
44+
// // empty slice if the set contains no elements. The elements returned will be
45+
// // in random order.
46+
// func (s Set) Slice() []string {
47+
// slice := make([]string, len(s))
48+
// i := 0
49+
// for v := range s {
50+
// slice[i] = v
51+
// i++
52+
// }
53+
// return slice
54+
// }
55+
//
56+
// // String implements the Stringer interface.
57+
// func (s Set) String() string {
58+
// return fmt.Sprint(s.Slice())
59+
// }
60+
//
61+
// // Union returns a new set which contains all elements that are in either a or
62+
// // b.
63+
// func Union(a, b Set) Set {
64+
// result := New()
65+
// for v := range a {
66+
// result.Add(v)
67+
// }
68+
// for v := range b {
69+
// result.Add(v)
70+
// }
71+
// return result
72+
// }
73+
//
74+
// // Intersect returns a new set which contains only elements that are in both a
75+
// // and b.
76+
// func Intersect(a, b Set) Set {
77+
// result := New()
78+
// for v := range a {
79+
// if b.Contains(v) {
80+
// result.Add(v)
81+
// }
82+
// }
83+
// return result
84+
// }
85+
//
86+
// // Diff returns a new set which contains all elements in a that are not in b.
87+
// func Diff(a, b Set) Set {
88+
// result := New()
89+
// for v := range a {
90+
// if !b.Contains(v) {
91+
// result.Add(v)
92+
// }
93+
// }
94+
// return result
95+
// }
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package stringset
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestStringSet_Match(t *testing.T) {
8+
9+
set := New()
10+
set.Add("aaaaa", "bbbbbb", "ccc", "ddd")
11+
t.Log(set.Contains("aaaaa"))
12+
t.Log(set.Contains("ccc"))
13+
t.Log(set.MatchFirst("aasdddccasdadsads"))
14+
set.Remove("ddd")
15+
t.Log(set.MatchFirst("aasdddccasdadsads"))
16+
}

0 commit comments

Comments
 (0)