diff --git a/main.go b/main.go index 4ca98ff..6c2f0e0 100644 --- a/main.go +++ b/main.go @@ -52,8 +52,12 @@ func main() { if cfg.UpstreamTimeout == 0 { cfg.UpstreamTimeout = 5 } + initDNSClient(cfg) + log.Debugf("%+v", cfg) + makeServers(cfg) + select {} } diff --git a/middleware.go b/middleware.go index 3b9e877..ee8177b 100644 --- a/middleware.go +++ b/middleware.go @@ -35,6 +35,8 @@ func (lh *logHandler) Status() int { var _ http.ResponseWriter = &logHandler{} +// LogHandler is a log middleware +// record request log func LogHandler(handler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer func() { @@ -50,6 +52,5 @@ func LogHandler(handler http.Handler) http.Handler { handler.ServeHTTP(lh, r) log.Infof("\"%s %s %s\" - %d %d \"%s\"", r.Method, r.RequestURI, r.Proto, lh.Status(), lh.size, r.UserAgent()) - }) } diff --git a/server_https.go b/server_https.go index 722a814..a784bf7 100644 --- a/server_https.go +++ b/server_https.go @@ -9,10 +9,12 @@ import ( "github.com/miekg/dns" ) +const dnsMsgType = "application/dns-message" + func (srv *server) handleHTTPReq(w http.ResponseWriter, r *http.Request) { ctype := r.Header.Get("content-type") - if !strings.HasPrefix(ctype, "application/dns-message") { - log.Errorf("request type %s, require application/dns-message", ctype) + if !strings.HasPrefix(ctype, dnsMsgType) { + log.Errorf("request type %s, require %s", ctype, dnsMsgType) http.Error(w, "dns message is required", http.StatusBadRequest) return } @@ -45,7 +47,7 @@ func (srv *server) handleHTTPReq(w http.ResponseWriter, r *http.Request) { for _, a := range m.Answer { log.Debugln("result", a.String()) } - w.Header().Set("content-type", "application/dns-message") + w.Header().Set("content-type", dnsMsgType) d, _ := m.Pack() w.Write(d) } diff --git a/upstream.go b/upstream.go index b41c77a..38fa477 100644 --- a/upstream.go +++ b/upstream.go @@ -10,7 +10,6 @@ import ( "time" "github.com/fangdingjun/go-log" - //nghttp2 "github.com/fangdingjun/nghttp2-go" "github.com/miekg/dns" "golang.org/x/net/http2" )