mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
vless: spport server mode
This commit is contained in:
1 parent
1ed7fbff65
commit
8b6739f12c
15 files changed
+328
-97
No files matched your search
+72
-1
@@ -1,8 +1,12 @@
|
||||
package vless
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/nadoo/glider/pool"
|
||||
)
|
||||
|
||||
// Atyp is vless addr type.
|
||||
@@ -19,6 +23,9 @@ const (
|
||||
// Addr is vless addr.
|
||||
type Addr []byte
|
||||
|
||||
// MaxHostLen is the maximum size of host in bytes.
|
||||
const MaxHostLen = 255
|
||||
|
||||
// Port is vless addr port.
|
||||
type Port uint16
|
||||
|
||||
@@ -43,7 +50,7 @@ func ParseAddr(s string) (Atyp, Addr, Port, error) {
|
||||
copy(addr[:], ip)
|
||||
}
|
||||
} else {
|
||||
if len(host) > 255 {
|
||||
if len(host) > MaxHostLen {
|
||||
return 0, nil, 0, err
|
||||
}
|
||||
addr = make([]byte, 1+len(host))
|
||||
@@ -59,3 +66,67 @@ func ParseAddr(s string) (Atyp, Addr, Port, error) {
|
||||
|
||||
return atyp, addr, Port(portnum), err
|
||||
}
|
||||
|
||||
// ReadAddr reads just enough bytes from r to get addr.
|
||||
func ReadAddr(r io.Reader) (atyp Atyp, host Addr, port Port, err error) {
|
||||
buf := pool.GetBuffer(2)
|
||||
defer pool.PutBuffer(buf)
|
||||
|
||||
// port
|
||||
_, err = io.ReadFull(r, buf[:2])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
port = Port(binary.BigEndian.Uint16(buf[:2]))
|
||||
|
||||
// atyp
|
||||
_, err = io.ReadFull(r, buf[:1])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
atyp = Atyp(buf[0])
|
||||
|
||||
switch atyp {
|
||||
case AtypIP4:
|
||||
host = make([]byte, net.IPv4len)
|
||||
_, err = io.ReadFull(r, host)
|
||||
return
|
||||
case AtypIP6:
|
||||
host = make([]byte, net.IPv6len)
|
||||
_, err = io.ReadFull(r, host)
|
||||
return
|
||||
case AtypDomain:
|
||||
_, err = io.ReadFull(r, buf[:1])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
host = make([]byte, int(buf[0]))
|
||||
_, err = io.ReadFull(r, host)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ReadAddrString reads just enough bytes from r to get addr string.
|
||||
func ReadAddrString(r io.Reader) (string, error) {
|
||||
atyp, host, port, err := ReadAddr(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return AddrString(atyp, host, port), nil
|
||||
}
|
||||
|
||||
// AddrString returns a addr string inf format "host:port".
|
||||
func AddrString(atyp Atyp, addr Addr, port Port) string {
|
||||
var host string
|
||||
|
||||
switch atyp {
|
||||
case AtypIP4, AtypIP6:
|
||||
host = net.IP(addr).String()
|
||||
case AtypDomain:
|
||||
host = string(addr)
|
||||
}
|
||||
|
||||
return net.JoinHostPort(host, strconv.Itoa(int(port)))
|
||||
}
|
||||
+37
-26
@@ -2,33 +2,55 @@ package vless
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/nadoo/glider/pool"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
const Version byte = 0
|
||||
// NewVLessDialer returns a vless proxy dialer.
|
||||
func NewVLessDialer(s string, dialer proxy.Dialer) (proxy.Dialer, error) {
|
||||
return NewVLess(s, dialer, nil)
|
||||
}
|
||||
|
||||
// CMD types.
|
||||
const (
|
||||
CmdTCP byte = 1
|
||||
CmdUDP byte = 2
|
||||
)
|
||||
// Addr returns forwarder's address.
|
||||
func (s *VLess) Addr() string {
|
||||
if s.addr == "" {
|
||||
return s.dialer.Addr()
|
||||
}
|
||||
return s.addr
|
||||
}
|
||||
|
||||
// Conn is a vless client connection.
|
||||
type Conn struct {
|
||||
// Dial connects to the address addr on the network net via the proxy.
|
||||
func (s *VLess) Dial(network, addr string) (net.Conn, error) {
|
||||
rc, err := s.dialer.Dial("tcp", s.addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewClientConn(rc, s.uuid, network, addr)
|
||||
}
|
||||
|
||||
// DialUDP connects to the given address via the proxy.
|
||||
func (s *VLess) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
|
||||
c, err := s.Dial("udp", addr)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
pkc := NewPktConn(c)
|
||||
return pkc, nil, nil
|
||||
}
|
||||
|
||||
// ClientConn is a vless client connection.
|
||||
type ClientConn struct {
|
||||
net.Conn
|
||||
rcved bool
|
||||
}
|
||||
|
||||
// ClientConn returns a new vless client conn.
|
||||
func ClientConn(c net.Conn, uuid [16]byte, network, target string) (*Conn, error) {
|
||||
// NewClientConn returns a new vless client conn.
|
||||
func NewClientConn(c net.Conn, uuid [16]byte, network, target string) (*ClientConn, error) {
|
||||
atyp, addr, port, err := ParseAddr(target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -45,7 +67,7 @@ func ClientConn(c net.Conn, uuid [16]byte, network, target string) (*Conn, error
|
||||
if network == "udp" {
|
||||
cmd = CmdUDP
|
||||
}
|
||||
buf.WriteByte(cmd) // cmd
|
||||
buf.WriteByte(byte(cmd)) // cmd
|
||||
|
||||
// target
|
||||
err = binary.Write(buf, binary.BigEndian, uint16(port)) // port
|
||||
@@ -56,10 +78,10 @@ func ClientConn(c net.Conn, uuid [16]byte, network, target string) (*Conn, error
|
||||
buf.Write(addr) //addr
|
||||
|
||||
_, err = c.Write(buf.Bytes())
|
||||
return &Conn{Conn: c}, err
|
||||
return &ClientConn{Conn: c}, err
|
||||
}
|
||||
|
||||
func (c *Conn) Read(b []byte) (n int, err error) {
|
||||
func (c *ClientConn) Read(b []byte) (n int, err error) {
|
||||
if !c.rcved {
|
||||
buf := pool.GetBuffer(2)
|
||||
defer pool.PutBuffer(buf)
|
||||
@@ -81,14 +103,3 @@ func (c *Conn) Read(b []byte) (n int, err error) {
|
||||
|
||||
return c.Conn.Read(b)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
_, err = hex.Decode(uuid[:], b)
|
||||
return
|
||||
}
|
||||
@@ -9,15 +9,11 @@ import (
|
||||
"github.com/nadoo/glider/pool"
|
||||
)
|
||||
|
||||
// PktConn .
|
||||
type PktConn struct {
|
||||
net.Conn
|
||||
}
|
||||
// PktConn is a udp Packet.Conn.
|
||||
type PktConn struct{ net.Conn }
|
||||
|
||||
// NewPktConn returns a PktConn.
|
||||
func NewPktConn(c net.Conn) *PktConn {
|
||||
return &PktConn{Conn: c}
|
||||
}
|
||||
func NewPktConn(c net.Conn) *PktConn { return &PktConn{Conn: c} }
|
||||
|
||||
// ReadFrom implements the necessary function of net.PacketConn.
|
||||
// TODO: we know that we use it in proxy.RelayUDP and the length of b is enough, check it later.
|
||||
@@ -35,7 +31,7 @@ func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
// Payload
|
||||
n, err := io.ReadFull(pc.Conn, b[:length])
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
return n, nil, err
|
||||
}
|
||||
|
||||
return n, nil, nil
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
package vless
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/nadoo/glider/log"
|
||||
"github.com/nadoo/glider/pool"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
// NewVLessServer returns a vless proxy server.
|
||||
func NewVLessServer(s string, p proxy.Proxy) (proxy.Server, error) {
|
||||
return NewVLess(s, nil, p)
|
||||
}
|
||||
|
||||
// ListenAndServe listen and serves connections.
|
||||
func (s *VLess) ListenAndServe() {
|
||||
l, err := net.Listen("tcp", s.addr)
|
||||
if err != nil {
|
||||
log.F("[vless] failed to listen on %s: %v", s.addr, err)
|
||||
return
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
log.F("[vless] listening TCP on %s", s.addr)
|
||||
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
log.F("[vless] failed to accept: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
go s.Serve(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Serve serves a connection.
|
||||
func (s *VLess) Serve(c net.Conn) {
|
||||
defer c.Close()
|
||||
|
||||
if c, ok := c.(*net.TCPConn); ok {
|
||||
c.SetKeepAlive(true)
|
||||
}
|
||||
|
||||
c = NewServerConn(c)
|
||||
_, err := s.readHeader(c)
|
||||
if err != nil {
|
||||
log.F("[vless] verify header error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
tgt, err := ReadAddrString(c)
|
||||
if err != nil {
|
||||
log.F("[vless] get target error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
dialer := s.proxy.NextDialer(tgt)
|
||||
rc, err := dialer.Dial("tcp", tgt)
|
||||
if err != nil {
|
||||
log.F("[vless] %s <-> %s via %s, error in dial: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
|
||||
return
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
log.F("[vless] %s <-> %s via %s", c.RemoteAddr(), tgt, dialer.Addr())
|
||||
|
||||
if err = proxy.Relay(c, rc); err != nil {
|
||||
log.F("[vless] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
|
||||
// record remote conn failure only
|
||||
if !strings.Contains(err.Error(), s.addr) {
|
||||
s.proxy.Record(dialer, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *VLess) readHeader(r io.Reader) (CmdType, error) {
|
||||
buf := pool.GetBuffer(16)
|
||||
defer pool.PutBuffer(buf)
|
||||
|
||||
// ver
|
||||
if _, err := io.ReadFull(r, buf[:1]); err != nil {
|
||||
return CmdErr, fmt.Errorf("get version error: %v", err)
|
||||
}
|
||||
|
||||
if buf[0] != Version {
|
||||
return CmdErr, fmt.Errorf("version %d not supported", buf[0])
|
||||
}
|
||||
|
||||
// uuid
|
||||
if _, err := io.ReadFull(r, buf[:16]); err != nil {
|
||||
return CmdErr, fmt.Errorf("get uuid error: %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(s.uuid[:], buf) {
|
||||
return CmdErr, fmt.Errorf("auth failed, client id: %02x", buf[:16])
|
||||
}
|
||||
|
||||
// addLen
|
||||
if _, err := io.ReadFull(r, buf[:1]); err != nil {
|
||||
return CmdErr, fmt.Errorf("get addon length error: %v", err)
|
||||
}
|
||||
|
||||
// ignore addons
|
||||
if addLen := int64(buf[0]); addLen > 0 {
|
||||
proxy.CopyN(ioutil.Discard, r, addLen)
|
||||
}
|
||||
|
||||
// cmd
|
||||
if _, err := io.ReadFull(r, buf[:1]); err != nil {
|
||||
return CmdErr, fmt.Errorf("get cmd error: %v", err)
|
||||
}
|
||||
|
||||
return CmdType(buf[0]), nil
|
||||
}
|
||||
|
||||
// ServerConn is a vless client connection.
|
||||
type ServerConn struct {
|
||||
net.Conn
|
||||
sent bool
|
||||
}
|
||||
|
||||
// NewServerConn returns a new vless client conn.
|
||||
func NewServerConn(c net.Conn) *ServerConn {
|
||||
return &ServerConn{Conn: c}
|
||||
}
|
||||
|
||||
func (c *ServerConn) Write(b []byte) (int, error) {
|
||||
if !c.sent {
|
||||
buf := pool.GetWriteBuffer()
|
||||
defer pool.PutWriteBuffer(buf)
|
||||
|
||||
buf.WriteByte(Version) // ver
|
||||
buf.WriteByte(0) // addonLen
|
||||
|
||||
buf.Write(b)
|
||||
c.sent = true
|
||||
|
||||
n, err := c.Conn.Write(buf.Bytes())
|
||||
return n - 2, err
|
||||
}
|
||||
|
||||
return c.Conn.Write(b)
|
||||
}
|
||||
+30
-33
@@ -1,25 +1,42 @@
|
||||
package vless
|
||||
|
||||
import (
|
||||
"net"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
// 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
|
||||
)
|
||||
|
||||
// VLess struct.
|
||||
type VLess struct {
|
||||
dialer proxy.Dialer
|
||||
proxy proxy.Proxy
|
||||
addr string
|
||||
uuid [16]byte
|
||||
}
|
||||
|
||||
func init() {
|
||||
proxy.RegisterDialer("vless", NewVLessDialer)
|
||||
proxy.RegisterServer("vless", NewVLessServer)
|
||||
}
|
||||
|
||||
// NewVLess returns a vless proxy.
|
||||
func NewVLess(s string, d proxy.Dialer) (*VLess, error) {
|
||||
func NewVLess(s string, d proxy.Dialer, p proxy.Proxy) (*VLess, error) {
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -31,43 +48,23 @@ func NewVLess(s string, d proxy.Dialer) (*VLess, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p := &VLess{
|
||||
v := &VLess{
|
||||
dialer: d,
|
||||
proxy: p,
|
||||
addr: addr,
|
||||
uuid: uuid,
|
||||
}
|
||||
|
||||
return p, nil
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// NewVLessDialer returns a vless proxy dialer.
|
||||
func NewVLessDialer(s string, dialer proxy.Dialer) (proxy.Dialer, error) {
|
||||
return NewVLess(s, dialer)
|
||||
}
|
||||
|
||||
// Addr returns forwarder's address.
|
||||
func (s *VLess) Addr() string {
|
||||
if s.addr == "" {
|
||||
return s.dialer.Addr()
|
||||
// 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)
|
||||
}
|
||||
return s.addr
|
||||
}
|
||||
|
||||
// Dial connects to the address addr on the network net via the proxy.
|
||||
func (s *VLess) Dial(network, addr string) (net.Conn, error) {
|
||||
rc, err := s.dialer.Dial("tcp", s.addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ClientConn(rc, s.uuid, network, addr)
|
||||
}
|
||||
|
||||
// DialUDP connects to the given address via the proxy.
|
||||
func (s *VLess) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
|
||||
c, err := s.Dial("udp", addr)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
pkc := NewPktConn(c)
|
||||
return pkc, nil, nil
|
||||
_, err = hex.Decode(uuid[:], b)
|
||||
return
|
||||
}
|
||||
Reference in new issue
Block a user