Skip to content

Commit

Permalink
Pull request: proxy: imp err handling
Browse files Browse the repository at this point in the history
Updates AdguardTeam/AdGuardHome#4128.

Squashed commit of the following:

commit f3f7044
Author: Ainar Garipov <[email protected]>
Date:   Thu Jan 20 16:49:59 2022 +0300

    proxy: imp err handling
  • Loading branch information
ainar-g committed Jan 20, 2022
1 parent 86e160e commit d094232
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions proxy/server_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@ import (
"github.com/miekg/dns"
)

func (p *Proxy) createTCPListeners() error {
func (p *Proxy) createTCPListeners() (err error) {
for _, a := range p.TCPListenAddr {
log.Printf("Creating a TCP server socket")
tcpListen, err := net.ListenTCP("tcp", a)

var tcpListen *net.TCPListener
tcpListen, err = net.ListenTCP("tcp", a)
if err != nil {
return fmt.Errorf("starting listening on tcp socket: %w", err)
}

p.tcpListen = append(p.tcpListen, tcpListen)
log.Printf("Listening to tcp://%s", tcpListen.Addr())
}

return nil
}

func (p *Proxy) createTLSListeners() error {
func (p *Proxy) createTLSListeners() (err error) {
for _, a := range p.TLSListenAddr {
log.Printf("Creating a TLS server socket")
tcpListen, err := net.ListenTCP("tcp", a)

var tcpListen *net.TCPListener
tcpListen, err = net.ListenTCP("tcp", a)
if err != nil {
return fmt.Errorf("starting tls listener: %w", err)
}
Expand All @@ -39,6 +44,7 @@ func (p *Proxy) createTLSListeners() error {
p.tlsListen = append(p.tlsListen, l)
log.Printf("Listening to tls://%s", l.Addr())
}

return nil
}

Expand Down Expand Up @@ -78,7 +84,7 @@ func (p *Proxy) handleTCPConnection(conn net.Conn, proto Proto) {
defer func() {
err := conn.Close()
if err != nil {
logWithClosed(err, "handling tcp: closing conn")
logWithNonCrit(err, "handling tcp: closing conn")
}
}()

Expand All @@ -92,12 +98,12 @@ func (p *Proxy) handleTCPConnection(conn net.Conn, proto Proto) {
err := conn.SetDeadline(time.Now().Add(defaultTimeout))
if err != nil {
// Consider deadline errors non-critical.
logWithClosed(err, "handling tcp: setting deadline")
logWithNonCrit(err, "handling tcp: setting deadline")
}

packet, err := proxyutil.ReadPrefixed(conn)
if err != nil {
logWithClosed(err, "handling tcp: reading msg")
logWithNonCrit(err, "handling tcp: reading msg")

break
}
Expand All @@ -121,11 +127,13 @@ func (p *Proxy) handleTCPConnection(conn net.Conn, proto Proto) {
}
}

// logWithClosed logs the error on the appropriate level depending on whether
// err is an error about a closed connection.
func logWithClosed(err error, msg string) {
// logWithNonCrit logs the error on the appropriate level depending on whether
// err is a critical error or not.
func logWithNonCrit(err error, msg string) {
if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) {
log.Debug("%s: connection is closed; original error: %s", msg, err)
} else if netErr := net.Error(nil); errors.As(err, &netErr) && netErr.Timeout() {
log.Debug("%s: connection timed out; original error: %s", msg, err)
} else {
log.Error("%s: %s", msg, err)
}
Expand Down

0 comments on commit d094232

Please sign in to comment.