sing-box/protocol/tuic/outbound.go

142 lines
4.1 KiB
Go
Raw Permalink Normal View History

2024-11-01 16:39:02 +00:00
package tuic
2023-07-23 06:42:19 +00:00
import (
"context"
"net"
"os"
"time"
"github.com/sagernet/sing-box/adapter"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing-box/adapter/outbound"
2023-07-23 06:42:19 +00:00
"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"
"github.com/sagernet/sing-quic/tuic"
2023-07-23 06:42:19 +00:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing/common/logger"
2023-07-23 06:42:19 +00:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2023-08-20 03:28:30 +00:00
"github.com/sagernet/sing/common/uot"
2023-07-23 06:42:19 +00:00
"github.com/gofrs/uuid/v5"
)
2024-11-01 16:39:02 +00:00
func RegisterOutbound(registry *outbound.Registry) {
outbound.Register[option.TUICOutboundOptions](registry, C.TypeTUIC, NewOutbound)
}
var _ adapter.InterfaceUpdateListener = (*Outbound)(nil)
2023-07-23 06:42:19 +00:00
2024-11-01 16:39:02 +00:00
type Outbound struct {
outbound.Adapter
logger logger.ContextLogger
2023-08-20 03:28:30 +00:00
client *tuic.Client
udpStream bool
2023-07-23 06:42:19 +00:00
}
2024-11-01 16:39:02 +00:00
func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TUICOutboundOptions) (adapter.Outbound, error) {
2023-07-23 06:42:19 +00:00
options.UDPFragmentDefault = true
if options.TLS == nil || !options.TLS.Enabled {
return nil, C.ErrTLSRequired
}
tlsConfig, err := tls.NewClient(ctx, options.Server, common.PtrValueOrDefault(options.TLS))
2023-07-23 06:42:19 +00:00
if err != nil {
return nil, err
}
userUUID, err := uuid.FromString(options.UUID)
if err != nil {
return nil, E.Cause(err, "invalid uuid")
}
2023-08-20 03:28:30 +00:00
var tuicUDPStream bool
if options.UDPOverStream && options.UDPRelayMode != "" {
return nil, E.New("udp_over_stream is conflict with udp_relay_mode")
}
2023-07-23 06:42:19 +00:00
switch options.UDPRelayMode {
case "native":
case "quic":
2023-08-20 03:28:30 +00:00
tuicUDPStream = true
2023-07-23 06:42:19 +00:00
}
outboundDialer, err := dialer.New(ctx, options.DialerOptions)
2023-08-08 08:14:03 +00:00
if err != nil {
return nil, err
}
2023-07-23 06:42:19 +00:00
client, err := tuic.NewClient(tuic.ClientOptions{
Context: ctx,
2023-08-08 08:14:03 +00:00
Dialer: outboundDialer,
2023-07-23 06:42:19 +00:00
ServerAddress: options.ServerOptions.Build(),
TLSConfig: tlsConfig,
UUID: userUUID,
Password: options.Password,
CongestionControl: options.CongestionControl,
2023-08-20 03:28:30 +00:00
UDPStream: tuicUDPStream,
2023-07-23 06:42:19 +00:00
ZeroRTTHandshake: options.ZeroRTTHandshake,
Heartbeat: time.Duration(options.Heartbeat),
})
if err != nil {
return nil, err
}
2024-11-01 16:39:02 +00:00
return &Outbound{
2024-11-21 10:10:41 +00:00
Adapter: outbound.NewAdapterWithDialerOptions(C.TypeTUIC, tag, options.Network.Build(), options.DialerOptions),
2024-11-01 16:39:02 +00:00
logger: logger,
2023-08-20 03:28:30 +00:00
client: client,
udpStream: options.UDPOverStream,
2023-07-23 06:42:19 +00:00
}, nil
}
2024-11-01 16:39:02 +00:00
func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2023-07-23 06:42:19 +00:00
switch N.NetworkName(network) {
case N.NetworkTCP:
h.logger.InfoContext(ctx, "outbound connection to ", destination)
return h.client.DialConn(ctx, destination)
case N.NetworkUDP:
2023-08-20 03:28:30 +00:00
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
2023-07-23 06:42:19 +00:00
}
default:
return nil, E.New("unsupported network: ", network)
}
}
2024-11-01 16:39:02 +00:00
func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2023-08-20 03:28:30 +00:00
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)
}
2023-07-23 06:42:19 +00:00
}
2024-11-01 16:39:02 +00:00
func (h *Outbound) InterfaceUpdated() {
2023-07-23 06:42:19 +00:00
_ = h.client.CloseWithError(E.New("network changed"))
}
2024-11-01 16:39:02 +00:00
func (h *Outbound) Close() error {
2023-07-23 06:42:19 +00:00
return h.client.CloseWithError(os.ErrClosed)
}