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