Disable logging by default

This commit is contained in:
Star Brilliant
2017-11-25 15:05:15 +08:00
parent f2d77e4a74
commit 2577b720c7
4 changed files with 19 additions and 7 deletions

View File

@@ -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))

View File

@@ -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()
}

View File

@@ -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)

View File

@@ -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)
}
}