diff --git a/doh-client/client.go b/doh-client/client.go index f9873c3..d942d1e 100644 --- a/doh-client/client.go +++ b/doh-client/client.go @@ -40,17 +40,19 @@ type Client struct { bootstrap string timeout uint noECS bool + verbose bool udpServer *dns.Server tcpServer *dns.Server httpClient *http.Client } -func NewClient(addr, upstream, bootstrap string, timeout uint, noECS bool) (c *Client, err error) { +func NewClient(addr, upstream, bootstrap string, timeout uint, noECS, verbose bool) (c *Client, err error) { c = &Client { addr: addr, upstream: upstream, timeout: timeout, noECS: noECS, + verbose: verbose, } c.udpServer = &dns.Server { Addr: addr, @@ -140,7 +142,9 @@ func (c *Client) handlerFunc(w dns.ResponseWriter, r *dns.Msg, isTCP bool) { questionType = strconv.Itoa(int(question.Qtype)) } - fmt.Printf("%s - - [%s] \"%s IN %s\"\n", w.RemoteAddr(), time.Now().Format("02/Jan/2006:15:04:05 -0700"), questionName, questionType) + if c.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)) diff --git a/doh-client/main.go b/doh-client/main.go index ef3c157..d37299b 100644 --- a/doh-client/main.go +++ b/doh-client/main.go @@ -29,9 +29,10 @@ func main() { 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") + verbose := flag.Bool("verbose", false, "Enable logging") flag.Parse() - client, err := NewClient(*addr, *upstream, *bootstrap, *timeout, *noECS) + client, err := NewClient(*addr, *upstream, *bootstrap, *timeout, *noECS, *verbose) if err != nil { log.Fatalln(err) } _ = client.Start() } diff --git a/doh-server/main.go b/doh-server/main.go index 1e0f91d..d5d88b2 100644 --- a/doh-server/main.go +++ b/doh-server/main.go @@ -31,6 +31,7 @@ func main() { 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") + verbose := flag.Bool("verbose", false, "Enable logging") flag.Parse() if (*cert != "") != (*key != "") { @@ -38,7 +39,7 @@ func main() { } upstreams := strings.Split(*upstream, ",") - server := NewServer(*addr, *cert, *key, *path, upstreams, *tcpOnly) + server := NewServer(*addr, *cert, *key, *path, upstreams, *tcpOnly, *verbose) err := server.Start() if err != nil { log.Fatalln(err) diff --git a/doh-server/server.go b/doh-server/server.go index 2257902..72b7cb4 100644 --- a/doh-server/server.go +++ b/doh-server/server.go @@ -42,12 +42,13 @@ type Server struct { path string upstreams []string tcpOnly bool + verbose bool udpClient *dns.Client tcpClient *dns.Client servemux *http.ServeMux } -func NewServer(addr, cert, key, path string, upstreams []string, tcpOnly bool) (s *Server) { +func NewServer(addr, cert, key, path string, upstreams []string, tcpOnly, verbose bool) (s *Server) { upstreamsCopy := make([]string, len(upstreams)) copy(upstreamsCopy, upstreams) s = &Server { @@ -57,6 +58,7 @@ func NewServer(addr, cert, key, path string, upstreams []string, tcpOnly bool) ( path: path, upstreams: upstreamsCopy, tcpOnly: tcpOnly, + verbose: verbose, udpClient: &dns.Client { Net: "udp", }, @@ -70,10 +72,14 @@ func NewServer(addr, cert, key, path string, upstreams []string, tcpOnly bool) ( } func (s *Server) Start() error { + servemux := http.Handler(s.servemux) + if s.verbose { + servemux = handlers.CombinedLoggingHandler(os.Stdout, servemux) + } if s.cert != "" || s.key != "" { - return http.ListenAndServeTLS(s.addr, s.cert, s.key, handlers.CombinedLoggingHandler(os.Stdout, s.servemux)) + return http.ListenAndServeTLS(s.addr, s.cert, s.key, servemux) } else { - return http.ListenAndServe(s.addr, handlers.CombinedLoggingHandler(os.Stdout, s.servemux)) + return http.ListenAndServe(s.addr, servemux) } }