sing-box/adapter/outbound/default.go

246 lines
8.3 KiB
Go
Raw Normal View History

package outbound
import (
"context"
"net"
2023-04-02 04:01:19 +00:00
"net/netip"
2022-10-20 02:57:57 +00:00
"os"
"time"
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
2023-10-08 14:11:03 +00:00
"github.com/sagernet/sing-dns"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
2023-03-23 07:14:21 +00:00
"github.com/sagernet/sing/common/canceler"
E "github.com/sagernet/sing/common/exceptions"
2023-10-25 04:00:00 +00:00
M "github.com/sagernet/sing/common/metadata"
2022-07-07 15:36:32 +00:00
N "github.com/sagernet/sing/common/network"
)
2022-07-07 15:36:32 +00:00
func NewConnection(ctx context.Context, this N.Dialer, conn net.Conn, metadata adapter.InboundContext) error {
2024-11-07 05:44:00 +00:00
defer conn.Close()
2022-07-07 15:36:32 +00:00
ctx = adapter.WithContext(ctx, &metadata)
var outConn net.Conn
var err error
if len(metadata.DestinationAddresses) > 0 {
2022-07-29 16:29:22 +00:00
outConn, err = N.DialSerial(ctx, this, N.NetworkTCP, metadata.Destination, metadata.DestinationAddresses)
2022-07-07 15:36:32 +00:00
} else {
2022-07-29 16:29:22 +00:00
outConn, err = this.DialContext(ctx, N.NetworkTCP, metadata.Destination)
2022-07-07 15:36:32 +00:00
}
if err != nil {
2023-08-31 12:07:32 +00:00
return N.ReportHandshakeFailure(conn, err)
}
2024-10-21 15:38:34 +00:00
err = N.ReportConnHandshakeSuccess(conn, outConn)
2023-08-31 12:07:32 +00:00
if err != nil {
outConn.Close()
2023-08-31 12:07:32 +00:00
return err
2022-07-07 15:36:32 +00:00
}
return CopyEarlyConn(ctx, conn, outConn)
}
2023-10-08 14:11:03 +00:00
func NewDirectConnection(ctx context.Context, router adapter.Router, this N.Dialer, conn net.Conn, metadata adapter.InboundContext, domainStrategy dns.DomainStrategy) error {
2024-11-07 05:44:00 +00:00
defer conn.Close()
2023-09-06 11:13:39 +00:00
ctx = adapter.WithContext(ctx, &metadata)
var outConn net.Conn
var err error
if len(metadata.DestinationAddresses) > 0 {
outConn, err = N.DialSerial(ctx, this, N.NetworkTCP, metadata.Destination, metadata.DestinationAddresses)
} else if metadata.Destination.IsFqdn() {
var destinationAddresses []netip.Addr
2023-10-08 14:11:03 +00:00
destinationAddresses, err = router.Lookup(ctx, metadata.Destination.Fqdn, domainStrategy)
2023-09-06 11:13:39 +00:00
if err != nil {
2023-08-31 12:07:32 +00:00
return N.ReportHandshakeFailure(conn, err)
2023-09-06 11:13:39 +00:00
}
outConn, err = N.DialSerial(ctx, this, N.NetworkTCP, metadata.Destination, destinationAddresses)
} else {
outConn, err = this.DialContext(ctx, N.NetworkTCP, metadata.Destination)
}
if err != nil {
2023-08-31 12:07:32 +00:00
return N.ReportHandshakeFailure(conn, err)
}
2024-10-21 15:38:34 +00:00
err = N.ReportConnHandshakeSuccess(conn, outConn)
2023-08-31 12:07:32 +00:00
if err != nil {
outConn.Close()
2023-08-31 12:07:32 +00:00
return err
2023-09-06 11:13:39 +00:00
}
return CopyEarlyConn(ctx, conn, outConn)
}
2022-07-07 15:36:32 +00:00
func NewPacketConnection(ctx context.Context, this N.Dialer, conn N.PacketConn, metadata adapter.InboundContext) error {
2024-11-07 05:44:00 +00:00
defer conn.Close()
2022-07-07 15:36:32 +00:00
ctx = adapter.WithContext(ctx, &metadata)
2024-11-07 05:44:00 +00:00
var (
outPacketConn net.PacketConn
outConn net.Conn
destinationAddress netip.Addr
err error
)
if metadata.UDPConnect {
if len(metadata.DestinationAddresses) > 0 {
outConn, err = N.DialSerial(ctx, this, N.NetworkUDP, metadata.Destination, metadata.DestinationAddresses)
} else {
outConn, err = this.DialContext(ctx, N.NetworkUDP, metadata.Destination)
}
if err != nil {
return N.ReportHandshakeFailure(conn, err)
}
outPacketConn = bufio.NewUnbindPacketConn(outConn)
connRemoteAddr := M.AddrFromNet(outConn.RemoteAddr())
if connRemoteAddr != metadata.Destination.Addr {
destinationAddress = connRemoteAddr
}
2022-07-07 15:36:32 +00:00
} else {
2024-11-07 05:44:00 +00:00
if len(metadata.DestinationAddresses) > 0 {
outPacketConn, destinationAddress, err = N.ListenSerial(ctx, this, metadata.Destination, metadata.DestinationAddresses)
} else {
outPacketConn, err = this.ListenPacket(ctx, metadata.Destination)
}
if err != nil {
return N.ReportHandshakeFailure(conn, err)
}
2023-08-31 12:07:32 +00:00
}
2024-11-07 05:44:00 +00:00
err = N.ReportPacketConnHandshakeSuccess(conn, outPacketConn)
2023-08-31 12:07:32 +00:00
if err != nil {
2024-11-07 05:44:00 +00:00
outPacketConn.Close()
2023-08-31 12:07:32 +00:00
return err
2022-07-07 15:36:32 +00:00
}
2023-04-02 04:01:19 +00:00
if destinationAddress.IsValid() {
2023-10-25 04:00:00 +00:00
if metadata.Destination.IsFqdn() {
2024-10-21 15:38:34 +00:00
if metadata.UDPDisableDomainUnmapping {
2024-11-07 05:44:00 +00:00
outPacketConn = bufio.NewUnidirectionalNATPacketConn(bufio.NewPacketConn(outPacketConn), M.SocksaddrFrom(destinationAddress, metadata.Destination.Port), metadata.Destination)
} else {
2024-11-07 05:44:00 +00:00
outPacketConn = bufio.NewNATPacketConn(bufio.NewPacketConn(outPacketConn), M.SocksaddrFrom(destinationAddress, metadata.Destination.Port), metadata.Destination)
}
2023-10-25 04:00:00 +00:00
}
if natConn, loaded := common.Cast[bufio.NATPacketConn](conn); loaded {
2023-04-02 04:01:19 +00:00
natConn.UpdateDestination(destinationAddress)
}
}
2022-08-12 04:13:57 +00:00
switch metadata.Protocol {
case C.ProtocolSTUN:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.STUNTimeout)
case C.ProtocolQUIC:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.QUICTimeout)
case C.ProtocolDNS:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.DNSTimeout)
}
2024-11-07 05:44:00 +00:00
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outPacketConn))
}
2023-10-08 14:11:03 +00:00
func NewDirectPacketConnection(ctx context.Context, router adapter.Router, this N.Dialer, conn N.PacketConn, metadata adapter.InboundContext, domainStrategy dns.DomainStrategy) error {
2024-11-07 05:44:00 +00:00
defer conn.Close()
2023-09-06 11:13:39 +00:00
ctx = adapter.WithContext(ctx, &metadata)
2024-11-07 05:44:00 +00:00
var (
outPacketConn net.PacketConn
outConn net.Conn
destinationAddress netip.Addr
err error
)
if metadata.UDPConnect {
if len(metadata.DestinationAddresses) > 0 {
outConn, err = N.DialSerial(ctx, this, N.NetworkUDP, metadata.Destination, metadata.DestinationAddresses)
} else if metadata.Destination.IsFqdn() {
var destinationAddresses []netip.Addr
destinationAddresses, err = router.Lookup(ctx, metadata.Destination.Fqdn, domainStrategy)
if err != nil {
return N.ReportHandshakeFailure(conn, err)
}
outConn, err = N.DialSerial(ctx, this, N.NetworkUDP, metadata.Destination, destinationAddresses)
} else {
outConn, err = this.DialContext(ctx, N.NetworkUDP, metadata.Destination)
}
2023-09-06 11:13:39 +00:00
if err != nil {
2023-08-31 12:07:32 +00:00
return N.ReportHandshakeFailure(conn, err)
2023-09-06 11:13:39 +00:00
}
2024-11-07 05:44:00 +00:00
connRemoteAddr := M.AddrFromNet(outConn.RemoteAddr())
if connRemoteAddr != metadata.Destination.Addr {
destinationAddress = connRemoteAddr
}
2023-09-06 11:13:39 +00:00
} else {
2024-11-07 05:44:00 +00:00
if len(metadata.DestinationAddresses) > 0 {
outPacketConn, destinationAddress, err = N.ListenSerial(ctx, this, metadata.Destination, metadata.DestinationAddresses)
} else if metadata.Destination.IsFqdn() {
var destinationAddresses []netip.Addr
destinationAddresses, err = router.Lookup(ctx, metadata.Destination.Fqdn, domainStrategy)
if err != nil {
return N.ReportHandshakeFailure(conn, err)
}
outPacketConn, destinationAddress, err = N.ListenSerial(ctx, this, metadata.Destination, destinationAddresses)
} else {
outPacketConn, err = this.ListenPacket(ctx, metadata.Destination)
}
if err != nil {
return N.ReportHandshakeFailure(conn, err)
}
2023-08-31 12:07:32 +00:00
}
2024-11-07 05:44:00 +00:00
err = N.ReportPacketConnHandshakeSuccess(conn, outPacketConn)
2023-08-31 12:07:32 +00:00
if err != nil {
2024-11-07 05:44:00 +00:00
outPacketConn.Close()
2023-08-31 12:07:32 +00:00
return err
2023-09-06 11:13:39 +00:00
}
if destinationAddress.IsValid() {
2023-10-25 04:00:00 +00:00
if metadata.Destination.IsFqdn() {
2024-11-07 05:44:00 +00:00
outPacketConn = bufio.NewNATPacketConn(bufio.NewPacketConn(outPacketConn), M.SocksaddrFrom(destinationAddress, metadata.Destination.Port), metadata.Destination)
2023-10-25 04:00:00 +00:00
}
if natConn, loaded := common.Cast[bufio.NATPacketConn](conn); loaded {
2023-09-06 11:13:39 +00:00
natConn.UpdateDestination(destinationAddress)
}
}
switch metadata.Protocol {
case C.ProtocolSTUN:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.STUNTimeout)
case C.ProtocolQUIC:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.QUICTimeout)
case C.ProtocolDNS:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.DNSTimeout)
}
2024-11-07 05:44:00 +00:00
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outPacketConn))
2023-09-06 11:13:39 +00:00
}
func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) error {
2022-08-08 12:56:53 +00:00
if cachedReader, isCached := conn.(N.CachedReader); isCached {
2022-07-11 12:55:14 +00:00
payload := cachedReader.ReadCached()
if payload != nil && !payload.IsEmpty() {
_, err := serverConn.Write(payload.Bytes())
2023-09-24 06:39:56 +00:00
payload.Release()
2022-07-11 12:55:14 +00:00
if err != nil {
serverConn.Close()
2022-07-11 12:55:14 +00:00
return err
}
return bufio.CopyConn(ctx, conn, serverConn)
}
}
2023-02-28 12:16:15 +00:00
if earlyConn, isEarlyConn := common.Cast[N.EarlyConn](serverConn); isEarlyConn && earlyConn.NeedHandshake() {
2023-07-03 13:45:32 +00:00
payload := buf.NewPacket()
2023-02-26 15:08:20 +00:00
err := conn.SetReadDeadline(time.Now().Add(C.ReadPayloadTimeout))
if err != os.ErrInvalid {
if err != nil {
2023-09-24 06:39:56 +00:00
payload.Release()
serverConn.Close()
2023-02-26 15:08:20 +00:00
return err
}
_, err = payload.ReadOnceFrom(conn)
if err != nil && !E.IsTimeout(err) {
2023-09-24 06:39:56 +00:00
payload.Release()
serverConn.Close()
2023-02-26 15:08:20 +00:00
return E.Cause(err, "read payload")
}
err = conn.SetReadDeadline(time.Time{})
if err != nil {
payload.Release()
serverConn.Close()
2023-02-26 15:08:20 +00:00
return err
}
2022-10-20 02:57:57 +00:00
}
2023-02-26 15:08:20 +00:00
_, err = serverConn.Write(payload.Bytes())
2023-09-24 06:39:56 +00:00
payload.Release()
2022-10-20 02:57:57 +00:00
if err != nil {
serverConn.Close()
2023-08-31 12:07:32 +00:00
return N.ReportHandshakeFailure(conn, err)
2022-10-20 02:57:57 +00:00
}
}
return bufio.CopyConn(ctx, conn, serverConn)
}