123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- package outbound
- import (
- "context"
- "net"
- "net/netip"
- "time"
- "github.com/sagernet/sing-box/adapter"
- "github.com/sagernet/sing-box/common/dialer"
- C "github.com/sagernet/sing-box/constant"
- "github.com/sagernet/sing-box/log"
- "github.com/sagernet/sing-box/option"
- "github.com/sagernet/sing-dns"
- "github.com/sagernet/sing/common/buf"
- "github.com/sagernet/sing/common/bufio"
- E "github.com/sagernet/sing/common/exceptions"
- M "github.com/sagernet/sing/common/metadata"
- N "github.com/sagernet/sing/common/network"
- "github.com/pires/go-proxyproto"
- )
- var (
- _ adapter.Outbound = (*Direct)(nil)
- _ N.ParallelDialer = (*Direct)(nil)
- )
- type Direct struct {
- myOutboundAdapter
- dialer N.Dialer
- domainStrategy dns.DomainStrategy
- fallbackDelay time.Duration
- overrideOption int
- overrideDestination M.Socksaddr
- proxyProto uint8
- }
- func NewDirect(router adapter.Router, logger log.ContextLogger, tag string, options option.DirectOutboundOptions) (*Direct, error) {
- options.UDPFragmentDefault = true
- outbound := &Direct{
- myOutboundAdapter: myOutboundAdapter{
- protocol: C.TypeDirect,
- network: []string{N.NetworkTCP, N.NetworkUDP},
- router: router,
- logger: logger,
- tag: tag,
- },
- domainStrategy: dns.DomainStrategy(options.DomainStrategy),
- fallbackDelay: time.Duration(options.FallbackDelay),
- dialer: dialer.New(router, options.DialerOptions),
- proxyProto: options.ProxyProtocol,
- }
- if options.ProxyProtocol > 2 {
- return nil, E.New("invalid proxy protocol option: ", options.ProxyProtocol)
- }
- 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}
- }
- return outbound, nil
- }
- func (h *Direct) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
- ctx, metadata := adapter.AppendContext(ctx)
- originDestination := metadata.Destination
- metadata.Outbound = h.tag
- metadata.Destination = 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
- }
- 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)
- }
- conn, err := h.dialer.DialContext(ctx, network, destination)
- if err != nil {
- return nil, err
- }
- 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 {
- domainStrategy = dns.DomainStrategy(metadata.InboundOptions.DomainStrategy)
- }
- conn, err := N.DialParallel(ctx, h.dialer, network, destination, destinationAddresses, domainStrategy == dns.DomainStrategyPreferIPv6, h.fallbackDelay)
- if err != nil {
- return nil, err
- }
- 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) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
- ctx, metadata := adapter.AppendContext(ctx)
- metadata.Outbound = h.tag
- metadata.Destination = 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
- }
- if h.overrideOption == 0 {
- return conn, nil
- } else {
- return &overridePacketConn{bufio.NewPacketConn(conn), destination}, nil
- }
- }
- func (h *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
- return NewConnection(ctx, h, conn, metadata)
- }
- func (h *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
- return NewPacketConnection(ctx, h, conn, metadata)
- }
- 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
- }
|