Skip to content

Commit

Permalink
proxy: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed May 23, 2024
1 parent 15645eb commit c976bf6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
8 changes: 8 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ func (p *Proxy) validateBasicAuth() (err error) {
return nil
}

// Returns true if proxy is started. It is safe for concurrent use.
func (p *Proxy) isStarted() (ok bool) {
p.RLock()
defer p.RUnlock()

return p.started
}

// type check
var _ service.Interface = (*Proxy)(nil)

Expand Down
4 changes: 1 addition & 3 deletions proxy/server_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,9 @@ func (p *Proxy) handleTCPConnection(conn net.Conn, proto Proto) {
}()

for {
p.RLock()
if !p.started {
if !p.isStarted() {
return
}
p.RUnlock()

err := conn.SetDeadline(time.Now().Add(defaultTimeout))
if err != nil {
Expand Down
27 changes: 16 additions & 11 deletions proxy/server_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,16 @@ func (p *Proxy) udpPacketLoop(conn *net.UDPConn, reqSema syncutil.Semaphore) {

b := make([]byte, dns.MaxMsgSize)
for {
p.RLock()
if !p.started {
if !p.isStarted() {
return
}
p.RUnlock()

n, localIP, remoteAddr, err := proxynetutil.UDPRead(conn, b, p.udpOOBSize)
// documentation says to handle the packet even if err occurs, so do that first
// The documentation says to handle the packet even if err occurs.
if n > 0 {
// make a copy of all bytes because ReadFrom() will overwrite contents of b on next call
// we need the contents to survive the call because we're handling them in goroutine
// Make a copy of all bytes because ReadFrom() will overwrite the
// contents of b on the next call. We need that contents to sustain
// the call because we're handling them in goroutines.
packet := make([]byte, n)
copy(packet, b)

Expand All @@ -94,18 +93,24 @@ func (p *Proxy) udpPacketLoop(conn *net.UDPConn, reqSema syncutil.Semaphore) {
p.udpHandlePacket(packet, localIP, remoteAddr, conn)
}()
}

if err != nil {
if errors.Is(err, net.ErrClosed) {
log.Debug("dnsproxy: udp connection %s closed", conn.LocalAddr())
} else {
log.Error("dnsproxy: reading from udp: %s", err)
}
logError(err, conn)

break
}
}
}

// logError writes suitable log message for given err.
func logError(err error, conn *net.UDPConn) {
if errors.Is(err, net.ErrClosed) {
log.Debug("dnsproxy: udp connection %s closed", conn.LocalAddr())
} else {
log.Error("dnsproxy: reading from udp: %s", err)
}
}

// udpHandlePacket processes the incoming UDP packet and sends a DNS response
func (p *Proxy) udpHandlePacket(
packet []byte,
Expand Down
2 changes: 1 addition & 1 deletion scripts/make/go-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ run_linter gocognit --over 10\
;

run_linter gocognit --over 35 ./main.go
run_linter gocognit --over 14 ./proxy/
run_linter gocognit --over 13 ./proxy/

run_linter ineffassign ./...

Expand Down

0 comments on commit c976bf6

Please sign in to comment.