Breaking change: Use configuration file

This commit is contained in:
Star Brilliant
2017-11-30 20:42:30 +08:00
parent e510d84809
commit 699c8fba16
12 changed files with 261 additions and 94 deletions
+19 -25
View File
@@ -42,53 +42,45 @@ import (
)
type Client struct {
addr string
upstream string
bootstraps []string
timeout uint
noECS bool
verbose bool
conf *config
bootstrap []string
udpServer *dns.Server
tcpServer *dns.Server
httpClient *http.Client
}
func NewClient(addr, upstream string, bootstraps []string, timeout uint, noECS, verbose bool) (c *Client, err error) {
func NewClient(conf *config) (c *Client, err error) {
c = &Client {
addr: addr,
upstream: upstream,
bootstraps: bootstraps,
timeout: timeout,
noECS: noECS,
verbose: verbose,
conf: conf,
}
c.udpServer = &dns.Server {
Addr: addr,
Addr: conf.Listen,
Net: "udp",
Handler: dns.HandlerFunc(c.udpHandlerFunc),
UDPSize: 4096,
}
c.tcpServer = &dns.Server {
Addr: addr,
Addr: conf.Listen,
Net: "tcp",
Handler: dns.HandlerFunc(c.tcpHandlerFunc),
}
bootResolver := net.DefaultResolver
if len(c.bootstraps) != 0 {
for i, bootstrap := range c.bootstraps {
if len(conf.Bootstrap) != 0 {
c.bootstrap = make([]string, len(conf.Bootstrap))
for i, bootstrap := range conf.Bootstrap {
bootstrapAddr, err := net.ResolveUDPAddr("udp", bootstrap)
if err != nil {
bootstrapAddr, err = net.ResolveUDPAddr("udp", "[" + bootstrap + "]:53")
}
if err != nil { return nil, err }
c.bootstraps[i] = bootstrapAddr.String()
c.bootstrap[i] = bootstrapAddr.String()
}
bootResolver = &net.Resolver {
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
var d net.Dialer
num_servers := len(c.bootstraps)
bootstrap := c.bootstraps[rand.Intn(num_servers)]
num_servers := len(c.bootstrap)
bootstrap := c.bootstrap[rand.Intn(num_servers)]
conn, err := d.DialContext(ctx, network, bootstrap)
return conn, err
},
@@ -96,12 +88,12 @@ func NewClient(addr, upstream string, bootstraps []string, timeout uint, noECS,
}
httpTransport := *http.DefaultTransport.(*http.Transport)
httpTransport.DialContext = (&net.Dialer {
Timeout: time.Duration(c.timeout) * time.Second,
Timeout: time.Duration(conf.Timeout) * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
Resolver: bootResolver,
}).DialContext
httpTransport.ResponseHeaderTimeout = time.Duration(c.timeout) * time.Second
httpTransport.ResponseHeaderTimeout = time.Duration(conf.Timeout) * time.Second
// Most CDNs require Cookie support to prevent DDoS attack
cookieJar, err := cookiejar.New(nil)
if err != nil { return nil, err }
@@ -159,11 +151,13 @@ func (c *Client) handlerFunc(w dns.ResponseWriter, r *dns.Msg, isTCP bool) {
questionType = strconv.Itoa(int(question.Qtype))
}
if c.verbose{
if c.conf.Verbose {
fmt.Printf("%s - - [%s] \"%s IN %s\"\n", w.RemoteAddr(), time.Now().Format("02/Jan/2006:15:04:05 -0700"), questionName, questionType)
}
requestURL := fmt.Sprintf("%s?name=%s&type=%s", c.upstream, url.QueryEscape(questionName), url.QueryEscape(questionType))
num_servers := len(c.conf.Upstream)
upstream := c.conf.Upstream[rand.Intn(num_servers)]
requestURL := fmt.Sprintf("%s?name=%s&type=%s", upstream, url.QueryEscape(questionName), url.QueryEscape(questionType))
if r.CheckingDisabled {
requestURL += "&cd=1"
@@ -260,7 +254,7 @@ var (
func (c *Client) findClientIP(w dns.ResponseWriter, r *dns.Msg) (ednsClientAddress net.IP, ednsClientNetmask uint8) {
ednsClientNetmask = 255
if c.noECS {
if c.conf.NoECS {
return net.IPv4(0, 0, 0, 0), 0
}
if opt := r.IsEdns0(); opt != nil {
+69
View File
@@ -0,0 +1,69 @@
/*
DNS-over-HTTPS
Copyright (C) 2017 Star Brilliant <m13253@hotmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
package main
import (
"fmt"
"github.com/BurntSushi/toml"
)
type config struct {
Listen string `toml:"listen"`
Upstream []string `toml:"upstream"`
Bootstrap []string `toml:"bootstrap"`
Timeout uint `toml:"timeout"`
NoECS bool `toml:"no_ecs"`
Verbose bool `toml:"verbose"`
}
func loadConfig(path string) (*config, error) {
conf := &config {}
metaData, err := toml.DecodeFile(path, conf)
if err != nil {
return nil, err
}
for _, key := range metaData.Undecoded() {
return nil, &configError { fmt.Sprintf("unknown option %q", key.String()) }
}
if conf.Listen == "" {
conf.Listen = "127.0.0.1:53"
}
if len(conf.Upstream) == 0 {
conf.Upstream = []string { "https://dns.google.com/resolve" }
}
if conf.Timeout == 0 {
conf.Timeout = 10
}
return conf, nil
}
type configError struct {
err string
}
func (e *configError) Error() string {
return e.err
}
+25
View File
@@ -0,0 +1,25 @@
# DNS listen port
listen = "127.0.0.1:53"
# HTTP path for upstream resolver
# If multiple servers are specified, a random one will be chosen each time.
upstream = [
"https://dns.google.com/resolve",
]
# Bootstrap DNS server to resolve the address of the upstream resolver
# If multiple servers are specified, a random one will be chosen each time.
# If empty, use the system DNS settings.
bootstrap = [
"8.8.8.8:53",
"8.8.4.4:53",
]
# Timeout for upstream request
timeout = 10
# Disable EDNS0-Client-Subnet, do not send client's IP address
no_ecs = false
# Enable logging
verbose = false
+13 -11
View File
@@ -26,23 +26,25 @@ package main
import (
"flag"
"log"
"strings"
)
func main() {
addr := flag.String("addr", "127.0.0.1:53", "DNS listen port")
upstream := flag.String("upstream", "https://dns.google.com/resolve", "HTTP path for upstream resolver")
bootstrap := flag.String("bootstrap", "", "The bootstrap DNS server to resolve the address of the upstream resolver")
timeout := flag.Uint("timeout", 10, "Timeout for upstream request")
noECS := flag.Bool("no-ecs", false, "Disable EDNS0-Client-Subnet, do not send client's IP address")
confPath := flag.String("conf", "doh-client.conf", "Configuration file")
verbose := flag.Bool("verbose", false, "Enable logging")
flag.Parse()
bootstraps := []string {}
if *bootstrap != "" {
bootstraps = strings.Split(*bootstrap, ",")
conf, err := loadConfig(*confPath)
if err != nil {
log.Fatalln(err)
}
if *verbose {
conf.Verbose = true
}
client, err := NewClient(conf)
if err != nil {
log.Fatalln(err)
}
client, err := NewClient(*addr, *upstream, bootstraps, *timeout, *noECS, *verbose)
if err != nil { log.Fatalln(err) }
_ = client.Start()
}