sing-box/outbound/direct.go

216 lines
6.7 KiB
Go
Raw Normal View History

2022-07-01 11:34:02 +00:00
package outbound
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"
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"
2022-09-03 02:01:09 +00:00
"github.com/sagernet/sing-dns"
2022-12-21 13:58:03 +00:00
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
2022-08-23 13:07:35 +00:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-08 15:03:57 +00:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2022-08-23 13:07:35 +00:00
"github.com/pires/go-proxyproto"
2022-06-30 13:27:56 +00:00
)
2022-09-03 02:01:09 +00:00
var (
_ adapter.Outbound = (*Direct)(nil)
_ N.ParallelDialer = (*Direct)(nil)
)
2022-06-30 13:27:56 +00:00
2022-07-01 11:34:02 +00:00
type Direct struct {
myOutboundAdapter
dialer N.Dialer
2022-09-03 02:01:09 +00:00
domainStrategy dns.DomainStrategy
fallbackDelay time.Duration
2022-06-30 13:27:56 +00:00
overrideOption int
overrideDestination M.Socksaddr
2022-08-23 13:07:35 +00:00
proxyProto uint8
2022-06-30 13:27:56 +00:00
}
2022-08-23 13:07:35 +00:00
func NewDirect(router adapter.Router, logger log.ContextLogger, tag string, options option.DirectOutboundOptions) (*Direct, error) {
options.UDPFragmentDefault = true
2022-07-01 11:34:02 +00:00
outbound := &Direct{
myOutboundAdapter: myOutboundAdapter{
2023-06-13 14:38:05 +00:00
protocol: C.TypeDirect,
network: []string{N.NetworkTCP, N.NetworkUDP},
router: router,
logger: logger,
tag: tag,
dependencies: withDialerDependency(options.DialerOptions),
2022-07-01 11:34:02 +00:00
},
2022-09-03 02:01:09 +00:00
domainStrategy: dns.DomainStrategy(options.DomainStrategy),
fallbackDelay: time.Duration(options.FallbackDelay),
2022-09-03 04:55:10 +00:00
dialer: dialer.New(router, options.DialerOptions),
2022-09-03 02:01:09 +00:00
proxyProto: options.ProxyProtocol,
2022-08-23 13:07:35 +00:00
}
if options.ProxyProtocol > 2 {
return nil, E.New("invalid proxy protocol option: ", options.ProxyProtocol)
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
}
2022-07-07 13:47:21 +00:00
func (h *Direct) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
ctx, metadata := adapter.AppendContext(ctx)
2022-08-23 13:07:35 +00:00
originDestination := metadata.Destination
2022-07-07 13:47:21 +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
}
2022-08-23 13:07:35 +00:00
conn, err := h.dialer.DialContext(ctx, network, destination)
if err != nil {
return nil, err
}
2022-09-03 02:01:09 +00:00
if h.proxyProto > 0 {
source := metadata.Source
if !source.IsValid() {
source = M.SocksaddrFromNet(conn.LocalAddr())
}
if originDestination.Addr.Is6() {
source = M.SocksaddrFrom(netip.AddrFrom16(source.Addr.As16()), source.Port)
}
header := proxyproto.HeaderProxyFromAddrs(h.proxyProto, source.TCPAddr(), originDestination.TCPAddr())
_, err = header.WriteTo(conn)
if err != nil {
conn.Close()
return nil, E.Cause(err, "write proxy protocol header")
}
}
return conn, nil
}
func (h *Direct) DialParallel(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr) (net.Conn, error) {
ctx, metadata := adapter.AppendContext(ctx)
originDestination := metadata.Destination
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 {
2022-10-07 12:30:27 +00:00
domainStrategy = dns.DomainStrategy(metadata.InboundOptions.DomainStrategy)
2022-09-03 02:01:09 +00:00
}
conn, err := N.DialParallel(ctx, h.dialer, network, destination, destinationAddresses, domainStrategy == dns.DomainStrategyPreferIPv6, h.fallbackDelay)
if err != nil {
return nil, err
}
2022-08-23 13:07:35 +00:00
if h.proxyProto > 0 {
source := metadata.Source
if !source.IsValid() {
source = M.SocksaddrFromNet(conn.LocalAddr())
}
if originDestination.Addr.Is6() {
source = M.SocksaddrFrom(netip.AddrFrom16(source.Addr.As16()), source.Port)
}
header := proxyproto.HeaderProxyFromAddrs(h.proxyProto, source.TCPAddr(), originDestination.TCPAddr())
_, err = header.WriteTo(conn)
if err != nil {
conn.Close()
return nil, E.Cause(err, "write proxy protocol header")
}
}
return conn, nil
2022-06-30 13:27:56 +00:00
}
2022-07-07 13:47:21 +00:00
func (h *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
ctx, metadata := adapter.AppendContext(ctx)
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)
}
conn, err := h.dialer.ListenPacket(ctx, destination)
if err != nil {
return nil, err
}
if h.overrideOption == 0 {
return conn, nil
} else {
return &overridePacketConn{bufio.NewPacketConn(conn), destination}, nil
}
2022-06-30 13:27:56 +00:00
}
2022-07-07 15:36:32 +00:00
func (h *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-07-25 08:01:10 +00:00
return NewConnection(ctx, h, conn, metadata)
2022-06-30 13:27:56 +00:00
}
2022-07-07 15:36:32 +00:00
func (h *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return NewPacketConnection(ctx, h, conn, metadata)
2022-06-30 13:27:56 +00:00
}
2022-12-21 13:58:03 +00:00
type overridePacketConn struct {
N.NetPacketConn
overrideDestination M.Socksaddr
}
func (c *overridePacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
return c.NetPacketConn.WritePacket(buffer, c.overrideDestination)
}
func (c *overridePacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
return c.NetPacketConn.WriteTo(p, c.overrideDestination.UDPAddr())
}
func (c *overridePacketConn) Upstream() any {
return c.NetPacketConn
}