-
Notifications
You must be signed in to change notification settings - Fork 3
/
internet.go
170 lines (137 loc) · 4.21 KB
/
internet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package faker
import (
"fmt"
"math"
"regexp"
"strings"
"github.com/corpix/uarand"
)
// Internetier interface
type Internetier interface {
Avatar() string
Color(baseRed255, baseGreen255, baseBlue255 float64) string
DomainName() string
DomainSuffix() string
DomainWord() string
Email(firstName, lastName, provider string) string
ExampleEmail(firstName, lastName string) string
IP() string
IPv6() string
Mac(sep string) string
Protocol() string
URL() string
UserAgent() string
UserName(firstName, lastName string) string
}
// Internet struct
type Internet struct {
*Fake
}
// Avatar returns a URL for an avatar.
func (i *Internet) Avatar() string {
return i.pick(internetPrefix + "/avatar_uri")
}
// Color returns a random hexadecimal color.
// based on awesome response : http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette
func (i *Internet) Color(baseRed255, baseGreen255, baseBlue255 float64) string {
red := math.Floor((float64(random(256)) + baseRed255) / 2)
green := math.Floor((float64(random(256)) + baseRed255) / 2)
blue := math.Floor((float64(random(256)) + baseRed255) / 2)
redStr := fmt.Sprintf("%02X", int(red))
greenStr := fmt.Sprintf("%02X", int(green))
blueStr := fmt.Sprintf("%02X", int(blue))
return "#" + redStr + greenStr + blueStr
}
// DomainName returns a random domain name.
func (i *Internet) DomainName() string {
return i.Internet().DomainWord() + "." + i.Internet().DomainSuffix()
}
// DomainSuffix returns a random domain suffix.
func (i *Internet) DomainSuffix() string {
return i.pick(internetPrefix + "/domain_suffix")
}
// DomainWord returns a random domain word.
func (i *Internet) DomainWord() string {
re := regexp.MustCompile(`([\\~#&*{}/:<>?|\"'])`)
return strings.ToLower(re.ReplaceAllString(i.Name().FirstName(), ""))
}
// Email returns a valid email address
func (i *Internet) Email(firstName, lastName, provider string) string {
if provider == "" {
provider = i.pick(internetPrefix + "/free_email")
}
return slugify(strings.ToLower(i.Internet().UserName(firstName, lastName))) + "@" + provider
}
// ExampleEmail Provides an email example
func (i *Internet) ExampleEmail(firstName, lastName string) string {
provider := i.pick(internetPrefix + "/example_email")
return i.Internet().Email(firstName, lastName, provider)
}
// IP returns a random IP
func (i *Internet) IP() string {
result := []string{}
for i := 0; i < 4; i++ {
result = append(result, string(random(255)))
}
return strings.Join(result, ".")
}
// IPv6 returns a random IPv6 address
func (i *Internet) IPv6() string {
result := []string{}
for i := 0; i < 8; i++ {
result = append(result, randHash())
}
return strings.Join(result, ".")
}
// Mac returns a random mac address.
func (i *Internet) Mac(sep string) string {
mac := ""
validSep := ":"
// if the client passed in a different separator than `:`,
// we will use it if it is in the list of acceptable separators (dash or no separator)
acceptedSep := strslice{"-", ""}
if acceptedSep.indexOf(sep) != -1 {
validSep = sep
}
for i := 0; i < 12; i++ {
mac += strings.TrimSpace(fmt.Sprintf("%2x", random(15)))
if (i%2 == 1) && (i != 11) {
mac += validSep
}
}
return mac
}
// Protocol Randomly returns http or https
func (i *Internet) Protocol() string {
protocols := []string{"http", "https"}
return selectElement(protocols)
}
// URL returns a random url.
func (i *Internet) URL() string {
return i.Internet().Protocol() + "://" + i.Internet().DomainName()
}
// UserAgent returns a random user agent
func (i *Internet) UserAgent() string {
return uarand.GetRandom()
}
// UserName returns a username based on one of several patterns
func (i *Internet) UserName(firstName, lastName string) string {
var result string
if firstName == "" {
firstName = i.Name().FirstName()
}
if lastName == "" {
lastName = i.Name().LastName()
}
switch random(2) {
case 0:
result = firstName + string(random(99))
case 1:
result = firstName + selectElement([]string{".", "_"}) + lastName
case 2:
result = firstName + selectElement([]string{".", "_"}) + lastName + string(random(99))
}
result = strings.Replace(result, "'", "", -1)
result = strings.Replace(result, " ", "", -1)
return result
}