forked from mpchadwick/dbanon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
149 lines (120 loc) · 3.29 KB
/
provider.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
package dbanon
import (
"regexp"
"strings"
"math/rand"
"syreclabs.com/go/faker"
"github.com/google/uuid"
)
type Provider struct {}
func NewProvider() *Provider {
p := &Provider{}
return p
}
type ProviderInterface interface {
Get(fakeType string, currentValue *string) string
}
func (p Provider) Get(fakeType string, currentValue *string) string {
if (strings.HasPrefix(fakeType, "dynamic")) {
return p.processDynamicFake(fakeType, currentValue)
}
switch fakeType {
// Name
case "first_name":
return faker.Name().FirstName()
case "last_name":
return faker.Name().LastName()
case "full_name":
return faker.Name().FirstName() + " " + faker.Name().LastName()
// Company
case "company_name":
return faker.Company().Name()
// Internet
case "username":
return faker.Internet().UserName()
case "password":
return faker.Internet().Password(8, 14)
case "ipv4":
return faker.Internet().IpV4Address()
case "url":
return faker.Internet().Url()
case "linkedin_url":
return "https://linkedin.com/in/" + faker.Internet().UserName()
case "md5":
return faker.Lorem().Characters(32)
case "uuid":
return uuid.NewString()
case "json":
return p.fakeJson()
case "query_params":
return p.fakeQueryParams()
// Dates
case "datetime":
return faker.Date().Birthday(0, 40).Format("2006-01-02 15:04:05")
// Geo
case "state":
return faker.Address().State()
case "city":
return faker.Address().City()
case "postcode":
return faker.Address().Postcode()
case "street":
return faker.Address().StreetAddress()
case "country_code":
return faker.Address().CountryCode()
// Currency
case "money":
numberOfDigits := rand.Intn(5 - 1) + 1
return faker.Number().Number(numberOfDigits)
case "money_decimal":
numberOfDigits := rand.Intn(5 - 1) + 1
return faker.Number().Decimal(numberOfDigits, 2)
// Contact
case "email":
return faker.Internet().Email()
case "telephone":
return faker.PhoneNumber().PhoneNumber()
}
logger := GetLogger()
logger.Error(fakeType + " does not match any known type")
return ""
}
func (p Provider) fakeJson() string {
return "{\"fake\": true}"
}
func (p Provider) fakeQueryParams() string {
return "?fake=true"
}
func (p Provider) processDynamicFake(fakeType string, currentValue *string) string {
// strip dynamic from fakeType
fakeType = strings.ReplaceAll(fakeType, "dynamic.", "")
var args []string
args, fakeType = p.processDynamicArgs(fakeType)
switch fakeType {
case "email":
return p.dynamicEmail(currentValue, args)
}
logger := GetLogger()
logger.Error(fakeType + " does not match any known dynamic type")
return ""
}
func (p Provider) processDynamicArgs(fakeType string) ([]string, string) {
reg := regexp.MustCompile(`\((.*?)\)`)
args := string(reg.Find([]byte(fakeType)))
fakeType = strings.ReplaceAll(fakeType, args, "")
args = strings.TrimPrefix(args, "(")
args = strings.TrimSuffix(args, ")")
return strings.Split(args, ","), fakeType
}
func (p Provider) dynamicEmail(currentValue *string, args []string) string {
if len(args) < 1 || args[0] == "" {
return faker.Internet().Email()
}
// args[0] is email we wish to not fake
ignoreEmail := args[0]
val := *currentValue
if strings.HasSuffix(val, ignoreEmail) {
return val
}
return faker.Internet().Email()
}