glider/proxy/vless/vless.go

77 lines
1.3 KiB
Go
Raw Normal View History

2020-10-01 20:59:45 +08:00
package vless
import (
2020-10-03 20:51:27 +08:00
"encoding/hex"
"errors"
2020-10-01 20:59:45 +08:00
"net/url"
2020-10-03 20:51:27 +08:00
"strings"
2020-10-01 20:59:45 +08:00
"github.com/nadoo/glider/proxy"
)
2020-10-03 20:51:27 +08:00
// Version of vless protocol.
const Version byte = 0
// CmdType is vless cmd type.
type CmdType byte
// CMD types.
const (
CmdErr CmdType = 0
CmdTCP CmdType = 1
CmdUDP CmdType = 2
)
2020-10-01 20:59:45 +08:00
// VLess struct.
type VLess struct {
dialer proxy.Dialer
proxy proxy.Proxy
addr string
uuid [16]byte
fallback string
2020-10-01 20:59:45 +08:00
}
func init() {
proxy.RegisterDialer("vless", NewVLessDialer)
2020-10-03 20:51:27 +08:00
proxy.RegisterServer("vless", NewVLessServer)
2020-10-01 20:59:45 +08:00
}
// NewVLess returns a vless proxy.
2020-10-03 20:51:27 +08:00
func NewVLess(s string, d proxy.Dialer, p proxy.Proxy) (*VLess, error) {
2020-10-01 20:59:45 +08:00
u, err := url.Parse(s)
if err != nil {
return nil, err
}
addr := u.Host
uuid, err := StrToUUID(u.User.Username())
if err != nil {
return nil, err
}
2020-10-03 20:51:27 +08:00
v := &VLess{
2020-10-01 20:59:45 +08:00
dialer: d,
2020-10-03 20:51:27 +08:00
proxy: p,
2020-10-01 20:59:45 +08:00
addr: addr,
uuid: uuid,
}
2020-10-04 23:32:21 +08:00
// v.fallback = "127.0.0.1:80"
if custom := u.Query().Get("fallback"); custom != "" {
v.fallback = custom
}
2020-10-03 20:51:27 +08:00
return v, nil
2020-10-01 20:59:45 +08:00
}
2020-10-03 20:51:27 +08:00
// StrToUUID converts string to uuid.
// s fomat: "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
func StrToUUID(s string) (uuid [16]byte, err error) {
b := []byte(strings.Replace(s, "-", "", -1))
if len(b) != 32 {
return uuid, errors.New("invalid UUID: " + s)
2020-10-02 19:09:12 +08:00
}
2020-10-03 20:51:27 +08:00
_, err = hex.Decode(uuid[:], b)
return
2020-10-01 20:59:45 +08:00
}