|
| 1 | +// Package passive names the keyless passive sources whose names seed a scan |
| 2 | +// (#193): Certificate Transparency through crt.sh, and the Wayback Machine's |
| 3 | +// URL index. A source only proposes candidates; the scan still resolves each |
| 4 | +// one, so a stale name costs a query, never a false result. |
| 5 | +package passive |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "slices" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/TMHSDigital/subenum/internal/ct" |
| 14 | +) |
| 15 | + |
| 16 | +// Source finds names under a domain without querying the domain's DNS. |
| 17 | +type Source interface { |
| 18 | + // Name is the source's short name, as -sources takes it. |
| 19 | + Name() string |
| 20 | + // Names returns names under domain relative to it ("api", "dev.api"), |
| 21 | + // sorted and deduplicated. |
| 22 | + Names(ctx context.Context, domain string) ([]string, error) |
| 23 | +} |
| 24 | + |
| 25 | +// Known lists the sources -sources accepts, in the order they run. |
| 26 | +var Known = []string{"ct", "wayback"} |
| 27 | + |
| 28 | +// Labels are human-readable names for messages. |
| 29 | +var Labels = map[string]string{"ct": "Certificate Transparency", "wayback": "Wayback Machine"} |
| 30 | + |
| 31 | +// Options configure the built-in sources. |
| 32 | +type Options struct { |
| 33 | + // UserAgent identifies the client to each service. |
| 34 | + UserAgent string |
| 35 | + // BaseURLs replace a source's endpoint, by source name; tests point them |
| 36 | + // at local servers. |
| 37 | + BaseURLs map[string]string |
| 38 | +} |
| 39 | + |
| 40 | +// ParseList parses a comma-separated -sources value: known names only, |
| 41 | +// lowercase, deduplicated, in Known order. Empty is no sources. |
| 42 | +func ParseList(s string) ([]string, error) { |
| 43 | + want := map[string]bool{} |
| 44 | + for _, part := range strings.Split(s, ",") { |
| 45 | + name := strings.ToLower(strings.TrimSpace(part)) |
| 46 | + if name == "" { |
| 47 | + continue |
| 48 | + } |
| 49 | + if !slices.Contains(Known, name) { |
| 50 | + return nil, fmt.Errorf("unknown source %q (want %s)", part, strings.Join(Known, ", ")) |
| 51 | + } |
| 52 | + want[name] = true |
| 53 | + } |
| 54 | + var out []string |
| 55 | + for _, name := range Known { |
| 56 | + if want[name] { |
| 57 | + out = append(out, name) |
| 58 | + } |
| 59 | + } |
| 60 | + return out, nil |
| 61 | +} |
| 62 | + |
| 63 | +// New returns the built-in source called name. |
| 64 | +func New(name string, opts Options) (Source, error) { |
| 65 | + switch name { |
| 66 | + case "ct": |
| 67 | + return ct.Client{BaseURL: opts.BaseURLs["ct"], UserAgent: opts.UserAgent}, nil |
| 68 | + case "wayback": |
| 69 | + return Wayback{BaseURL: opts.BaseURLs["wayback"], UserAgent: opts.UserAgent}, nil |
| 70 | + } |
| 71 | + return nil, fmt.Errorf("unknown source %q", name) |
| 72 | +} |
0 commit comments