Merge pull request #9 from gdm85/master

Add client/server support for multiple listen addresses
This commit is contained in:
Star Brilliant
2018-04-16 03:36:30 +08:00
committed by GitHub
6 changed files with 68 additions and 43 deletions

View File

@@ -42,8 +42,8 @@ import (
type Client struct {
conf *config
bootstrap []string
udpServer *dns.Server
tcpServer *dns.Server
udpServers []*dns.Server
tcpServers []*dns.Server
bootstrapResolver *net.Resolver
cookieJar *cookiejar.Jar
httpClientMux *sync.RWMutex
@@ -64,16 +64,21 @@ func NewClient(conf *config) (c *Client, err error) {
c = &Client{
conf: conf,
}
c.udpServer = &dns.Server{
Addr: conf.Listen,
Net: "udp",
Handler: dns.HandlerFunc(c.udpHandlerFunc),
UDPSize: 4096,
}
c.tcpServer = &dns.Server{
Addr: conf.Listen,
Net: "tcp",
Handler: dns.HandlerFunc(c.tcpHandlerFunc),
udpH := dns.HandlerFunc(c.udpHandlerFunc)
tcpH := dns.HandlerFunc(c.tcpHandlerFunc)
for _, addr := range conf.Listen {
c.udpServers = append(c.udpServers, &dns.Server{
Addr: addr,
Net: "udp",
Handler: udpH,
UDPSize: 4096,
})
c.tcpServers = append(c.tcpServers, &dns.Server{
Addr: addr,
Net: "tcp",
Handler: tcpH,
})
}
c.bootstrapResolver = net.DefaultResolver
if len(conf.Bootstrap) != 0 {
@@ -149,27 +154,25 @@ func (c *Client) newHTTPClient() error {
}
func (c *Client) Start() error {
result := make(chan error)
go func() {
err := c.udpServer.ListenAndServe()
if err != nil {
log.Println(err)
}
result <- err
}()
go func() {
err := c.tcpServer.ListenAndServe()
if err != nil {
log.Println(err)
}
result <- err
}()
err := <-result
if err != nil {
return err
result := make(chan error, len(c.udpServers)+len(c.tcpServers))
for _, srv := range append(c.udpServers, c.tcpServers...) {
go func(srv *dns.Server) {
err := srv.ListenAndServe()
if err != nil {
log.Println(err)
}
result <- err
}(srv)
}
err = <-result
return err
for i := 0; i < cap(result); i++ {
err := <-result
if err != nil {
return err
}
}
close(result)
return nil
}
func (c *Client) handlerFunc(w dns.ResponseWriter, r *dns.Msg, isTCP bool) {

View File

@@ -30,7 +30,7 @@ import (
)
type config struct {
Listen string `toml:"listen"`
Listen []string `toml:"listen"`
UpstreamGoogle []string `toml:"upstream_google"`
UpstreamIETF []string `toml:"upstream_ietf"`
Bootstrap []string `toml:"bootstrap"`
@@ -50,8 +50,8 @@ func loadConfig(path string) (*config, error) {
return nil, &configError{fmt.Sprintf("unknown option %q", key.String())}
}
if conf.Listen == "" {
conf.Listen = "127.0.0.1:53"
if len(conf.Listen) == 0 {
conf.Listen = []string{"127.0.0.1:53"}
}
if len(conf.UpstreamGoogle) == 0 && len(conf.UpstreamIETF) == 0 {
conf.UpstreamGoogle = []string{"https://dns.google.com/resolve"}

View File

@@ -1,5 +1,7 @@
# DNS listen port
listen = "127.0.0.1:53"
listen = [
"127.0.0.1:53"
]
# HTTP path for upstream resolver
# If multiple servers are specified, a random one will be chosen each time.

View File

@@ -30,7 +30,7 @@ import (
)
type config struct {
Listen string `toml:"listen"`
Listen []string `toml:"listen"`
Cert string `toml:"cert"`
Key string `toml:"key"`
Path string `toml:"path"`
@@ -51,9 +51,10 @@ func loadConfig(path string) (*config, error) {
return nil, &configError{fmt.Sprintf("unknown option %q", key.String())}
}
if conf.Listen == "" {
conf.Listen = "127.0.0.1:8053"
if len(conf.Listen) == 0 {
conf.Listen = []string{"127.0.0.1:8053"}
}
if conf.Path == "" {
conf.Path = "/dns-query"
}

View File

@@ -1,5 +1,7 @@
# HTTP listen port
listen = "127.0.0.1:8053"
listen = [
"127.0.0.1:8053"
]
# TLS certification file
cert = ""

View File

@@ -75,10 +75,27 @@ func (s *Server) Start() error {
if s.conf.Verbose {
servemux = handlers.CombinedLoggingHandler(os.Stdout, servemux)
}
if s.conf.Cert != "" || s.conf.Key != "" {
return http.ListenAndServeTLS(s.conf.Listen, s.conf.Cert, s.conf.Key, servemux)
listeners := make(chan error, len(s.conf.Listen))
for _, addr := range s.conf.Listen {
if s.conf.Cert != "" || s.conf.Key != "" {
go func() {
listeners <- http.ListenAndServeTLS(addr, s.conf.Cert, s.conf.Key, servemux)
}()
continue
}
go func() {
listeners <- http.ListenAndServe(addr, servemux)
}()
}
return http.ListenAndServe(s.conf.Listen, servemux)
// wait for all handlers
for i := 0; i < cap(listeners); i++ {
err := <-listeners
if err != nil {
return err
}
}
close(listeners)
return nil
}
func (s *Server) handlerFunc(w http.ResponseWriter, r *http.Request) {