Improve multiplex log

This commit is contained in:
世界 2022-08-04 09:11:39 +08:00
parent 32201cacda
commit 1c3c154d6d
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 41 additions and 22 deletions

View file

@ -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) { func NewClientWithOptions(ctx context.Context, dialer N.Dialer, options option.MultiplexOptions) (N.Dialer, error) {
if !options.Enabled { if !options.Enabled {
return dialer, nil return nil, nil
} }
if options.MaxConnections == 0 && options.MaxStreams == 0 { if options.MaxConnections == 0 && options.MaxStreams == 0 {
options.MinStreams = 8 options.MinStreams = 8

View file

@ -23,18 +23,18 @@ var Destination = M.Socksaddr{
} }
const ( const (
ProtocolYAMux Protocol = 0 ProtocolSMux Protocol = iota
ProtocolSMux Protocol = 1 ProtocolYAMux
) )
type Protocol byte type Protocol byte
func ParseProtocol(name string) (Protocol, error) { func ParseProtocol(name string) (Protocol, error) {
switch name { switch name {
case "", "yamux": case "", "smux":
return ProtocolYAMux, nil
case "smux":
return ProtocolSMux, nil return ProtocolSMux, nil
case "yamux":
return ProtocolYAMux, nil
default: default:
return ProtocolYAMux, E.New("unknown multiplex protocol: ", name) 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) { func (p Protocol) newServer(conn net.Conn) (abstractSession, error) {
switch p { switch p {
case ProtocolYAMux:
return yamux.Server(conn, yaMuxConfig())
case ProtocolSMux: case ProtocolSMux:
session, err := smux.Server(conn, nil) session, err := smux.Server(conn, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &smuxSession{session}, nil return &smuxSession{session}, nil
case ProtocolYAMux:
return yamux.Server(conn, yaMuxConfig())
default: default:
panic("unknown protocol") 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) { func (p Protocol) newClient(conn net.Conn) (abstractSession, error) {
switch p { switch p {
case ProtocolYAMux:
return yamux.Client(conn, yaMuxConfig())
case ProtocolSMux: case ProtocolSMux:
session, err := smux.Client(conn, nil) session, err := smux.Client(conn, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &smuxSession{session}, nil return &smuxSession{session}, nil
case ProtocolYAMux:
return yamux.Client(conn, yaMuxConfig())
default: default:
panic("unknown protocol") panic("unknown protocol")
} }
@ -80,10 +80,10 @@ func yaMuxConfig() *yamux.Config {
func (p Protocol) String() string { func (p Protocol) String() string {
switch p { switch p {
case ProtocolYAMux:
return "yamux"
case ProtocolSMux: case ProtocolSMux:
return "smux" return "smux"
case ProtocolYAMux:
return "yamux"
default: default:
return "unknown" return "unknown"
} }

View file

@ -7,7 +7,7 @@
```json ```json
{ {
"enabled": true, "enabled": true,
"protocol": "yamux", "protocol": "smux",
"max_connections": 4, "max_connections": 4,
"min_streams": 4, "min_streams": 4,
"max_streams": 0 "max_streams": 0
@ -26,10 +26,10 @@ Multiplex protocol.
| Protocol | Description | | Protocol | Description |
|----------|------------------------------------| |----------|------------------------------------|
| yamux | https://github.com/hashicorp/yamux |
| smux | https://github.com/xtaci/smux | | smux | https://github.com/xtaci/smux |
| yamux | https://github.com/hashicorp/yamux |
YAMux is used by default. SMux is used by default.
#### max_connections #### max_connections

View file

@ -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) { func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
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) return h.multiplexDialer.DialContext(ctx, network, destination)
}
} }
func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
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) return h.multiplexDialer.ListenPacket(ctx, destination)
}
} }
func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { 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 metadata.Destination = destination
switch N.NetworkName(network) { switch N.NetworkName(network) {
case N.NetworkTCP: case N.NetworkTCP:
h.logger.InfoContext(ctx, "outbound connection to ", destination)
outConn, err := h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr) outConn, err := h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return h.method.DialEarlyConn(outConn, destination), nil return h.method.DialEarlyConn(outConn, destination), nil
case N.NetworkUDP: case N.NetworkUDP:
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr) outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -105,7 +125,6 @@ func (h *shadowsocksDialer) ListenPacket(ctx context.Context, destination M.Sock
ctx, metadata := adapter.AppendContext(ctx) ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag metadata.Outbound = h.tag
metadata.Destination = destination metadata.Destination = destination
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr) outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -38,9 +38,9 @@ func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainS
defer cancel() defer cancel()
addrs, err := r.dnsClient.Lookup(ctx, transport, domain, strategy) addrs, err := r.dnsClient.Lookup(ctx, transport, domain, strategy)
if len(addrs) > 0 { 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 { } 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 return addrs, err
} }