godns/server.go

60 lines
950 B
Go
Raw Permalink Normal View History

2013-07-23 11:10:38 +00:00
package main
import (
log "github.com/sirupsen/logrus"
2020-01-25 17:43:02 +00:00
"strings"
2013-07-23 11:10:38 +00:00
"time"
"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
networks []string
2013-07-23 11:10:38 +00:00
rTimeout time.Duration
wTimeout time.Duration
}
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
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) {
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 {
log.Errorf("Start %s listener on %s failed:%s", ds.Net, ds.Addr, err.Error())
2013-07-23 11:10:38 +00:00
}
}