Skip to content

Commit

Permalink
Refinement: minor fixes (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
cty authored Jun 1, 2021
1 parent 15817b5 commit 7f8d638
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions tunnel/trojan/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,27 @@ func (c *PacketConn) ReadWithMetadata(payload []byte) (int, *tunnel.Metadata, er
return 0, nil, common.NewError("failed to parse udp packet addr").Base(err)
}
lengthBuf := [2]byte{}
_, err := io.ReadFull(c.Conn, lengthBuf[:])
if err != nil {
if _, err := io.ReadFull(c.Conn, lengthBuf[:]); err != nil {
return 0, nil, common.NewError("failed to read length")
}
length := int(binary.BigEndian.Uint16(lengthBuf[:]))

crlf := [2]byte{}
io.ReadFull(c.Conn, crlf[:])
if err != nil {
if _, err := io.ReadFull(c.Conn, crlf[:]); err != nil {
return 0, nil, common.NewError("failed to read crlf")
}

if len(payload) < length || length > MaxPacketSize {
io.CopyN(ioutil.Discard, c.Conn, int64(length)) // drain the rest of the packet
return 0, nil, common.NewError("incoming packet size is too large")
}
_, err = io.ReadFull(c.Conn, payload[:length])
if err != nil {

if _, err := io.ReadFull(c.Conn, payload[:length]); err != nil {
return 0, nil, common.NewError("failed to read payload")
}

log.Debug("udp packet from", c.RemoteAddr(), "metadata", addr.String(), "size", length)
return length, &tunnel.Metadata{
Address: addr,
}, err
}, nil
}

0 comments on commit 7f8d638

Please sign in to comment.