Skip to content

Add random domain and random email functions #353

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

Open
wants to merge 2 commits into
base: main
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
35 changes: 32 additions & 3 deletions random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package random
import (
"crypto/rand"
"math/big"
"strings"

"github.com/vulncheck-oss/go-exploit/output"
)

var (
Expand All @@ -16,7 +19,10 @@ func RandIntRange(rangeMin int, rangeMax int) int {
rangeMaxBig := big.NewInt(int64(rangeMax) - int64(rangeMin))
n, err := rand.Int(rand.Reader, rangeMaxBig)
if err != nil {
panic(err)
// if random fails the process will die anyway: https://github.com/golang/go/issues/66821
output.PrintfFrameworkError("Random generation error: %s", err.Error())

return 0
}

return int(n.Int64() + int64(rangeMin))
Expand All @@ -26,7 +32,10 @@ func RandIntRange(rangeMin int, rangeMax int) int {
func RandPositiveInt(rangeMax int) int {
n, err := rand.Int(rand.Reader, big.NewInt(int64(rangeMax)))
if err != nil {
panic(err)
// if random fails the process will die anyway: https://github.com/golang/go/issues/66821
output.PrintfFrameworkError("Random generation error: %s", err.Error())

return 0
}

return int(n.Int64())
Expand All @@ -42,7 +51,7 @@ func RandLetters(n int) string {
return string(runeSlice)
}

// RandLetters generates a random alpha string with no bad chars of length n.
// RandLettersNoBadChars generates a random alpha string with no bad chars of length n.
// This will return an empty string if the caller badchars all "letters".
func RandLettersNoBadChars(n int, badchars []rune) string {
// rebuild the letters slice without the bad chars. O(n^2) implementation
Expand Down Expand Up @@ -111,3 +120,23 @@ func RandDigits(n int) string {
func RandDigitsRange(rangeMin int, rangeMax int) string {
return RandDigits(RandIntRange(rangeMin, rangeMax))
}

// RandDomain generates a random domain name with a common TLDs. The domain will be between 8 and 14 characters.
func RandDomain() string {
return strings.ToLower(RandLettersRange(4, 10) + "." + CommonTLDs[RandPositiveInt(len(CommonTLDs))])
}

// RandEmail generates a random email address using common domain TLDs. The largest size of the
// generated domain will be 23 characters and smallest will be 13 characters. The goal is not to
// generate a set of RFC valid domains, but simple lower case emails that are valid for most
// automated account registration or usage, so these might be "too simple" or for some uses.
func RandEmail() string {
return strings.ToLower(RandLettersRange(4, 8) + "@" + RandDomain())
}

// CommonTLDs contains the 3 most common DNS TLDs.
var CommonTLDs = []string{
"com",
"org",
"net",
}
25 changes: 25 additions & 0 deletions random/random_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package random

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -140,3 +141,27 @@ func Test_RandDigitsRange(t *testing.T) {
}
}
}

func Test_RandDomain(t *testing.T) {
for range 100 {
r := RandDomain()
if len(r) > 14 || len(r) < 8 {
t.Error("Domain generated with an unexpected length: " + r)
}
if !strings.Contains(r, ".") {
t.Error("Domain generated without expected characters: " + r)
}
}
}

func Test_RandEmail(t *testing.T) {
for range 100 {
r := RandEmail()
if len(r) > 23 || len(r) < 13 {
t.Error("Domain generated with an unexpected length: " + r)
}
if !strings.Contains(r, ".") {
t.Error("Domain generated without expected characters: " + r)
}
}
}