-
Notifications
You must be signed in to change notification settings - Fork 16
/
config.go
247 lines (220 loc) · 11.4 KB
/
config.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
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: AGPL
package shared
import (
"embed"
"fmt"
"github.com/gobwas/glob"
"github.com/pb33f/harhar"
"log/slog"
"regexp"
)
type WiretapConfiguration struct {
Contract string `json:"-" yaml:"-"`
RedirectHost string `json:"redirectHost,omitempty" yaml:"redirectHost,omitempty"`
RedirectPort string `json:"redirectPort,omitempty" yaml:"redirectPort,omitempty"`
RedirectBasePath string `json:"redirectBasePath,omitempty" yaml:"redirectBasePath,omitempty"`
RedirectProtocol string `json:"redirectProtocol,omitempty" yaml:"redirectProtocol,omitempty"`
RedirectURL string `json:"redirectURL,omitempty" yaml:"redirectURL,omitempty"`
Port string `json:"port,omitempty" yaml:"port,omitempty"`
MonitorPort string `json:"monitorPort,omitempty" yaml:"monitorPort,omitempty"`
WebSocketHost string `json:"webSocketHost,omitempty" yaml:"webSocketHost,omitempty"`
WebSocketPort string `json:"webSocketPort,omitempty" yaml:"webSocketPort,omitempty"`
GlobalAPIDelay int `json:"globalAPIDelay,omitempty" yaml:"globalAPIDelay,omitempty"`
StaticDir string `json:"staticDir,omitempty" yaml:"staticDir,omitempty"`
StaticIndex string `json:"staticIndex,omitempty" yaml:"staticIndex,omitempty"`
PathConfigurations map[string]*WiretapPathConfig `json:"paths,omitempty" yaml:"paths,omitempty"`
Headers *WiretapHeaderConfig `json:"headers,omitempty" yaml:"headers,omitempty"`
StaticPaths []string `json:"staticPaths,omitempty" yaml:"staticPaths,omitempty"`
Variables map[string]string `json:"variables,omitempty" yaml:"variables,omitempty"`
Spec string `json:"contract,omitempty" yaml:"contract,omitempty"`
Certificate string `json:"certificate,omitempty" yaml:"certificate,omitempty"`
CertificateKey string `json:"certificateKey,omitempty" yaml:"certificateKey,omitempty"`
HardErrors bool `json:"hardValidation,omitempty" yaml:"hardValidation,omitempty"`
HardErrorCode int `json:"hardValidationCode,omitempty" yaml:"hardValidationCode,omitempty"`
HardErrorReturnCode int `json:"hardValidationReturnCode,omitempty" yaml:"hardValidationReturnCode,omitempty"`
PathDelays map[string]int `json:"pathDelays,omitempty" yaml:"pathDelays,omitempty"`
MockMode bool `json:"mockMode,omitempty" yaml:"mockMode,omitempty"`
MockModePretty bool `json:"mockModePretty,omitempty" yaml:"mockModePretty,omitempty"`
Base string `json:"base,omitempty" yaml:"base,omitempty"`
HAR string `json:"har,omitempty" yaml:"har,omitempty"`
HARValidate bool `json:"harValidate,omitempty" yaml:"harValidate,omitempty"`
HARPathAllowList []string `json:"harPathAllowList,omitempty" yaml:"harPathAllowList,omitempty"`
StreamReport bool `json:"streamReport,omitempty" yaml:"streamReport,omitempty"`
ReportFile string `json:"reportFilename,omitempty" yaml:"reportFilename,omitempty"`
IgnoreRedirects []string `json:"ignoreRedirects,omitempty" yaml:"ignoreRedirects,omitempty"`
RedirectAllowList []string `json:"redirectAllowList,omitempty" yaml:"redirectAllowList,omitempty"`
WebsocketConfigs map[string]*WiretapWebsocketConfig `json:"websockets" yaml:"websockets"`
IgnoreValidation []string `json:"ignoreValidation,omitempty" yaml:"ignoreValidation,omitempty"`
ValidationAllowList []string `json:"validationAllowList,omitempty" yaml:"validationAllowList,omitempty"`
StrictRedirectLocation bool `json:"strictRedirectLocation,omitempty" yaml:"strictRedirectLocation,omitempty"`
HARFile *harhar.HAR `json:"-" yaml:"-"`
CompiledPathDelays map[string]*CompiledPathDelay `json:"-" yaml:"-"`
CompiledVariables map[string]*CompiledVariable `json:"-" yaml:"-"`
Version string `json:"-" yaml:"-"`
StaticPathsCompiled []glob.Glob `json:"-" yaml:"-"`
CompiledPaths map[string]*CompiledPath `json:"-"`
CompiledIgnoreRedirects []*CompiledRedirect `json:"-" yaml:"-"`
CompiledRedirectAllowList []*CompiledRedirect `json:"-" yaml:"-"`
CompiledIgnoreValidations []*CompiledRedirect `json:"-" yaml:"-"`
CompiledValidationAllowList []*CompiledRedirect `json:"-" yaml:"-"`
FS embed.FS `json:"-"`
Logger *slog.Logger
}
func (wtc *WiretapConfiguration) CompilePaths() {
wtc.CompiledPaths = make(map[string]*CompiledPath)
for x := range wtc.PathConfigurations {
wtc.CompiledPaths[x] = wtc.PathConfigurations[x].Compile(x)
}
if len(wtc.StaticPaths) > 0 {
comp := make([]glob.Glob, len(wtc.StaticPaths))
for x, path := range wtc.StaticPaths {
comp[x] = glob.MustCompile(path)
}
wtc.StaticPathsCompiled = comp
}
}
func (wtc *WiretapConfiguration) CompilePathDelays() {
wtc.CompiledPathDelays = make(map[string]*CompiledPathDelay)
for k, v := range wtc.PathDelays {
compiled := &CompiledPathDelay{
CompiledPathDelay: glob.MustCompile(wtc.ReplaceWithVariables(k)),
PathDelayValue: v,
}
wtc.CompiledPathDelays[k] = compiled
}
}
func (wtc *WiretapConfiguration) CompileVariables() {
wtc.CompiledVariables = make(map[string]*CompiledVariable)
for x := range wtc.Variables {
compiled := &CompiledVariable{
CompiledVariable: regexp.MustCompile(fmt.Sprintf("\\${(%s)}", x)),
VariableValue: wtc.Variables[x],
}
wtc.CompiledVariables[x] = compiled
}
}
func (wtc *WiretapConfiguration) CompileIgnoreRedirects() {
wtc.CompiledIgnoreRedirects = make([]*CompiledRedirect, 0)
for _, x := range wtc.IgnoreRedirects {
compiled := &CompiledRedirect{
CompiledPath: glob.MustCompile(wtc.ReplaceWithVariables(x)),
}
wtc.CompiledIgnoreRedirects = append(wtc.CompiledIgnoreRedirects, compiled)
}
}
func (wtc *WiretapConfiguration) CompileRedirectAllowList() {
wtc.CompiledRedirectAllowList = make([]*CompiledRedirect, 0)
for _, x := range wtc.RedirectAllowList {
compiled := &CompiledRedirect{
CompiledPath: glob.MustCompile(wtc.ReplaceWithVariables(x)),
}
wtc.CompiledRedirectAllowList = append(wtc.CompiledRedirectAllowList, compiled)
}
}
func (wtc *WiretapConfiguration) CompileIgnoreValidations() {
wtc.CompiledIgnoreValidations = make([]*CompiledRedirect, 0)
for _, x := range wtc.IgnoreValidation {
compiled := &CompiledRedirect{
CompiledPath: glob.MustCompile(wtc.ReplaceWithVariables(x)),
}
wtc.CompiledIgnoreValidations = append(wtc.CompiledIgnoreValidations, compiled)
}
}
func (wtc *WiretapConfiguration) CompileValidationAllowList() {
wtc.CompiledValidationAllowList = make([]*CompiledRedirect, 0)
for _, x := range wtc.ValidationAllowList {
compiled := &CompiledRedirect{
CompiledPath: glob.MustCompile(wtc.ReplaceWithVariables(x)),
}
wtc.CompiledValidationAllowList = append(wtc.CompiledValidationAllowList, compiled)
}
}
func (wtc *WiretapConfiguration) ReplaceWithVariables(input string) string {
for x := range wtc.Variables {
if wtc.Variables[x] != "" && wtc.CompiledVariables[x] != nil {
input = wtc.CompiledVariables[x].CompiledVariable.
ReplaceAllString(input, wtc.CompiledVariables[x].VariableValue)
}
}
return input
}
func (wtc *WiretapConfiguration) GetHttpProtocol() string {
protocol := "http"
if wtc.CertificateKey != "" && wtc.Certificate != "" {
protocol = "https"
}
return protocol
}
func (wtc *WiretapConfiguration) GetApiGateway() string {
return fmt.Sprintf("%s://%s", wtc.GetHttpProtocol(), wtc.GetApiGatewayHost())
}
func (wtc *WiretapConfiguration) GetApiGatewayHost() string {
return fmt.Sprintf("localhost:%s", wtc.Port)
}
func (wtc *WiretapConfiguration) GetMonitorUI() string {
return fmt.Sprintf("%s://localhost:%s", wtc.GetHttpProtocol(), wtc.MonitorPort)
}
type WiretapWebsocketConfig struct {
VerifyCert *bool `json:"verifyCert" yaml:"verifyCert"`
DropHeaders []string `json:"dropHeaders" yaml:"dropHeaders"`
}
type WiretapPathConfig struct {
Target string `json:"target,omitempty" yaml:"target,omitempty"`
PathRewrite map[string]string `json:"pathRewrite,omitempty" yaml:"pathRewrite,omitempty"`
ChangeOrigin bool `json:"changeOrigin,omitempty" yaml:"changeOrigin,omitempty"`
Headers *WiretapHeaderConfig `json:"headers,omitempty" yaml:"headers,omitempty"`
Secure bool `json:"secure,omitempty" yaml:"secure,omitempty"`
Auth string `json:"auth,omitempty" yaml:"auth,omitempty"`
CompiledPath *CompiledPath `json:"-"`
}
type CompiledPath struct {
PathConfig *WiretapPathConfig
CompiledKey glob.Glob
CompiledTarget glob.Glob
CompiledPathRewrite map[string]*regexp.Regexp
}
type CompiledPathDelay struct {
CompiledPathDelay glob.Glob
PathDelayValue int
}
type CompiledVariable struct {
CompiledVariable *regexp.Regexp
VariableValue string
}
type CompiledPathRewrite struct {
PathConfig *WiretapPathConfig
Key string
CompiledKey glob.Glob
CompiledTarget glob.Glob
}
type CompiledRedirect struct {
CompiledPath glob.Glob
}
type WiretapHeaderConfig struct {
DropHeaders []string `json:"drop,omitempty" yaml:"drop,omitempty"`
InjectHeaders map[string]string `json:"inject,omitempty" yaml:"inject,omitempty"`
RewriteHeaders map[string]string `json:"rewrite,omitempty" yaml:"rewrite,omitempty"`
}
func (wpc *WiretapPathConfig) Compile(key string) *CompiledPath {
cp := &CompiledPath{
PathConfig: wpc,
CompiledKey: glob.MustCompile(key),
CompiledTarget: glob.MustCompile(wpc.Target),
}
wpc.CompiledPath = cp
cp.CompiledPathRewrite = make(map[string]*regexp.Regexp)
for x := range wpc.PathRewrite {
cp.CompiledPathRewrite[x] = regexp.MustCompile(x)
}
return cp
}
const ConfigKey = "config"
const HARKey = "har"
const WiretapHostPlaceholder = "%WIRETAP_HOST%"
const WiretapPortPlaceholder = "%WIRETAP_PORT%"
const WiretapTLSPlaceholder = "%WIRETAP_TLS%"
const WiretapVersionPlaceholder = "%WIRETAP_VERSION%"
const IndexFile = "index.html"
const UILocation = "ui/dist"
const UIAssetsLocation = "ui/dist/assets"