Skip to content

Commit

Permalink
Pull request: all: fmt, imp logs
Browse files Browse the repository at this point in the history
Updates AdguardTeam/AdGuardHome#4065.

Squashed commit of the following:

commit 89b65c9
Author: Ainar Garipov <[email protected]>
Date:   Mon Jan 17 18:40:50 2022 +0300

    proxy: imp code

commit 344c528
Author: Ainar Garipov <[email protected]>
Date:   Mon Jan 17 18:20:53 2022 +0300

    all: fmt, imp logs
  • Loading branch information
ainar-g committed Jan 17, 2022
1 parent 3df4133 commit 86e160e
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func run(options *Options) {
log.SetLevel(log.DEBUG)
}
if options.LogOutput != "" {
file, err := os.OpenFile(options.LogOutput, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
file, err := os.OpenFile(options.LogOutput, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
log.Fatalf("cannot create a log file: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion proxy/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func minTTL(h *dns.RR_Header, ttl uint32) uint32 {

// Updates a given TTL to fall within the range specified by the cacheMinTTL and
// cacheMaxTTL settings.
func respectTTLOverrides(ttl uint32, cacheMinTTL uint32, cacheMaxTTL uint32) uint32 {
func respectTTLOverrides(ttl, cacheMinTTL, cacheMaxTTL uint32) uint32 {
if ttl < cacheMinTTL {
return cacheMinTTL
}
Expand Down
2 changes: 1 addition & 1 deletion proxy/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ type testCase struct {

// requireEqualMsgs asserts the messages are equal except their ID, Rdlength, and
// the case of questions.
func requireEqualMsgs(t *testing.T, expected *dns.Msg, actual *dns.Msg) {
func requireEqualMsgs(t *testing.T, expected, actual *dns.Msg) {
t.Helper()

temp := *expected
Expand Down
2 changes: 1 addition & 1 deletion proxy/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func genSOA(request *dns.Msg, retry uint32) []dns.RR {
}

// parseECS parses the EDNS client subnet option from m.
func parseECS(m *dns.Msg) (addr net.IP, mask uint8, scope uint8) {
func parseECS(m *dns.Msg) (addr net.IP, mask, scope uint8) {
opt := m.IsEdns0()
if opt == nil {
return nil, 0, 0
Expand Down
3 changes: 2 additions & 1 deletion proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ func TestProxy_Resolve_dnssecCache(t *testing.T) {

txtDataChunkNum := txtDataLen / txtDataChunkLen
if txtDataLen%txtDataChunkLen > 0 {
txtDataChunkNum += 1
txtDataChunkNum++
}

txts := make([]string, txtDataChunkNum)
randData := make([]byte, txtDataLen)
n, err := rand.Read(randData)
Expand Down
24 changes: 15 additions & 9 deletions proxy/server_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ func (p *Proxy) tcpPacketLoop(l net.Listener, proto Proto, requestGoroutinesSema
}
}

// handleTCPConnection starts a loop that handles an incoming TCP connection
// proto is either "tcp" or "tls"
// handleTCPConnection starts a loop that handles an incoming TCP connection.
// proto must be either ProtoTCP or ProtoTLS.
func (p *Proxy) handleTCPConnection(conn net.Conn, proto Proto) {
defer log.OnPanic("proxy.handleTCPConnection")

log.Tracef("handling tcp: started handling %s request from %s", proto, conn.RemoteAddr())
defer func() {
err := conn.Close()
if err != nil {
log.Error("handling tcp: closing conn: %s", err)
logWithClosed(err, "handling tcp: closing conn")
}
}()

Expand All @@ -92,16 +92,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.
log.Error("handling tcp: setting deadline: %s", err)
logWithClosed(err, "handling tcp: setting deadline")
}

packet, err := proxyutil.ReadPrefixed(conn)
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) {
log.Debug("handling tcp: connection closed: %s", err)
} else {
log.Error("handling tcp: reading msg: %s", err)
}
logWithClosed(err, "handling tcp: reading msg")

break
}
Expand All @@ -125,6 +121,16 @@ 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) {
if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) {
log.Debug("%s: connection is closed; original error: %s", msg, err)
} else {
log.Error("%s: %s", msg, err)
}
}

// Writes a response to the TCP (or TLS) client
func (p *Proxy) respondTCP(d *DNSContext) error {
resp := d.Res
Expand Down
2 changes: 1 addition & 1 deletion proxyutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func SortIPAddrs(ipAddrs []net.IPAddr) []net.IPAddr {
return ipAddrs
}

func compareIPAddrs(left net.IPAddr, right net.IPAddr) int {
func compareIPAddrs(left, right net.IPAddr) int {
l4 := left.IP.To4()
r4 := right.IP.To4()
if l4 != nil && r4 == nil {
Expand Down
4 changes: 1 addition & 3 deletions proxyutil/udp_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ const (

// udpGetOOBSize obtains the destination IP from OOB data.
func udpGetOOBSize() (oobSize int) {
l4, l6 :=
len(ipv4.NewControlMessage(ipv4Flags)),
len(ipv6.NewControlMessage(ipv6Flags))
l4, l6 := len(ipv4.NewControlMessage(ipv4Flags)), len(ipv6.NewControlMessage(ipv6Flags))

if l4 >= l6 {
return l4
Expand Down

0 comments on commit 86e160e

Please sign in to comment.