Browse Source

update fakedns handler

pull/15/head
Jason 5 years ago
parent
commit
3e1b86efe1
  1. 8
      component/fakedns/middleware.go
  2. 17
      component/fakedns/resolver.go

8
component/fakedns/middleware.go

@ -112,3 +112,11 @@ func hostResolve(hosts *T.Trie, r *D.Msg) *D.Msg {
setMsgTTL(msg, dnsDefaultTTL)
return msg
}
func newHandler(hosts *T.Trie, pool *F.Pool, backendDNS []string) D.HandlerFunc {
return func(w D.ResponseWriter, r *D.Msg) {
msg := resolve(hosts, pool, backendDNS, r)
w.WriteMsg(msg)
return
}
}

17
component/fakedns/resolver.go

@ -24,8 +24,9 @@ const (
type Resolver struct {
b []string
h *T.Trie
p *F.Pool
t *T.Trie
h D.HandlerFunc
*D.Server
ServeAddr string
@ -36,8 +37,7 @@ func (r *Resolver) ServeDNS(w D.ResponseWriter, req *D.Msg) {
D.HandleFailed(w, req)
return
}
msg := resolve(r.h, r.p, r.b, req)
w.WriteMsg(msg)
r.h(w, req)
}
func (r *Resolver) Start() error {
@ -56,7 +56,7 @@ func (r *Resolver) Start() error {
return err
}
r.Server = &D.Server{Addr: r.ServeAddr, PacketConn: pc}
r.Server = &D.Server{Addr: r.ServeAddr, PacketConn: pc, Handler: r.h}
go func() {
r.ActivateAndServe()
}()
@ -105,7 +105,7 @@ func NewResolver(a, h, b string) (*Resolver, error) {
return nil, err
}
hosts := func(str string) *T.Trie {
tree := func(str string) *T.Trie {
tree := T.New()
s := strings.Split(str, ",")
for _, host := range s {
@ -122,10 +122,13 @@ func NewResolver(a, h, b string) (*Resolver, error) {
return tree
}(h)
backendDNS := strings.Split(b, ",")
handler := newHandler(tree, pool, backendDNS)
return &Resolver{
b: strings.Split(b, ","),
h: hosts,
b: backendDNS,
p: pool,
t: tree,
h: handler,
ServeAddr: a,
}, nil
}

Loading…
Cancel
Save