feat: add statistic and only-tcp options for smux

This commit is contained in:
wwqgtxx 2023-04-23 20:55:42 +08:00
parent 0c146bd04c
commit 0c61057551
2 changed files with 18 additions and 8 deletions

View File

@ -16,9 +16,10 @@ import (
type SingMux struct { type SingMux struct {
C.ProxyAdapter C.ProxyAdapter
base ProxyBase base ProxyBase
client *mux.Client client *mux.Client
dialer *muxSingDialer dialer *muxSingDialer
onlyTcp bool
} }
type SingMuxOption struct { type SingMuxOption struct {
@ -28,6 +29,8 @@ type SingMuxOption struct {
MinStreams int `proxy:"min-streams,omitempty"` MinStreams int `proxy:"min-streams,omitempty"`
MaxStreams int `proxy:"max-streams,omitempty"` MaxStreams int `proxy:"max-streams,omitempty"`
Padding bool `proxy:"padding,omitempty"` Padding bool `proxy:"padding,omitempty"`
Statistic bool `proxy:"statistic,omitempty"`
OnlyTcp bool `proxy:"only-tcp,omitempty"`
} }
type ProxyBase interface { type ProxyBase interface {
@ -35,19 +38,20 @@ type ProxyBase interface {
} }
type muxSingDialer struct { type muxSingDialer struct {
dialer dialer.Dialer dialer dialer.Dialer
proxy C.ProxyAdapter proxy C.ProxyAdapter
statistic bool
} }
var _ N.Dialer = (*muxSingDialer)(nil) var _ N.Dialer = (*muxSingDialer)(nil)
func (d *muxSingDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { func (d *muxSingDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
var cDialer C.Dialer = proxydialer.New(d.proxy, d.dialer, false) var cDialer C.Dialer = proxydialer.New(d.proxy, d.dialer, d.statistic)
return cDialer.DialContext(ctx, network, destination.String()) return cDialer.DialContext(ctx, network, destination.String())
} }
func (d *muxSingDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { func (d *muxSingDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
var cDialer C.Dialer = proxydialer.New(d.proxy, d.dialer, false) var cDialer C.Dialer = proxydialer.New(d.proxy, d.dialer, d.statistic)
return cDialer.ListenPacket(ctx, "udp", "", destination.AddrPort()) return cDialer.ListenPacket(ctx, "udp", "", destination.AddrPort())
} }
@ -62,6 +66,9 @@ func (s *SingMux) DialContext(ctx context.Context, metadata *C.Metadata, opts ..
} }
func (s *SingMux) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) { func (s *SingMux) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
if s.onlyTcp {
return s.ProxyAdapter.ListenPacketContext(ctx, metadata, opts...)
}
options := s.base.DialOptions(opts...) options := s.base.DialOptions(opts...)
s.dialer.dialer = dialer.NewDialer(options...) s.dialer.dialer = dialer.NewDialer(options...)
pc, err := s.client.ListenPacket(ctx, M.ParseSocksaddr(metadata.RemoteAddress())) pc, err := s.client.ListenPacket(ctx, M.ParseSocksaddr(metadata.RemoteAddress()))
@ -75,7 +82,7 @@ func (s *SingMux) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
} }
func NewSingMux(option SingMuxOption, proxy C.ProxyAdapter, base ProxyBase) (C.ProxyAdapter, error) { func NewSingMux(option SingMuxOption, proxy C.ProxyAdapter, base ProxyBase) (C.ProxyAdapter, error) {
singDialer := &muxSingDialer{dialer: dialer.NewDialer(), proxy: proxy} singDialer := &muxSingDialer{dialer: dialer.NewDialer(), proxy: proxy, statistic: option.Statistic}
client, err := mux.NewClient(mux.Options{ client, err := mux.NewClient(mux.Options{
Context: context.TODO(), Context: context.TODO(),
Dialer: singDialer, Dialer: singDialer,
@ -93,5 +100,6 @@ func NewSingMux(option SingMuxOption, proxy C.ProxyAdapter, base ProxyBase) (C.P
base: base, base: base,
client: client, client: client,
dialer: singDialer, dialer: singDialer,
onlyTcp: option.OnlyTcp,
}, nil }, nil
} }

View File

@ -309,6 +309,8 @@ proxies: # socks5
# min-streams: 4 # Minimum multiplexed streams in a connection before opening a new connection. Conflict with max-streams. # min-streams: 4 # Minimum multiplexed streams in a connection before opening a new connection. Conflict with max-streams.
# max-streams: 0 # Maximum multiplexed streams in a connection before opening a new connection. Conflict with max-connections and min-streams. # max-streams: 0 # Maximum multiplexed streams in a connection before opening a new connection. Conflict with max-connections and min-streams.
# padding: false # Enable padding. Requires sing-box server version 1.3-beta9 or later. # padding: false # Enable padding. Requires sing-box server version 1.3-beta9 or later.
# statistic: false # 控制是否将底层连接显示在面板中,方面打断底层连接
# only-tcp: false # 如果设置为true, smux的设置将不会对udp生效udp连接会直接走底层协议
- name: "ss2" - name: "ss2"
type: ss type: ss