2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-25 17:43:02 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"meow.tf/joker/godns/log"
|
|
|
|
"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
|
|
|
|
port int
|
|
|
|
rTimeout time.Duration
|
|
|
|
wTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Run() {
|
2018-07-03 05:35:20 +00:00
|
|
|
handler := NewHandler()
|
2013-07-23 11:10:38 +00:00
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
nets := viper.GetStringSlice("networks")
|
2013-07-23 11:10:38 +00:00
|
|
|
|
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 nets {
|
|
|
|
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) {
|
2020-01-25 17:43:02 +00:00
|
|
|
log.Info("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 {
|
2020-01-25 17:43:02 +00:00
|
|
|
log.Error("Start %s listener on %s failed:%s", ds.Net, ds.Addr, err.Error())
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
}
|