2018-12-05 21:13:29 +08:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2022-11-12 13:18:36 +08:00
|
|
|
stdContext "context"
|
2021-01-23 14:49:46 +08:00
|
|
|
"errors"
|
2018-12-05 21:13:29 +08:00
|
|
|
"net"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/common/sockopt"
|
|
|
|
"github.com/metacubex/mihomo/context"
|
|
|
|
"github.com/metacubex/mihomo/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) {
|
2022-11-12 13:18:36 +08:00
|
|
|
msg, err := handlerWithContext(stdContext.Background(), s.handler, r)
|
2021-01-23 14:49:46 +08:00
|
|
|
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
|
|
|
|
2022-11-12 13:18:36 +08:00
|
|
|
func handlerWithContext(stdCtx stdContext.Context, handler handler, msg *D.Msg) (*D.Msg, error) {
|
2021-01-23 14:49:46 +08:00
|
|
|
if len(msg.Question) == 0 {
|
|
|
|
return nil, errors.New("at least one question is required")
|
2020-09-17 10:48:42 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 13:18:36 +08:00
|
|
|
ctx := context.NewDNSContext(stdCtx, msg)
|
2021-01-23 14:49:46 +08:00
|
|
|
return handler(ctx, msg)
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|
|
|
|
|
2022-01-04 17:58:50 +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 {
|
2022-01-04 17:58:50 +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
|
|
|
}
|
|
|
|
|
2022-01-05 11:41:31 +08:00
|
|
|
if addr == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
2022-01-04 17:58:50 +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
|
|
|
}
|