github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/vless/vless.go (about)

     1  // modified from https://github.com/yaling888/clash/blob/plus-pro/transport/vless/vless.go
     2  package vless
     3  
     4  import (
     5  	"context"
     6  	"net"
     7  
     8  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
     9  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/point"
    10  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/protocol"
    11  	"github.com/Asutorufa/yuhaiin/pkg/utils/uuid"
    12  )
    13  
    14  // Version of vmess
    15  const Version byte = 0
    16  
    17  // Command types
    18  const (
    19  	CommandTCP byte = 1
    20  	CommandUDP byte = 2
    21  )
    22  
    23  // Addr types
    24  const (
    25  	AtypIPv4       byte = 1
    26  	AtypDomainName byte = 2
    27  	AtypIPv6       byte = 3
    28  )
    29  
    30  // DstAddr store destination address
    31  type DstAddr struct {
    32  	UDP      bool
    33  	AddrType byte
    34  	Addr     []byte
    35  	Port     uint
    36  }
    37  
    38  // Client is vless connection generator
    39  type Client struct {
    40  	netapi.Proxy
    41  	uuid uuid.UUID
    42  }
    43  
    44  func (c *Client) Conn(ctx context.Context, addr netapi.Address) (net.Conn, error) {
    45  	conn, err := c.Proxy.Conn(ctx, addr)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	return newConn(conn, c, false, addr)
    51  }
    52  
    53  func (c *Client) PacketConn(ctx context.Context, addr netapi.Address) (net.PacketConn, error) {
    54  	conn, err := c.Proxy.Conn(ctx, addr)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	return newConn(conn, c, true, addr)
    60  }
    61  
    62  func init() {
    63  	point.RegisterProtocol(NewClient)
    64  }
    65  
    66  func NewClient(config *protocol.Protocol_Vless) point.WrapProxy {
    67  	return func(p netapi.Proxy) (netapi.Proxy, error) {
    68  		uid, err := uuid.ParseStd(config.Vless.GetUuid())
    69  		if err != nil {
    70  			return nil, err
    71  		}
    72  
    73  		return &Client{Proxy: p, uuid: uid}, nil
    74  	}
    75  }