-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves argument processing into its own function; fixes #16
- Loading branch information
Showing
3 changed files
with
160 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// headerArgs is the type used to store the header arguments | ||
type headerArgs []string | ||
|
||
func (h *headerArgs) Set(val string) error { | ||
*h = append(*h, val) | ||
return nil | ||
} | ||
|
||
func (h headerArgs) String() string { | ||
return "string" | ||
} | ||
|
||
type config struct { | ||
concurrency int | ||
delay int | ||
headers headerArgs | ||
method string | ||
saveStatus int | ||
verbose bool | ||
suffix string | ||
prefix string | ||
output string | ||
} | ||
|
||
func processArgs() config { | ||
|
||
// concurrency param | ||
concurrency := 20 | ||
flag.IntVar(&concurrency, "concurrency", 20, "") | ||
flag.IntVar(&concurrency, "c", 20, "") | ||
|
||
// delay param | ||
delay := 5000 | ||
flag.IntVar(&delay, "delay", 5000, "") | ||
flag.IntVar(&delay, "d", 5000, "") | ||
|
||
// headers param | ||
var headers headerArgs | ||
flag.Var(&headers, "header", "") | ||
flag.Var(&headers, "H", "") | ||
|
||
// method param | ||
method := "GET" | ||
flag.StringVar(&method, "method", "GET", "") | ||
flag.StringVar(&method, "X", "GET", "") | ||
|
||
// savestatus param | ||
saveStatus := 0 | ||
flag.IntVar(&saveStatus, "savestatus", 0, "") | ||
flag.IntVar(&saveStatus, "s", 0, "") | ||
|
||
// verbose param | ||
verbose := false | ||
flag.BoolVar(&verbose, "verbose", false, "") | ||
flag.BoolVar(&verbose, "v", false, "") | ||
|
||
flag.Parse() | ||
|
||
// suffixes might be in a file, or it might be a single value | ||
suffix := flag.Arg(0) | ||
if suffix == "" { | ||
suffix = "suffixes" | ||
} | ||
|
||
// prefixes are always in a file | ||
prefix := flag.Arg(1) | ||
if prefix == "" { | ||
prefix = "prefixes" | ||
} | ||
|
||
// default the output directory to ./out | ||
output := flag.Arg(2) | ||
if output == "" { | ||
output = "./out" | ||
} | ||
|
||
return config{ | ||
concurrency: concurrency, | ||
delay: delay, | ||
headers: headers, | ||
method: method, | ||
saveStatus: saveStatus, | ||
verbose: verbose, | ||
suffix: suffix, | ||
prefix: prefix, | ||
output: output, | ||
} | ||
} | ||
|
||
func init() { | ||
flag.Usage = func() { | ||
h := "Request many paths (suffixes) for many hosts (prefixes)\n\n" | ||
|
||
h += "Usage:\n" | ||
h += " meg [suffix|suffixFile] [prefixFile] [outputDir]\n\n" | ||
|
||
h += "Options:\n" | ||
h += " -c, --concurrency <val> Set the concurrency level (defaut: 20)\n" | ||
h += " -d, --delay <val> Milliseconds between requests to the same host (defaut: 5000)\n" | ||
h += " -H, --header <header> Send a custom HTTP header\n" | ||
h += " -s, --savestatus <status> Save only responses with specific status code\n" | ||
h += " -v, --verbose Verbose mode\n" | ||
h += " -X, --method <method> HTTP method (default: GET)\n\n" | ||
|
||
h += "Defaults:\n" | ||
h += " suffixFile: ./suffixes\n" | ||
h += " prefixFile: ./prefixes\n" | ||
h += " outputDir: ./out\n\n" | ||
|
||
h += "Suffix file format:\n" | ||
h += " /robots.txt\n" | ||
h += " /package.json\n" | ||
h += " /security.txt\n\n" | ||
|
||
h += "Prefix file format:\n" | ||
h += " http://example.com\n" | ||
h += " https://example.edu\n" | ||
h += " https://example.net\n\n" | ||
|
||
h += "Examples:\n" | ||
h += " meg /robots.txt\n" | ||
h += " meg hosts.txt paths.txt output\n" | ||
|
||
fmt.Fprintf(os.Stderr, h) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters