mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-09-09 21:04:08 +08:00
ktls: Add warning for inappropriate scenarios
This commit is contained in:
parent
2599ab5117
commit
47649a7552
@ -20,7 +20,12 @@ func NewDialerFromOptions(ctx context.Context, logger logger.ContextLogger, dial
|
|||||||
if !options.Enabled {
|
if !options.Enabled {
|
||||||
return dialer, nil
|
return dialer, nil
|
||||||
}
|
}
|
||||||
config, err := NewClient(ctx, logger, serverAddress, options)
|
config, err := NewClientWithOptions(ClientOptions{
|
||||||
|
Context: ctx,
|
||||||
|
Logger: logger,
|
||||||
|
ServerAddress: serverAddress,
|
||||||
|
Options: options,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -28,15 +33,40 @@ func NewDialerFromOptions(ctx context.Context, logger logger.ContextLogger, dial
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions) (Config, error) {
|
func NewClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions) (Config, error) {
|
||||||
if !options.Enabled {
|
return NewClientWithOptions(ClientOptions{
|
||||||
|
Context: ctx,
|
||||||
|
Logger: logger,
|
||||||
|
ServerAddress: serverAddress,
|
||||||
|
Options: options,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientOptions struct {
|
||||||
|
Context context.Context
|
||||||
|
Logger logger.ContextLogger
|
||||||
|
ServerAddress string
|
||||||
|
Options option.OutboundTLSOptions
|
||||||
|
KTLSCompatible bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClientWithOptions(options ClientOptions) (Config, error) {
|
||||||
|
if !options.Options.Enabled {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
if options.Reality != nil && options.Reality.Enabled {
|
if !options.KTLSCompatible {
|
||||||
return NewRealityClient(ctx, logger, serverAddress, options)
|
if options.Options.KernelTx {
|
||||||
} else if options.UTLS != nil && options.UTLS.Enabled {
|
options.Logger.Warn("enabling kTLS TX in current scenarios will definitely reduce performance, please checkout https://sing-box.sagernet.org/configuration/shared/tls/#kernel_tx")
|
||||||
return NewUTLSClient(ctx, logger, serverAddress, options)
|
}
|
||||||
}
|
}
|
||||||
return NewSTDClient(ctx, logger, serverAddress, options)
|
if options.Options.KernelRx {
|
||||||
|
options.Logger.Warn("enabling kTLS RX will definitely reduce performance, please checkout https://sing-box.sagernet.org/configuration/shared/tls/#kernel_rx")
|
||||||
|
}
|
||||||
|
if options.Options.Reality != nil && options.Options.Reality.Enabled {
|
||||||
|
return NewRealityClient(options.Context, options.Logger, options.ServerAddress, options.Options)
|
||||||
|
} else if options.Options.UTLS != nil && options.Options.UTLS.Enabled {
|
||||||
|
return NewUTLSClient(options.Context, options.Logger, options.ServerAddress, options.Options)
|
||||||
|
}
|
||||||
|
return NewSTDClient(options.Context, options.Logger, options.ServerAddress, options.Options)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ClientHandshake(ctx context.Context, conn net.Conn, config Config) (Conn, error) {
|
func ClientHandshake(ctx context.Context, conn net.Conn, config Config) (Conn, error) {
|
||||||
|
@ -12,14 +12,37 @@ import (
|
|||||||
aTLS "github.com/sagernet/sing/common/tls"
|
aTLS "github.com/sagernet/sing/common/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ServerOptions struct {
|
||||||
|
Context context.Context
|
||||||
|
Logger log.ContextLogger
|
||||||
|
Options option.InboundTLSOptions
|
||||||
|
KTLSCompatible bool
|
||||||
|
}
|
||||||
|
|
||||||
func NewServer(ctx context.Context, logger log.ContextLogger, options option.InboundTLSOptions) (ServerConfig, error) {
|
func NewServer(ctx context.Context, logger log.ContextLogger, options option.InboundTLSOptions) (ServerConfig, error) {
|
||||||
if !options.Enabled {
|
return NewServerWithOptions(ServerOptions{
|
||||||
|
Context: ctx,
|
||||||
|
Logger: logger,
|
||||||
|
Options: options,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServerWithOptions(options ServerOptions) (ServerConfig, error) {
|
||||||
|
if !options.Options.Enabled {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
if options.Reality != nil && options.Reality.Enabled {
|
if !options.KTLSCompatible {
|
||||||
return NewRealityServer(ctx, logger, options)
|
if options.Options.KernelTx {
|
||||||
|
options.Logger.Warn("enabling kTLS TX in current scenarios will definitely reduce performance, please checkout https://sing-box.sagernet.org/configuration/shared/tls/#kernel_tx")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return NewSTDServer(ctx, logger, options)
|
if options.Options.KernelRx {
|
||||||
|
options.Logger.Warn("enabling kTLS RX will definitely reduce performance, please checkout https://sing-box.sagernet.org/configuration/shared/tls/#kernel_rx")
|
||||||
|
}
|
||||||
|
if options.Options.Reality != nil && options.Options.Reality.Enabled {
|
||||||
|
return NewRealityServer(options.Context, options.Logger, options.Options)
|
||||||
|
}
|
||||||
|
return NewSTDServer(options.Context, options.Logger, options.Options)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ServerHandshake(ctx context.Context, conn net.Conn, config ServerConfig) (Conn, error) {
|
func ServerHandshake(ctx context.Context, conn net.Conn, config ServerConfig) (Conn, error) {
|
||||||
|
@ -43,7 +43,12 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
|
|||||||
authenticator: auth.NewAuthenticator(options.Users),
|
authenticator: auth.NewAuthenticator(options.Users),
|
||||||
}
|
}
|
||||||
if options.TLS != nil {
|
if options.TLS != nil {
|
||||||
tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
|
tlsConfig, err := tls.NewServerWithOptions(tls.ServerOptions{
|
||||||
|
Context: ctx,
|
||||||
|
Logger: logger,
|
||||||
|
Options: common.PtrValueOrDefault(options.TLS),
|
||||||
|
KTLSCompatible: true,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,12 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
|
|||||||
authenticator: auth.NewAuthenticator(options.Users),
|
authenticator: auth.NewAuthenticator(options.Users),
|
||||||
}
|
}
|
||||||
if options.TLS != nil {
|
if options.TLS != nil {
|
||||||
tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
|
tlsConfig, err := tls.NewServerWithOptions(tls.ServerOptions{
|
||||||
|
Context: ctx,
|
||||||
|
Logger: logger,
|
||||||
|
Options: common.PtrValueOrDefault(options.TLS),
|
||||||
|
KTLSCompatible: true,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,13 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
|
|||||||
users: options.Users,
|
users: options.Users,
|
||||||
}
|
}
|
||||||
if options.TLS != nil {
|
if options.TLS != nil {
|
||||||
tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
|
tlsConfig, err := tls.NewServerWithOptions(tls.ServerOptions{
|
||||||
|
Context: ctx,
|
||||||
|
Logger: logger,
|
||||||
|
Options: common.PtrValueOrDefault(options.TLS),
|
||||||
|
KTLSCompatible: common.PtrValueOrDefault(options.Transport).Type == "" &&
|
||||||
|
!common.PtrValueOrDefault(options.Multiplex).Enabled,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,14 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
|||||||
key: trojan.Key(options.Password),
|
key: trojan.Key(options.Password),
|
||||||
}
|
}
|
||||||
if options.TLS != nil {
|
if options.TLS != nil {
|
||||||
outbound.tlsConfig, err = tls.NewClient(ctx, logger, options.Server, common.PtrValueOrDefault(options.TLS))
|
outbound.tlsConfig, err = tls.NewClientWithOptions(tls.ClientOptions{
|
||||||
|
Context: ctx,
|
||||||
|
Logger: logger,
|
||||||
|
ServerAddress: options.Server,
|
||||||
|
Options: common.PtrValueOrDefault(options.TLS),
|
||||||
|
KTLSCompatible: common.PtrValueOrDefault(options.Transport).Type == "" &&
|
||||||
|
!common.PtrValueOrDefault(options.Multiplex).Enabled,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,16 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
|
|||||||
}))
|
}))
|
||||||
inbound.service = service
|
inbound.service = service
|
||||||
if options.TLS != nil {
|
if options.TLS != nil {
|
||||||
inbound.tlsConfig, err = tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
|
inbound.tlsConfig, err = tls.NewServerWithOptions(tls.ServerOptions{
|
||||||
|
Context: ctx,
|
||||||
|
Logger: logger,
|
||||||
|
Options: common.PtrValueOrDefault(options.TLS),
|
||||||
|
KTLSCompatible: common.PtrValueOrDefault(options.Transport).Type == "" &&
|
||||||
|
!common.PtrValueOrDefault(options.Multiplex).Enabled &&
|
||||||
|
common.All(options.Users, func(it option.VLESSUser) bool {
|
||||||
|
return it.Flow == ""
|
||||||
|
}),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,15 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
|||||||
serverAddr: options.ServerOptions.Build(),
|
serverAddr: options.ServerOptions.Build(),
|
||||||
}
|
}
|
||||||
if options.TLS != nil {
|
if options.TLS != nil {
|
||||||
outbound.tlsConfig, err = tls.NewClient(ctx, logger, options.Server, common.PtrValueOrDefault(options.TLS))
|
outbound.tlsConfig, err = tls.NewClientWithOptions(tls.ClientOptions{
|
||||||
|
Context: ctx,
|
||||||
|
Logger: logger,
|
||||||
|
ServerAddress: options.Server,
|
||||||
|
Options: common.PtrValueOrDefault(options.TLS),
|
||||||
|
KTLSCompatible: common.PtrValueOrDefault(options.Transport).Type == "" &&
|
||||||
|
!common.PtrValueOrDefault(options.Multiplex).Enabled &&
|
||||||
|
options.Flow == "",
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user