fix(paths): change output paths

This commit is contained in:
2025-09-27 22:28:22 +01:00
parent 952f6e657b
commit 7f1b118832
4 changed files with 851 additions and 15 deletions

View File

@@ -5,6 +5,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
@@ -77,13 +78,19 @@ func (r *MeasurementRunner) runMeasurement(upstream string, domains []string, qT
// Setup output files
csvPath, pcapPath := GenerateOutputPaths(r.config.OutputDir, upstream, r.config.DNSSEC, r.config.KeepAlive)
// Create directory if it doesn't exist
if err := os.MkdirAll(filepath.Dir(csvPath), 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
keepAliveStr := ""
if r.config.KeepAlive {
keepAliveStr = " (keep-alive)"
}
fmt.Printf(">>> Measuring %s (dnssec=%v%s) → %s\n", upstream, r.config.DNSSEC, keepAliveStr,
strings.TrimSuffix(strings.TrimSuffix(csvPath, ".csv"), r.config.OutputDir+"/"))
// Show relative path for cleaner output
relPath, _ := filepath.Rel(r.config.OutputDir, csvPath)
fmt.Printf(">>> Measuring %s (dnssec=%v%s) → %s\n", upstream, r.config.DNSSEC, keepAliveStr, relPath)
// Setup packet capture
packetCapture, err := capture.NewPacketCapture(r.config.Interface, pcapPath)

View File

@@ -11,19 +11,110 @@ import (
func GenerateOutputPaths(outputDir, upstream string, dnssec, keepAlive bool) (csvPath, pcapPath string) {
proto := DetectProtocol(upstream)
serverName := ExtractServerName(upstream)
ts := time.Now().Format("20060102_1504")
dnssecStr := map[bool]string{true: "on", false: "off"}[dnssec]
keepAliveStr := map[bool]string{true: "on", false: "off"}[keepAlive]
cleanServer := cleanServerName(serverName)
base := fmt.Sprintf("%s_%s_dnssec_%s_keepalive_%s_%s",
proto, sanitize(serverName), dnssecStr, keepAliveStr, ts)
// Create date-based subdirectory
date := time.Now().Format("2006-01-02")
timestamp := time.Now().Format("150405")
return filepath.Join(outputDir, base+".csv"),
filepath.Join(outputDir, base+".pcap")
// Organize by date and server
subDir := filepath.Join(outputDir, date, cleanServer)
// Create simple filename
base := proto
// Add flags if enabled
var flags []string
if dnssec {
flags = append(flags, "dnssec")
}
if keepAlive {
flags = append(flags, "persist")
}
if len(flags) > 0 {
base = fmt.Sprintf("%s-%s", base, strings.Join(flags, "-"))
}
// Add timestamp
filename := fmt.Sprintf("%s-%s", base, timestamp)
return filepath.Join(subDir, filename+".csv"),
filepath.Join(subDir, filename+".pcap")
}
func sanitize(s string) string {
return strings.NewReplacer(":", "_", "/", "_", ".", "_").Replace(s)
func cleanServerName(server string) string {
// Map common servers to short names
serverMap := map[string]string{
"1.1.1.1": "cloudflare",
"1.0.0.1": "cloudflare",
"cloudflare-dns.com": "cloudflare",
"one.one.one.one": "cloudflare",
"8.8.8.8": "google",
"8.8.4.4": "google",
"dns.google": "google",
"dns.google.com": "google",
"9.9.9.9": "quad9",
"149.112.112.112": "quad9",
"dns.quad9.net": "quad9",
"208.67.222.222": "opendns",
"208.67.220.220": "opendns",
"resolver1.opendns.com": "opendns",
"94.140.14.14": "adguard",
"94.140.15.15": "adguard",
"dns.adguard.com": "adguard",
}
// Clean the server name first
cleaned := strings.ToLower(server)
cleaned = strings.TrimPrefix(cleaned, "https://")
cleaned = strings.TrimPrefix(cleaned, "http://")
cleaned = strings.Split(cleaned, "/")[0] // Remove path
cleaned = strings.Split(cleaned, ":")[0] // Remove port
// Check if we have a mapping
if shortName, exists := serverMap[cleaned]; exists {
return shortName
}
// For unknown servers, create a reasonable short name
parts := strings.Split(cleaned, ".")
if len(parts) >= 2 {
// For domains like dns.example.com, take "example"
if len(parts) >= 3 {
return parts[len(parts)-2] // Second to last part
}
// For IPs or simple domains, take first part
return parts[0]
}
return sanitizeShort(cleaned)
}
func sanitizeShort(s string) string {
// Keep only alphanumeric and dash
var result strings.Builder
for _, r := range s {
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
result.WriteRune(r)
} else if r == '.' || r == '_' || r == '-' {
result.WriteRune('-')
}
}
cleaned := result.String()
// Remove consecutive dashes and trim
for strings.Contains(cleaned, "--") {
cleaned = strings.ReplaceAll(cleaned, "--", "-")
}
cleaned = strings.Trim(cleaned, "-")
// Limit length
if len(cleaned) > 15 {
cleaned = cleaned[:15]
}
return cleaned
}
func DetectProtocol(upstream string) string {