mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
vless: added vless support
This commit is contained in:
1 parent
02220387e9
commit
c01c2d1839
7 files changed
+237
-5
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package vless
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Atyp is vless addr type.
|
||||
type Atyp byte
|
||||
|
||||
// Atyp
|
||||
const (
|
||||
AtypErr Atyp = 0
|
||||
AtypIP4 Atyp = 1
|
||||
AtypDomain Atyp = 2
|
||||
AtypIP6 Atyp = 3
|
||||
)
|
||||
|
||||
// Addr is vless addr.
|
||||
type Addr []byte
|
||||
|
||||
// Port is vless addr port.
|
||||
type Port uint16
|
||||
|
||||
// ParseAddr parses the address in string s.
|
||||
func ParseAddr(s string) (Atyp, Addr, Port, error) {
|
||||
var atyp Atyp
|
||||
var addr Addr
|
||||
|
||||
host, port, err := net.SplitHostPort(s)
|
||||
if err != nil {
|
||||
return 0, nil, 0, err
|
||||
}
|
||||
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
if ip4 := ip.To4(); ip4 != nil {
|
||||
addr = make([]byte, net.IPv4len)
|
||||
atyp = AtypIP4
|
||||
copy(addr[:], ip4)
|
||||
} else {
|
||||
addr = make([]byte, net.IPv6len)
|
||||
atyp = AtypIP6
|
||||
copy(addr[:], ip)
|
||||
}
|
||||
} else {
|
||||
if len(host) > 255 {
|
||||
return 0, nil, 0, err
|
||||
}
|
||||
addr = make([]byte, 1+len(host))
|
||||
atyp = AtypDomain
|
||||
addr[0] = byte(len(host))
|
||||
copy(addr[1:], host)
|
||||
}
|
||||
|
||||
portnum, err := strconv.ParseUint(port, 10, 16)
|
||||
if err != nil {
|
||||
return 0, nil, 0, err
|
||||
}
|
||||
|
||||
return atyp, addr, Port(portnum), err
|
||||
}
|
||||
Reference in new issue
Block a user