Skip to content

Commit

Permalink
Adds ability to follow redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
tomnomnom committed Jan 7, 2018
1 parent 03d2130 commit d466028
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
50 changes: 30 additions & 20 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ func (h headerArgs) String() string {
}

type config struct {
concurrency int
delay int
headers headerArgs
method string
saveStatus int
requester requester
verbose bool
paths string
hosts string
output string
concurrency int
delay int
headers headerArgs
followLocation bool
method string
saveStatus int
verbose bool

paths string
hosts string
output string

requester requester
}

func processArgs() config {
Expand All @@ -48,6 +51,11 @@ func processArgs() config {
flag.Var(&headers, "header", "")
flag.Var(&headers, "H", "")

// follow location param
followLocation := false
flag.BoolVar(&followLocation, "location", false, "")
flag.BoolVar(&followLocation, "L", false, "")

// method param
method := "GET"
flag.StringVar(&method, "method", "GET", "")
Expand Down Expand Up @@ -95,16 +103,17 @@ func processArgs() config {
}

return config{
concurrency: concurrency,
delay: delay,
headers: headers,
method: method,
saveStatus: saveStatus,
requester: requesterFn,
verbose: verbose,
paths: paths,
hosts: hosts,
output: output,
concurrency: concurrency,
delay: delay,
headers: headers,
followLocation: followLocation,
method: method,
saveStatus: saveStatus,
requester: requesterFn,
verbose: verbose,
paths: paths,
hosts: hosts,
output: output,
}
}

Expand All @@ -119,6 +128,7 @@ func init() {
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 += " -L, --location Follow redirects / location header\n"
h += " -r, --rawhttp Use the rawhttp library for requests (experimental)\n"
h += " -s, --savestatus <status> Save only responses with specific status code\n"
h += " -v, --verbose Verbose mode\n"
Expand Down
15 changes: 8 additions & 7 deletions gohttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ var transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

var checkRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}

var httpClient = &http.Client{
Transport: transport,
CheckRedirect: checkRedirect,
Timeout: time.Second * 10,
Transport: transport,
Timeout: time.Second * 10,
}

func goRequest(r request) response {

if !r.followLocation {
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}

req, err := http.NewRequest(r.method, r.URL(), nil)
if err != nil {
return response{request: r, err: err}
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ func main() {
host = u.String()

requests <- request{
method: c.method,
host: host,
path: prefixedPath,
headers: c.headers,
method: c.method,
host: host,
path: prefixedPath,
headers: c.headers,
followLocation: c.followLocation,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type request struct {
path string
host string
headers []string

followLocation bool
}

// Hostname returns the hostname part of the request
Expand Down

0 comments on commit d466028

Please sign in to comment.