github.com/sagernet/sing-box@v1.9.0-rc.20/common/dialer/router.go (about)

     1  package dialer
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  
     7  	"github.com/sagernet/sing-box/adapter"
     8  	M "github.com/sagernet/sing/common/metadata"
     9  	N "github.com/sagernet/sing/common/network"
    10  )
    11  
    12  type RouterDialer struct {
    13  	router adapter.Router
    14  }
    15  
    16  func NewRouter(router adapter.Router) N.Dialer {
    17  	return &RouterDialer{router: router}
    18  }
    19  
    20  func (d *RouterDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
    21  	dialer, err := d.router.DefaultOutbound(network)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	return dialer.DialContext(ctx, network, destination)
    26  }
    27  
    28  func (d *RouterDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
    29  	dialer, err := d.router.DefaultOutbound(N.NetworkUDP)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	return dialer.ListenPacket(ctx, destination)
    34  }
    35  
    36  func (d *RouterDialer) Upstream() any {
    37  	return d.router
    38  }