From 490f66d443ebef9e0ddbe5952cb38ea45410e5e0 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Tue, 3 Mar 2020 10:00:52 +0800 Subject: [PATCH] code style optmize in dispatch func --- app/dispatcher/default.go | 9 +++------ app/dns/dohdns.go | 13 +++++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app/dispatcher/default.go b/app/dispatcher/default.go index b74300af..7c22e05a 100644 --- a/app/dispatcher/default.go +++ b/app/dispatcher/default.go @@ -288,12 +288,9 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport. return } - accessMessage := log.AccessMessageFromContext(ctx) - if accessMessage != nil { - if len(handler.Tag()) > 0 { - accessMessage.Detour = handler.Tag() - } else { - accessMessage.Detour = "" + if accessMessage := log.AccessMessageFromContext(ctx); accessMessage != nil { + if tag := handler.Tag(); tag != "" { + accessMessage.Detour = tag } log.Record(accessMessage) } diff --git a/app/dns/dohdns.go b/app/dns/dohdns.go index 892d79ac..89f96df8 100644 --- a/app/dns/dohdns.go +++ b/app/dns/dohdns.go @@ -6,12 +6,14 @@ import ( "bytes" "context" "fmt" + "io" "io/ioutil" "net/http" "net/url" "sync" "sync/atomic" "time" + dns_feature "v2ray.com/core/features/dns" "golang.org/x/net/dns/dnsmessage" @@ -57,7 +59,7 @@ func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, clientIP net. IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 30 * time.Second, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { - dest, err := net.ParseDestination(fmt.Sprintf("%s:%s", network, addr)) + dest, err := net.ParseDestination(network + ":" + addr) if err != nil { return nil, err } @@ -89,7 +91,7 @@ func NewDoHLocalNameServer(url *url.URL, clientIP net.IP) *DoHNameServer { tr := &http.Transport{ IdleConnTimeout: 90 * time.Second, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { - dest, err := net.ParseDestination(fmt.Sprintf("%s:%s", network, addr)) + dest, err := net.ParseDestination(network + ":" + addr) if err != nil { return nil, err } @@ -114,7 +116,7 @@ func baseDOHNameServer(url *url.URL, prefix string, clientIP net.IP) *DoHNameSer ips: make(map[string]record), clientIP: clientIP, pub: pubsub.NewService(), - name: fmt.Sprintf("%s//%s", prefix, url.Host), + name: prefix + "//" + url.Host, dohURL: url.String(), } s.cleanup = &task.Periodic{ @@ -277,10 +279,9 @@ func (s *DoHNameServer) dohHTTPSContext(ctx context.Context, b []byte) ([]byte, } defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - err = fmt.Errorf("DOH HTTPS server returned with non-OK code %d", resp.StatusCode) - return nil, err + io.Copy(ioutil.Discard, resp.Body) // flush resp.Body so that the conn is reusable + return nil, fmt.Errorf("DOH server returned code %d", resp.StatusCode) } return ioutil.ReadAll(resp.Body)