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
+78
View File
@@ -0,0 +1,78 @@
/*
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"`
Cert string `toml:"cert"`
Key string `toml:"key"`
Path string `toml:"path"`
Upstream []string `toml:"upstream"`
Tries uint `toml:"tries"`
TCPOnly bool `toml:"tcp_only"`
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:8053"
}
if conf.Path == "" {
conf.Path = "/resolve"
}
if len(conf.Upstream) == 0 {
conf.Upstream = []string { "8.8.8.8:53", "8.8.4.4:53" }
}
if conf.Tries == 0 {
conf.Tries = 3
}
if (conf.Cert != "") != (conf.Key != "") {
return nil, &configError { "You must specify both -cert and -key to enable TLS" }
}
return conf, nil
}
type configError struct {
err string
}
func (e *configError) Error() string {
return e.err
}
+27
View File
@@ -0,0 +1,27 @@
# HTTP listen port
listen = "127.0.0.1:8053"
# TLS certification file
cert = ""
# TLS key file
key = ""
# HTTP path for resolve application
path = "/resolve"
# Upstream DNS resolver
# If multiple servers are specified, a random one will be chosen each time.
upstream = [
"8.8.8.8:53",
"8.8.4.4:53",
]
# Number of tries if upstream DNS fails
tries = 3
# Only use TCP for DNS query
tcp_only = false
# Enable logging
verbose = false
+10 -12
View File
@@ -26,26 +26,24 @@ package main
import (
"flag"
"log"
"strings"
)
func main() {
addr := flag.String("addr", "127.0.0.1:8053", "HTTP listen port")
cert := flag.String("cert", "", "TLS certification file")
key := flag.String("key", "", "TLS key file")
path := flag.String("path", "/resolve", "HTTP path for resolve application")
upstream := flag.String("upstream", "8.8.8.8:53,8.8.4.4:53", "Upstream DNS resolver")
tcpOnly := flag.Bool("tcp", false, "Only use TCP for DNS query")
confPath := flag.String("conf", "doh-server.conf", "Configuration file")
verbose := flag.Bool("verbose", false, "Enable logging")
flag.Parse()
if (*cert != "") != (*key != "") {
log.Fatalln("You must specify both -cert and -key to enable TLS")
conf, err := loadConfig(*confPath)
if err != nil {
log.Fatalln(err)
}
upstreams := strings.Split(*upstream, ",")
server := NewServer(*addr, *cert, *key, *path, upstreams, *tcpOnly, *verbose)
err := server.Start()
if *verbose {
conf.Verbose = true
}
server := NewServer(conf)
err = server.Start()
if err != nil {
log.Fatalln(err)
}
+13 -40
View File
@@ -41,29 +41,15 @@ import (
)
type Server struct {
addr string
cert string
key string
path string
upstreams []string
tcpOnly bool
verbose bool
conf *config
udpClient *dns.Client
tcpClient *dns.Client
servemux *http.ServeMux
}
func NewServer(addr, cert, key, path string, upstreams []string, tcpOnly, verbose bool) (s *Server) {
upstreamsCopy := make([]string, len(upstreams))
copy(upstreamsCopy, upstreams)
func NewServer(conf *config) (s *Server) {
s = &Server {
addr: addr,
cert: cert,
key: key,
path: path,
upstreams: upstreamsCopy,
tcpOnly: tcpOnly,
verbose: verbose,
conf: conf,
udpClient: &dns.Client {
Net: "udp",
},
@@ -72,19 +58,19 @@ func NewServer(addr, cert, key, path string, upstreams []string, tcpOnly, verbos
},
servemux: http.NewServeMux(),
}
s.servemux.HandleFunc(path, s.handlerFunc)
s.servemux.HandleFunc(conf.Path, s.handlerFunc)
return
}
func (s *Server) Start() error {
servemux := http.Handler(s.servemux)
if s.verbose {
if s.conf.Verbose {
servemux = handlers.CombinedLoggingHandler(os.Stdout, servemux)
}
if s.cert != "" || s.key != "" {
return http.ListenAndServeTLS(s.addr, s.cert, s.key, servemux)
if s.conf.Cert != "" || s.conf.Key != "" {
return http.ListenAndServeTLS(s.conf.Listen, s.conf.Cert, s.conf.Key, servemux)
} else {
return http.ListenAndServe(s.addr, servemux)
return http.ListenAndServe(s.conf.Listen, servemux)
}
}
@@ -211,6 +197,7 @@ func (s *Server) handlerFunc(w http.ResponseWriter, r *http.Request) {
respJson := jsonDNS.Marshal(resp)
respStr, err := json.Marshal(respJson)
if err != nil {
log.Println(err)
jsonDNS.FormatError(w, fmt.Sprintf("DNS packet parse failure (%s)", err.Error()), 500)
return
}
@@ -259,31 +246,17 @@ func (s *Server) findClientIP(r *http.Request) net.IP {
}
func (s *Server) doDNSQuery(msg *dns.Msg) (resp *dns.Msg, err error) {
num_servers := len(s.upstreams)
init_server := rand.Intn(num_servers)
for i := 0; i < num_servers; i++ {
var server string
if init_server + i < num_servers {
server = s.upstreams[i + init_server]
} else {
server = s.upstreams[i + init_server - num_servers]
}
if !s.tcpOnly {
num_servers := len(s.conf.Upstream)
for i := uint(0); i < s.conf.Tries; i++ {
server := s.conf.Upstream[rand.Intn(num_servers)]
if !s.conf.TCPOnly {
resp, _, err = s.udpClient.Exchange(msg, server)
if err == dns.ErrTruncated {
log.Println(err)
resp, _, err = s.tcpClient.Exchange(msg, server)
if err == dns.ErrTruncated {
log.Println(err)
return
}
}
} else {
resp, _, err = s.tcpClient.Exchange(msg, server)
if err == dns.ErrTruncated {
log.Println(err)
return
}
}
if err == nil {
return