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

protocol: fixed deadline datarace #86 #87

Merged
merged 2 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
run: go vet

- name: Test
run: go test -v -covermode=count -coverprofile=coverage.out
run: go test -race -v -covermode=atomic -coverprofile=coverage.out

- name: actions-goveralls
uses: shogo82148/actions-goveralls@v1.2.2
Expand Down
21 changes: 13 additions & 8 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net"
"sync"
"sync/atomic"
"time"
)

Expand Down Expand Up @@ -33,15 +34,15 @@ type Listener struct {
// return the address of the client instead of the proxy address. Each connection
// will have its own readHeaderTimeout and readDeadline set by the Accept() call.
type Conn struct {
bufReader *bufio.Reader
readDeadline atomic.Value // time.Time
once sync.Once
readErr error
conn net.Conn
Validate Validator
bufReader *bufio.Reader
header *Header
once sync.Once
ProxyHeaderPolicy Policy
Validate Validator
readErr error
readHeaderTimeout time.Duration
readDeadline time.Time
}

// Validator receives a header and decides whether it is a valid one
Expand Down Expand Up @@ -215,7 +216,7 @@ func (p *Conn) UDPConn() (conn *net.UDPConn, ok bool) {

// SetDeadline wraps original conn.SetDeadline
func (p *Conn) SetDeadline(t time.Time) error {
p.readDeadline = t
p.readDeadline.Store(t)
return p.conn.SetDeadline(t)
}

Expand All @@ -224,7 +225,7 @@ func (p *Conn) SetReadDeadline(t time.Time) error {
// Set a local var that tells us the desired deadline. This is
// needed in order to reset the read deadline to the one that is
// desired by the user, rather than an empty deadline.
p.readDeadline = t
p.readDeadline.Store(t)
return p.conn.SetReadDeadline(t)
}

Expand All @@ -250,7 +251,11 @@ func (p *Conn) readHeader() error {
// Therefore, we check whether the error is a net.Timeout and if it is, we decide
// the proxy proto does not exist and set the error accordingly.
if p.readHeaderTimeout > 0 {
p.conn.SetReadDeadline(p.readDeadline)
t := p.readDeadline.Load()
if t == nil {
t = time.Time{}
}
p.conn.SetReadDeadline(t.(time.Time))
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
err = ErrNoProxyProtocol
}
Expand Down