Browse Source

code style optmize in dispatch func

vcptr 5 years ago
parent
commit
5d13ec9196
2 changed files with 10 additions and 12 deletions
  1. 3 6
      app/dispatcher/default.go
  2. 7 6
      app/dns/dohdns.go

+ 3 - 6
app/dispatcher/default.go

@@ -288,12 +288,9 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport.
 		return
 		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)
 		log.Record(accessMessage)
 	}
 	}

+ 7 - 6
app/dns/dohdns.go

@@ -6,12 +6,14 @@ import (
 	"bytes"
 	"bytes"
 	"context"
 	"context"
 	"fmt"
 	"fmt"
+	"io"
 	"io/ioutil"
 	"io/ioutil"
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
 	"sync"
 	"sync"
 	"sync/atomic"
 	"sync/atomic"
 	"time"
 	"time"
+
 	dns_feature "v2ray.com/core/features/dns"
 	dns_feature "v2ray.com/core/features/dns"
 
 
 	"golang.org/x/net/dns/dnsmessage"
 	"golang.org/x/net/dns/dnsmessage"
@@ -57,7 +59,7 @@ func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, clientIP net.
 		IdleConnTimeout:     90 * time.Second,
 		IdleConnTimeout:     90 * time.Second,
 		TLSHandshakeTimeout: 30 * time.Second,
 		TLSHandshakeTimeout: 30 * time.Second,
 		DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
 		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 {
 			if err != nil {
 				return nil, err
 				return nil, err
 			}
 			}
@@ -89,7 +91,7 @@ func NewDoHLocalNameServer(url *url.URL, clientIP net.IP) *DoHNameServer {
 	tr := &http.Transport{
 	tr := &http.Transport{
 		IdleConnTimeout: 90 * time.Second,
 		IdleConnTimeout: 90 * time.Second,
 		DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
 		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 {
 			if err != nil {
 				return nil, err
 				return nil, err
 			}
 			}
@@ -114,7 +116,7 @@ func baseDOHNameServer(url *url.URL, prefix string, clientIP net.IP) *DoHNameSer
 		ips:      make(map[string]record),
 		ips:      make(map[string]record),
 		clientIP: clientIP,
 		clientIP: clientIP,
 		pub:      pubsub.NewService(),
 		pub:      pubsub.NewService(),
-		name:     fmt.Sprintf("%s//%s", prefix, url.Host),
+		name:     prefix + "//" + url.Host,
 		dohURL:   url.String(),
 		dohURL:   url.String(),
 	}
 	}
 	s.cleanup = &task.Periodic{
 	s.cleanup = &task.Periodic{
@@ -277,10 +279,9 @@ func (s *DoHNameServer) dohHTTPSContext(ctx context.Context, b []byte) ([]byte,
 	}
 	}
 
 
 	defer resp.Body.Close()
 	defer resp.Body.Close()
-
 	if resp.StatusCode != http.StatusOK {
 	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)
 	return ioutil.ReadAll(resp.Body)