Skip to content

Commit

Permalink
Refactor core scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
wolveix committed Dec 15, 2023
1 parent d09217b commit f8e311a
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 309 deletions.
2 changes: 1 addition & 1 deletion cmd/dss/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
Use: "dss",
Short: "Scan a domain's DNS records.",
Long: "Scan a domain's DNS records.\nhttps://github.com/GlobalCyberAlliance/domain-security-scanner",
Version: "2.4.4",
Version: "2.4.5",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if debug {
log = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}).With().Timestamp().Logger().Level(zerolog.DebugLevel)
Expand Down
17 changes: 8 additions & 9 deletions cmd/dss/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,33 @@ var cmdScan = &cobra.Command{
Long: "Scan DNS records for one or multiple domains.\nBy default, the command will listen on STDIN, allowing you to type or pipe multiple domains.",
Run: func(command *cobra.Command, args []string) {
opts := []scanner.ScannerOption{
scanner.ConcurrentScans(concurrent),
scanner.UseCache(cacheEnabled),
scanner.UseNameservers(nameservers),
scanner.WithCache(cacheEnabled),
scanner.WithConcurrentScans(concurrent),
scanner.WithDKIMSelectors(dkimSelector...),
scanner.WithDNSBuffer(dnsBuffer),
scanner.WithNameservers(nameservers),
scanner.WithTimeout(time.Duration(timeout) * time.Second),
}

var source scanner.Source

if len(args) == 0 && zoneFile {
source = scanner.ZonefileSource(os.Stdin)
source = scanner.NewSource(os.Stdin, scanner.ZonefileSourceType)
} else if len(args) > 0 && zoneFile {
log.Fatal().Msg("-z flag provided, but not reading from STDIN")
} else if len(args) == 0 {
log.Info().Msg("Accepting input from STDIN. Type a domain and hit enter.")
source = scanner.TextSource(os.Stdin)
source = scanner.NewSource(os.Stdin, scanner.TextSourceType)
} else {
sr := strings.NewReader(strings.Join(args, "\n"))
source = scanner.TextSource(sr)
reader := strings.NewReader(strings.Join(args, "\n"))
source = scanner.NewSource(reader, scanner.TextSourceType)
}

sc, err := scanner.New(opts...)
if err != nil {
log.Fatal().Err(err).Msg("An unexpected error occurred.")
}

sc.DKIMSelectors = dkimSelector

domainAdvisor := advisor.NewAdvisor(time.Duration(timeout)*time.Second, cacheEnabled)

if format == "csv" && outputFile == "" {
Expand Down
14 changes: 8 additions & 6 deletions cmd/dss/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ var (
server := http.NewServer(log)

opts := []scanner.ScannerOption{
scanner.ConcurrentScans(concurrent),
scanner.UseCache(cacheEnabled),
scanner.UseNameservers(nameservers),
scanner.WithCache(cacheEnabled),
scanner.WithConcurrentScans(concurrent),
scanner.WithDKIMSelectors(dkimSelector...),
scanner.WithDNSBuffer(dnsBuffer),
scanner.WithNameservers(nameservers),
scanner.WithTimeout(time.Duration(timeout) * time.Second),
}

Expand All @@ -75,10 +76,11 @@ var (
Short: "Serve DNS security queries via a dedicated email account",
Run: func(command *cobra.Command, args []string) {
opts := []scanner.ScannerOption{
scanner.ConcurrentScans(concurrent),
scanner.UseCache(cacheEnabled),
scanner.UseNameservers(nameservers),
scanner.WithCache(cacheEnabled),
scanner.WithConcurrentScans(concurrent),
scanner.WithDKIMSelectors(dkimSelector...),
scanner.WithDNSBuffer(dnsBuffer),
scanner.WithNameservers(nameservers),
scanner.WithTimeout(time.Duration(timeout) * time.Second),
}

Expand Down
11 changes: 9 additions & 2 deletions pkg/http/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,17 @@ func (s *Server) handleScanDomains(c *gin.Context) {
}

domainList := strings.NewReader(strings.Join(domains.Domains, "\n"))
source := scanner.TextSource(domainList)
source := scanner.NewSource(domainList, scanner.TextSourceType)

// TODO: temporary solution to allow for custom DKIM selectors in the API.
// This implementation is not ideal, as it will overwrite the selectors for
// future scans.
if queryParam, ok := c.GetQuery("dkimSelector"); ok {
s.Scanner.DKIMSelectors = strings.Split(queryParam, ",")
if err := s.Scanner.OverwriteOption(scanner.WithDKIMSelectors(strings.Split(queryParam, ",")...)); err != nil {
s.logger.Error().Err(err).Msg("fai")
s.respond(c, 400, err.Error())
return
}
}

var resultsWithAdvice []model.ScanResultWithAdvice
Expand Down
2 changes: 1 addition & 1 deletion pkg/mail/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *Server) handler() error {
}

sourceDomainList := strings.NewReader(strings.Join(domainList, "\n"))
source := scanner.TextSource(sourceDomainList)
source := scanner.NewSource(sourceDomainList, scanner.TextSourceType)

for result := range s.Scanner.Start(source) {
sender := addresses[result.Domain].Address
Expand Down
Loading

0 comments on commit f8e311a

Please sign in to comment.