Skip to content

Commit

Permalink
Adjust matchers after #208: return reading errors (#236)
Browse files Browse the repository at this point in the history
* Adjust matchers after #208: return reading errors

* Adjust matchers after #208: modify error handling in tests
  • Loading branch information
vnxme authored Aug 23, 2024
1 parent 22a0393 commit 3d22d6d
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions modules/l4dns/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (m *MatchDNS) Match(cx *layer4.Connection) (bool, error) {
msgBuf = make([]byte, msgBytes)
_, err = io.ReadFull(cx, msgBuf)
if err != nil {
return false, nil
return false, err
}

// Validate the remaining connection buffer
Expand All @@ -101,7 +101,7 @@ func (m *MatchDNS) Match(cx *layer4.Connection) (bool, error) {
msgBuf = make([]byte, dnsHeaderBytes)
n, err := io.ReadAtLeast(cx, msgBuf, int(dnsHeaderBytes))
if err != nil {
return false, nil
return false, err
}

// Read the remaining bytes and validate their length
Expand Down
3 changes: 2 additions & 1 deletion modules/l4dns/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package l4dns

import (
"context"
"errors"
"io"
"net"
"testing"
Expand All @@ -28,7 +29,7 @@ import (

func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
t.Fatalf("Unexpected error: %s\n", err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions modules/l4rdp/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (m *MatchRDP) Match(cx *layer4.Connection) (bool, error) {
headerBuf := make([]byte, RDPConnReqBytesMin)
n, err := io.ReadFull(cx, headerBuf)
if err != nil || n < int(RDPConnReqBytesMin) {
return false, nil
return false, err
}

// Parse TPKTHeader
Expand Down Expand Up @@ -112,7 +112,7 @@ func (m *MatchRDP) Match(cx *layer4.Connection) (bool, error) {
payloadBuf := make([]byte, payloadBytesTotal)
n, err = io.ReadFull(cx, payloadBuf)
if err != nil || n < int(payloadBytesTotal) {
return false, nil
return false, err
}

// Validate the remaining connection buffer
Expand All @@ -121,7 +121,7 @@ func (m *MatchRDP) Match(cx *layer4.Connection) (bool, error) {
extraBuf := make([]byte, 1)
n, err = io.ReadFull(cx, extraBuf)
if err == nil && n == len(extraBuf) {
return false, nil
return false, err
}

// Find CRLF which divides token/cookie from RDPNegReq and RDPCorrInfo
Expand Down
3 changes: 2 additions & 1 deletion modules/l4rdp/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package l4rdp
import (
"bytes"
"context"
"errors"
"io"
"net"
"testing"
Expand All @@ -29,7 +30,7 @@ import (

func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
t.Fatalf("Unexpected error: %s\n", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/l4regexp/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (m *MatchRegexp) Match(cx *layer4.Connection) (bool, error) {
buf := make([]byte, m.Count)
n, err := io.ReadFull(cx, buf)
if err != nil || n < int(m.Count) {
return false, nil
return false, err
}

// Match these bytes against the regular expression
Expand Down
6 changes: 4 additions & 2 deletions modules/l4regexp/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ package l4regexp

import (
"context"
"errors"
"io"
"net"
"testing"

"github.com/caddyserver/caddy/v2"
"github.com/mholt/caddy-l4/layer4"
"go.uber.org/zap"

"github.com/mholt/caddy-l4/layer4"
)

func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
t.Fatalf("Unexpected error: %s\n", err)
}
}
Expand Down

0 comments on commit 3d22d6d

Please sign in to comment.