mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
proxy: nat fullcone support for tproxy, trojan, ss, socks5 (fix #253)
This commit is contained in:
1 parent
d68f361c35
commit
a98995e2cb
42 files changed
+366
-280
No files matched your search
+12
-3
@@ -39,10 +39,19 @@ func (s *VLess) dial(network, addr string) (net.Conn, error) {
|
||||
}
|
||||
|
||||
// DialUDP connects to the given address via the proxy.
|
||||
func (s *VLess) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
|
||||
func (s *VLess) DialUDP(network, addr string) (net.PacketConn, error) {
|
||||
c, err := s.dial("udp", addr)
|
||||
// TODO: check the addr in return value
|
||||
return NewPktConn(c), nil, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tgtAddr, err := net.ResolveUDPAddr("udp", addr)
|
||||
if err != nil {
|
||||
log.F("[vless] error in ResolveUDPAddr: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewPktConn(c, tgtAddr), err
|
||||
}
|
||||
|
||||
// ClientConn is a vless client connection.
|
||||
|
||||
+11
-7
@@ -10,31 +10,35 @@ import (
|
||||
)
|
||||
|
||||
// PktConn is a udp Packet.Conn.
|
||||
type PktConn struct{ net.Conn }
|
||||
type PktConn struct {
|
||||
net.Conn
|
||||
target *net.UDPAddr
|
||||
}
|
||||
|
||||
// NewPktConn returns a PktConn.
|
||||
func NewPktConn(c net.Conn) *PktConn { return &PktConn{Conn: c} }
|
||||
func NewPktConn(c net.Conn, target *net.UDPAddr) *PktConn {
|
||||
return &PktConn{Conn: c, target: target}
|
||||
}
|
||||
|
||||
// ReadFrom implements the necessary function of net.PacketConn.
|
||||
// TODO: we know that we use it in proxy.CopyUDP and the length of b is enough, check it later.
|
||||
func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
if len(b) < 2 {
|
||||
return 0, nil, errors.New("buf size is not enough")
|
||||
return 0, pc.target, errors.New("buf size is not enough")
|
||||
}
|
||||
|
||||
// Length
|
||||
if _, err := io.ReadFull(pc.Conn, b[:2]); err != nil {
|
||||
return 0, nil, err
|
||||
return 0, pc.target, err
|
||||
}
|
||||
length := int(binary.BigEndian.Uint16(b[:2]))
|
||||
|
||||
if len(b) < length {
|
||||
return 0, nil, errors.New("buf size is not enough")
|
||||
return 0, pc.target, errors.New("buf size is not enough")
|
||||
}
|
||||
|
||||
// Payload
|
||||
n, err := io.ReadFull(pc.Conn, b[:length])
|
||||
return n, nil, err
|
||||
return n, pc.target, err
|
||||
}
|
||||
|
||||
// WriteTo implements the necessary function of net.PacketConn.
|
||||
|
||||
@@ -162,10 +162,10 @@ func (s *VLess) ServeUoT(c net.Conn, tgt string) {
|
||||
return
|
||||
}
|
||||
|
||||
pc := NewPktConn(c)
|
||||
log.F("[vless] %s <-tcp-> %s - %s <-udp-> %s", c.RemoteAddr(), c.LocalAddr(), rc.LocalAddr(), tgt)
|
||||
pc := NewPktConn(c, tgtAddr)
|
||||
log.F("[vless] %s <-UoT-> %s <-> %s", c.RemoteAddr(), rc.LocalAddr(), tgt)
|
||||
|
||||
go proxy.CopyUDP(rc, tgtAddr, pc, 2*time.Minute, 5*time.Second)
|
||||
go proxy.CopyUDP(rc, nil, pc, 2*time.Minute, 5*time.Second)
|
||||
proxy.CopyUDP(pc, nil, rc, 2*time.Minute, 5*time.Second)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user