sing-box/outbound/default.go

166 lines
4.8 KiB
Go
Raw Normal View History

package outbound
import (
"context"
"net"
"runtime"
"time"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing-box/adapter"
2022-07-25 22:02:39 +08:00
"github.com/sagernet/sing-box/common/canceler"
2022-07-08 23:03:57 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
2022-07-07 23:36:32 +08:00
N "github.com/sagernet/sing/common/network"
)
type myOutboundAdapter struct {
protocol string
2022-07-23 09:15:47 +08:00
network []string
router adapter.Router
2022-07-12 15:17:29 +08:00
logger log.ContextLogger
tag string
}
func (a *myOutboundAdapter) Type() string {
return a.protocol
}
func (a *myOutboundAdapter) Tag() string {
return a.tag
}
func (a *myOutboundAdapter) Network() []string {
return a.network
}
2022-07-07 23:36:32 +08:00
func NewConnection(ctx context.Context, this N.Dialer, conn net.Conn, metadata adapter.InboundContext) error {
ctx = adapter.WithContext(ctx, &metadata)
var outConn net.Conn
var err error
if len(metadata.DestinationAddresses) > 0 {
2022-07-30 00:29:22 +08:00
outConn, err = N.DialSerial(ctx, this, N.NetworkTCP, metadata.Destination, metadata.DestinationAddresses)
2022-07-07 23:36:32 +08:00
} else {
2022-07-30 00:29:22 +08:00
outConn, err = this.DialContext(ctx, N.NetworkTCP, metadata.Destination)
2022-07-07 23:36:32 +08:00
}
if err != nil {
2022-07-30 00:29:22 +08:00
return N.HandshakeFailure(conn, err)
2022-07-07 23:36:32 +08:00
}
2022-08-08 20:56:53 +08:00
if cachedReader, isCached := conn.(N.CachedReader); isCached {
payload := cachedReader.ReadCached()
if payload != nil && !payload.IsEmpty() {
_, err = outConn.Write(payload.Bytes())
if err != nil {
return err
}
}
}
2022-07-07 23:36:32 +08:00
return bufio.CopyConn(ctx, conn, outConn)
}
func NewEarlyConnection(ctx context.Context, this N.Dialer, conn net.Conn, metadata adapter.InboundContext) error {
ctx = adapter.WithContext(ctx, &metadata)
var outConn net.Conn
var err error
if len(metadata.DestinationAddresses) > 0 {
2022-07-30 00:29:22 +08:00
outConn, err = N.DialSerial(ctx, this, N.NetworkTCP, metadata.Destination, metadata.DestinationAddresses)
2022-07-07 23:36:32 +08:00
} else {
2022-07-30 00:29:22 +08:00
outConn, err = this.DialContext(ctx, N.NetworkTCP, metadata.Destination)
2022-07-07 23:36:32 +08:00
}
if err != nil {
2022-07-30 00:29:22 +08:00
return N.HandshakeFailure(conn, err)
2022-07-07 23:36:32 +08:00
}
return CopyEarlyConn(ctx, conn, outConn)
}
func NewPacketConnection(ctx context.Context, this N.Dialer, conn N.PacketConn, metadata adapter.InboundContext) error {
switch metadata.Protocol {
case C.ProtocolQUIC, C.ProtocolDNS:
return connectPacketConnection(ctx, this, conn, metadata)
}
2022-07-07 23:36:32 +08:00
ctx = adapter.WithContext(ctx, &metadata)
var outConn net.PacketConn
var err error
if len(metadata.DestinationAddresses) > 0 {
2022-07-11 18:44:59 +08:00
outConn, err = N.ListenSerial(ctx, this, metadata.Destination, metadata.DestinationAddresses)
2022-07-07 23:36:32 +08:00
} else {
outConn, err = this.ListenPacket(ctx, metadata.Destination)
}
if err != nil {
2022-07-30 00:29:22 +08:00
return N.HandshakeFailure(conn, err)
2022-07-07 23:36:32 +08:00
}
2022-07-25 22:02:39 +08:00
if metadata.Protocol != "" {
switch metadata.Protocol {
case C.ProtocolQUIC:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.QUICTimeout)
case C.ProtocolDNS:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.DNSTimeout)
case C.ProtocolSTUN:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.STUNTimeout)
}
}
2022-07-07 23:36:32 +08:00
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outConn))
}
func connectPacketConnection(ctx context.Context, this N.Dialer, conn N.PacketConn, metadata adapter.InboundContext) error {
ctx = adapter.WithContext(ctx, &metadata)
var outConn net.Conn
var err error
if len(metadata.DestinationAddresses) > 0 {
2022-08-09 14:48:08 +08:00
outConn, err = N.DialSerial(ctx, this, N.NetworkUDP, metadata.Destination, metadata.DestinationAddresses)
} else {
2022-08-09 14:48:08 +08:00
outConn, err = this.DialContext(ctx, N.NetworkUDP, metadata.Destination)
}
if err != nil {
return N.HandshakeFailure(conn, err)
}
if metadata.Protocol != "" {
switch metadata.Protocol {
case C.ProtocolQUIC:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.QUICTimeout)
case C.ProtocolDNS:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.DNSTimeout)
case C.ProtocolSTUN:
ctx, conn = canceler.NewPacketConn(ctx, conn, C.STUNTimeout)
}
}
return bufio.CopyPacketConn(ctx, conn, bufio.NewUnbindPacketConn(outConn))
}
func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) error {
2022-08-08 20:56:53 +08:00
if cachedReader, isCached := conn.(N.CachedReader); isCached {
2022-07-11 20:55:14 +08:00
payload := cachedReader.ReadCached()
if payload != nil && !payload.IsEmpty() {
_, err := serverConn.Write(payload.Bytes())
if err != nil {
return err
}
return bufio.CopyConn(ctx, conn, serverConn)
}
}
_payload := buf.StackNew()
payload := common.Dup(_payload)
2022-07-18 20:40:14 +08:00
err := conn.SetReadDeadline(time.Now().Add(C.ReadPayloadTimeout))
if err != nil {
return err
}
2022-07-26 12:37:03 +08:00
_, err = payload.ReadOnceFrom(conn)
if err != nil && !E.IsTimeout(err) {
return E.Cause(err, "read payload")
}
err = conn.SetReadDeadline(time.Time{})
if err != nil {
payload.Release()
return err
}
_, err = serverConn.Write(payload.Bytes())
if err != nil {
2022-07-30 00:29:22 +08:00
return N.HandshakeFailure(conn, err)
}
runtime.KeepAlive(_payload)
return bufio.CopyConn(ctx, conn, serverConn)
}