Allow multiple bootstrap servers

This commit is contained in:
Star Brilliant
2017-11-25 15:46:29 +08:00
parent 2577b720c7
commit a0bd8eb8a1
3 changed files with 22 additions and 11 deletions
+15 -9
View File
@@ -22,6 +22,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math/rand"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
@@ -37,7 +38,7 @@ import (
type Client struct { type Client struct {
addr string addr string
upstream string upstream string
bootstrap string bootstraps []string
timeout uint timeout uint
noECS bool noECS bool
verbose bool verbose bool
@@ -46,10 +47,11 @@ type Client struct {
httpClient *http.Client httpClient *http.Client
} }
func NewClient(addr, upstream, bootstrap string, timeout uint, noECS, verbose bool) (c *Client, err error) { func NewClient(addr, upstream string, bootstraps []string, timeout uint, noECS, verbose bool) (c *Client, err error) {
c = &Client { c = &Client {
addr: addr, addr: addr,
upstream: upstream, upstream: upstream,
bootstraps: bootstraps,
timeout: timeout, timeout: timeout,
noECS: noECS, noECS: noECS,
verbose: verbose, verbose: verbose,
@@ -66,18 +68,22 @@ func NewClient(addr, upstream, bootstrap string, timeout uint, noECS, verbose bo
Handler: dns.HandlerFunc(c.tcpHandlerFunc), Handler: dns.HandlerFunc(c.tcpHandlerFunc),
} }
bootResolver := net.DefaultResolver bootResolver := net.DefaultResolver
if bootstrap != "" { if len(c.bootstraps) != 0 {
bootstrapAddr, err := net.ResolveUDPAddr("udp", bootstrap) for i, bootstrap := range c.bootstraps {
if err != nil { bootstrapAddr, err := net.ResolveUDPAddr("udp", bootstrap)
bootstrapAddr, err = net.ResolveUDPAddr("udp", "[" + bootstrap + "]:53") if err != nil {
bootstrapAddr, err = net.ResolveUDPAddr("udp", "[" + bootstrap + "]:53")
}
if err != nil { return nil, err }
c.bootstraps[i] = bootstrapAddr.String()
} }
if err != nil { return nil, err }
c.bootstrap = bootstrapAddr.String()
bootResolver = &net.Resolver { bootResolver = &net.Resolver {
PreferGo: true, PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
var d net.Dialer var d net.Dialer
conn, err := d.DialContext(ctx, network, c.bootstrap) num_servers := len(c.bootstraps)
bootstrap := c.bootstraps[rand.Intn(num_servers)]
conn, err := d.DialContext(ctx, network, bootstrap)
return conn, err return conn, err
}, },
} }
+6 -1
View File
@@ -21,6 +21,7 @@ package main
import ( import (
"flag" "flag"
"log" "log"
"strings"
) )
func main() { func main() {
@@ -32,7 +33,11 @@ func main() {
verbose := flag.Bool("verbose", false, "Enable logging") verbose := flag.Bool("verbose", false, "Enable logging")
flag.Parse() flag.Parse()
client, err := NewClient(*addr, *upstream, *bootstrap, *timeout, *noECS, *verbose) bootstraps := []string {}
if *bootstrap != "" {
bootstraps = strings.Split(*bootstrap, ",")
}
client, err := NewClient(*addr, *upstream, bootstraps, *timeout, *noECS, *verbose)
if err != nil { log.Fatalln(err) } if err != nil { log.Fatalln(err) }
_ = client.Start() _ = client.Start()
} }
+1 -1
View File
@@ -4,7 +4,7 @@ After=network.target
[Service] [Service]
AmbientCapabilities=CAP_NET_BIND_SERVICE AmbientCapabilities=CAP_NET_BIND_SERVICE
ExecStart=/usr/local/bin/doh-client -addr 127.0.0.1:53 -upstream https://dns.google.com/resolve -bootstrap 8.8.8.8:53 ExecStart=/usr/local/bin/doh-client -addr 127.0.0.1:53 -upstream https://dns.google.com/resolve -bootstrap 8.8.8.8:53,8.8.4.4:53
LimitNOFILE=1048576 LimitNOFILE=1048576
Restart=always Restart=always
RestartSec=3 RestartSec=3