Skip to content

Commit

Permalink
fix: linter
Browse files Browse the repository at this point in the history
  • Loading branch information
GoliathLabs committed Nov 30, 2024
1 parent d190874 commit cf82a9d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
5 changes: 2 additions & 3 deletions internal/dnsproxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dnsproxy

import (
"fmt"
"net"
"runtime/debug"
"time"
Expand Down Expand Up @@ -80,14 +79,14 @@ func (d *DNSProxy) Lookup(m *dns.Msg) (*dns.Msg, error) {
target := net.JoinHostPort(upstream, "53")
resp, _, err := d.udpClient.Exchange(m, target)
if err != nil && firstErr == nil {
logrus.Warnf(errors.Wrap(err, fmt.Sprintf("DNS lookup failed for upstream %s", upstream)).Error())
logrus.Warnf("DNS lookup failed for upstream %s: %v", upstream, err)
firstErr = err
} else if err == nil {
// Retry truncated responses over TCP
if resp.Truncated {
resp, _, err = d.tcpClient.Exchange(m, target)
if err != nil && firstErr == nil {
logrus.Warnf(errors.Wrap(err, fmt.Sprintf("DNS lookup failed over TCP for upstream %s", upstream)).Error())
logrus.Warnf("DNS lookup failed over TCP for upstream %s: %v", upstream, err)
firstErr = err
continue
}
Expand Down
39 changes: 39 additions & 0 deletions internal/dnsproxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"net"
"testing"
"time"

"github.com/miekg/dns"
"github.com/patrickmn/go-cache"
)

var ffmucUpstreams, _ = net.LookupHost("dns.ffmuc.net")
Expand Down Expand Up @@ -56,3 +59,39 @@ func TestDNSProxy_ServeDNS(t *testing.T) {
}
})
}

func TestDNSProxy_Lookup(t *testing.T) {
proxy := &DNSProxy{
udpClient: &dns.Client{Net: "udp"},
tcpClient: &dns.Client{Net: "tcp"},
cache: cache.New(5*time.Minute, 10*time.Minute),
upstream: ffmucUpstreams,
}

t.Run("Cache hit", func(t *testing.T) {
msg := new(dns.Msg)
msg.SetQuestion("example.com.", dns.TypeA)
proxy.cache.Set(makekey(msg), msg, cache.DefaultExpiration)

resp, err := proxy.Lookup(msg)
if err != nil {
t.Fatal(err)
}
if resp == nil {
t.Fatal("expected response, got nil")
}
})

t.Run("Cache miss", func(t *testing.T) {
msg := new(dns.Msg)
msg.SetQuestion("example.com.", dns.TypeA)

resp, err := proxy.Lookup(msg)
if err == nil {
t.Fatal("expected error, got nil")
}
if resp != nil {
t.Fatal("expected nil response, got non-nil")
}
})
}
6 changes: 3 additions & 3 deletions internal/services/device_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (d *DeviceService) AddDevice(ctx context.Context, req *proto.AddDeviceReq)
device, err := d.DeviceManager.AddDevice(user, req.GetName(), req.GetPublicKey(), req.GetPresharedKey())
if err != nil {
ctxlogrus.Extract(ctx).Error(err)
return nil, status.Errorf(codes.Internal, err.Error())
return nil, status.Errorf(codes.Internal, "%v", err)
}

return mapDevice(device), nil
Expand Down Expand Up @@ -68,7 +68,7 @@ func (d *DeviceService) DeleteDevice(ctx context.Context, req *proto.DeleteDevic

if err := d.DeviceManager.DeleteDevice(deviceOwner, req.GetName()); err != nil {
ctxlogrus.Extract(ctx).Error(err)
return nil, status.Errorf(codes.Internal, "failed to delete device")
return nil, status.Errorf(codes.Internal, "failed to delete device: %v", err)
}

return &emptypb.Empty{}, nil
Expand All @@ -87,7 +87,7 @@ func (d *DeviceService) ListAllDevices(ctx context.Context, req *proto.ListAllDe
devices, err := d.DeviceManager.ListAllDevices()
if err != nil {
ctxlogrus.Extract(ctx).Error(err)
return nil, status.Errorf(codes.Internal, "Failed to retrieve devices")
return nil, status.Errorf(codes.Internal, "failed to retrieve devices: %v", err)
}

return &proto.ListAllDevicesRes{
Expand Down

0 comments on commit cf82a9d

Please sign in to comment.