|
| 1 | +package runner |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/projectdiscovery/gologger" |
| 9 | + "github.com/projectdiscovery/gologger/levels" |
| 10 | +) |
| 11 | + |
| 12 | +type Options struct { |
| 13 | + ListenAddress string |
| 14 | + Folder string |
| 15 | + BasicAuth string |
| 16 | + username string |
| 17 | + password string |
| 18 | + Realm string |
| 19 | + TLSCertificate string |
| 20 | + TLSKey string |
| 21 | + TLSDomain string |
| 22 | + HTTPS bool |
| 23 | + Verbose bool |
| 24 | + EnableUpload bool |
| 25 | + EnableTCP bool |
| 26 | + RulesFile string |
| 27 | + TCPWithTLS bool |
| 28 | + Version bool |
| 29 | + Silent bool |
| 30 | +} |
| 31 | + |
| 32 | +// ParseOptions parses the command line options for application |
| 33 | +func ParseOptions() *Options { |
| 34 | + options := &Options{} |
| 35 | + flag.StringVar(&options.ListenAddress, "listen", "0.0.0.0:8000", "Address:Port") |
| 36 | + flag.BoolVar(&options.EnableTCP, "tcp", false, "TCP Server") |
| 37 | + flag.BoolVar(&options.TCPWithTLS, "tls", false, "Enable TCP TLS") |
| 38 | + flag.StringVar(&options.RulesFile, "rules", "", "Rules yaml file") |
| 39 | + flag.StringVar(&options.Folder, "path", ".", "Folder") |
| 40 | + flag.BoolVar(&options.EnableUpload, "upload", false, "Enable upload via PUT") |
| 41 | + flag.BoolVar(&options.HTTPS, "https", false, "HTTPS") |
| 42 | + flag.StringVar(&options.TLSCertificate, "cert", "", "HTTPS Certificate") |
| 43 | + flag.StringVar(&options.TLSKey, "key", "", "HTTPS Certificate Key") |
| 44 | + flag.StringVar(&options.TLSDomain, "domain", "local.host", "Domain") |
| 45 | + flag.BoolVar(&options.Verbose, "verbose", false, "Verbose") |
| 46 | + flag.StringVar(&options.BasicAuth, "basic-auth", "", "Basic auth (username:password)") |
| 47 | + flag.StringVar(&options.Realm, "realm", "Please enter username and password", "Realm") |
| 48 | + flag.BoolVar(&options.Version, "version", false, "Show version of the software") |
| 49 | + flag.BoolVar(&options.Silent, "silent", false, "Show only results in the output") |
| 50 | + |
| 51 | + flag.Parse() |
| 52 | + |
| 53 | + // Read the inputs and configure the logging |
| 54 | + options.configureOutput() |
| 55 | + |
| 56 | + showBanner() |
| 57 | + |
| 58 | + if options.Version { |
| 59 | + gologger.Info().Msgf("Current Version: %s\n", Version) |
| 60 | + os.Exit(0) |
| 61 | + } |
| 62 | + |
| 63 | + options.validateOptions() |
| 64 | + |
| 65 | + return options |
| 66 | +} |
| 67 | + |
| 68 | +func (options *Options) validateOptions() { |
| 69 | + if flag.NArg() > 0 && options.Folder == "." { |
| 70 | + options.Folder = flag.Args()[0] |
| 71 | + } |
| 72 | + |
| 73 | + if options.BasicAuth != "" { |
| 74 | + baTokens := strings.SplitN(options.BasicAuth, ":", 2) |
| 75 | + if len(baTokens) > 0 { |
| 76 | + options.username = baTokens[0] |
| 77 | + } |
| 78 | + if len(baTokens) > 1 { |
| 79 | + options.password = baTokens[1] |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// configureOutput configures the output on the screen |
| 85 | +func (options *Options) configureOutput() { |
| 86 | + // If the user desires verbose output, show verbose output |
| 87 | + if options.Verbose { |
| 88 | + gologger.DefaultLogger.SetMaxLevel(levels.LevelVerbose) |
| 89 | + } |
| 90 | + if options.Silent { |
| 91 | + gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent) |
| 92 | + } |
| 93 | +} |
0 commit comments