This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
internet.go
283 lines (250 loc) · 6.99 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
package faker
import (
"fmt"
"net"
"reflect"
"strings"
"github.com/bxcodec/faker/v4/pkg/options"
)
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",
}
// GetNetworker returns a new Networker interface of Internet
func GetNetworker(opts ...options.OptionFunc) Networker {
opt := options.BuildOptions(opts)
internet := &Internet{
fakerOption: *opt,
}
return internet
}
// 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 {
fakerOption options.Options
}
func (internet Internet) email() (string, error) {
var err error
var emailName, emailDomain string
if emailName, err = randomString(7, internet.fakerOption); err != nil {
return "", err
}
if emailDomain, err = randomString(7, internet.fakerOption); 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(opts ...options.OptionFunc) string {
return singleFakeData(EmailTag, func() interface{} {
opt := options.BuildOptions(opts)
i := Internet{fakerOption: *opt}
r, err := i.email()
if err != nil {
panic(err.Error())
}
return r
}, opts...).(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(opts ...options.OptionFunc) string {
return singleFakeData(MacAddressTag, func() interface{} {
opt := options.BuildOptions(opts)
i := Internet{fakerOption: *opt}
return i.macAddress()
}, opts...).(string)
}
func (internet Internet) domainName() (string, error) {
domainPart, err := randomString(7, internet.fakerOption)
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(opts ...options.OptionFunc) string {
return singleFakeData(DomainNameTag, func() interface{} {
opt := options.BuildOptions(opts)
i := Internet{fakerOption: *opt}
d, err := i.domainName()
if err != nil {
panic(err.Error())
}
return d
}, opts...).(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(opts ...options.OptionFunc) string {
return singleFakeData(URLTag, func() interface{} {
opt := options.BuildOptions(opts)
i := Internet{fakerOption: *opt}
u, err := i.url()
if err != nil {
panic(err.Error())
}
return u
}, opts...).(string)
}
func (internet Internet) username() (string, error) {
return randomString(7, internet.fakerOption)
}
// UserName generates random username
func (internet Internet) UserName(v reflect.Value) (interface{}, error) {
return internet.username()
}
// Username get username randomly in string
func Username(opts ...options.OptionFunc) string {
return singleFakeData(UserNameTag, func() interface{} {
opt := options.BuildOptions(opts)
i := Internet{fakerOption: *opt}
u, err := i.username()
if err != nil {
panic(err.Error())
}
return u
}, opts...).(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(opts ...options.OptionFunc) string {
return singleFakeData(IPV4Tag, func() interface{} {
opt := options.BuildOptions(opts)
i := Internet{fakerOption: *opt}
return i.ipv4()
}, opts...).(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(opts ...options.OptionFunc) string {
return singleFakeData(IPV6Tag, func() interface{} {
opt := options.BuildOptions(opts)
i := Internet{fakerOption: *opt}
return i.ipv6()
}, opts...).(string)
}
func (internet Internet) password() (string, error) {
return randomString(50, internet.fakerOption)
}
// 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(opts ...options.OptionFunc) string {
return singleFakeData(PASSWORD, func() interface{} {
opt := options.BuildOptions(opts)
i := Internet{fakerOption: *opt}
p, err := i.password()
if err != nil {
panic(err.Error())
}
return p
}, opts...).(string)
}
func (internet Internet) jwt() (string, error) {
element, err := randomString(40, internet.fakerOption)
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(opts ...options.OptionFunc) string {
return singleFakeData(JWT, func() interface{} {
opt := options.BuildOptions(opts)
i := Internet{fakerOption: *opt}
p, err := i.jwt()
if err != nil {
panic(err.Error())
}
return p
}, opts...).(string)
}