sing-box/route/conn.go

275 lines
9 KiB
Go
Raw Normal View History

2024-11-20 03:32:02 +00:00
package route
import (
"context"
"io"
"net"
"net/netip"
2024-11-27 10:08:19 +00:00
"sync"
2024-11-20 03:32:02 +00:00
"sync/atomic"
2024-11-24 06:45:40 +00:00
"time"
2024-11-20 03:32:02 +00:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer"
2024-11-24 06:45:40 +00:00
C "github.com/sagernet/sing-box/constant"
2024-11-20 03:32:02 +00:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/bufio"
2024-11-24 06:45:40 +00:00
"github.com/sagernet/sing/common/canceler"
2024-11-20 03:32:02 +00:00
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"
2024-11-27 10:08:19 +00:00
"github.com/sagernet/sing/common/x/list"
2024-11-20 03:32:02 +00:00
)
var _ adapter.ConnectionManager = (*ConnectionManager)(nil)
type ConnectionManager struct {
2024-11-27 10:08:19 +00:00
logger logger.ContextLogger
access sync.Mutex
connections list.List[io.Closer]
2024-11-20 03:32:02 +00:00
}
func NewConnectionManager(logger logger.ContextLogger) *ConnectionManager {
return &ConnectionManager{
2024-11-27 10:08:19 +00:00
logger: logger,
2024-11-20 03:32:02 +00:00
}
}
func (m *ConnectionManager) Start(stage adapter.StartStage) error {
2024-11-27 10:08:19 +00:00
return nil
2024-11-20 03:32:02 +00:00
}
func (m *ConnectionManager) Close() error {
2024-11-27 10:08:19 +00:00
m.access.Lock()
defer m.access.Unlock()
for element := m.connections.Front(); element != nil; element = element.Next() {
common.Close(element.Value)
}
m.connections.Init()
return nil
2024-11-20 03:32:02 +00:00
}
func (m *ConnectionManager) NewConnection(ctx context.Context, this N.Dialer, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
ctx = adapter.WithContext(ctx, &metadata)
var (
remoteConn net.Conn
err error
)
if len(metadata.DestinationAddresses) > 0 {
remoteConn, err = dialer.DialSerialNetwork(ctx, this, N.NetworkTCP, metadata.Destination, metadata.DestinationAddresses, metadata.NetworkStrategy, metadata.NetworkType, metadata.FallbackNetworkType, metadata.FallbackDelay)
} else {
remoteConn, err = this.DialContext(ctx, N.NetworkTCP, metadata.Destination)
}
if err != nil {
2024-11-27 10:08:19 +00:00
err = E.Cause(err, "open outbound connection")
2024-11-20 03:32:02 +00:00
N.CloseOnHandshakeFailure(conn, onClose, err)
2024-11-27 10:08:19 +00:00
m.logger.ErrorContext(ctx, err)
2024-11-20 03:32:02 +00:00
return
}
err = N.ReportConnHandshakeSuccess(conn, remoteConn)
if err != nil {
2024-11-27 10:08:19 +00:00
err = E.Cause(err, "report handshake success")
2024-11-20 03:32:02 +00:00
remoteConn.Close()
N.CloseOnHandshakeFailure(conn, onClose, err)
2024-11-27 10:08:19 +00:00
m.logger.ErrorContext(ctx, err)
2024-11-20 03:32:02 +00:00
return
}
2024-11-27 10:08:19 +00:00
m.access.Lock()
element := m.connections.PushBack(conn)
m.access.Unlock()
onClose = N.AppendClose(onClose, func(it error) {
m.access.Lock()
defer m.access.Unlock()
m.connections.Remove(element)
})
2024-11-20 03:32:02 +00:00
var done atomic.Bool
go m.connectionCopy(ctx, conn, remoteConn, false, &done, onClose)
go m.connectionCopy(ctx, remoteConn, conn, true, &done, onClose)
}
func (m *ConnectionManager) NewPacketConnection(ctx context.Context, this N.Dialer, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
ctx = adapter.WithContext(ctx, &metadata)
var (
remotePacketConn net.PacketConn
remoteConn net.Conn
destinationAddress netip.Addr
err error
)
if metadata.UDPConnect {
if len(metadata.DestinationAddresses) > 0 {
if parallelDialer, isParallelDialer := this.(dialer.ParallelInterfaceDialer); isParallelDialer {
remoteConn, err = dialer.DialSerialNetwork(ctx, parallelDialer, N.NetworkUDP, metadata.Destination, metadata.DestinationAddresses, metadata.NetworkStrategy, metadata.NetworkType, metadata.FallbackNetworkType, metadata.FallbackDelay)
} else {
remoteConn, err = N.DialSerial(ctx, this, N.NetworkUDP, metadata.Destination, metadata.DestinationAddresses)
}
} else {
remoteConn, err = this.DialContext(ctx, N.NetworkUDP, metadata.Destination)
}
if err != nil {
N.CloseOnHandshakeFailure(conn, onClose, err)
m.logger.ErrorContext(ctx, "open outbound packet connection: ", err)
return
}
remotePacketConn = bufio.NewUnbindPacketConn(remoteConn)
connRemoteAddr := M.AddrFromNet(remoteConn.RemoteAddr())
if connRemoteAddr != metadata.Destination.Addr {
destinationAddress = connRemoteAddr
}
} else {
if len(metadata.DestinationAddresses) > 0 {
remotePacketConn, destinationAddress, err = dialer.ListenSerialNetworkPacket(ctx, this, metadata.Destination, metadata.DestinationAddresses, metadata.NetworkStrategy, metadata.NetworkType, metadata.FallbackNetworkType, metadata.FallbackDelay)
} else {
remotePacketConn, err = this.ListenPacket(ctx, metadata.Destination)
}
if err != nil {
N.CloseOnHandshakeFailure(conn, onClose, err)
m.logger.ErrorContext(ctx, "listen outbound packet connection: ", err)
return
}
}
err = N.ReportPacketConnHandshakeSuccess(conn, remotePacketConn)
if err != nil {
conn.Close()
remotePacketConn.Close()
m.logger.ErrorContext(ctx, "report handshake success: ", err)
return
}
if destinationAddress.IsValid() {
var originDestination M.Socksaddr
if metadata.RouteOriginalDestination.IsValid() {
originDestination = metadata.RouteOriginalDestination
} else {
originDestination = metadata.Destination
}
if metadata.Destination != M.SocksaddrFrom(destinationAddress, metadata.Destination.Port) {
if metadata.UDPDisableDomainUnmapping {
remotePacketConn = bufio.NewUnidirectionalNATPacketConn(bufio.NewPacketConn(remotePacketConn), M.SocksaddrFrom(destinationAddress, metadata.Destination.Port), originDestination)
} else {
remotePacketConn = bufio.NewNATPacketConn(bufio.NewPacketConn(remotePacketConn), M.SocksaddrFrom(destinationAddress, metadata.Destination.Port), originDestination)
}
}
if natConn, loaded := common.Cast[bufio.NATPacketConn](conn); loaded {
natConn.UpdateDestination(destinationAddress)
}
}
2024-11-24 06:45:40 +00:00
var udpTimeout time.Duration
if metadata.UDPTimeout > 0 {
udpTimeout = metadata.UDPTimeout
} else {
protocol := metadata.Protocol
if protocol == "" {
protocol = C.PortProtocols[metadata.Destination.Port]
}
if protocol != "" {
udpTimeout = C.ProtocolTimeouts[protocol]
}
}
if udpTimeout > 0 {
ctx, conn = canceler.NewPacketConn(ctx, conn, udpTimeout)
}
2024-11-20 03:32:02 +00:00
destination := bufio.NewPacketConn(remotePacketConn)
2024-11-27 10:08:19 +00:00
m.access.Lock()
element := m.connections.PushBack(conn)
m.access.Unlock()
onClose = N.AppendClose(onClose, func(it error) {
m.access.Lock()
defer m.access.Unlock()
m.connections.Remove(element)
})
2024-11-20 03:32:02 +00:00
var done atomic.Bool
go m.packetConnectionCopy(ctx, conn, destination, false, &done, onClose)
go m.packetConnectionCopy(ctx, destination, conn, true, &done, onClose)
}
2024-11-27 10:08:19 +00:00
func (m *ConnectionManager) connectionCopy(ctx context.Context, source io.Reader, destination io.Writer, direction bool, done *atomic.Bool, onClose N.CloseHandlerFunc) {
2024-11-20 03:32:02 +00:00
originSource := source
2024-11-27 10:08:19 +00:00
originDestination := destination
var readCounters, writeCounters []N.CountFunc
2024-11-20 03:32:02 +00:00
for {
2024-11-27 10:08:19 +00:00
source, readCounters = N.UnwrapCountReader(source, readCounters)
destination, writeCounters = N.UnwrapCountWriter(destination, writeCounters)
if cachedSrc, isCached := source.(N.CachedReader); isCached {
cachedBuffer := cachedSrc.ReadCached()
if cachedBuffer != nil {
dataLen := cachedBuffer.Len()
_, err := destination.Write(cachedBuffer.Bytes())
cachedBuffer.Release()
if err != nil {
if done.Swap(true) {
onClose(err)
}
common.Close(originSource, originDestination)
if !direction {
m.logger.ErrorContext(ctx, "connection upload payload: ", err)
} else {
m.logger.ErrorContext(ctx, "connection download payload: ", err)
}
return
}
for _, counter := range readCounters {
counter(int64(dataLen))
}
for _, counter := range writeCounters {
counter(int64(dataLen))
}
2024-11-20 03:32:02 +00:00
}
2024-11-27 10:08:19 +00:00
continue
2024-11-20 03:32:02 +00:00
}
break
}
2024-11-27 10:08:19 +00:00
_, err := bufio.CopyWithCounters(destination, source, originSource, readCounters, writeCounters)
if err != nil {
common.Close(originDestination)
} else if duplexDst, isDuplex := destination.(N.WriteCloser); isDuplex {
err = duplexDst.CloseWrite()
2024-11-20 03:32:02 +00:00
if err != nil {
2024-11-27 10:08:19 +00:00
common.Close(originSource, originDestination)
2024-11-20 03:32:02 +00:00
}
2024-11-27 10:08:19 +00:00
} else {
common.Close(originDestination)
2024-11-20 03:32:02 +00:00
}
2024-11-27 10:08:19 +00:00
if done.Swap(true) {
onClose(err)
common.Close(originSource, originDestination)
}
if !direction {
if err == nil {
m.logger.DebugContext(ctx, "connection upload finished")
} else if !E.IsClosedOrCanceled(err) {
m.logger.ErrorContext(ctx, "connection upload closed: ", err)
} else {
m.logger.TraceContext(ctx, "connection upload closed")
}
} else {
if err == nil {
m.logger.DebugContext(ctx, "connection download finished")
} else if !E.IsClosedOrCanceled(err) {
m.logger.ErrorContext(ctx, "connection download closed: ", err)
} else {
m.logger.TraceContext(ctx, "connection download closed")
}
2024-11-20 03:32:02 +00:00
}
2024-11-27 10:08:19 +00:00
}
func (m *ConnectionManager) packetConnectionCopy(ctx context.Context, source N.PacketReader, destination N.PacketWriter, direction bool, done *atomic.Bool, onClose N.CloseHandlerFunc) {
_, err := bufio.CopyPacket(destination, source)
2024-11-20 03:32:02 +00:00
if !direction {
if E.IsClosedOrCanceled(err) {
m.logger.TraceContext(ctx, "packet upload closed")
} else {
m.logger.DebugContext(ctx, "packet upload closed: ", err)
}
} else {
if E.IsClosedOrCanceled(err) {
m.logger.TraceContext(ctx, "packet download closed")
} else {
m.logger.DebugContext(ctx, "packet download closed: ", err)
}
}
if !done.Swap(true) {
2024-11-27 10:08:19 +00:00
onClose(err)
2024-11-20 03:32:02 +00:00
}
common.Close(source, destination)
}