diff --git a/common/mux/client.go b/common/mux/client.go index 4be9915d..f94675d1 100644 --- a/common/mux/client.go +++ b/common/mux/client.go @@ -43,7 +43,7 @@ func NewClient(ctx context.Context, dialer N.Dialer, protocol Protocol, maxConne func NewClientWithOptions(ctx context.Context, dialer N.Dialer, options option.MultiplexOptions) (N.Dialer, error) { if !options.Enabled { - return dialer, nil + return nil, nil } if options.MaxConnections == 0 && options.MaxStreams == 0 { options.MinStreams = 8 diff --git a/common/mux/protocol.go b/common/mux/protocol.go index 95baccb2..80ce2898 100644 --- a/common/mux/protocol.go +++ b/common/mux/protocol.go @@ -23,18 +23,18 @@ var Destination = M.Socksaddr{ } const ( - ProtocolYAMux Protocol = 0 - ProtocolSMux Protocol = 1 + ProtocolSMux Protocol = iota + ProtocolYAMux ) type Protocol byte func ParseProtocol(name string) (Protocol, error) { switch name { - case "", "yamux": - return ProtocolYAMux, nil - case "smux": + case "", "smux": return ProtocolSMux, nil + case "yamux": + return ProtocolYAMux, nil default: return ProtocolYAMux, E.New("unknown multiplex protocol: ", name) } @@ -42,14 +42,14 @@ func ParseProtocol(name string) (Protocol, error) { func (p Protocol) newServer(conn net.Conn) (abstractSession, error) { switch p { - case ProtocolYAMux: - return yamux.Server(conn, yaMuxConfig()) case ProtocolSMux: session, err := smux.Server(conn, nil) if err != nil { return nil, err } return &smuxSession{session}, nil + case ProtocolYAMux: + return yamux.Server(conn, yaMuxConfig()) default: panic("unknown protocol") } @@ -57,14 +57,14 @@ func (p Protocol) newServer(conn net.Conn) (abstractSession, error) { func (p Protocol) newClient(conn net.Conn) (abstractSession, error) { switch p { - case ProtocolYAMux: - return yamux.Client(conn, yaMuxConfig()) case ProtocolSMux: session, err := smux.Client(conn, nil) if err != nil { return nil, err } return &smuxSession{session}, nil + case ProtocolYAMux: + return yamux.Client(conn, yaMuxConfig()) default: panic("unknown protocol") } @@ -80,10 +80,10 @@ func yaMuxConfig() *yamux.Config { func (p Protocol) String() string { switch p { - case ProtocolYAMux: - return "yamux" case ProtocolSMux: return "smux" + case ProtocolYAMux: + return "yamux" default: return "unknown" } diff --git a/docs/configuration/shared/multiplex.md b/docs/configuration/shared/multiplex.md index 73b6bdd0..b48b04c7 100644 --- a/docs/configuration/shared/multiplex.md +++ b/docs/configuration/shared/multiplex.md @@ -7,7 +7,7 @@ ```json { "enabled": true, - "protocol": "yamux", + "protocol": "smux", "max_connections": 4, "min_streams": 4, "max_streams": 0 @@ -26,10 +26,10 @@ Multiplex protocol. | Protocol | Description | |----------|------------------------------------| -| yamux | https://github.com/hashicorp/yamux | | smux | https://github.com/xtaci/smux | +| yamux | https://github.com/hashicorp/yamux | -YAMux is used by default. +SMux is used by default. #### max_connections diff --git a/outbound/shadowsocks.go b/outbound/shadowsocks.go index 3fa4fd33..ba5f52e5 100644 --- a/outbound/shadowsocks.go +++ b/outbound/shadowsocks.go @@ -54,11 +54,33 @@ func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.Conte } func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - return h.multiplexDialer.DialContext(ctx, network, destination) + if h.multiplexDialer == nil { + switch N.NetworkName(network) { + case N.NetworkTCP: + h.logger.InfoContext(ctx, "outbound connection to ", destination) + case N.NetworkUDP: + h.logger.InfoContext(ctx, "outbound packet connection to ", destination) + } + return (*shadowsocksDialer)(h).DialContext(ctx, network, destination) + } else { + switch N.NetworkName(network) { + case N.NetworkTCP: + h.logger.InfoContext(ctx, "outbound multiplex connection to ", destination) + case N.NetworkUDP: + h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination) + } + return h.multiplexDialer.DialContext(ctx, network, destination) + } } func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { - return h.multiplexDialer.ListenPacket(ctx, destination) + if h.multiplexDialer == nil { + h.logger.InfoContext(ctx, "outbound packet connection to ", destination) + return (*shadowsocksDialer)(h).ListenPacket(ctx, destination) + } else { + h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination) + return h.multiplexDialer.ListenPacket(ctx, destination) + } } func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { @@ -83,14 +105,12 @@ func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, des metadata.Destination = destination switch N.NetworkName(network) { case N.NetworkTCP: - h.logger.InfoContext(ctx, "outbound connection to ", destination) outConn, err := h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr) if err != nil { return nil, err } return h.method.DialEarlyConn(outConn, destination), nil case N.NetworkUDP: - h.logger.InfoContext(ctx, "outbound packet connection to ", destination) outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr) if err != nil { return nil, err @@ -105,7 +125,6 @@ func (h *shadowsocksDialer) ListenPacket(ctx context.Context, destination M.Sock ctx, metadata := adapter.AppendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination - h.logger.InfoContext(ctx, "outbound packet connection to ", destination) outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr) if err != nil { return nil, err diff --git a/route/router_dns.go b/route/router_dns.go index 8d8c8b44..63a35e77 100644 --- a/route/router_dns.go +++ b/route/router_dns.go @@ -38,9 +38,9 @@ func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainS defer cancel() addrs, err := r.dnsClient.Lookup(ctx, transport, domain, strategy) if len(addrs) > 0 { - r.logger.InfoContext(ctx, "lookup succeed for ", domain, ": ", strings.Join(F.MapToString(addrs), " ")) + r.dnsLogger.InfoContext(ctx, "lookup succeed for ", domain, ": ", strings.Join(F.MapToString(addrs), " ")) } else { - r.logger.ErrorContext(ctx, E.Cause(err, "lookup failed for ", domain)) + r.dnsLogger.ErrorContext(ctx, E.Cause(err, "lookup failed for ", domain)) } return addrs, err }