github.com/moqsien/xraycore@v1.8.5/common/singbridge/dialer.go (about)

     1  package singbridge
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	M "github.com/sagernet/sing/common/metadata"
     8  	N "github.com/sagernet/sing/common/network"
     9  	"github.com/moqsien/xraycore/common/net"
    10  	"github.com/moqsien/xraycore/common/net/cnc"
    11  	"github.com/moqsien/xraycore/common/session"
    12  	"github.com/moqsien/xraycore/proxy"
    13  	"github.com/moqsien/xraycore/transport"
    14  	"github.com/moqsien/xraycore/transport/internet"
    15  	"github.com/moqsien/xraycore/transport/pipe"
    16  )
    17  
    18  var _ N.Dialer = (*XrayDialer)(nil)
    19  
    20  type XrayDialer struct {
    21  	internet.Dialer
    22  }
    23  
    24  func NewDialer(dialer internet.Dialer) *XrayDialer {
    25  	return &XrayDialer{dialer}
    26  }
    27  
    28  func (d *XrayDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
    29  	return d.Dialer.Dial(ctx, ToDestination(destination, ToNetwork(network)))
    30  }
    31  
    32  func (d *XrayDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
    33  	return nil, os.ErrInvalid
    34  }
    35  
    36  type XrayOutboundDialer struct {
    37  	outbound proxy.Outbound
    38  	dialer   internet.Dialer
    39  }
    40  
    41  func NewOutboundDialer(outbound proxy.Outbound, dialer internet.Dialer) *XrayOutboundDialer {
    42  	return &XrayOutboundDialer{outbound, dialer}
    43  }
    44  
    45  func (d *XrayOutboundDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
    46  	ctx = session.ContextWithOutbound(context.Background(), &session.Outbound{
    47  		Target: ToDestination(destination, ToNetwork(network)),
    48  	})
    49  	opts := []pipe.Option{pipe.WithSizeLimit(64 * 1024)}
    50  	uplinkReader, uplinkWriter := pipe.New(opts...)
    51  	downlinkReader, downlinkWriter := pipe.New(opts...)
    52  	conn := cnc.NewConnection(cnc.ConnectionInputMulti(downlinkWriter), cnc.ConnectionOutputMulti(uplinkReader))
    53  	go d.outbound.Process(ctx, &transport.Link{Reader: downlinkReader, Writer: uplinkWriter}, d.dialer)
    54  	return conn, nil
    55  }
    56  
    57  func (d *XrayOutboundDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
    58  	return nil, os.ErrInvalid
    59  }