diff --git a/internal/dnsproxy/proxy.go b/internal/dnsproxy/proxy.go index f7ff9e2e..d8131918 100644 --- a/internal/dnsproxy/proxy.go +++ b/internal/dnsproxy/proxy.go @@ -1,7 +1,6 @@ package dnsproxy import ( - "fmt" "net" "runtime/debug" "time" @@ -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 } diff --git a/internal/dnsproxy/proxy_test.go b/internal/dnsproxy/proxy_test.go index 40fab589..b58dc765 100644 --- a/internal/dnsproxy/proxy_test.go +++ b/internal/dnsproxy/proxy_test.go @@ -5,6 +5,9 @@ import ( "net" "testing" "time" + + "github.com/miekg/dns" + "github.com/patrickmn/go-cache" ) var ffmucUpstreams, _ = net.LookupHost("dns.ffmuc.net") @@ -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") + } + }) +} diff --git a/internal/services/device_service.go b/internal/services/device_service.go index deb6e06c..d7a8198a 100644 --- a/internal/services/device_service.go +++ b/internal/services/device_service.go @@ -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 @@ -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 @@ -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{