mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
udp: remove uottun and optimize udp implementation
This commit is contained in:
1 parent
78e03d7fbf
commit
d510ea45b0
19 files changed
+83
-249
No files matched your search
+14
-9
@@ -7,7 +7,6 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/nadoo/glider/pool"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
"github.com/nadoo/glider/proxy/socks"
|
||||
)
|
||||
|
||||
@@ -36,14 +35,19 @@ func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
if len(b) < 2 {
|
||||
return 0, nil, errors.New("buf size is not enough")
|
||||
}
|
||||
|
||||
// Length
|
||||
if _, err = io.ReadFull(pc.Conn, b[:2]); err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
length := int(binary.BigEndian.Uint16(b[:2]))
|
||||
if length > proxy.UDPBufSize {
|
||||
return 0, nil, errors.New("packet invalid")
|
||||
|
||||
if len(b) < length {
|
||||
return 0, nil, errors.New("buf size is not enough")
|
||||
}
|
||||
|
||||
// CRLF
|
||||
@@ -51,10 +55,6 @@ func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -70,10 +70,15 @@ func (pc *PktConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||
buf := pool.GetWriteBuffer()
|
||||
defer pool.PutWriteBuffer(buf)
|
||||
|
||||
buf.Write(pc.tgtAddr)
|
||||
tgtLen, _ := buf.Write(pc.tgtAddr)
|
||||
binary.Write(buf, binary.BigEndian, uint16(len(b)))
|
||||
buf.WriteString("\r\n")
|
||||
buf.Write(b)
|
||||
|
||||
return pc.Write(buf.Bytes())
|
||||
n, err := pc.Write(buf.Bytes())
|
||||
if n > tgtLen+4 {
|
||||
return n - tgtLen - 4, nil
|
||||
}
|
||||
|
||||
return 0, err
|
||||
}
|
||||
Reference in new issue
Block a user