chore: listeners can set mux-option

This commit is contained in:
wwqgtxx 2023-11-23 08:20:26 +08:00
parent 8b4499e461
commit 96f0254a48
16 changed files with 109 additions and 42 deletions

View File

@ -1,6 +1,10 @@
package config package config
import "encoding/json" import (
"github.com/metacubex/mihomo/listener/sing"
"encoding/json"
)
type Hysteria2Server struct { type Hysteria2Server struct {
Enable bool `yaml:"enable" json:"enable"` Enable bool `yaml:"enable" json:"enable"`
@ -17,6 +21,7 @@ type Hysteria2Server struct {
IgnoreClientBandwidth bool `yaml:"ignore-client-bandwidth" json:"ignore-client-bandwidth,omitempty"` IgnoreClientBandwidth bool `yaml:"ignore-client-bandwidth" json:"ignore-client-bandwidth,omitempty"`
Masquerade string `yaml:"masquerade" json:"masquerade,omitempty"` Masquerade string `yaml:"masquerade" json:"masquerade,omitempty"`
CWND int `yaml:"cwnd" json:"cwnd,omitempty"` CWND int `yaml:"cwnd" json:"cwnd,omitempty"`
MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"`
} }
func (h Hysteria2Server) String() string { func (h Hysteria2Server) String() string {

View File

@ -1,15 +1,18 @@
package config package config
import ( import (
"github.com/metacubex/mihomo/listener/sing"
"encoding/json" "encoding/json"
) )
type ShadowsocksServer struct { type ShadowsocksServer struct {
Enable bool Enable bool
Listen string Listen string
Password string Password string
Cipher string Cipher string
Udp bool Udp bool
MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"`
} }
func (t ShadowsocksServer) String() string { func (t ShadowsocksServer) String() string {

View File

@ -1,6 +1,8 @@
package config package config
import ( import (
"github.com/metacubex/mihomo/listener/sing"
"encoding/json" "encoding/json"
) )
@ -18,6 +20,7 @@ type TuicServer struct {
MaxUdpRelayPacketSize int `yaml:"max-udp-relay-packet-size" json:"max-udp-relay-packet-size,omitempty"` MaxUdpRelayPacketSize int `yaml:"max-udp-relay-packet-size" json:"max-udp-relay-packet-size,omitempty"`
MaxDatagramFrameSize int `yaml:"max-datagram-frame-size" json:"max-datagram-frame-size,omitempty"` MaxDatagramFrameSize int `yaml:"max-datagram-frame-size" json:"max-datagram-frame-size,omitempty"`
CWND int `yaml:"cwnd" json:"cwnd,omitempty"` CWND int `yaml:"cwnd" json:"cwnd,omitempty"`
MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"`
} }
func (t TuicServer) String() string { func (t TuicServer) String() string {

View File

@ -1,6 +1,8 @@
package config package config
import ( import (
"github.com/metacubex/mihomo/listener/sing"
"encoding/json" "encoding/json"
) )
@ -17,6 +19,7 @@ type VmessServer struct {
WsPath string WsPath string
Certificate string Certificate string
PrivateKey string PrivateKey string
MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"`
} }
func (t VmessServer) String() string { func (t VmessServer) String() string {

View File

@ -21,6 +21,7 @@ type Hysteria2Option struct {
IgnoreClientBandwidth bool `inbound:"ignore-client-bandwidth,omitempty"` IgnoreClientBandwidth bool `inbound:"ignore-client-bandwidth,omitempty"`
Masquerade string `inbound:"masquerade,omitempty"` Masquerade string `inbound:"masquerade,omitempty"`
CWND int `inbound:"cwnd,omitempty"` CWND int `inbound:"cwnd,omitempty"`
MuxOption MuxOption `inbound:"mux-option,omitempty"`
} }
func (o Hysteria2Option) Equal(config C.InboundConfig) bool { func (o Hysteria2Option) Equal(config C.InboundConfig) bool {
@ -57,6 +58,7 @@ func NewHysteria2(options *Hysteria2Option) (*Hysteria2, error) {
IgnoreClientBandwidth: options.IgnoreClientBandwidth, IgnoreClientBandwidth: options.IgnoreClientBandwidth,
Masquerade: options.Masquerade, Masquerade: options.Masquerade,
CWND: options.CWND, CWND: options.CWND,
MuxOption: options.MuxOption.Build(),
}, },
}, nil }, nil
} }

25
listener/inbound/mux.go Normal file
View File

@ -0,0 +1,25 @@
package inbound
import "github.com/metacubex/mihomo/listener/sing"
type MuxOption struct {
Padding bool `inbound:"padding,omitempty"`
Brutal BrutalOptions `inbound:"brutal,omitempty"`
}
type BrutalOptions struct {
Enabled bool `inbound:"enabled,omitempty"`
Up string `inbound:"up,omitempty"`
Down string `inbound:"down,omitempty"`
}
func (m MuxOption) Build() sing.MuxOption {
return sing.MuxOption{
Padding: m.Padding,
Brutal: sing.BrutalOptions{
Enabled: m.Brutal.Enabled,
Up: m.Brutal.Up,
Down: m.Brutal.Down,
},
}
}

View File

@ -9,9 +9,10 @@ import (
type ShadowSocksOption struct { type ShadowSocksOption struct {
BaseOption BaseOption
Password string `inbound:"password"` Password string `inbound:"password"`
Cipher string `inbound:"cipher"` Cipher string `inbound:"cipher"`
UDP bool `inbound:"udp,omitempty"` UDP bool `inbound:"udp,omitempty"`
MuxOption MuxOption `inbound:"mux-option,omitempty"`
} }
func (o ShadowSocksOption) Equal(config C.InboundConfig) bool { func (o ShadowSocksOption) Equal(config C.InboundConfig) bool {
@ -34,11 +35,12 @@ func NewShadowSocks(options *ShadowSocksOption) (*ShadowSocks, error) {
Base: base, Base: base,
config: options, config: options,
ss: LC.ShadowsocksServer{ ss: LC.ShadowsocksServer{
Enable: true, Enable: true,
Listen: base.RawAddress(), Listen: base.RawAddress(),
Password: options.Password, Password: options.Password,
Cipher: options.Cipher, Cipher: options.Cipher,
Udp: options.UDP, Udp: options.UDP,
MuxOption: options.MuxOption.Build(),
}, },
}, nil }, nil
} }

View File

@ -19,6 +19,7 @@ type TuicOption struct {
ALPN []string `inbound:"alpn,omitempty"` ALPN []string `inbound:"alpn,omitempty"`
MaxUdpRelayPacketSize int `inbound:"max-udp-relay-packet-size,omitempty"` MaxUdpRelayPacketSize int `inbound:"max-udp-relay-packet-size,omitempty"`
CWND int `inbound:"cwnd,omitempty"` CWND int `inbound:"cwnd,omitempty"`
MuxOption MuxOption `inbound:"mux-option,omitempty"`
} }
func (o TuicOption) Equal(config C.InboundConfig) bool { func (o TuicOption) Equal(config C.InboundConfig) bool {
@ -53,6 +54,7 @@ func NewTuic(options *TuicOption) (*Tuic, error) {
ALPN: options.ALPN, ALPN: options.ALPN,
MaxUdpRelayPacketSize: options.MaxUdpRelayPacketSize, MaxUdpRelayPacketSize: options.MaxUdpRelayPacketSize,
CWND: options.CWND, CWND: options.CWND,
MuxOption: options.MuxOption.Build(),
}, },
}, nil }, nil
} }

