github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/network/dialer.go (about)

     1  package network
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"net/netip"
     7  
     8  	M "github.com/sagernet/sing/common/metadata"
     9  )
    10  
    11  type Dialer interface {
    12  	DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error)
    13  	ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error)
    14  }
    15  
    16  type PayloadDialer interface {
    17  	DialPayloadContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error)
    18  }
    19  
    20  type ParallelDialer interface {
    21  	Dialer
    22  	DialParallel(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr) (net.Conn, error)
    23  }
    24  
    25  var SystemDialer ParallelDialer = &DefaultDialer{}
    26  
    27  type DefaultDialer struct {
    28  	net.Dialer
    29  	net.ListenConfig
    30  }
    31  
    32  func (d *DefaultDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
    33  	return d.Dialer.DialContext(ctx, network, destination.String())
    34  }
    35  
    36  func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
    37  	return d.ListenConfig.ListenPacket(ctx, "udp", "")
    38  }
    39  
    40  func (d *DefaultDialer) DialParallel(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr) (net.Conn, error) {
    41  	return DialParallel(ctx, d, network, destination, destinationAddresses, false, 0)
    42  }