Skip to content

Commit

Permalink
introduce a fail flag controlling when dnspyre exits with non-zero code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tantalor93 committed Apr 24, 2024
1 parent 4241a72 commit 33afcbb
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ var (
benchmark = dnsbench.Benchmark{
Writer: os.Stdout,
}

failConditions []string
)

const (
ioerrorFailCondition = "ioerror"
negativeFailCondition = "negative"
errorFailCondition = "error"
idmismatchFailCondition = "idmismatch"
)

func init() {
Expand Down Expand Up @@ -130,6 +139,12 @@ func init() {
pApp.Flag("progress", "Controls whether the progress bar is shown. Enabled by default.").
Default("true").BoolVar(&benchmark.ProgressBar)

pApp.Flag("fail", "Controls conditions upon which the dnspyre will exit with a non-zero exit code. Repeatable flag. "+
"Supported options are 'ioerror' (fail if there is at least 1 IO error), 'negative' (fail if there is at least 1 negative DNS answer), "+
"'error' (fail if there is at least 1 error DNS response), 'idmismatch' (fail there is at least 1 ID mismatch between DNS request and response).").
PlaceHolder(ioerrorFailCondition).
EnumsVar(&failConditions, ioerrorFailCondition, negativeFailCondition, errorFailCondition, idmismatchFailCondition)

pApp.Arg("queries", "Queries to issue. It can be a local file referenced using @<file-path>, for example @data/2-domains. "+
"It can also be resource accessible using HTTP, like https://raw.githubusercontent.com/Tantalor93/dnspyre/master/data/1000-domains, in that "+
"case, the file will be downloaded and saved in-memory. "+
Expand Down Expand Up @@ -183,6 +198,30 @@ func Execute() {
}

close(sigsInt)

if len(failConditions) > 0 {
stats := reporter.Merge(&benchmark, res)
for _, f := range failConditions {
switch f {
case ioerrorFailCondition:
if stats.Counters.IOError > 0 {
os.Exit(1)
}
case negativeFailCondition:
if stats.Counters.Negative > 0 {
os.Exit(1)
}
case errorFailCondition:
if stats.Counters.Error > 0 {
os.Exit(1)
}
case idmismatchFailCondition:
if stats.Counters.IDmismatch > 0 {
os.Exit(1)
}
}
}
}
}

func getSupportedDNSTypes() []string {
Expand Down

0 comments on commit 33afcbb

Please sign in to comment.