2024-11-01 16:39:02 +00:00
|
|
|
package direct
|
2022-06-30 13:27:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2022-08-23 13:07:35 +00:00
|
|
|
"net/netip"
|
2022-09-03 02:01:09 +00:00
|
|
|
"time"
|
2022-06-30 13:27:56 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2024-11-01 16:39:02 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter/outbound"
|
2022-07-07 13:47:21 +00:00
|
|
|
"github.com/sagernet/sing-box/common/dialer"
|
2022-06-30 13:27:56 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
2022-07-02 06:07:50 +00:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2024-11-13 11:05:28 +00:00
|
|
|
"github.com/sagernet/sing-dns"
|
2024-11-12 11:37:10 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-12-21 13:58:03 +00:00
|
|
|
"github.com/sagernet/sing/common/bufio"
|
2022-08-23 13:07:35 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2024-11-01 16:39:02 +00:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
2022-07-08 15:03:57 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-06-30 13:27:56 +00:00
|
|
|
)
|
|
|
|
|
2024-11-01 16:39:02 +00:00
|
|
|
func RegisterOutbound(registry *outbound.Registry) {
|
|
|
|
outbound.Register[option.DirectOutboundOptions](registry, C.TypeDirect, NewOutbound)
|
|
|
|
}
|
|
|
|
|
2024-11-12 11:37:10 +00:00
|
|
|
var (
|
|
|
|
_ N.ParallelDialer = (*Outbound)(nil)
|
|
|
|
_ dialer.ParallelNetworkDialer = (*Outbound)(nil)
|
|
|
|
)
|
2022-06-30 13:27:56 +00:00
|
|
|
|
2024-11-01 16:39:02 +00:00
|
|
|
type Outbound struct {
|
|
|
|
outbound.Adapter
|
2024-11-12 11:37:10 +00:00
|
|
|
logger logger.ContextLogger
|
|
|
|
dialer dialer.ParallelInterfaceDialer
|
|
|
|
domainStrategy dns.DomainStrategy
|
|
|
|
fallbackDelay time.Duration
|
|
|
|
networkStrategy C.NetworkStrategy
|
2024-11-13 11:05:28 +00:00
|
|
|
networkType []C.InterfaceType
|
|
|
|
fallbackNetworkType []C.InterfaceType
|
2024-11-12 11:37:10 +00:00
|
|
|
networkFallbackDelay time.Duration
|
|
|
|
overrideOption int
|
|
|
|
overrideDestination M.Socksaddr
|
2024-10-21 15:38:34 +00:00
|
|
|
// loopBack *loopBackDetector
|
2022-06-30 13:27:56 +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.DirectOutboundOptions) (adapter.Outbound, error) {
|
2022-09-21 07:32:32 +00:00
|
|
|
options.UDPFragmentDefault = true
|
2024-11-12 11:37:10 +00:00
|
|
|
outboundDialer, err := dialer.NewDirect(ctx, options.DialerOptions)
|
2023-08-08 08:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-11-01 16:39:02 +00:00
|
|
|
outbound := &Outbound{
|
2024-11-21 10:10:41 +00:00
|
|
|
Adapter: outbound.NewAdapterWithDialerOptions(C.TypeDirect, tag, []string{N.NetworkTCP, N.NetworkUDP}, options.DialerOptions),
|
2024-11-12 11:37:10 +00:00
|
|
|
logger: logger,
|
|
|
|
domainStrategy: dns.DomainStrategy(options.DomainStrategy),
|
|
|
|
fallbackDelay: time.Duration(options.FallbackDelay),
|
|
|
|
networkStrategy: C.NetworkStrategy(options.NetworkStrategy),
|
2024-11-13 11:05:28 +00:00
|
|
|
networkType: common.Map(options.NetworkType, option.InterfaceType.Build),
|
|
|
|
fallbackNetworkType: common.Map(options.FallbackNetworkType, option.InterfaceType.Build),
|
2024-11-12 11:37:10 +00:00
|
|
|
networkFallbackDelay: time.Duration(options.NetworkFallbackDelay),
|
|
|
|
dialer: outboundDialer,
|
2024-10-21 15:38:34 +00:00
|
|
|
// loopBack: newLoopBackDetector(router),
|
2022-08-23 13:07:35 +00:00
|
|
|
}
|
2023-10-25 04:00:00 +00:00
|
|
|
if options.ProxyProtocol != 0 {
|
|
|
|
return nil, E.New("Proxy Protocol is deprecated and removed in sing-box 1.6.0")
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
if options.OverrideAddress != "" && options.OverridePort != 0 {
|
|
|
|
outbound.overrideOption = 1
|
|
|
|
outbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
|
|
|
|
} else if options.OverrideAddress != "" {
|
|
|
|
outbound.overrideOption = 2
|
|
|
|
outbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
|
|
|
|
} else if options.OverridePort != 0 {
|
|
|
|
outbound.overrideOption = 3
|
|
|
|
outbound.overrideDestination = M.Socksaddr{Port: options.OverridePort}
|
|
|
|
}
|
2022-08-23 13:07:35 +00:00
|
|
|
return outbound, nil
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2024-11-01 16:39:02 +00:00
|
|
|
func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
2024-10-26 23:45:15 +00:00
|
|
|
ctx, metadata := adapter.ExtendContext(ctx)
|
2024-11-01 16:39:02 +00:00
|
|
|
metadata.Outbound = h.Tag()
|
2022-07-07 15:36:32 +00:00
|
|
|
metadata.Destination = destination
|
2022-07-07 13:47:21 +00:00
|
|
|
switch h.overrideOption {
|
2022-06-30 13:27:56 +00:00
|
|
|
case 1:
|
2022-07-07 13:47:21 +00:00
|
|
|
destination = h.overrideDestination
|
2022-07-02 06:07:50 +00:00
|
|
|
case 2:
|
2022-07-07 13:47:21 +00:00
|
|
|
newDestination := h.overrideDestination
|
2022-06-30 13:27:56 +00:00
|
|
|
newDestination.Port = destination.Port
|
|
|
|
destination = newDestination
|
2022-07-02 06:07:50 +00:00
|
|
|
case 3:
|
2022-07-07 13:47:21 +00:00
|
|
|
destination.Port = h.overrideDestination.Port
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-08-23 13:07:35 +00:00
|
|
|
network = N.NetworkName(network)
|
2022-06-30 13:27:56 +00:00
|
|
|
switch network {
|
2022-07-29 16:29:22 +00:00
|
|
|
case N.NetworkTCP:
|
2022-07-12 07:17:29 +00:00
|
|
|
h.logger.InfoContext(ctx, "outbound connection to ", destination)
|
2022-07-29 16:29:22 +00:00
|
|
|
case N.NetworkUDP:
|
2022-07-12 07:17:29 +00:00
|
|
|
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
/*conn, err := h.dialer.DialContext(ctx, network, destination)
|
2023-12-20 02:52:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
return h.loopBack.NewConn(conn), nil*/
|
|
|
|
return h.dialer.DialContext(ctx, network, destination)
|
2022-09-03 02:01:09 +00:00
|
|
|
}
|
|
|
|
|
2024-11-12 11:37:10 +00:00
|
|
|
func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
|
|
ctx, metadata := adapter.ExtendContext(ctx)
|
|
|
|
metadata.Outbound = h.Tag()
|
|
|
|
metadata.Destination = destination
|
|
|
|
originDestination := destination
|
|
|
|
switch h.overrideOption {
|
|
|
|
case 1:
|
|
|
|
destination = h.overrideDestination
|
|
|
|
case 2:
|
|
|
|
newDestination := h.overrideDestination
|
|
|
|
newDestination.Port = destination.Port
|
|
|
|
destination = newDestination
|
|
|
|
case 3:
|
|
|
|
destination.Port = h.overrideDestination.Port
|
|
|
|
}
|
|
|
|
if h.overrideOption == 0 {
|
|
|
|
h.logger.InfoContext(ctx, "outbound packet connection")
|
|
|
|
} else {
|
|
|
|
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
|
|
|
}
|
|
|
|
conn, err := h.dialer.ListenPacket(ctx, destination)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// conn = h.loopBack.NewPacketConn(bufio.NewPacketConn(conn), destination)
|
|
|
|
if originDestination != destination {
|
|
|
|
conn = bufio.NewNATPacketConn(bufio.NewPacketConn(conn), destination, originDestination)
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2024-11-01 16:39:02 +00:00
|
|
|
func (h *Outbound) DialParallel(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr) (net.Conn, error) {
|
2024-10-26 23:45:15 +00:00
|
|
|
ctx, metadata := adapter.ExtendContext(ctx)
|
2024-11-01 16:39:02 +00:00
|
|
|
metadata.Outbound = h.Tag()
|
2022-09-03 02:01:09 +00:00
|
|
|
metadata.Destination = destination
|
|
|
|
switch h.overrideOption {
|
|
|
|
case 1, 2:
|
|
|
|
// override address
|
|
|
|
return h.DialContext(ctx, network, destination)
|
|
|
|
case 3:
|
|
|
|
destination.Port = h.overrideDestination.Port
|
|
|
|
}
|
|
|
|
network = N.NetworkName(network)
|
|
|
|
switch network {
|
|
|
|
case N.NetworkTCP:
|
|
|
|
h.logger.InfoContext(ctx, "outbound connection to ", destination)
|
|
|
|
case N.NetworkUDP:
|
|
|
|
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
|
|
|
}
|
|
|
|
var domainStrategy dns.DomainStrategy
|
|
|
|
if h.domainStrategy != dns.DomainStrategyAsIS {
|
|
|
|
domainStrategy = h.domainStrategy
|
|
|
|
} else {
|
2022-10-07 12:30:27 +00:00
|
|
|
domainStrategy = dns.DomainStrategy(metadata.InboundOptions.DomainStrategy)
|
2022-09-03 02:01:09 +00:00
|
|
|
}
|
2024-11-12 11:37:10 +00:00
|
|
|
switch domainStrategy {
|
|
|
|
case dns.DomainStrategyUseIPv4:
|
|
|
|
destinationAddresses = common.Filter(destinationAddresses, netip.Addr.Is4)
|
|
|
|
if len(destinationAddresses) == 0 {
|
|
|
|
return nil, E.New("no IPv4 address available for ", destination)
|
|
|
|
}
|
|
|
|
case dns.DomainStrategyUseIPv6:
|
|
|
|
destinationAddresses = common.Filter(destinationAddresses, netip.Addr.Is6)
|
|
|
|
if len(destinationAddresses) == 0 {
|
|
|
|
return nil, E.New("no IPv6 address available for ", destination)
|
|
|
|
}
|
|
|
|
}
|
2024-11-13 11:05:28 +00:00
|
|
|
return dialer.DialParallelNetwork(ctx, h.dialer, network, destination, destinationAddresses, domainStrategy == dns.DomainStrategyPreferIPv6, h.networkStrategy, h.networkType, h.fallbackNetworkType, h.fallbackDelay)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 11:05:28 +00:00
|
|
|
func (h *Outbound) DialParallelNetwork(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr, networkStrategy C.NetworkStrategy, networkType []C.InterfaceType, fallbackNetworkType []C.InterfaceType, fallbackDelay time.Duration) (net.Conn, error) {
|
2024-11-12 11:37:10 +00:00
|
|
|
ctx, metadata := adapter.ExtendContext(ctx)
|
|
|
|
metadata.Outbound = h.Tag()
|
|
|
|
metadata.Destination = destination
|
|
|
|
switch h.overrideOption {
|
|
|
|
case 1, 2:
|
|
|
|
// override address
|
|
|
|
return h.DialContext(ctx, network, destination)
|
|
|
|
case 3:
|
|
|
|
destination.Port = h.overrideDestination.Port
|
|
|
|
}
|
|
|
|
network = N.NetworkName(network)
|
|
|
|
switch network {
|
|
|
|
case N.NetworkTCP:
|
|
|
|
h.logger.InfoContext(ctx, "outbound connection to ", destination)
|
|
|
|
case N.NetworkUDP:
|
|
|
|
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
|
|
|
}
|
|
|
|
var domainStrategy dns.DomainStrategy
|
|
|
|
if h.domainStrategy != dns.DomainStrategyAsIS {
|
|
|
|
domainStrategy = h.domainStrategy
|
|
|
|
} else {
|
|
|
|
domainStrategy = dns.DomainStrategy(metadata.InboundOptions.DomainStrategy)
|
|
|
|
}
|
|
|
|
switch domainStrategy {
|
|
|
|
case dns.DomainStrategyUseIPv4:
|
|
|
|
destinationAddresses = common.Filter(destinationAddresses, netip.Addr.Is4)
|
|
|
|
if len(destinationAddresses) == 0 {
|
|
|
|
return nil, E.New("no IPv4 address available for ", destination)
|
|
|
|
}
|
|
|
|
case dns.DomainStrategyUseIPv6:
|
|
|
|
destinationAddresses = common.Filter(destinationAddresses, netip.Addr.Is6)
|
|
|
|
if len(destinationAddresses) == 0 {
|
|
|
|
return nil, E.New("no IPv6 address available for ", destination)
|
|
|
|
}
|
|
|
|
}
|
2024-11-13 11:05:28 +00:00
|
|
|
return dialer.DialParallelNetwork(ctx, h.dialer, network, destination, destinationAddresses, domainStrategy == dns.DomainStrategyPreferIPv6, networkStrategy, networkType, fallbackNetworkType, fallbackDelay)
|
2024-11-12 11:37:10 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 11:05:28 +00:00
|
|
|
func (h *Outbound) ListenSerialNetworkPacket(ctx context.Context, destination M.Socksaddr, destinationAddresses []netip.Addr, networkStrategy C.NetworkStrategy, networkType []C.InterfaceType, fallbackNetworkType []C.InterfaceType, fallbackDelay time.Duration) (net.PacketConn, netip.Addr, error) {
|
2023-10-25 04:00:00 +00:00
|
|
|
ctx, metadata := adapter.ExtendContext(ctx)
|
2024-11-01 16:39:02 +00:00
|
|
|
metadata.Outbound = h.Tag()
|
2022-07-07 15:36:32 +00:00
|
|
|
metadata.Destination = destination
|
2022-12-21 13:58:03 +00:00
|
|
|
switch h.overrideOption {
|
|
|
|
case 1:
|
|
|
|
destination = h.overrideDestination
|
|
|
|
case 2:
|
|
|
|
newDestination := h.overrideDestination
|
|
|
|
newDestination.Port = destination.Port
|
|
|
|
destination = newDestination
|
|
|
|
case 3:
|
|
|
|
destination.Port = h.overrideDestination.Port
|
|
|
|
}
|
|
|
|
if h.overrideOption == 0 {
|
|
|
|
h.logger.InfoContext(ctx, "outbound packet connection")
|
|
|
|
} else {
|
|
|
|
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
|
|
|
}
|
2024-11-13 11:05:28 +00:00
|
|
|
conn, newDestination, err := dialer.ListenSerialNetworkPacket(ctx, h.dialer, destination, destinationAddresses, networkStrategy, networkType, fallbackNetworkType, fallbackDelay)
|
2022-12-21 13:58:03 +00:00
|
|
|
if err != nil {
|
2024-11-12 11:37:10 +00:00
|
|
|
return nil, netip.Addr{}, err
|
2022-12-21 13:58:03 +00:00
|
|
|
}
|
2024-11-12 11:37:10 +00:00
|
|
|
return conn, newDestination, nil
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2024-11-01 16:39:02 +00:00
|
|
|
/*func (h *Outbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
2024-04-17 13:35:26 +00:00
|
|
|
if h.loopBack.CheckConn(metadata.Source.AddrPort(), M.AddrPortFromNet(conn.LocalAddr())) {
|
2023-12-20 02:52:11 +00:00
|
|
|
return E.New("reject loopback connection to ", metadata.Destination)
|
|
|
|
}
|
2022-07-25 08:01:10 +00:00
|
|
|
return NewConnection(ctx, h, conn, metadata)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2024-11-01 16:39:02 +00:00
|
|
|
func (h *Outbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
2024-04-17 13:35:26 +00:00
|
|
|
if h.loopBack.CheckPacketConn(metadata.Source.AddrPort(), M.AddrPortFromNet(conn.LocalAddr())) {
|
2023-12-20 02:52:11 +00:00
|
|
|
return E.New("reject loopback packet connection to ", metadata.Destination)
|
|
|
|
}
|
2022-07-07 15:36:32 +00:00
|
|
|
return NewPacketConnection(ctx, h, conn, metadata)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
*/
|