2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-15 03:42:24 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-01-25 17:43:02 +00:00
|
|
|
"strings"
|
2013-07-23 11:10:38 +00:00
|
|
|
"time"
|
2015-02-10 09:00:59 +00:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2019-09-26 04:43:17 +00:00
|
|
|
)
|
2013-07-23 11:10:38 +00:00
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
host string
|
2020-01-25 18:48:26 +00:00
|
|
|
networks []string
|
2013-07-23 11:10:38 +00:00
|
|
|
rTimeout time.Duration
|
|
|
|
wTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
func (s *Server) Run(handler *Handler) {
|
2020-01-25 17:43:02 +00:00
|
|
|
var addr string
|
|
|
|
var split []string
|
2013-07-23 11:10:38 +00:00
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
// Defaults: tcp, udp
|
2020-01-25 18:48:26 +00:00
|
|
|
for _, net := range s.networks {
|
2020-01-25 17:43:02 +00:00
|
|
|
split = strings.Split(net, ":")
|
2013-07-23 11:10:38 +00:00
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
net = split[0]
|
|
|
|
|
|
|
|
addr = s.host
|
2013-07-23 11:10:38 +00:00
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
if len(split) == 1 {
|
|
|
|
addr += ":53"
|
|
|
|
} else {
|
|
|
|
addr += ":" + split[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
h := dns.NewServeMux()
|
|
|
|
h.HandleFunc(".", handler.Bind(net))
|
|
|
|
|
|
|
|
server := &dns.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Net: net,
|
|
|
|
Handler: h,
|
|
|
|
ReadTimeout: s.rTimeout,
|
|
|
|
WriteTimeout: s.wTimeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
go s.start(server)
|
|
|
|
}
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) start(ds *dns.Server) {
|
2021-04-15 03:42:24 +00:00
|
|
|
log.Infof("Start %s listener on %s", ds.Net, ds.Addr)
|
2018-07-03 05:35:20 +00:00
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
err := ds.ListenAndServe()
|
2020-01-25 17:43:02 +00:00
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
if err != nil {
|
2021-04-15 03:42:24 +00:00
|
|
|
log.Errorf("Start %s listener on %s failed:%s", ds.Net, ds.Addr, err.Error())
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
}
|