forked from bxcodec/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internet.go
279 lines (245 loc) · 6.17 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
package faker
import (
"fmt"
"math/rand"
"net"
"reflect"
"strings"
)
var tld = []string{"com", "biz", "info", "net", "org", "ru"}
var urlFormats = []string{
"http://www.%s/",
"https://www.%s/",
"http://%s/",
"https://%s/",
"http://www.%s/%s",
"https://www.%s/%s",
"http://%s/%s",
"https://%s/%s",
"http://%s/%s.html",
"https://%s/%s.html",
"http://%s/%s.php",
"https://%s/%s.php",
}
var internet Networker
// GetNetworker returns a new Networker interface of Internet
func GetNetworker() Networker {
mu.Lock()
defer mu.Unlock()
if internet == nil {
internet = &Internet{}
}
return internet
}
// SetNetwork sets custom Network
func SetNetwork(net Networker) {
internet = net
}
// Networker is logical layer for Internet
type Networker interface {
Email(v reflect.Value) (interface{}, error)
MacAddress(v reflect.Value) (interface{}, error)
DomainName(v reflect.Value) (interface{}, error)
URL(v reflect.Value) (interface{}, error)
UserName(v reflect.Value) (interface{}, error)
IPv4(v reflect.Value) (interface{}, error)
IPv6(v reflect.Value) (interface{}, error)
Password(v reflect.Value) (interface{}, error)
Jwt(v reflect.Value) (interface{}, error)
}
// Internet struct
type Internet struct{}
func (internet Internet) email() (string, error) {
var err error
var emailName, emailDomain string
if emailName, err = randomString(7, &LangENG); err != nil {
return "", err
}
if emailDomain, err = randomString(7, &LangENG); err != nil {
return "", err
}
return (emailName + "@" + emailDomain + "." + randomElementFromSliceString(tld)), nil
}
// Email generates random email id
func (internet Internet) Email(v reflect.Value) (interface{}, error) {
return internet.email()
}
// Email get email randomly in string
func Email() string {
return singleFakeData(EmailTag, func() interface{} {
i := Internet{}
r, err := i.email()
if err != nil {
panic(err.Error())
}
return r
}).(string)
}
func (internet Internet) macAddress() string {
ip := make([]byte, 6)
for i := 0; i < 6; i++ {
ip[i] = byte(rand.Intn(256))
}
return net.HardwareAddr(ip).String()
}
// MacAddress generates random MacAddress
func (internet Internet) MacAddress(v reflect.Value) (interface{}, error) {
return internet.macAddress(), nil
}
// MacAddress get mac address randomly in string
func MacAddress() string {
return singleFakeData(MacAddressTag, func() interface{} {
i := Internet{}
return i.macAddress()
}).(string)
}
func (internet Internet) domainName() (string, error) {
domainPart, err := randomString(7, &LangENG)
if err != nil {
return "", err
}
return (domainPart + "." + randomElementFromSliceString(tld)), nil
}
// DomainName generates random domain name
func (internet Internet) DomainName(v reflect.Value) (interface{}, error) {
return internet.domainName()
}
// DomainName get email domain name in string
func DomainName() string {
return singleFakeData(DomainNameTag, func() interface{} {
i := Internet{}
d, err := i.domainName()
if err != nil {
panic(err.Error())
}
return d
}).(string)
}
func (internet Internet) url() (string, error) {
format := randomElementFromSliceString(urlFormats)
countVerbs := strings.Count(format, "%s")
d, err := internet.domainName()
if err != nil {
return "", nil
}
if countVerbs == 1 {
return fmt.Sprintf(format, d), nil
}
u, err := internet.username()
if err != nil {
return "", err
}
return fmt.Sprintf(format, d, u), nil
}
// URL generates random URL standardized in urlFormats const
func (internet Internet) URL(v reflect.Value) (interface{}, error) {
return internet.url()
}
// URL get Url randomly in string
func URL() string {
return singleFakeData(URLTag, func() interface{} {
i := Internet{}
u, err := i.url()
if err != nil {
panic(err.Error())
}
return u
}).(string)
}
func (internet Internet) username() (string, error) {
return randomString(7, &LangENG)
}
// UserName generates random username
func (internet Internet) UserName(v reflect.Value) (interface{}, error) {
return internet.username()
}
// Username get username randomly in string
func Username() string {
return singleFakeData(UserNameTag, func() interface{} {
i := Internet{}
u, err := i.username()
if err != nil {
panic(err.Error())
}
return u
}).(string)
}
func (internet Internet) ipv4() string {
size := 4
ip := make([]byte, size)
for i := 0; i < size; i++ {
ip[i] = byte(rand.Intn(256))
}
return net.IP(ip).To4().String()
}
// IPv4 generates random IPv4 address
func (internet Internet) IPv4(v reflect.Value) (interface{}, error) {
return internet.ipv4(), nil
}
// IPv4 get IPv4 randomly in string
func IPv4() string {
return singleFakeData(IPV4Tag, func() interface{} {
i := Internet{}
return i.ipv4()
}).(string)
}
func (internet Internet) ipv6() string {
size := 16
ip := make([]byte, size)
for i := 0; i < size; i++ {
ip[i] = byte(rand.Intn(256))
}
return net.IP(ip).To16().String()
}
// IPv6 generates random IPv6 address
func (internet Internet) IPv6(v reflect.Value) (interface{}, error) {
return internet.ipv6(), nil
}
// IPv6 get IPv6 randomly in string
func IPv6() string {
return singleFakeData(IPV6Tag, func() interface{} {
i := Internet{}
return i.ipv6()
}).(string)
}
func (internet Internet) password() (string, error) {
return randomString(50, &LangENG)
}
// Password returns a hashed password
func (internet Internet) Password(v reflect.Value) (interface{}, error) {
return internet.password()
}
// Password get password randomly in string
func Password() string {
return singleFakeData(PASSWORD, func() interface{} {
i := Internet{}
p, err := i.password()
if err != nil {
panic(err.Error())
}
return p
}).(string)
}
func (internet Internet) jwt() (string, error) {
element, err := randomString(40, &LangENG)
sl := element[:]
if err != nil {
return "", err
}
return strings.Join([]string{sl, sl, sl}, "."), nil
}
// Jwt returns a jwt-like random string in xxxx.yyyy.zzzz style
func (internet Internet) Jwt(v reflect.Value) (interface{}, error) {
return internet.jwt()
}
// Jwt get jwt-like string
func Jwt() string {
return singleFakeData(JWT, func() interface{} {
i := Internet{}
p, err := i.jwt()
if err != nil {
panic(err.Error())
}
return p
}).(string)
}