forked from jaswdr/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internet.go
294 lines (235 loc) · 9.52 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package faker
import (
"net/http"
"regexp"
"strconv"
"strings"
"sync"
)
var (
freeEmailDomain = []string{"gmail.com", "yahoo.com", "hotmail.com"}
tld = []string{"com", "com", "com", "com", "com", "com", "biz", "info", "net", "org"}
emailFormats = []string{"{{user}}@{{domain}}", "{{user}}@{{freeEmailDomain}}"}
urlFormats = []string{
"http://www.{{domain}}/",
"http://{{domain}}/",
"http://www.{{domain}}/{{slug}}",
"http://www.{{domain}}/{{slug}}",
"https://www.{{domain}}/{{slug}}",
"http://www.{{domain}}/{{slug}}.html",
"http://{{domain}}/{{slug}}",
"http://{{domain}}/{{slug}}",
"http://{{domain}}/{{slug}}.html",
"https://{{domain}}/{{slug}}.html",
}
statusCodes = []string{"100", "101", "102", "200", "201", "202", "203", "204", "205", "206", "207", "208", "226", "300", "301", "302", "303", "304", "305", "306", "307", "308", "400", "401", "402", "403", "404", "405", "406", "407", "408", "409", "410", "411", "412", "413", "414", "415", "416", "417", "418", "420", "422", "423", "424", "425", "426", "428", "429", "431", "444", "449", "450", "451", "499", "500", "501", "502", "503", "504", "505", "506", "507", "508", "509", "510", "511", "598", "599"}
statusCodeMessages = []string{"Continue", "Switching Protocols", "Processing (WebDAV)", "OK", "Created", "Accepted", "Non-Authoritative Information", "No Content", "Reset Content", "Partial Content", "Multi-Status (WebDAV)", "Already Reported (WebDAV)", "IM Used", "Multiple Choices", "Moved Permanently", "Found", "See Other", "Not Modified", "Use Proxy", "(Unused)", "Temporary Redirect", "Permanent Redirect (experimental)", "Bad Request", "Unauthorized", "Payment Required", "Forbidden", "Not Found", "Method Not Allowed", "Not Acceptable", "Proxy Authentication Required", "Request Timeout", "Conflict", "Gone", "Length Required", "Precondition Failed", "Request Entity Too Large", "Request-URI Too Long", "Unsupported Media Type", "Requested Range Not Satisfiable", "Expectation Failed", "I'm a teapot (RFC 2324)", "Enhance Your Calm (Twitter)", "Unprocessable Entity (WebDAV)", "Locked (WebDAV)", "Failed Dependency (WebDAV)", "Reserved for WebDAV", "Upgrade Required", "Precondition Required", "Too Many Requests", "Request Header Fields Too Large", "No Response (Nginx)", "Retry With (Microsoft)", "Blocked by Windows Parental Controls (Microsoft)", "Unavailable For Legal Reasons", "Client Closed Request (Nginx)", "Internal Server Error", "Not Implemented", "Bad Gateway", "Service Unavailable", "Gateway Timeout", "HTTP Version Not Supported", "Variant Also Negotiates (Experimental)", "Insufficient Storage (WebDAV)", "Loop Detected (WebDAV)", "Bandwidth Limit Exceeded (Apache)", "Not Extended", "Network Authentication Required", "Network read timeout error", "Network connect timeout error"}
)
// Internet is a faker struct for Internet
type Internet struct {
Faker *Faker
}
var validEmailOnlyValidCharacters = regexp.MustCompile(`[^a-z0-9._%+\-]+`)
func transformIntoValidEmailName(name string) string {
name = strings.ToLower(name)
name = validEmailOnlyValidCharacters.ReplaceAllString(name, "_")
return name
}
var (
cacheInternetUserFirstName []string
cacheInternetUserLastName []string
cacheInternetUserOnce sync.Once
cacheInternetUserFunc = func() {
cacheFirstNamesOnce.Do(cacheFirstNamesFunc)
for _, fn := range cacheFirstNames {
cacheInternetUserFirstName = append(cacheInternetUserFirstName, transformIntoValidEmailName(fn))
}
for _, ln := range lastName {
cacheInternetUserLastName = append(cacheInternetUserLastName, transformIntoValidEmailName(ln))
}
}
)
// User returns a fake user for Internet
func (i Internet) User() string {
cacheInternetUserOnce.Do(cacheInternetUserFunc)
variant := i.Faker.IntBetween(0, 3)
switch variant {
case 0:
ln := i.Faker.RandomStringElement(cacheInternetUserLastName)
fn := i.Faker.RandomStringElement(cacheInternetUserFirstName)
return ln + "." + fn
case 1:
fn := i.Faker.RandomStringElement(cacheInternetUserFirstName)
ln := i.Faker.RandomStringElement(cacheInternetUserLastName)
return fn + "." + ln
case 2:
fn := i.Faker.RandomStringElement(cacheInternetUserFirstName)
return fn
case 3:
ln := i.Faker.RandomStringElement(cacheInternetUserLastName)
return ln
default:
panic("bad")
}
}
// Password returns a fake password for Internet
func (i Internet) Password() string {
pattern := strings.Repeat("*", i.Faker.IntBetween(6, 16))
return i.Faker.Asciify(pattern)
}
// Domain returns a fake domain for Internet
func (i Internet) Domain() string {
domain := strings.ToLower(i.Faker.Lexify("???"))
return domain + "." + i.TLD()
}
// FreeEmailDomain returns a fake free email domain for Internet
func (i Internet) FreeEmailDomain() string {
return i.Faker.RandomStringElement(freeEmailDomain)
}
// SafeEmailDomain returns a fake safe email domain for Internet
func (Internet) SafeEmailDomain() string {
return "example.org"
}
// Email returns a fake email address for Internet
func (i Internet) Email() string {
email := i.Faker.RandomStringElement(emailFormats)
// {{user}}
email = strings.Replace(email, "{{user}}", transformIntoValidEmailName(i.User()), 1)
// {{domain}}
email = strings.Replace(email, "{{domain}}", i.Domain(), 1)
// {{freeEmailDomain}}
email = strings.Replace(email, "{{freeEmailDomain}}", i.FreeEmailDomain(), 1)
return email
}
// FreeEmail returns a fake free email address for Internet
func (i Internet) FreeEmail() string {
domain := i.Faker.RandomStringElement(freeEmailDomain)
return transformIntoValidEmailName(i.User()) + "@" + domain
}
// SafeEmail returns a fake safe email address for Internet
func (i Internet) SafeEmail() string {
return transformIntoValidEmailName(i.User()) + "@" + i.SafeEmailDomain()
}
// CompanyEmail returns a fake company email address for Internet
func (i Internet) CompanyEmail() string {
c := i.Faker.Company()
companyName := transformIntoValidEmailName(c.Name())
domain := companyName + "." + i.Domain()
return transformIntoValidEmailName(i.User()) + "@" + domain
}
// TLD returns a fake tld for Internet
func (i Internet) TLD() string {
return i.Faker.RandomStringElement(tld)
}
// Slug returns a fake slug for Internet
func (i Internet) Slug() string {
slug := strings.Repeat("?", i.Faker.IntBetween(1, 5)) +
"-" +
strings.Repeat("?", i.Faker.IntBetween(1, 6))
return strings.ToLower(i.Faker.Lexify(slug))
}
// URL returns a fake url for Internet
func (i Internet) URL() string {
url := i.Faker.RandomStringElement(urlFormats)
// {{domain}}
url = strings.Replace(url, "{{domain}}", i.Domain(), 1)
// {{slug}}
url = strings.Replace(url, "{{slug}}", i.Slug(), 1)
return url
}
// Ipv4 returns a fake ipv4 for Internet
func (i Internet) Ipv4() string {
ips := make([]string, 0, 4)
ips = append(ips, strconv.Itoa(i.Faker.IntBetween(1, 255)))
for j := 0; j < 3; j++ {
ips = append(ips, strconv.Itoa(i.Faker.IntBetween(0, 255)))
}
return strings.Join(ips, ".")
}
// LocalIpv4 returns a fake local ipv4 for Internet
func (i Internet) LocalIpv4() string {
ips := make([]string, 0, 4)
ips = append(ips, i.Faker.RandomStringElement([]string{"10", "172", "192"}))
if ips[0] == "10" {
for j := 0; j < 3; j++ {
ips = append(ips, strconv.Itoa(i.Faker.IntBetween(0, 255)))
}
}
if ips[0] == "172" {
ips = append(ips, strconv.Itoa(i.Faker.IntBetween(16, 31)))
for j := 0; j < 2; j++ {
ips = append(ips, strconv.Itoa(i.Faker.IntBetween(0, 255)))
}
}
if ips[0] == "192" {
ips = append(ips, "168")
for j := 0; j < 2; j++ {
ips = append(ips, strconv.Itoa(i.Faker.IntBetween(0, 255)))
}
}
return strings.Join(ips, ".")
}
// Ipv6 returns a fake ipv6 for Internet
func (i Internet) Ipv6() string {
ips := make([]string, 0, 8)
for j := 0; j < 8; j++ {
block := ""
for w := 0; w < 4; w++ {
block = block + strconv.Itoa(i.Faker.RandomDigitNotNull())
}
ips = append(ips, block)
}
return strings.Join(ips, ":")
}
// MacAddress returns a fake mac address for Internet
func (i Internet) MacAddress() string {
values := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
mac := make([]string, 0, 6)
for j := 0; j < 6; j++ {
m := i.Faker.RandomStringElement(values)
m = m + i.Faker.RandomStringElement(values)
mac = append(mac, m)
}
return strings.Join(mac, ":")
}
// HTTPMethod returns a fake http method for Internet
func (i Internet) HTTPMethod() string {
return i.Faker.RandomStringElement([]string{
http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodConnect,
http.MethodOptions,
http.MethodTrace,
})
}
// Query returns a fake query for Internet
func (i Internet) Query() string {
lorem := i.Faker.Lorem()
query := "?" + lorem.Word() + "=" + lorem.Word()
for j := 0; j < i.Faker.IntBetween(1, 3); j++ {
if i.Faker.Boolean().Bool() {
query += "&" + lorem.Word() + "=" + lorem.Word()
} else {
query += "&" + lorem.Word() + "=" + strconv.Itoa(i.Faker.RandomDigitNotNull())
}
}
return query
}
// StatusCode returns a fake status code for Internet
func (i Internet) StatusCode() int {
statusCode, _ := strconv.Atoi(i.Faker.RandomStringElement(statusCodes))
return statusCode
}
// StatusCodeMessage returns a fake status code message for Internet
func (i Internet) StatusCodeMessage() string {
return i.Faker.RandomStringElement(statusCodeMessages)
}
// StatusCodeWithMessage returns a fake status code with message for Internet
func (i Internet) StatusCodeWithMessage() string {
index := i.Faker.IntBetween(0, len(statusCodes)-1)
return statusCodes[index] + " " + statusCodeMessages[index]
}