Skip to content

Commit a408276

Browse files
committed
Save partial results on Ctrl+C instead of discarding them
Handle SIGINT gracefully across all commands: stop dispatching new jobs, collect completed results, and write them to the output file.
1 parent 04ba059 commit a408276

14 files changed

Lines changed: 184 additions & 26 deletions

File tree

cmd/chain.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
6+
"os"
7+
"os/signal"
58
"strconv"
69
"strings"
710
"time"
@@ -227,6 +230,14 @@ func runChain(cmd *cobra.Command, args []string) error {
227230
return fmt.Errorf("--output / -o flag is required")
228231
}
229232

230-
report := scanner.RunChain(ips, workers, steps, newProgressFactory())
233+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
234+
defer stop()
235+
236+
report := scanner.RunChainCtx(ctx, ips, workers, steps, newProgressFactory())
237+
238+
if ctx.Err() != nil {
239+
fmt.Fprintf(os.Stderr, "\n⚠ Interrupted — saving partial results to %s\n", outputFile)
240+
}
241+
231242
return scanner.WriteChainReport(report, outputFile)
232243
}

cmd/doh_e2e.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
48
"time"
59

610
"github.com/SamNet-dev/findns/internal/scanner"
@@ -43,9 +47,16 @@ func runDoHE2E(cmd *cobra.Command, args []string) error {
4347
ports := scanner.PortPool(30000, workers)
4448
check := scanner.DoHDnsttCheckBin(bin, domain, pubkey, testURL, proxyAuth, ports)
4549

50+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
51+
defer stop()
52+
4653
start := time.Now()
47-
results := scanner.RunPool(urls, workers, dur, check, newProgress("doh/e2e"))
54+
results := scanner.RunPoolCtx(ctx, urls, workers, dur, check, newProgress("doh/e2e"))
4855
elapsed := time.Since(start)
4956

57+
if ctx.Err() != nil {
58+
fmt.Fprintf(os.Stderr, "\n⚠ Interrupted — saving partial results\n")
59+
}
60+
5061
return writeReport("doh/e2e", results, elapsed, "e2e_ms")
5162
}

cmd/doh_resolve.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
48
"time"
59

610
"github.com/SamNet-dev/findns/internal/scanner"
@@ -30,9 +34,16 @@ func runDoHResolve(cmd *cobra.Command, args []string) error {
3034
dur := time.Duration(timeout) * time.Second
3135
check := scanner.DoHResolveCheck(domain, count)
3236

37+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
38+
defer stop()
39+
3340
start := time.Now()
34-
results := scanner.RunPool(urls, workers, dur, check, newProgress("doh/resolve"))
41+
results := scanner.RunPoolCtx(ctx, urls, workers, dur, check, newProgress("doh/resolve"))
3542
elapsed := time.Since(start)
3643

44+
if ctx.Err() != nil {
45+
fmt.Fprintf(os.Stderr, "\n⚠ Interrupted — saving partial results\n")
46+
}
47+
3748
return writeReport("doh/resolve", results, elapsed, "resolve_ms")
3849
}

cmd/doh_tunnel.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
48
"time"
59

610
"github.com/SamNet-dev/findns/internal/scanner"
@@ -30,9 +34,16 @@ func runDoHTunnel(cmd *cobra.Command, args []string) error {
3034
dur := time.Duration(timeout) * time.Second
3135
check := scanner.DoHTunnelCheck(domain, count)
3236

37+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
38+
defer stop()
39+
3340
start := time.Now()
34-
results := scanner.RunPool(urls, workers, dur, check, newProgress("doh/resolve/tunnel"))
41+
results := scanner.RunPoolCtx(ctx, urls, workers, dur, check, newProgress("doh/resolve/tunnel"))
3542
elapsed := time.Since(start)
3643

44+
if ctx.Err() != nil {
45+
fmt.Fprintf(os.Stderr, "\n⚠ Interrupted — saving partial results\n")
46+
}
47+
3748
return writeReport("doh/resolve/tunnel", results, elapsed, "resolve_ms")
3849
}

cmd/e2e_dnstt.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
48
"time"
59

610
"github.com/SamNet-dev/findns/internal/scanner"
@@ -43,9 +47,16 @@ func runE2EDnstt(cmd *cobra.Command, args []string) error {
4347
ports := scanner.PortPool(30000, workers)
4448
check := scanner.DnsttCheckBin(bin, domain, pubkey, testURL, proxyAuth, ports)
4549

50+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
51+
defer stop()
52+
4653
start := time.Now()
47-
results := scanner.RunPool(ips, workers, dur, check, newProgress("e2e/dnstt"))
54+
results := scanner.RunPoolCtx(ctx, ips, workers, dur, check, newProgress("e2e/dnstt"))
4855
elapsed := time.Since(start)
4956

57+
if ctx.Err() != nil {
58+
fmt.Fprintf(os.Stderr, "\n⚠ Interrupted — saving partial results\n")
59+
}
60+
5061
return writeReport("e2e/dnstt", results, elapsed, "e2e_ms")
5162
}

