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) {
if !options.Enabled {
return dialer, nil
return nil, nil
}
if options.MaxConnections == 0 && options.MaxStreams == 0 {
options.MinStreams = 8

View file

@ -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"
}

View file

@ -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

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) {
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

View file

@ -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
}