Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: relay udp forward #664

Merged
merged 1 commit into from
Nov 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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