vless.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package vless
  2. import (
  3. "net"
  4. "github.com/metacubex/mihomo/common/utils"
  5. "github.com/gofrs/uuid/v5"
  6. )
  7. const (
  8. XRO = "xtls-rprx-origin"
  9. XRD = "xtls-rprx-direct"
  10. XRS = "xtls-rprx-splice"
  11. XRV = "xtls-rprx-vision"
  12. Version byte = 0 // protocol version. preview version is 0
  13. )
  14. // Command types
  15. const (
  16. CommandTCP byte = 1
  17. CommandUDP byte = 2
  18. CommandMux byte = 3
  19. )
  20. // Addr types
  21. const (
  22. AtypIPv4 byte = 1
  23. AtypDomainName byte = 2
  24. AtypIPv6 byte = 3
  25. )
  26. // DstAddr store destination address
  27. type DstAddr struct {
  28. UDP bool
  29. AddrType byte
  30. Addr []byte
  31. Port uint16
  32. Mux bool // currently used for XUDP only
  33. }
  34. // Client is vless connection generator
  35. type Client struct {
  36. uuid *uuid.UUID
  37. Addons *Addons
  38. }
  39. // StreamConn return a Conn with net.Conn and DstAddr
  40. func (c *Client) StreamConn(conn net.Conn, dst *DstAddr) (net.Conn, error) {
  41. return newConn(conn, c, dst)
  42. }
  43. // NewClient return Client instance
  44. func NewClient(uuidStr string, addons *Addons) (*Client, error) {
  45. uid, err := utils.UUIDMap(uuidStr)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return &Client{
  50. uuid: &uid,
  51. Addons: addons,
  52. }, nil
  53. }