Skip to content

Commit

Permalink
proxy: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Jun 21, 2024
1 parent 90744ad commit 12d62a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
25 changes: 24 additions & 1 deletion proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package proxy
import (
"context"
"fmt"
"io"
"log/slog"
"net"
"time"
Expand Down Expand Up @@ -221,7 +222,7 @@ func (p *Proxy) respond(d *DNSContext) {
}

if err != nil {
logWithNonCrit(err, fmt.Sprintf("responding %s request", d.Proto), p.logger)
logWithNonCrit(err, "responding request", d.Proto, p.logger)
}
}

Expand Down Expand Up @@ -249,3 +250,25 @@ func (p *Proxy) logDNSMessage(m *dns.Msg) {
p.logger.Debug("in", "msg", m)
}
}

// logWithNonCrit logs the error on the appropriate level depending on whether
// err is a critical error or not.
func logWithNonCrit(err error, msg string, proto Proto, l *slog.Logger) {
if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) || isEPIPE(err) {
l.Debug(
"connection is closed",
"proto", proto,
"details", msg,
slogutil.KeyError, err,
)
} else if netErr := net.Error(nil); errors.As(err, &netErr) && netErr.Timeout() {
l.Debug(
"connection timed out",
"proto", proto,
"details", msg,
slogutil.KeyError, err,
)
} else {
l.Error(msg, "proto", proto, slogutil.KeyError, err)
}
}
21 changes: 4 additions & 17 deletions proxy/server_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/binary"
"fmt"
"io"
"log/slog"
"net"
"time"

Expand Down Expand Up @@ -98,7 +97,7 @@ func (p *Proxy) handleTCPConnection(conn net.Conn, proto Proto, reqSema syncutil
defer func() {
err := conn.Close()
if err != nil {
logWithNonCrit(err, "closing tcp conn", p.logger)
logWithNonCrit(err, "closing conn", ProtoTCP, p.logger)
}
}()

Expand All @@ -108,7 +107,7 @@ func (p *Proxy) handleTCPConnection(conn net.Conn, proto Proto, reqSema syncutil
err := conn.SetDeadline(time.Now().Add(defaultTimeout))
if err != nil {
// Consider deadline errors non-critical.
logWithNonCrit(err, "setting tcp deadline", p.logger)
logWithNonCrit(err, "setting deadline", ProtoTCP, p.logger)
}

req := p.readDNSReq(conn)
Expand All @@ -121,7 +120,7 @@ func (p *Proxy) handleTCPConnection(conn net.Conn, proto Proto, reqSema syncutil

err = p.handleDNSRequest(d)
if err != nil {
logWithNonCrit(err, fmt.Sprintf("handling %s request", d.Proto), p.logger)
logWithNonCrit(err, "handling request", ProtoTCP, p.logger)
}
}
}
Expand All @@ -131,7 +130,7 @@ func (p *Proxy) handleTCPConnection(conn net.Conn, proto Proto, reqSema syncutil
func (p *Proxy) readDNSReq(conn net.Conn) (req *dns.Msg) {
packet, err := readPrefixed(conn)
if err != nil {
logWithNonCrit(err, "handling tcp: reading msg", p.logger)
logWithNonCrit(err, "reading msg", ProtoTCP, p.logger)

return nil
}
Expand Down Expand Up @@ -173,18 +172,6 @@ func readPrefixed(conn net.Conn) (b []byte, err error) {
return b, nil
}

// logWithNonCrit logs the error on the appropriate level depending on whether
// err is a critical error or not.
func logWithNonCrit(err error, msg string, l *slog.Logger) {
if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) || isEPIPE(err) {
l.Debug("connection is closed", "details", msg, slogutil.KeyError, err)
} else if netErr := net.Error(nil); errors.As(err, &netErr) && netErr.Timeout() {
l.Debug("connection timed out", "details", msg, slogutil.KeyError, err)
} else {
l.Error(msg, slogutil.KeyError, err)
}
}

// Writes a response to the TCP (or TLS) client
func (p *Proxy) respondTCP(d *DNSContext) error {
resp := d.Res
Expand Down

0 comments on commit 12d62a0

Please sign in to comment.