Skip to content

Commit

Permalink
Fix: relay udp forward
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang.Liu authored and ginuerzh committed Nov 18, 2020
1 parent 1c32df3 commit 3ea5708
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,11 @@ func (h *relayHandler) Handle(conn net.Conn) {

type relayConn struct {
net.Conn
isServer bool
udp bool
wbuf bytes.Buffer
once sync.Once
isServer bool
udp bool
wbuf bytes.Buffer
once sync.Once
headerSent bool
}

func (c *relayConn) Read(b []byte) (n int, err error) {
Expand Down Expand Up @@ -323,6 +324,7 @@ func (c *relayConn) Write(b []byte) (n int, err error) {
var bb [2]byte
binary.BigEndian.PutUint16(bb[:2], uint16(len(b)))
c.wbuf.Write(bb[:])
c.headerSent = true
}
c.wbuf.Write(b) // append the data to the cached header
// _, err = c.Conn.Write(c.wbuf.Bytes())
Expand All @@ -334,7 +336,13 @@ func (c *relayConn) Write(b []byte) (n int, err error) {
if !c.udp {
return c.Conn.Write(b)
}

if !c.headerSent {
c.headerSent = true
b2 := make([]byte, len(b)+2)
copy(b2, b)
_, err = c.Conn.Write(b2)
return
}
nsize := 2 + len(b)
var buf []byte
if nsize <= mediumBufferSize {
Expand Down

0 comments on commit 3ea5708

Please sign in to comment.