diff --git a/cmd/soundtouch-service/main.go b/cmd/soundtouch-service/main.go index e50cecd..de278f2 100644 --- a/cmd/soundtouch-service/main.go +++ b/cmd/soundtouch-service/main.go @@ -713,6 +713,7 @@ func setupRouter(server *handlers.Server) *chi.Mux { r.Delete("/interactions/sessions", server.HandleCleanupSessions) r.Get("/dns-discoveries", server.HandleGetDNSDiscoveries) + r.Get("/dns-discoveries/download", server.HandleDownloadDNSDiscoveries) r.Delete("/dns-discoveries", server.HandleClearDNSDiscoveries) r.Get("/devices/{deviceId}/events", server.HandleGetDeviceEvents) diff --git a/pkg/service/handlers/handlers_setup.go b/pkg/service/handlers/handlers_setup.go index 8cdd7c3..866f177 100644 --- a/pkg/service/handlers/handlers_setup.go +++ b/pkg/service/handlers/handlers_setup.go @@ -419,6 +419,32 @@ func (s *Server) HandleRevertMigration(w http.ResponseWriter, r *http.Request) { // HandleGetDNSDiscoveries returns recorded DNS discoveries. func (s *Server) HandleGetDNSDiscoveries(w http.ResponseWriter, _ *http.Request) { + result := s.getMergedDNSDiscoveries() + + w.Header().Set("Content-Type", "application/json") + + if err := json.NewEncoder(w).Encode(result); err != nil { + http.Error(w, "Failed to encode response", http.StatusInternalServerError) + return + } +} + +// HandleDownloadDNSDiscoveries returns recorded DNS discoveries as a downloadable JSON file. +func (s *Server) HandleDownloadDNSDiscoveries(w http.ResponseWriter, _ *http.Request) { + result := s.getMergedDNSDiscoveries() + + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Disposition", "attachment; filename=\"dns-discoveries.json\"") + + encoder := json.NewEncoder(w) + encoder.SetIndent("", " ") + + if err := encoder.Encode(result); err != nil { + log.Printf("Error encoding DNS discoveries for download: %v", err) + } +} + +func (s *Server) getMergedDNSDiscoveries() []datastore.DNSDiscoveryEntry { // 1. Get current in-memory discoveries inMemory := s.GetDNSDiscovery() @@ -469,12 +495,7 @@ func (s *Server) HandleGetDNSDiscoveries(w http.ResponseWriter, _ *http.Request) log.Printf("Warning: Failed to persist merged DNS discoveries: %v", err) } - w.Header().Set("Content-Type", "application/json") - - if err := json.NewEncoder(w).Encode(result); err != nil { - http.Error(w, "Failed to encode response", http.StatusInternalServerError) - return - } + return result } // HandleClearDNSDiscoveries clears recorded DNS discoveries. diff --git a/pkg/service/handlers/web/index.html b/pkg/service/handlers/web/index.html index 4c43ffc..869fff1 100644 --- a/pkg/service/handlers/web/index.html +++ b/pkg/service/handlers/web/index.html @@ -406,7 +406,10 @@

DNS Discoveries

- +
+ + +

Hosts discovered via the AfterTouch DNS server. "Self" means the domain was intercepted and redirected to this service.

diff --git a/pkg/service/handlers/web/js/script.js b/pkg/service/handlers/web/js/script.js index 6a58861..62bd921 100644 --- a/pkg/service/handlers/web/js/script.js +++ b/pkg/service/handlers/web/js/script.js @@ -585,6 +585,10 @@ async function clearDNSDiscoveries() { } } +function downloadDNSDiscoveries() { + window.location.href = '/setup/dns-discoveries/download'; +} + async function showDeviceEvents() { const overlay = document.getElementById('device-events-overlay'); overlay.style.display = 'block';