/
config.go
403 lines (371 loc) · 9.32 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package main
import (
"flag"
"fmt"
"github.com/BurntSushi/toml"
"github.com/bryanaustin/yaarp"
"io"
"os"
"path/filepath"
"strconv"
"strings"
)
type Profile struct {
Name string
Listen string
Proxy string //TODO: Rename to send
Protocol string
ListenCertPath string
ListenCertRaw string
ListenPrivatePath string
ListenPrivateRaw string
ListenAuthorityPath string
ListenAuthorityRaw string
SendCertPath string
SendCertRaw string
SendPrivatePath string
SendPrivateRaw string
SendAuthorityPath string
SendAuthorityRaw string
Source string
}
type Configurations struct {
ConfigDir string
Profiles []*Profile
}
const (
EnvProfilePrefix = "MTLSPROXY_PROFILE_"
EnvProtocolSuffix = "_PROTOCOL"
EnvListenSuffix = "_LISTEN"
EnvProxySuffix = "_PROXY"
EnvListenCertSuffix = "_CERT_LISTEN"
EnvSendCertSuffix = "_CERT_SEND"
EnvListenPrivateSuffix = "_PRIVATE_LISTEN"
EnvSendPrivateSuffix = "_PRIVATE_SEND"
EnvAuthorityListenSuffix = "_AUTHORITY_LISTEN" //TODO: Rename _LISTEN_AUTHORITY
EnvAuthoritySendSuffix = "_AUTHORITY_SEND"
)
var (
Debug bool
)
func (c Configurations) getProfiles() (nups []*Profile, err error) {
nups = make([]*Profile, len(c.Profiles))
for i := range nups {
nups[i] = c.Profiles[i].Copy()
}
if len(c.ConfigDir) < 1 {
return
}
var cfd *os.File
cfd, err = os.Open(c.ConfigDir)
if err != nil {
err = fmt.Errorf("opening config directory: %w", err)
return
}
var diritems []os.DirEntry
for {
diritems, err = cfd.ReadDir(16)
if err == io.EOF {
err = nil
break
}
if err != nil {
err = fmt.Errorf("reading contents of config directory: %w", err)
return
}
for _, item := range diritems {
if item.IsDir() {
continue
}
var ps map[string]*Profile
path := filepath.Join(c.ConfigDir, item.Name())
_, err = toml.DecodeFile(path, &ps)
if err != nil {
err = fmt.Errorf("reading configuration %q: %w", path, err)
return
}
pl := make([]*Profile, 0, len(ps))
for k := range ps {
ps[k].Name = k
ps[k].Source = path
pl = append(pl, ps[k])
}
nups = mergeProfiles(nups, pl...)
}
}
return
}
func getImmutableConfigs() (c *Configurations, err error) {
c = new(Configurations)
flag.BoolVar(&Debug, "debug", false, "enable debug logging")
flag.StringVar(&c.ConfigDir, "configdir", "", "directory for config files")
yaarp.Parse()
if env := os.Getenv("MTLSPROXY_DEBUG"); !Debug && len(env) > 0 {
Debug, err = strconv.ParseBool(env)
if err != nil {
return
}
}
if env := os.Getenv("MTLSPROXY_CONFIG_DIR"); len(c.ConfigDir) < 1 && len(env) > 0 {
c.ConfigDir = env
}
c.Profiles = profilesFromEnv()
return
}
func profilesFromEnv() (ps []*Profile) {
allenvs := os.Environ()
matchedPrefix := make([]string, 0, len(allenvs))
findoradd := func(name string) *Profile {
for i := range ps {
if ps[i].Name == name {
return ps[i]
}
}
nu := &Profile{Name: name}
ps = append(ps, nu)
return nu
}
for _, x := range allenvs {
if strings.HasPrefix(x, EnvProfilePrefix) {
matchedPrefix = append(matchedPrefix, x[len(EnvProfilePrefix):])
}
}
for _, x := range matchedPrefix {
if r := profileSuffix(x, EnvListenSuffix); len(r) > 0 {
p := findoradd(r)
p.Listen = os.Getenv(EnvProfilePrefix + x)
continue
}
if r := profileSuffix(x, EnvProxySuffix); len(r) > 0 {
p := findoradd(r)
p.Proxy = os.Getenv(EnvProfilePrefix + x)
continue
}
if r := profileSuffix(x, EnvProtocolSuffix); len(r) > 0 {
p := findoradd(r)
p.Protocol = os.Getenv(EnvProfilePrefix + x)
continue
}
if r := profileSuffix(x, EnvListenCertSuffix); len(r) > 0 {
p := findoradd(r)
p.ListenCertRaw = os.Getenv(EnvProfilePrefix + x)
continue
}
if r := profileSuffix(x, EnvSendCertSuffix); len(r) > 0 {
p := findoradd(r)
p.SendCertRaw = os.Getenv(EnvProfilePrefix + x)
continue
}
if r := profileSuffix(x, EnvListenPrivateSuffix); len(r) > 0 {
p := findoradd(r)
p.ListenPrivateRaw = os.Getenv(EnvProfilePrefix + x)
continue
}
if r := profileSuffix(x, EnvSendPrivateSuffix); len(r) > 0 {
p := findoradd(r)
p.SendPrivateRaw = os.Getenv(EnvProfilePrefix + x)
continue
}
if r := profileSuffix(x, EnvAuthorityListenSuffix); len(r) > 0 {
p := findoradd(r)
p.ListenAuthorityRaw = os.Getenv(EnvProfilePrefix + x)
continue
}
if r := profileSuffix(x, EnvAuthoritySendSuffix); len(r) > 0 {
p := findoradd(r)
p.SendAuthorityRaw = os.Getenv(EnvProfilePrefix + x)
continue
}
}
return
}
func mergeProfiles(b []*Profile, n ...*Profile) []*Profile {
result := make([]*Profile, 0, len(b)+len(n))
result = append(result, b...)
for _, p := range n {
found := -1
for i := range result {
if result[i].Name == p.Name {
found = i
break
}
}
if found > -1 {
result[found] = mergeProfile(result[found], p)
} else {
result = append(result, p)
}
}
return result
}
func mergeProfile(a, b *Profile) *Profile {
if a == nil {
if b != nil {
return b
}
a = new(Profile)
}
if b == nil {
return a
}
if len(a.Listen) < 1 {
a.Listen = b.Listen
}
if len(a.Proxy) < 1 {
a.Proxy = b.Proxy
}
if len(a.Protocol) < 1 {
a.Protocol = b.Protocol
}
if len(a.SendCertPath) < 1 {
a.SendCertPath = b.SendCertPath
}
if len(a.SendCertRaw) < 1 {
a.SendCertRaw = b.SendCertRaw
}
if len(a.ListenCertPath) < 1 {
a.ListenCertPath = b.ListenCertPath
}
if len(a.ListenCertRaw) < 1 {
a.ListenCertRaw = b.ListenCertRaw
}
if len(a.ListenPrivatePath) < 1 {
a.ListenPrivatePath = b.ListenPrivatePath
}
if len(a.ListenPrivateRaw) < 1 {
a.ListenPrivateRaw = b.ListenPrivateRaw
}
if len(a.SendPrivatePath) < 1 {
a.SendPrivatePath = b.SendPrivatePath
}
if len(a.SendPrivateRaw) < 1 {
a.SendPrivateRaw = b.SendPrivateRaw
}
if len(a.ListenAuthorityPath) < 1 {
a.ListenAuthorityPath = b.ListenAuthorityPath
}
if len(a.ListenAuthorityRaw) < 1 {
a.ListenAuthorityRaw = b.ListenAuthorityRaw
}
if len(a.SendAuthorityPath) < 1 {
a.SendAuthorityPath = b.SendAuthorityPath
}
if len(a.SendAuthorityRaw) < 1 {
a.SendAuthorityRaw = b.SendAuthorityRaw
}
return a
}
func profileSuffix(x, s string) string {
if strings.HasSuffix(x, s) {
index := len(x) - len(s)
return x[index:]
}
return ""
}
func (p Profile) Copy() (nu *Profile) {
nu = new(Profile)
nu.Name = p.Name
nu.Listen = p.Listen
nu.Proxy = p.Proxy
nu.Protocol = p.Protocol
nu.ListenCertPath = p.ListenCertPath
nu.ListenCertRaw = p.ListenCertRaw
nu.ListenPrivatePath = p.ListenPrivatePath
nu.ListenPrivateRaw = p.ListenPrivateRaw
nu.ListenAuthorityPath = p.ListenAuthorityPath
nu.ListenAuthorityRaw = p.ListenAuthorityRaw
nu.SendCertPath = p.SendCertPath
nu.SendCertRaw = p.SendCertRaw
nu.SendPrivatePath = p.SendPrivatePath
nu.SendPrivateRaw = p.SendPrivateRaw
nu.SendAuthorityPath = p.SendAuthorityPath
nu.SendAuthorityRaw = p.SendAuthorityRaw
nu.Source = p.Source
return
}
// resolve will load any files from the filesystem that are pending
func (p *Profile) Resolve() error {
if len(p.ListenCertRaw) < 1 && len(p.ListenCertPath) > 0 {
b, err := os.ReadFile(p.ListenCertPath)
if err != nil {
return fmt.Errorf("reading file %q: %w", p.ListenCertPath, err)
}
p.ListenCertRaw = string(b)
}
if len(p.SendCertRaw) < 1 && len(p.SendCertPath) > 0 {
b, err := os.ReadFile(p.SendCertPath)
if err != nil {
return fmt.Errorf("reading file %q: %w", p.SendCertPath, err)
}
p.SendCertRaw = string(b)
}
if len(p.ListenPrivateRaw) < 1 && len(p.ListenPrivatePath) > 0 {
b, err := os.ReadFile(p.ListenPrivatePath)
if err != nil {
return fmt.Errorf("reading file %q: %w", p.ListenPrivatePath, err)
}
p.ListenPrivateRaw = string(b)
}
if len(p.SendPrivateRaw) < 1 && len(p.SendPrivatePath) > 0 {
b, err := os.ReadFile(p.SendPrivatePath)
if err != nil {
return fmt.Errorf("reading file %q: %w", p.SendPrivatePath, err)
}
p.SendPrivateRaw = string(b)
}
if len(p.ListenAuthorityRaw) < 1 && len(p.ListenAuthorityPath) > 0 {
b, err := os.ReadFile(p.ListenAuthorityPath)
if err != nil {
return fmt.Errorf("reading file %q: %w", p.ListenAuthorityPath, err)
}
p.ListenAuthorityRaw = string(b)
}
if len(p.SendAuthorityRaw) < 1 && len(p.SendAuthorityPath) > 0 {
b, err := os.ReadFile(p.SendAuthorityPath)
if err != nil {
return fmt.Errorf("reading file %q: %w", p.SendAuthorityPath, err)
}
p.SendAuthorityRaw = string(b)
}
return nil
}
// ListenChanged will compare profiles to see if the listen side of the connection
// needs to be changed.
func (p *Profile) ListenChanged(q *Profile) bool {
if p.Listen != q.Listen {
return true
}
if p.Protocol != q.Protocol {
return true
}
if p.ListenAuthorityRaw != q.ListenAuthorityRaw {
return true
}
if p.ListenCertRaw != q.ListenCertRaw {
return true
}
if p.ListenPrivateRaw != q.ListenPrivateRaw {
return true
}
return false
}
// DestinationChanged will compare profiles to see if the destination side of the
// connection needs to be changed.
func (p *Profile) DestinationChanged(q *Profile) bool {
if p.Proxy != q.Proxy {
return true
}
if p.Protocol != q.Protocol {
return true
}
if p.SendAuthorityRaw != q.SendAuthorityRaw {
return true
}
if p.SendCertRaw != q.SendCertRaw {
return true
}
if p.SendPrivateRaw != q.SendPrivateRaw {
return true
}
return false
}