1 | 1 | | package input |
2 | 2 | | |
3 | 3 | | import ( |
4 | | - | "flag" |
5 | 4 | | "io" |
6 | 5 | | "os" |
7 | 6 | | "strings" |
8 | 7 | | |
9 | 8 | | "github.com/edoardottt/csprecon/pkg/output" |
10 | | - | |
| 9 | + | "github.com/projectdiscovery/goflags" |
11 | 10 | | "github.com/projectdiscovery/gologger" |
12 | 11 | | "github.com/projectdiscovery/gologger/levels" |
13 | 12 | | ) |
| skipped 7 lines |
21 | 20 | | Input string |
22 | 21 | | FileInput string |
23 | 22 | | FileOutput string |
24 | | - | Domain string |
| 23 | + | Domain goflags.StringSlice |
25 | 24 | | Verbose bool |
26 | 25 | | Output io.Writer |
27 | 26 | | Silent bool |
| skipped 14 lines |
42 | 41 | | func ParseOptions() *Options { |
43 | 42 | | options := &Options{} |
44 | 43 | | |
45 | | - | flag.StringVar(&options.Input, "u", "", `Input domain`) |
46 | | - | flag.StringVar(&options.FileInput, "l", "", `File containing input domains`) |
47 | | - | flag.StringVar(&options.Domain, "d", "", `Filter results belonging to this domain`) |
48 | | - | flag.StringVar(&options.FileOutput, "o", "", `File to write output results`) |
49 | | - | flag.BoolVar(&options.Verbose, "v", false, `Verbose output`) |
50 | | - | flag.BoolVar(&options.Silent, "s", false, `Print only results`) |
51 | | - | flag.IntVar(&options.Concurrency, "c", DefaultConcurrency, "Concurrency level") |
52 | | - | flag.IntVar(&options.Timeout, "t", DefaultTimeout, "Connection timeout in seconds") |
| 44 | + | flagSet := goflags.NewFlagSet() |
| 45 | + | flagSet.SetDescription(`Discover new target domains using Content Security Policy.`) |
| 46 | + | |
| 47 | + | // Input |
| 48 | + | flagSet.CreateGroup("input", "Input", |
| 49 | + | flagSet.StringVar(&options.Input, "u", "", `Input domain`), |
| 50 | + | flagSet.StringVar(&options.FileInput, "l", "", `File containing input domains`), |
| 51 | + | ) |
53 | 52 | | |
54 | | - | if help() { |
| 53 | + | flagSet.CreateGroup("configs", "Configurations", |
| 54 | + | flagSet.StringSliceVar(&options.Domain, "d", nil, `Filter results belonging to these domains (comma separated)`, goflags.CommaSeparatedStringSliceOptions), |
| 55 | + | flagSet.IntVar(&options.Concurrency, "c", DefaultConcurrency, `Concurrency level`), |
| 56 | + | flagSet.IntVar(&options.Timeout, "t", DefaultTimeout, `Connection timeout in seconds`), |
| 57 | + | ) |
| 58 | + | |
| 59 | + | // Output |
| 60 | + | flagSet.CreateGroup("output", "Output", |
| 61 | + | flagSet.StringVar(&options.FileOutput, "o", "", `File to write output results`), |
| 62 | + | flagSet.BoolVar(&options.Verbose, "v", false, `Verbose output`), |
| 63 | + | flagSet.BoolVar(&options.Silent, "s", false, `Print only results`), |
| 64 | + | ) |
| 65 | + | |
| 66 | + | if help() || noArgs() || !options.Silent { |
55 | 67 | | output.ShowBanner() |
56 | 68 | | } |
57 | 69 | | |
58 | | - | flag.Parse() |
| 70 | + | if err := flagSet.Parse(); err != nil { |
| 71 | + | output.ShowBanner() |
| 72 | + | gologger.Fatal().Msgf("%s\n", err) |
| 73 | + | } |
59 | 74 | | |
60 | 75 | | // Read the inputs and configure the logging. |
61 | 76 | | options.configureOutput() |
62 | | - | |
63 | | - | if !options.Silent { |
64 | | - | output.ShowBanner() |
65 | | - | } |
66 | 77 | | |
67 | 78 | | if err := options.validateOptions(); err != nil { |
68 | 79 | | gologger.Fatal().Msgf("%s\n", err) |
| skipped 14 lines |
83 | 94 | | return false |
84 | 95 | | } |
85 | 96 | | |
| 97 | + | func noArgs() bool { |
| 98 | + | // User passed no flag. |
| 99 | + | return len(os.Args) < 2 |
| 100 | + | } |
| 101 | + | |