-
Notifications
You must be signed in to change notification settings - Fork 16
/
config.go
160 lines (145 loc) · 7.2 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
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: AGPL
package shared
import (
"embed"
"fmt"
"github.com/gobwas/glob"
"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"`
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:"-"`
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) 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
}
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 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 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"