View File

@ -13,6 +13,7 @@ type VmessOption struct {
WsPath string `inbound:"ws-path,omitempty"` WsPath string `inbound:"ws-path,omitempty"`
Certificate string `inbound:"certificate,omitempty"` Certificate string `inbound:"certificate,omitempty"`
PrivateKey string `inbound:"private-key,omitempty"` PrivateKey string `inbound:"private-key,omitempty"`
MuxOption MuxOption `inbound:"mux-option,omitempty"`
} }
type VmessUser struct { type VmessUser struct {
@ -55,6 +56,7 @@ func NewVmess(options *VmessOption) (*Vmess, error) {
WsPath: options.WsPath, WsPath: options.WsPath,
Certificate: options.Certificate, Certificate: options.Certificate,
PrivateKey: options.PrivateKey, PrivateKey: options.PrivateKey,
MuxOption: options.MuxOption.Build(),
}, },
}, nil }, nil
} }

View File

@ -9,6 +9,7 @@ import (
"time" "time"
"github.com/metacubex/mihomo/adapter/inbound" "github.com/metacubex/mihomo/adapter/inbound"
"github.com/metacubex/mihomo/adapter/outbound"
N "github.com/metacubex/mihomo/common/net" N "github.com/metacubex/mihomo/common/net"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log" "github.com/metacubex/mihomo/log"
@ -26,11 +27,27 @@ import (
const UDPTimeout = 5 * time.Minute const UDPTimeout = 5 * time.Minute
type ListenerHandler struct { type ListenerConfig struct {
Tunnel C.Tunnel Tunnel C.Tunnel
Type C.Type Type C.Type
Additions []inbound.Addition Additions []inbound.Addition
UDPTimeout time.Duration UDPTimeout time.Duration
MuxOption MuxOption
}
type MuxOption struct {
Padding bool `yaml:"padding" json:"padding,omitempty"`
Brutal BrutalOptions `yaml:"brutal" json:"brutal,omitempty"`
}
type BrutalOptions struct {
Enabled bool `yaml:"enabled" json:"enabled"`
Up string `yaml:"up" json:"up,omitempty"`
Down string `yaml:"down" json:"down,omitempty"`
}
type ListenerHandler struct {
ListenerConfig
muxService *mux.Service muxService *mux.Service
} }
@ -49,15 +66,19 @@ func ConvertMetadata(metadata *C.Metadata) M.Metadata {
} }
} }
func (h *ListenerHandler) Initialize() (err error) { func NewListenerHandler(lc ListenerConfig) (h *ListenerHandler, err error) {
h = &ListenerHandler{ListenerConfig: lc}
h.muxService, err = mux.NewService(mux.ServiceOptions{ h.muxService, err = mux.NewService(mux.ServiceOptions{
NewStreamContext: func(ctx context.Context, conn net.Conn) context.Context { NewStreamContext: func(ctx context.Context, conn net.Conn) context.Context {
return ctx return ctx
}, },
Logger: log.SingLogger, Logger: log.SingLogger,
Handler: h, Handler: h,
Brutal: mux.BrutalOptions{ Padding: lc.MuxOption.Padding,
// TODO: sing-mux tcp brutal inbound Brutal: mux.BrutalOptions{
Enabled: lc.MuxOption.Brutal.Enabled,
SendBPS: outbound.StringToBps(lc.MuxOption.Brutal.Up),
ReceiveBPS: outbound.StringToBps(lc.MuxOption.Brutal.Down),
}, },
}) })
return return

View File

@ -42,12 +42,12 @@ func New(config LC.Hysteria2Server, tunnel C.Tunnel, additions ...inbound.Additi
} }
} }
h := &sing.ListenerHandler{ h, err := sing.NewListenerHandler(sing.ListenerConfig{
Tunnel: tunnel, Tunnel: tunnel,
Type: C.HYSTERIA2, Type: C.HYSTERIA2,
Additions: additions, Additions: additions,
} MuxOption: config.MuxOption,
err = h.Initialize() })
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -50,12 +50,12 @@ func New(config LC.ShadowsocksServer, tunnel C.Tunnel, additions ...inbound.Addi
udpTimeout := int64(sing.UDPTimeout.Seconds()) udpTimeout := int64(sing.UDPTimeout.Seconds())
h := &sing.ListenerHandler{ h, err := sing.NewListenerHandler(sing.ListenerConfig{
Tunnel: tunnel, Tunnel: tunnel,
Type: C.SHADOWSOCKS, Type: C.SHADOWSOCKS,
Additions: additions, Additions: additions,
} MuxOption: config.MuxOption,
err = h.Initialize() })
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -26,7 +26,7 @@ const DefaultDnsReadTimeout = time.Second * 10
const DefaultDnsRelayTimeout = time.Second * 5 const DefaultDnsRelayTimeout = time.Second * 5
type ListenerHandler struct { type ListenerHandler struct {
sing.ListenerHandler *sing.ListenerHandler
DnsAdds []netip.AddrPort DnsAdds []netip.AddrPort
} }

View File

@ -8,7 +8,6 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/metacubex/mihomo/adapter/inbound" "github.com/metacubex/mihomo/adapter/inbound"
"github.com/metacubex/mihomo/component/dialer" "github.com/metacubex/mihomo/component/dialer"
@ -150,19 +149,19 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis
dnsAdds = append(dnsAdds, addrPort) dnsAdds = append(dnsAdds, addrPort)
} }
handler := &ListenerHandler{ h, err := sing.NewListenerHandler(sing.ListenerConfig{
ListenerHandler: sing.ListenerHandler{ Tunnel: tunnel,
Tunnel: tunnel, Type: C.TUN,
Type: C.TUN, Additions: additions,
Additions: additions, })
UDPTimeout: time.Second * time.Duration(udpTimeout),
},
DnsAdds: dnsAdds,
}
err = handler.Initialize()
if err != nil { if err != nil {
return nil, err return nil, err
} }
handler := &ListenerHandler{
ListenerHandler: h,
DnsAdds: dnsAdds,
}
l = &Listener{ l = &Listener{
closed: false, closed: false,
options: options, options: options,

View File

@ -40,12 +40,12 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition)
_listener = sl _listener = sl
}() }()
} }
h := &sing.ListenerHandler{ h, err := sing.NewListenerHandler(sing.ListenerConfig{
Tunnel: tunnel, Tunnel: tunnel,
Type: C.VMESS, Type: C.VMESS,
Additions: additions, Additions: additions,
} MuxOption: config.MuxOption,
err = h.Initialize() })
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -38,12 +38,12 @@ func New(config LC.TuicServer, tunnel C.Tunnel, additions ...inbound.Addition) (
inbound.WithSpecialRules(""), inbound.WithSpecialRules(""),
} }
} }
h := &sing.ListenerHandler{ h, err := sing.NewListenerHandler(sing.ListenerConfig{
Tunnel: tunnel, Tunnel: tunnel,
Type: C.TUIC, Type: C.TUIC,
Additions: additions, Additions: additions,
} MuxOption: config.MuxOption,
err := h.Initialize() })
if err != nil { if err != nil {
return nil, err return nil, err
} }