2018-12-05 21:13:29 +08:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2021-01-23 14:49:46 +08:00
|
|
|
"errors"
|
2018-12-05 21:13:29 +08:00
|
|
|
"net"
|
|
|
|
|
2020-04-11 21:45:56 +08:00
|
|
|
"github.com/Dreamacro/clash/common/sockopt"
|
2021-01-23 14:49:46 +08:00
|
|
|
"github.com/Dreamacro/clash/context"
|
2020-06-15 10:34:15 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2020-04-11 21:45:56 +08:00
|
|
|
|
2018-12-05 21:13:29 +08:00
|
|
|
D "github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
address string
|
|
|
|
server = &Server{}
|
2019-05-03 00:05:14 +08:00
|
|
|
|
|
|
|
dnsDefaultTTL uint32 = 600
|
2018-12-05 21:13:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
*D.Server
|
2019-07-14 19:29:58 +08:00
|
|
|
handler handler
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
|
2021-01-23 14:49:46 +08:00
|
|
|
// ServeDNS implement D.Handler ServeDNS
|
2018-12-05 21:13:29 +08:00
|
|
|
func (s *Server) ServeDNS(w D.ResponseWriter, r *D.Msg) {
|
2021-01-23 14:49:46 +08:00
|
|
|
msg, err := handlerWithContext(s.handler, r)
|
|
|
|
if err != nil {
|
2019-07-14 19:29:58 +08:00
|
|
|
D.HandleFailed(w, r)
|
2019-05-03 00:05:14 +08:00
|
|
|
return
|
|
|
|
}
|
2021-06-24 13:38:44 +08:00
|
|
|
msg.Compress = true
|
2021-01-23 14:49:46 +08:00
|
|
|
w.WriteMsg(msg)
|
|
|
|
}
|
2019-05-03 00:05:14 +08:00
|
|
|
|
2021-01-23 14:49:46 +08:00
|
|
|
func handlerWithContext(handler handler, msg *D.Msg) (*D.Msg, error) {
|
|
|
|
if len(msg.Question) == 0 {
|
|
|
|
return nil, errors.New("at least one question is required")
|
2020-09-17 10:48:42 +08:00
|
|
|
}
|
|
|
|
|
2021-01-23 14:49:46 +08:00
|
|
|
ctx := context.NewDNSContext(msg)
|
|
|
|
return handler(ctx, msg)
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|
|
|
|
|
2021-07-01 22:49:29 +08:00
|
|
|
func (s *Server) SetHandler(handler handler) {
|
2019-07-14 19:29:58 +08:00
|
|
|
s.handler = handler
|
2019-03-23 19:41:41 +08:00
|
|
|
}
|
2018-12-05 21:13:29 +08:00
|
|
|
|
2021-12-26 22:08:53 +08:00
|
|
|
func ReCreateServer(addr string, resolver *Resolver, mapper *ResolverEnhancer) {
|
2019-07-15 10:18:42 +08:00
|
|
|
if addr == address && resolver != nil {
|
2021-07-01 22:49:29 +08:00
|
|
|
handler := NewHandler(resolver, mapper)
|
|
|
|
server.SetHandler(handler)
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-23 19:41:41 +08:00
|
|
|
if server.Server != nil {
|
|
|
|
server.Shutdown()
|
2020-06-18 18:11:02 +08:00
|
|
|
server = &Server{}
|
2019-05-18 17:52:42 +08:00
|
|
|
address = ""
|
2019-03-23 19:41:41 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 22:08:53 +08:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start DNS server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-12-05 21:13:29 +08:00
|
|
|
_, port, err := net.SplitHostPort(addr)
|
|
|
|
if port == "0" || port == "" || err != nil {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", addr)
|
|
|
|
if err != nil {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p, err := net.ListenUDP("udp", udpAddr)
|
|
|
|
if err != nil {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:45:56 +08:00
|
|
|
err = sockopt.UDPReuseaddr(p)
|
|
|
|
if err != nil {
|
2020-06-15 10:34:15 +08:00
|
|
|
log.Warnln("Failed to Reuse UDP Address: %s", err)
|
2021-12-26 22:08:53 +08:00
|
|
|
|
|
|
|
err = nil
|
2020-04-11 21:45:56 +08:00
|
|
|
}
|
|
|
|
|
2018-12-05 21:13:29 +08:00
|
|
|
address = addr
|
2021-07-01 22:49:29 +08:00
|
|
|
handler := NewHandler(resolver, mapper)
|
2019-07-14 19:29:58 +08:00
|
|
|
server = &Server{handler: handler}
|
2018-12-05 21:13:29 +08:00
|
|
|
server.Server = &D.Server{Addr: addr, PacketConn: p, Handler: server}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
server.ActivateAndServe()
|
|
|
|
}()
|
2021-12-26 22:08:53 +08:00
|
|
|
|
|
|
|
log.Infoln("DNS server listening at: %s", p.LocalAddr().String())
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|