mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
vless: added udp support
This commit is contained in:
1 parent
bc68535dcd
commit
b323a62ce6
10 files changed
+97
-34
No files matched your search
@@ -27,8 +27,8 @@ type Conn struct {
|
||||
rcved bool
|
||||
}
|
||||
|
||||
// NewConn returns a new vless client conn.
|
||||
func NewConn(c net.Conn, uuid [16]byte, target string) (*Conn, error) {
|
||||
// ClientConn returns a new vless client conn.
|
||||
func ClientConn(c net.Conn, uuid [16]byte, network, target string) (*Conn, error) {
|
||||
atyp, addr, port, err := ParseAddr(target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -40,7 +40,12 @@ func NewConn(c net.Conn, uuid [16]byte, target string) (*Conn, error) {
|
||||
buf.WriteByte(Version) // ver
|
||||
buf.Write(uuid[:]) // uuid
|
||||
buf.WriteByte(0) // addLen
|
||||
buf.WriteByte(CmdTCP) // cmd
|
||||
|
||||
cmd := CmdTCP
|
||||
if network == "udp" {
|
||||
cmd = CmdUDP
|
||||
}
|
||||
buf.WriteByte(cmd) // cmd
|
||||
|
||||
// target
|
||||
err = binary.Write(buf, binary.BigEndian, uint16(port)) // port
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package vless
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/nadoo/glider/pool"
|
||||
)
|
||||
|
||||
// PktConn .
|
||||
type PktConn struct {
|
||||
net.Conn
|
||||
}
|
||||
|
||||
// NewPktConn returns a PktConn.
|
||||
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.
|
||||
func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
// Length
|
||||
if _, err := io.ReadFull(pc.Conn, b[:2]); err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
length := int(binary.BigEndian.Uint16(b[:2]))
|
||||
|
||||
if len(b) < length {
|
||||
return 0, nil, errors.New("buf size is not enough")
|
||||
}
|
||||
|
||||
// Payload
|
||||
n, err := io.ReadFull(pc.Conn, b[:length])
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
return n, nil, nil
|
||||
}
|
||||
|
||||
// WriteTo implements the necessary function of net.PacketConn.
|
||||
func (pc *PktConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||
buf := pool.GetWriteBuffer()
|
||||
defer pool.PutWriteBuffer(buf)
|
||||
|
||||
binary.Write(buf, binary.BigEndian, uint16(len(b)))
|
||||
buf.Write(b)
|
||||
|
||||
return pc.Write(buf.Bytes())
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package vless
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/url"
|
||||
|
||||
@@ -60,10 +59,15 @@ func (s *VLess) Dial(network, addr string) (net.Conn, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewConn(rc, s.uuid, addr)
|
||||
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) {
|
||||
return nil, nil, errors.New("vless client does not support udp now")
|
||||
c, err := s.Dial("udp", addr)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
pkc := NewPktConn(c)
|
||||
return pkc, nil, nil
|
||||
}
|
||||
Reference in new issue
Block a user