60 lines
950 B
Go
60 lines
950 B
Go
package main
|
|
|
|
import (
|
|
log "github.com/sirupsen/logrus"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
type Server struct {
|
|
host string
|
|
networks []string
|
|
rTimeout time.Duration
|
|
wTimeout time.Duration
|
|
}
|
|
|
|
func (s *Server) Run(handler *Handler) {
|
|
var addr string
|
|
var split []string
|
|
|
|
// Defaults: tcp, udp
|
|
for _, net := range s.networks {
|
|
split = strings.Split(net, ":")
|
|
|
|
net = split[0]
|
|
|
|
addr = s.host
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func (s *Server) start(ds *dns.Server) {
|
|
log.Infof("Start %s listener on %s", ds.Net, ds.Addr)
|
|
|
|
err := ds.ListenAndServe()
|
|
|
|
if err != nil {
|
|
log.Errorf("Start %s listener on %s failed:%s", ds.Net, ds.Addr, err.Error())
|
|
}
|
|
}
|