Skip to content

Commit

Permalink
some random templates
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Mar 27, 2022
1 parent 9319c18 commit d38301e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/examples/http-login.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
},
"body": "username=testuser{{random_int}}&password={{random_payload_byte 10 | base64_encode}}"
"body": "username={{random_alphanum 10}}&password={{random_payload_byte (random_int_n 5 | add 5) | base64_encode}}"
}
}
}
Expand Down
34 changes: 33 additions & 1 deletion src/utils/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,37 @@ func randomUUID() string {
return uuid.New().String()
}

func mod(lhs, rhs uint32) uint32 {
func randomChar(from string) byte {
if len(from) == 0 {
return 0
}
return from[rand.Intn(len(from))]
}

func randomString(n int, from string) string {
b := make([]byte, n)
for i := range b {
b[i] = randomChar(from)
}
return string(b)
}

func randomAlpha(n int) string {
return randomString(n, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
}

func randomAplhaNum(n int) string {
return randomString(n, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
}

func mod(lhs, rhs int) int {
return lhs % rhs
}

func add(lhs, rhs int) int {
return lhs + rhs
}

// ContextKey used to work with context and not trigger linter
type ContextKey string

Expand All @@ -82,6 +109,10 @@ func Parse(input string) (*template.Template, error) {
// TODO: consider adding ability to populate custom data
return template.New("tpl").Funcs(template.FuncMap{
"random_uuid": randomUUID,
"random_char": randomChar,
"random_string": randomString,
"random_alpha": randomAlpha,
"random_alphanum": randomAplhaNum,
"random_int_n": rand.Intn,
"random_int": rand.Int,
"random_payload": RandomPayload,
Expand Down Expand Up @@ -110,6 +141,7 @@ func Parse(input string) (*template.Template, error) {
"split": strings.Split,
"get_url": getURLContent,
"mod": mod,
"add": add,
"ctx_key": ctxKey,
"cookie_string": cookieString,
}).Parse(strings.ReplaceAll(input, "\\", ""))
Expand Down

0 comments on commit d38301e

Please sign in to comment.