cmd/e2e_slipstream.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
48
"time"
59

610
"github.com/SamNet-dev/findns/internal/scanner"
@@ -42,9 +46,16 @@ func runE2ESlipstream(cmd *cobra.Command, args []string) error {
4246
ports := scanner.PortPool(30000, workers)
4347
check := scanner.SlipstreamCheckBin(bin, domain, certPath, testURL, proxyAuth, ports)
4448

49+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
50+
defer stop()
51+
4552
start := time.Now()
46-
results := scanner.RunPool(ips, workers, dur, check, newProgress("e2e/slipstream"))
53+
results := scanner.RunPoolCtx(ctx, ips, workers, dur, check, newProgress("e2e/slipstream"))
4754
elapsed := time.Since(start)
4855

56+
if ctx.Err() != nil {
57+
fmt.Fprintf(os.Stderr, "\n⚠ Interrupted — saving partial results\n")
58+
}
59+
4960
return writeReport("e2e/slipstream", results, elapsed, "e2e_ms")
5061
}

cmd/edns.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
48
"time"
59

610
"github.com/SamNet-dev/findns/internal/scanner"
@@ -30,9 +34,16 @@ func runEDNS(cmd *cobra.Command, args []string) error {
3034
dur := time.Duration(timeout) * time.Second
3135
check := scanner.EDNSCheck(domain, count)
3236

37+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
38+
defer stop()
39+
3340
start := time.Now()
34-
results := scanner.RunPool(ips, workers, dur, check, newProgress("edns"))
41+
results := scanner.RunPoolCtx(ctx, ips, workers, dur, check, newProgress("edns"))
3542
elapsed := time.Since(start)
3643

44+
if ctx.Err() != nil {
45+
fmt.Fprintf(os.Stderr, "\n⚠ Interrupted — saving partial results\n")
46+
}
47+
3748
return writeReport("edns", results, elapsed, "edns_max")
3849
}

cmd/nxdomain.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
48
"time"
59

610
"github.com/SamNet-dev/findns/internal/scanner"
@@ -26,9 +30,16 @@ func runNXDomain(cmd *cobra.Command, args []string) error {
2630
dur := time.Duration(timeout) * time.Second
2731
check := scanner.NXDomainCheck(count)
2832

33+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
34+
defer stop()
35+
2936
start := time.Now()
30-
results := scanner.RunPool(ips, workers, dur, check, newProgress("nxdomain"))
37+
results := scanner.RunPoolCtx(ctx, ips, workers, dur, check, newProgress("nxdomain"))
3138
elapsed := time.Since(start)
3239

40+
if ctx.Err() != nil {
41+
fmt.Fprintf(os.Stderr, "\n⚠ Interrupted — saving partial results\n")
42+
}
43+
3344
return writeReport("nxdomain", results, elapsed, "hijack")
3445
}

cmd/ping.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
48
"time"
59

610
"github.com/SamNet-dev/findns/internal/scanner"
@@ -26,9 +30,16 @@ func runPing(cmd *cobra.Command, args []string) error {
2630
dur := time.Duration(timeout) * time.Second
2731
check := scanner.PingCheck(count)
2832

33+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
34+
defer stop()
35+
2936
start := time.Now()
30-
results := scanner.RunPool(ips, workers, dur, check, newProgress("ping"))
37+
results := scanner.RunPoolCtx(ctx, ips, workers, dur, check, newProgress("ping"))
3138
elapsed := time.Since(start)
3239

40+
if ctx.Err() != nil {
41+
fmt.Fprintf(os.Stderr, "\n⚠ Interrupted — saving partial results\n")
42+
}
43+
3344
return writeReport("ping", results, elapsed, "ping_ms")
3445
}

cmd/resolve.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
48
"time"
59

610
"github.com/SamNet-dev/findns/internal/scanner"
@@ -30,9 +34,16 @@ func runResolve(cmd *cobra.Command, args []string) error {
3034
dur := time.Duration(timeout) * time.Second
3135
check := scanner.ResolveCheck(domain, count)
3236

37+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
38+
defer stop()
39+
3340
start := time.Now()
34-
results := scanner.RunPool(ips, workers, dur, check, newProgress("resolve"))
41+
results := scanner.RunPoolCtx(ctx, ips, workers, dur, check, newProgress("resolve"))
3542
elapsed := time.Since(start)
3643

44+
if ctx.Err() != nil {
45+
fmt.Fprintf(os.Stderr, "\n⚠ Interrupted — saving partial results\n")
46+
}
47+
3748
return writeReport("resolve", results, elapsed, "resolve_ms")
3849
}

0 commit comments

Comments
 (0)