2022-07-03 20:59:25 +08:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2023-04-02 12:01:19 +08:00
|
|
|
"net/netip"
|
2022-10-20 10:57:57 +08:00
|
|
|
"os"
|
2022-07-03 20:59:25 +08:00
|
|
|
"time"
|
|
|
|
|
2022-07-08 23:03:57 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
2023-06-13 22:38:05 +08:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-07-03 20:59:25 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
"github.com/sagernet/sing/common/buf"
|
|
|
|
"github.com/sagernet/sing/common/bufio"
|
2023-03-23 15:14:21 +08:00
|
|
|
"github.com/sagernet/sing/common/canceler"
|
2022-07-03 20:59:25 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-07-07 23:36:32 +08:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-03 20:59:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type myOutboundAdapter struct {
|
2023-06-13 22:38:05 +08:00
|
|
|
protocol string
|
|
|
|
network []string
|
|
|
|
router adapter.Router
|
|
|
|
logger log.ContextLogger
|
|
|
|
tag string
|
|
|
|
dependencies []string
|
2022-07-03 20:59:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *myOutboundAdapter) Type() string {
|
|
|
|
return a.protocol
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *myOutboundAdapter) Tag() string {
|
|
|
|
return a.tag
|
|
|
|
}
|
|
|
|
|
2022-07-03 23:23:18 +08:00
|
|
|
func (a *myOutboundAdapter) Network() []string {
|
|
|
|
return a.network
|
|
|
|
}
|
|
|
|
|
2023-06-13 22:38:05 +08:00
|
|
|
func (a *myOutboundAdapter) Dependencies() []string {
|
|
|
|
return a.dependencies
|
|
|
|
}
|
|
|
|
|
2023-04-13 16:02:28 +08:00
|
|
|
func (a *myOutboundAdapter) NewError(ctx context.Context, err error) {
|
|
|
|
NewError(a.logger, ctx, err)
|
|
|
|
}
|
|
|
|
|
2023-06-13 22:38:05 +08:00
|
|
|
func withDialerDependency(options option.DialerOptions) []string {
|
|
|
|
if options.Detour != "" {
|
|
|
|
return []string{options.Detour}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
return CopyEarlyConn(ctx, conn, outConn)
|
|
|
|
}
|
|
|
|
|
2023-09-06 19:13:39 +08:00
|
|
|
func NewDirectConnection(ctx context.Context, router adapter.Router, 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 {
|
|
|
|
outConn, err = N.DialSerial(ctx, this, N.NetworkTCP, metadata.Destination, metadata.DestinationAddresses)
|
|
|
|
} else if metadata.Destination.IsFqdn() {
|
|
|
|
var destinationAddresses []netip.Addr
|
|
|
|
destinationAddresses, err = router.LookupDefault(ctx, metadata.Destination.Fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return N.HandshakeFailure(conn, err)
|
|
|
|
}
|
|
|
|
outConn, err = N.DialSerial(ctx, this, N.NetworkTCP, metadata.Destination, destinationAddresses)
|
|
|
|
} else {
|
|
|
|
outConn, err = this.DialContext(ctx, N.NetworkTCP, metadata.Destination)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return N.HandshakeFailure(conn, err)
|
|
|
|
}
|
|
|
|
return CopyEarlyConn(ctx, conn, outConn)
|
|
|
|
}
|
|
|
|
|
2022-07-07 23:36:32 +08:00
|
|
|
func NewPacketConnection(ctx context.Context, this N.Dialer, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
|
ctx = adapter.WithContext(ctx, &metadata)
|
|
|
|
var outConn net.PacketConn
|
2023-04-02 12:01:19 +08:00
|
|
|
var destinationAddress netip.Addr
|
2022-07-07 23:36:32 +08:00
|
|
|
var err error
|
|
|
|
if len(metadata.DestinationAddresses) > 0 {
|
2023-04-02 12:01:19 +08:00
|
|
|
outConn, destinationAddress, 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
|
|
|
}
|
2023-04-02 12:01:19 +08:00
|
|
|
if destinationAddress.IsValid() {
|
|
|
|
if natConn, loaded := common.Cast[bufio.NATPacketConn](conn); loaded {
|
|
|
|
natConn.UpdateDestination(destinationAddress)
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 12:13:57 +08: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)
|
2022-07-30 08:39:13 +08:00
|
|
|
}
|
2022-11-24 12:00:01 +08:00
|
|
|
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outConn))
|
2022-07-30 08:39:13 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 19:13:39 +08:00
|
|
|
func NewDirectPacketConnection(ctx context.Context, router adapter.Router, this N.Dialer, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
|
ctx = adapter.WithContext(ctx, &metadata)
|
|
|
|
var outConn net.PacketConn
|
|
|
|
var destinationAddress netip.Addr
|
|
|
|
var err error
|
|
|
|
if len(metadata.DestinationAddresses) > 0 {
|
|
|
|
outConn, destinationAddress, err = N.ListenSerial(ctx, this, metadata.Destination, metadata.DestinationAddresses)
|
|
|
|
} else if metadata.Destination.IsFqdn() {
|
|
|
|
var destinationAddresses []netip.Addr
|
|
|
|
destinationAddresses, err = router.LookupDefault(ctx, metadata.Destination.Fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return N.HandshakeFailure(conn, err)
|
|
|
|
}
|
|
|
|
outConn, destinationAddress, err = N.ListenSerial(ctx, this, metadata.Destination, destinationAddresses)
|
|
|
|
} else {
|
|
|
|
outConn, err = this.ListenPacket(ctx, metadata.Destination)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return N.HandshakeFailure(conn, err)
|
|
|
|
}
|
|
|
|
if destinationAddress.IsValid() {
|
|
|
|
if natConn, loaded := common.Cast[bufio.NATPacketConn](conn); loaded {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outConn))
|
|
|
|
}
|
|
|
|
|
2022-07-03 20:59:25 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2023-02-28 20:16:15 +08:00
|
|
|
if earlyConn, isEarlyConn := common.Cast[N.EarlyConn](serverConn); isEarlyConn && earlyConn.NeedHandshake() {
|
2023-07-03 21:45:32 +08:00
|
|
|
payload := buf.NewPacket()
|
2023-02-26 23:08:20 +08:00
|
|
|
err := conn.SetReadDeadline(time.Now().Add(C.ReadPayloadTimeout))
|
|
|
|
if err != os.ErrInvalid {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, 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
|
|
|
|
}
|
2022-10-20 10:57:57 +08:00
|
|
|
}
|
2023-02-26 23:08:20 +08:00
|
|
|
_, err = serverConn.Write(payload.Bytes())
|
2022-10-20 10:57:57 +08:00
|
|
|
if err != nil {
|
2023-02-26 23:08:20 +08:00
|
|
|
return N.HandshakeFailure(conn, err)
|
2022-10-20 10:57:57 +08:00
|
|
|
}
|
2023-02-26 23:08:20 +08:00
|
|
|
payload.Release()
|
2022-07-03 20:59:25 +08:00
|
|
|
}
|
|
|
|
return bufio.CopyConn(ctx, conn, serverConn)
|
|
|
|
}
|
2023-04-13 16:02:28 +08:00
|
|
|
|
|
|
|
func NewError(logger log.ContextLogger, ctx context.Context, err error) {
|
|
|
|
common.Close(err)
|
|
|
|
if E.IsClosedOrCanceled(err) {
|
|
|
|
logger.DebugContext(ctx, "connection closed: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.ErrorContext(ctx, err)
|
|
|
|
}
|