Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for tracker links in paths, and filtering responses by header #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (s saveStatusArgs) Includes(search int) bool {
}

type config struct {
trackerLink string
saveHeader string
body string
concurrency int
delay int
Expand All @@ -61,6 +63,10 @@ type config struct {

func processArgs() config {

// tracker link params
trackerLink := ""
flag.StringVar(&trackerLink, "tracker", "", "")

// body param
body := ""
flag.StringVar(&body, "body", "", "")
Expand Down Expand Up @@ -96,6 +102,11 @@ func processArgs() config {
flag.Var(&saveStatus, "savestatus", "")
flag.Var(&saveStatus, "s", "")

// saveheader parms
saveHeader := ""
flag.StringVar(&saveHeader, "saveheader", "", "")
flag.StringVar(&saveHeader, "sh", "", "")

// timeout param
timeout := 10000
flag.IntVar(&timeout, "timeout", 10000, "")
Expand Down Expand Up @@ -142,6 +153,8 @@ func processArgs() config {
}

return config{
trackerLink: trackerLink,
saveHeader: saveHeader,
body: body,
concurrency: concurrency,
delay: delay,
Expand Down Expand Up @@ -177,6 +190,8 @@ func init() {
h += " -t, --timeout <millis> Set the HTTP timeout (default: 10000)\n"
h += " -v, --verbose Verbose mode\n"
h += " -X, --method <method> HTTP method (default: GET)\n\n"
h += " -tracker <tracker link> Replaces {tracker} in paths with the specified tracker link\n"
h += " -sh --saveheader <header> Save only responses containing a specific header\n"

h += "Defaults:\n"
h += " pathsFile: ./paths\n"
Expand Down
26 changes: 25 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"sync"
"time"
"strings"
)

const (
Expand Down Expand Up @@ -88,6 +89,10 @@ func main() {
continue
}

if c.saveHeader != "" && !res.hasHeader(c.saveHeader) {
continue
}

if res.err != nil {
fmt.Fprintf(os.Stderr, "request failed: %s\n", res.err)
continue
Expand Down Expand Up @@ -115,10 +120,16 @@ func main() {
// so we should strip that off and add it to
// the beginning of the path.
u, err := url.Parse(host)

if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse host: %s\n", err)
continue
}

if len(c.trackerLink) > 0 {
path = replaceTracker(path, u.Host, c.trackerLink)
}

prefixedPath := u.Path + path
u.Path = ""

Expand All @@ -136,7 +147,7 @@ func main() {
timeout: time.Duration(c.timeout * 1000000),
}
}
}
}

// once all of the requests have been sent we can
// close the requests channel
Expand Down Expand Up @@ -169,6 +180,19 @@ func readLines(filename string) ([]string, error) {
return lines, sc.Err()
}

func replaceTracker(path string, host string, trackerLink string) string {
now := time.Now()
seconds := now.Unix()

host = strings.Split(host, ":")[0]

tracker := fmt.Sprintf("%d.%s.%s", seconds, host, trackerLink)

newPath := strings.Replace(path, "{tracker}", tracker, -1)

return newPath
}

// readLinesOrLiteral tries to read lines from a file, returning
// the arg in a string slice if the file doesn't exist, unless
// the arg matches its default value
Expand Down
10 changes: 10 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path"
"strings"
)

// a response is a wrapper around an HTTP response;
Expand Down Expand Up @@ -89,3 +90,12 @@ func (r response) save(pathPrefix string, noHeaders bool) (string, error) {

return p, nil
}

func (r response) hasHeader(header string) bool {
for _, item := range r.headers {
if strings.TrimSpace(item) == header {
return true
}
}
return false
}