2021-07-01 14:49:29 +00:00
|
|
|
package vless
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2023-01-11 14:01:15 +00:00
|
|
|
"github.com/Dreamacro/clash/common/utils"
|
|
|
|
|
2021-07-01 14:49:29 +00:00
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
)
|
|
|
|
|
2021-07-06 15:55:34 +00:00
|
|
|
const (
|
|
|
|
XRO = "xtls-rprx-origin"
|
|
|
|
XRD = "xtls-rprx-direct"
|
|
|
|
XRS = "xtls-rprx-splice"
|
2023-02-25 05:12:19 +00:00
|
|
|
XRV = "xtls-rprx-vision"
|
2021-07-06 15:55:34 +00:00
|
|
|
|
|
|
|
Version byte = 0 // protocol version. preview version is 0
|
|
|
|
)
|
2021-07-01 14:49:29 +00:00
|
|
|
|
|
|
|
// Command types
|
|
|
|
const (
|
|
|
|
CommandTCP byte = 1
|
|
|
|
CommandUDP byte = 2
|
2023-01-11 14:01:15 +00:00
|
|
|
CommandMux byte = 3
|
2021-07-01 14:49:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Addr types
|
|
|
|
const (
|
|
|
|
AtypIPv4 byte = 1
|
|
|
|
AtypDomainName byte = 2
|
|
|
|
AtypIPv6 byte = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
// DstAddr store destination address
|
|
|
|
type DstAddr struct {
|
|
|
|
UDP bool
|
|
|
|
AddrType byte
|
|
|
|
Addr []byte
|
2023-01-11 14:01:15 +00:00
|
|
|
Port uint16
|
|
|
|
Mux bool // currently used for XUDP only
|
2021-07-01 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client is vless connection generator
|
|
|
|
type Client struct {
|
2021-07-06 15:55:34 +00:00
|
|
|
uuid *uuid.UUID
|
|
|
|
Addons *Addons
|
|
|
|
XTLSShow bool
|
2021-07-01 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StreamConn return a Conn with net.Conn and DstAddr
|
|
|
|
func (c *Client) StreamConn(conn net.Conn, dst *DstAddr) (net.Conn, error) {
|
2021-07-06 15:55:34 +00:00
|
|
|
return newConn(conn, c, dst)
|
2021-07-01 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient return Client instance
|
2021-07-06 15:55:34 +00:00
|
|
|
func NewClient(uuidStr string, addons *Addons, xtlsShow bool) (*Client, error) {
|
2022-05-06 05:28:09 +00:00
|
|
|
uid, err := utils.UUIDMap(uuidStr)
|
2021-07-01 14:49:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
2021-07-06 15:55:34 +00:00
|
|
|
uuid: &uid,
|
|
|
|
Addons: addons,
|
|
|
|
XTLSShow: xtlsShow,
|
2021-07-01 14:49:29 +00:00
|
|
|
}, nil
|
|
|
|
}
|