1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-28 06:46:14 -04:00

code style optmize in dispatch func

This commit is contained in:
vcptr 2020-03-03 10:00:52 +08:00 committed by kslr
parent 04a0c04934
commit 5d13ec9196
2 changed files with 10 additions and 12 deletions

View File

@ -288,12 +288,9 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport.
return return
} }
accessMessage := log.AccessMessageFromContext(ctx) if accessMessage := log.AccessMessageFromContext(ctx); accessMessage != nil {
if accessMessage != nil { if tag := handler.Tag(); tag != "" {
if len(handler.Tag()) > 0 { accessMessage.Detour = tag
accessMessage.Detour = handler.Tag()
} else {
accessMessage.Detour = ""
} }
log.Record(accessMessage) log.Record(accessMessage)
} }

View File

@ -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) io.Copy(ioutil.Discard, resp.Body) // flush resp.Body so that the conn is reusable
return nil, err return nil, fmt.Errorf("DOH server returned code %d", resp.StatusCode)
} }
return ioutil.ReadAll(resp.Body) return ioutil.ReadAll(resp.Body)