Skip to content

Commit

Permalink
Use buffered number to calculate header length
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderYastrebov committed Oct 15, 2024
1 parent c73338a commit 8d833b8
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 6 deletions.
1 change: 0 additions & 1 deletion header.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ type Header struct {
SourceAddr net.Addr
DestinationAddr net.Addr
rawTLVs []byte
length int
}

// HeaderProxyFromAddrs creates a new PROXY header from a source and a
Expand Down
5 changes: 2 additions & 3 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,12 @@ func (p *Conn) readHeader() error {
const bufSize = 256

bb := bytes.NewBuffer(make([]byte, 0, bufSize))
tr := io.TeeReader(p.conn, bb)
br := bufio.NewReaderSize(tr, bufSize)
br := bufio.NewReaderSize(io.TeeReader(p.conn, bb), bufSize)

header, err := Read(br)

if err == nil {
_, err = io.CopyN(io.Discard, bb, int64(header.length))
_, err = io.CopyN(io.Discard, bb, int64(bb.Len()-br.Buffered()))
}

if bb.Len() == 0 {
Expand Down
1 change: 0 additions & 1 deletion v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ func parseVersion1(reader *bufio.Reader) (*Header, error) {
// Command doesn't exist in v1 but set it for other parts of this library
// to rely on it for determining connection details.
header := initVersion1()
header.length = len(buf)

// Transport protocol has been processed already.
header.TransportProtocol = transportProtocol
Expand Down
1 change: 0 additions & 1 deletion v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func parseVersion2(reader *bufio.Reader) (header *Header, err error) {
if !header.validateLength(length) {
return nil, ErrInvalidLength
}
header.length = 16 + int(length)

// Return early if the length is zero, which means that
// there's no address information and TLVs present for UNSPEC.
Expand Down

0 comments on commit 8d833b8

Please sign in to comment.