mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 22:28:41 +00:00
QUIC clients took the standard TLS config directly, so with ECH enabled and no static config, hysteria, hysteria2, tuic and DNS over QUIC never queried the ECH config list and sent the real server name in the clear without reporting an error. Query it before the QUIC handshake as the TCP path does.
179 lines
5.3 KiB
Go
179 lines
5.3 KiB
Go
package tuic
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
"github.com/sagernet/sing-box/adapter/outbound"
|
|
"github.com/sagernet/sing-box/common/dialer"
|
|
"github.com/sagernet/sing-box/common/tls"
|
|
C "github.com/sagernet/sing-box/constant"
|
|
"github.com/sagernet/sing-box/log"
|
|
"github.com/sagernet/sing-box/option"
|
|
qtls "github.com/sagernet/sing-quic"
|
|
"github.com/sagernet/sing-quic/tuic"
|
|
"github.com/sagernet/sing/common"
|
|
"github.com/sagernet/sing/common/bufio"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
"github.com/sagernet/sing/common/logger"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
N "github.com/sagernet/sing/common/network"
|
|
"github.com/sagernet/sing/common/uot"
|
|
|
|
"github.com/gofrs/uuid/v5"
|
|
)
|
|
|
|
func RegisterOutbound(registry *outbound.Registry) {
|
|
outbound.Register[option.TUICOutboundOptions](registry, C.TypeTUIC, NewOutbound)
|
|
}
|
|
|
|
var (
|
|
_ adapter.InterfaceUpdateListener = (*Outbound)(nil)
|
|
_ adapter.IdleConnectionKeeper = (*Outbound)(nil)
|
|
_ adapter.OutboundWithMultiplex = (*Outbound)(nil)
|
|
)
|
|
|
|
type Outbound struct {
|
|
outbound.Adapter
|
|
logger logger.ContextLogger
|
|
client *tuic.Client
|
|
udpStream bool
|
|
}
|
|
|
|
func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TUICOutboundOptions) (adapter.Outbound, error) {
|
|
if options.TLS == nil || !options.TLS.Enabled {
|
|
return nil, C.ErrTLSRequired
|
|
}
|
|
tlsConfig, err := tls.NewClientWithOptions(tls.ClientOptions{
|
|
Context: ctx,
|
|
Logger: logger,
|
|
ServerAddress: options.Server,
|
|
Options: common.PtrValueOrDefault(options.TLS),
|
|
DomainResolver: options.DomainResolver,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
userUUID, err := uuid.FromString(options.UUID)
|
|
if err != nil {
|
|
return nil, E.Cause(err, "invalid uuid")
|
|
}
|
|
var tuicUDPStream bool
|
|
if options.UDPOverStream && options.UDPRelayMode != "" {
|
|
return nil, E.New("udp_over_stream is conflict with udp_relay_mode")
|
|
}
|
|
switch options.UDPRelayMode {
|
|
case "native":
|
|
case "quic":
|
|
tuicUDPStream = true
|
|
}
|
|
outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
client, err := tuic.NewClient(tuic.ClientOptions{
|
|
Context: ctx,
|
|
Dialer: outboundDialer,
|
|
ServerAddress: options.ServerOptions.Build(),
|
|
TLSConfig: tlsConfig,
|
|
QUICOptions: qtls.QUICOptions{
|
|
IdleTimeout: options.IdleTimeout.Build(),
|
|
KeepAlivePeriod: options.KeepAlivePeriod.Build(),
|
|
StreamReceiveWindow: options.StreamReceiveWindow.Value(),
|
|
ConnectionReceiveWindow: options.ConnectionReceiveWindow.Value(),
|
|
MaxConcurrentStreams: options.MaxConcurrentStreams,
|
|
InitialPacketSize: options.InitialPacketSize,
|
|
DisablePathMTUDiscovery: options.DisablePathMTUDiscovery,
|
|
},
|
|
UUID: userUUID,
|
|
Password: options.Password,
|
|
CongestionControl: options.CongestionControl,
|
|
UDPStream: tuicUDPStream,
|
|
ZeroRTTHandshake: options.ZeroRTTHandshake,
|
|
Heartbeat: time.Duration(options.Heartbeat),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Outbound{
|
|
Adapter: outbound.NewAdapterWithDialerOptions(C.TypeTUIC, tag, options.Network.Build(), options.DialerOptions),
|
|
logger: logger,
|
|
client: client,
|
|
udpStream: options.UDPOverStream,
|
|
}, nil
|
|
}
|
|
|
|
func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
switch N.NetworkName(network) {
|
|
case N.NetworkTCP:
|
|
h.logger.InfoContext(ctx, "outbound connection to ", destination)
|
|
return h.client.DialConn(ctx, destination)
|
|
case N.NetworkUDP:
|
|
if h.udpStream {
|
|
h.logger.InfoContext(ctx, "outbound stream packet connection to ", destination)
|
|
streamConn, err := h.client.DialConn(ctx, uot.RequestDestination(uot.Version))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return uot.NewLazyConn(streamConn, uot.Request{
|
|
IsConnect: true,
|
|
Destination: destination,
|
|
}), nil
|
|
} else {
|
|
conn, err := h.ListenPacket(ctx, destination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return bufio.NewBindPacketConn(conn, destination), nil
|
|
}
|
|
default:
|
|
return nil, E.New("unsupported network: ", network)
|
|
}
|
|
}
|
|
|
|
func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
if h.udpStream {
|
|
h.logger.InfoContext(ctx, "outbound stream packet connection to ", destination)
|
|
streamConn, err := h.client.DialConn(ctx, uot.RequestDestination(uot.Version))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return uot.NewLazyConn(streamConn, uot.Request{
|
|
IsConnect: false,
|
|
Destination: destination,
|
|
}), nil
|
|
} else {
|
|
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
|
return h.client.ListenPacket(ctx)
|
|
}
|
|
}
|
|
|
|
func (h *Outbound) InterfaceUpdated(ctx context.Context) {
|
|
_ = h.client.CloseWithError(E.New("network changed"))
|
|
}
|
|
|
|
func (h *Outbound) MultiplexEnabled() bool {
|
|
return true
|
|
}
|
|
|
|
func (h *Outbound) SetKeepIdleConnections(keep bool) {
|
|
h.client.SetKeepIdleConnections(keep)
|
|
}
|
|
|
|
func (h *Outbound) CloseIdleConnections() {
|
|
h.client.CloseIdleConnections()
|
|
}
|
|
|
|
func (h *Outbound) Start(stage adapter.StartStage, scope *adapter.Scope) error {
|
|
if stage != adapter.StartStateInitialize {
|
|
return nil
|
|
}
|
|
scope.Add(func() error {
|
|
return h.client.CloseWithError(os.ErrClosed)
|
|
})
|
|
return nil
|
|
}
|