|
|
@@ -20,6 +20,7 @@ import (
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
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"
|
|
|
)
|
|
|
@@ -31,7 +32,8 @@ var (
|
|
|
|
|
|
type TUIC struct {
|
|
|
myOutboundAdapter
|
|
|
- client *tuic.Client
|
|
|
+ client *tuic.Client
|
|
|
+ udpStream bool
|
|
|
}
|
|
|
|
|
|
func NewTUIC(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TUICOutboundOptions) (*TUIC, error) {
|
|
|
@@ -51,11 +53,14 @@ func NewTUIC(ctx context.Context, router adapter.Router, logger log.ContextLogge
|
|
|
if err != nil {
|
|
|
return nil, E.Cause(err, "invalid uuid")
|
|
|
}
|
|
|
- var udpStream bool
|
|
|
+ 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":
|
|
|
- udpStream = true
|
|
|
+ tuicUDPStream = true
|
|
|
}
|
|
|
outboundDialer, err := dialer.New(router, options.DialerOptions)
|
|
|
if err != nil {
|
|
|
@@ -69,7 +74,7 @@ func NewTUIC(ctx context.Context, router adapter.Router, logger log.ContextLogge
|
|
|
UUID: userUUID,
|
|
|
Password: options.Password,
|
|
|
CongestionControl: options.CongestionControl,
|
|
|
- UDPStream: udpStream,
|
|
|
+ UDPStream: tuicUDPStream,
|
|
|
ZeroRTTHandshake: options.ZeroRTTHandshake,
|
|
|
Heartbeat: time.Duration(options.Heartbeat),
|
|
|
})
|
|
|
@@ -85,7 +90,8 @@ func NewTUIC(ctx context.Context, router adapter.Router, logger log.ContextLogge
|
|
|
tag: tag,
|
|
|
dependencies: withDialerDependency(options.DialerOptions),
|
|
|
},
|
|
|
- client: client,
|
|
|
+ client: client,
|
|
|
+ udpStream: options.UDPOverStream,
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
@@ -95,19 +101,43 @@ func (h *TUIC) DialContext(ctx context.Context, network string, destination M.So
|
|
|
h.logger.InfoContext(ctx, "outbound connection to ", destination)
|
|
|
return h.client.DialConn(ctx, destination)
|
|
|
case N.NetworkUDP:
|
|
|
- conn, err := h.ListenPacket(ctx, destination)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
+ 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
|
|
|
}
|
|
|
- return bufio.NewBindPacketConn(conn, destination), nil
|
|
|
default:
|
|
|
return nil, E.New("unsupported network: ", network)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (h *TUIC) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
|
- h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
|
|
- return h.client.ListenPacket(ctx)
|
|
|
+ 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 *TUIC) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|