github.com/igoogolx/clash@v1.19.8/adapter/outbound/direct.go (about)

     1  package outbound
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  
     7  	"github.com/igoogolx/clash/component/dialer"
     8  	C "github.com/igoogolx/clash/constant"
     9  )
    10  
    11  type Direct struct {
    12  	*Base
    13  }
    14  
    15  // DialContext implements C.ProxyAdapter
    16  func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
    17  	c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), d.Base.DialOptions(opts...)...)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	tcpKeepAlive(c)
    22  	return NewConn(c, d), nil
    23  }
    24  
    25  // ListenPacketContext implements C.ProxyAdapter
    26  func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
    27  	pc, err := dialer.ListenPacket(ctx, "udp", "", d.Base.DialOptions(opts...)...)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	return newPacketConn(&directPacketConn{pc}, d), nil
    32  }
    33  
    34  type directPacketConn struct {
    35  	net.PacketConn
    36  }
    37  
    38  func NewDirect() *Direct {
    39  	return &Direct{
    40  		Base: &Base{
    41  			name: "DIRECT",
    42  			tp:   C.Direct,
    43  			udp:  true,
    44  		},
    45  	}
    46  }