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

View File

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

View File

@ -1,6 +1,8 @@
package config
import (
"github.com/metacubex/mihomo/listener/sing"
"encoding/json"
)
@ -18,6 +20,7 @@ type TuicServer struct {
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"`
CWND int `yaml:"cwnd" json:"cwnd,omitempty"`
MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"`
}
func (t TuicServer) String() string {

View File

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

View File

@ -21,6 +21,7 @@ type Hysteria2Option struct {
IgnoreClientBandwidth bool `inbound:"ignore-client-bandwidth,omitempty"`
Masquerade string `inbound:"masquerade,omitempty"`
CWND int `inbound:"cwnd,omitempty"`
MuxOption MuxOption `inbound:"mux-option,omitempty"`
}
func (o Hysteria2Option) Equal(config C.InboundConfig) bool {
@ -57,6 +58,7 @@ func NewHysteria2(options *Hysteria2Option) (*Hysteria2, error) {
IgnoreClientBandwidth: options.IgnoreClientBandwidth,
Masquerade: options.Masquerade,
CWND: options.CWND,
MuxOption: options.MuxOption.Build(),
},
}, 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 {
BaseOption
Password string `inbound:"password"`
Cipher string `inbound:"cipher"`
UDP bool `inbound:"udp,omitempty"`
Password string `inbound:"password"`
Cipher string `inbound:"cipher"`
UDP bool `inbound:"udp,omitempty"`
MuxOption MuxOption `inbound:"mux-option,omitempty"`
}
func (o ShadowSocksOption) Equal(config C.InboundConfig) bool {
@ -34,11 +35,12 @@ func NewShadowSocks(options *ShadowSocksOption) (*ShadowSocks, error) {
Base: base,
config: options,
ss: LC.ShadowsocksServer{
Enable: true,
Listen: base.RawAddress(),
Password: options.Password,
Cipher: options.Cipher,
Udp: options.UDP,
Enable: true,
Listen: base.RawAddress(),
Password: options.Password,
Cipher: options.Cipher,
Udp: options.UDP,
MuxOption: options.MuxOption.Build(),
},
}, nil
}

View File

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

View File

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

View File

@ -9,6 +9,7 @@ import (
"time"
"github.com/metacubex/mihomo/adapter/inbound"
"github.com/metacubex/mihomo/adapter/outbound"
N "github.com/metacubex/mihomo/common/net"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
@ -26,11 +27,27 @@ import (
const UDPTimeout = 5 * time.Minute
type ListenerHandler struct {
type ListenerConfig struct {
Tunnel C.Tunnel
Type C.Type
Additions []inbound.Addition
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
}
@ -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{
NewStreamContext: func(ctx context.Context, conn net.Conn) context.Context {
return ctx
},
Logger: log.SingLogger,
Handler: h,
Brutal: mux.BrutalOptions{
// TODO: sing-mux tcp brutal inbound
Padding: lc.MuxOption.Padding,
Brutal: mux.BrutalOptions{
Enabled: lc.MuxOption.Brutal.Enabled,
SendBPS: outbound.StringToBps(lc.MuxOption.Brutal.Up),
ReceiveBPS: outbound.StringToBps(lc.MuxOption.Brutal.Down),
},
})
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,
Type: C.HYSTERIA2,
Additions: additions,
}
err = h.Initialize()
MuxOption: config.MuxOption,
})
if err != nil {
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())
h := &sing.ListenerHandler{
h, err := sing.NewListenerHandler(sing.ListenerConfig{
Tunnel: tunnel,
Type: C.SHADOWSOCKS,
Additions: additions,
}
err = h.Initialize()
MuxOption: config.MuxOption,
})
if err != nil {
return nil, err
}

View File

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

View File

@ -8,7 +8,6 @@ import (
"runtime"
"strconv"
"strings"
"time"
"github.com/metacubex/mihomo/adapter/inbound"
"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)
}
handler := &ListenerHandler{
ListenerHandler: sing.ListenerHandler{
Tunnel: tunnel,
Type: C.TUN,
Additions: additions,
UDPTimeout: time.Second * time.Duration(udpTimeout),
},
DnsAdds: dnsAdds,
}
err = handler.Initialize()
h, err := sing.NewListenerHandler(sing.ListenerConfig{
Tunnel: tunnel,
Type: C.TUN,
Additions: additions,
})
if err != nil {
return nil, err
}
handler := &ListenerHandler{
ListenerHandler: h,
DnsAdds: dnsAdds,
}
l = &Listener{
closed: false,
options: options,

View File

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

View File

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