-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp.go
481 lines (380 loc) · 10.7 KB
/
http.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package ngroklistener
import (
"fmt"
"strconv"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"go.uber.org/zap"
"golang.ngrok.com/ngrok/config"
)
func init() {
caddy.RegisterModule(new(HTTP))
}
// ngrok HTTP tunnel
type HTTP struct {
opts []config.HTTPEndpointOption
// Rejects connections that do not match the given CIDRs
AllowCIDR []string `json:"allow_cidr,omitempty"`
// Rejects connections that match the given CIDRs and allows all other CIDRs.
DenyCIDR []string `json:"deny_cidr,omitempty"`
// the domain for this edge.
Domain string `json:"domain,omitempty"`
// opaque metadata string for this tunnel.
Metadata string `json:"metadata,omitempty"`
// sets the scheme for this edge.
Scheme string `json:"scheme,omitempty"`
// the 5XX response ratio at which the ngrok edge will stop sending requests to this tunnel.
CircuitBreaker float64 `json:"circuit_breaker,omitempty"`
// enables gzip compression.
Compression bool `json:"compression,omitempty"`
// enables the websocket-to-tcp converter.
WebsocketTCPConverter bool `json:"websocket_tcp_converter,omitempty"`
// A map of basicauth, username and password value pairs for this tunnel.
BasicAuth []basicAuthCred `json:"basic_auth,omitempty"`
OIDC *oidc `json:"oidc,omitempty"`
OAuth *oauth `json:"oauth,omitempty"`
WebhookVerification *webhookVerification `json:"webhook_verification,omitempty"`
RequestHeader *httpRequestHeaders `json:"request_header,omitempty"`
ResponseHeader *httpResponseHeaders `json:"header,omitempty"`
l *zap.Logger
}
type basicAuthCred struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
// CaddyModule implements caddy.Module
func (*HTTP) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "caddy.listeners.ngrok.tunnels.http",
New: func() caddy.Module {
return new(HTTP)
},
}
}
// Provision implements caddy.Provisioner
func (t *HTTP) Provision(ctx caddy.Context) error {
t.l = ctx.Logger()
t.doReplace()
if err := t.provisionOpts(ctx); err != nil {
return fmt.Errorf("provisioning http tunnel opts: %v", err)
}
return nil
}
func (t *HTTP) provisionOpts(ctx caddy.Context) error {
if t.Domain != "" {
t.opts = append(t.opts, config.WithDomain(t.Domain))
}
if t.Metadata != "" {
t.opts = append(t.opts, config.WithMetadata(t.Metadata))
}
if len(t.AllowCIDR) > 0 {
t.opts = append(t.opts, config.WithAllowCIDRString(t.AllowCIDR...))
}
if len(t.DenyCIDR) > 0 {
t.opts = append(t.opts, config.WithDenyCIDRString(t.DenyCIDR...))
}
if t.CircuitBreaker != 0 {
t.opts = append(t.opts, config.WithCircuitBreaker(t.CircuitBreaker))
}
if t.Compression {
t.opts = append(t.opts, config.WithCompression())
}
if t.Scheme != "" {
switch t.Scheme {
case "http":
t.opts = append(t.opts, config.WithScheme(config.SchemeHTTP))
case "https":
t.opts = append(t.opts, config.WithScheme(config.SchemeHTTPS))
default:
return fmt.Errorf("unrecognized http tunnel scheme %s", t.Scheme)
}
}
if t.WebsocketTCPConverter {
t.opts = append(t.opts, config.WithWebsocketTCPConversion())
}
for _, basic_auth := range t.BasicAuth {
t.opts = append(t.opts, config.WithBasicAuth(basic_auth.Username, basic_auth.Password))
}
if t.OIDC != nil {
err := t.OIDC.Provision(ctx)
if err != nil {
return fmt.Errorf("provisioning oidc: %v", err)
}
t.opts = append(t.opts, t.OIDC.opt)
}
if t.OAuth != nil {
err := t.OAuth.Provision(ctx)
if err != nil {
return fmt.Errorf("provisioning oauth: %v", err)
}
t.opts = append(t.opts, t.OAuth.opt)
}
if t.WebhookVerification != nil {
err := t.WebhookVerification.Provision(ctx)
if err != nil {
return fmt.Errorf("provisioning webhook_verification: %v", err)
}
t.opts = append(t.opts, t.WebhookVerification.opt)
}
if t.RequestHeader != nil {
err := t.RequestHeader.Provision(ctx)
if err != nil {
return fmt.Errorf("provisioning request_header: %v", err)
}
t.opts = append(t.opts, t.RequestHeader.opts...)
}
if t.ResponseHeader != nil {
err := t.ResponseHeader.Provision(ctx)
if err != nil {
return fmt.Errorf("provisioning header: %v", err)
}
t.opts = append(t.opts, t.ResponseHeader.opts...)
}
return nil
}
func (t *HTTP) doReplace() {
repl := caddy.NewReplacer()
replaceableFields := []*string{
&t.Metadata,
&t.Domain,
&t.Scheme,
}
for _, field := range replaceableFields {
actual := repl.ReplaceKnown(*field, "")
*field = actual
}
for index, cidr := range t.AllowCIDR {
actual := repl.ReplaceKnown(cidr, "")
t.AllowCIDR[index] = actual
}
for index, cidr := range t.DenyCIDR {
actual := repl.ReplaceKnown(cidr, "")
t.DenyCIDR[index] = actual
}
for i, basic_auth := range t.BasicAuth {
actualUsername := repl.ReplaceKnown(basic_auth.Username, "")
actualPassword := repl.ReplaceKnown(basic_auth.Password, "")
t.BasicAuth[i] = basicAuthCred{Username: actualUsername, Password: actualPassword}
}
}
// convert to ngrok's Tunnel type
func (t *HTTP) NgrokTunnel() config.Tunnel {
return config.HTTPEndpoint(t.opts...)
}
func (t *HTTP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
if d.NextArg() {
return d.ArgErr()
}
for nesting := d.Nesting(); d.NextBlock(nesting); {
subdirective := d.Val()
switch subdirective {
case "domain":
if !d.AllArgs(&t.Domain) {
return d.ArgErr()
}
case "metadata":
if !d.AllArgs(&t.Metadata) {
return d.ArgErr()
}
case "allow":
if err := t.unmarshalAllowCidr(d); err != nil {
return err
}
case "deny":
if err := t.unmarshalDenyCidr(d); err != nil {
return err
}
case "circuit_breaker":
if err := t.unmarshalCircuitBreaker(d); err != nil {
return err
}
case "compression":
if err := t.unmarshalCompression(d); err != nil {
return err
}
case "scheme":
if !d.AllArgs(&t.Scheme) {
return d.ArgErr()
}
case "websocket_tcp_converter":
if err := t.unmarshalWebsocketTCPConverter(d); err != nil {
return err
}
case "basic_auth":
if err := t.unmarshalBasicAuth(d); err != nil {
return err
}
case "oidc":
if err := t.unmarshalOIDC(d); err != nil {
return err
}
case "oauth":
if err := t.unmarshalOAuth(d); err != nil {
return err
}
case "webhook_verification":
if err := t.unmarshalWebhookVerification(d); err != nil {
return err
}
case "request_header":
if err := t.unmarshalRequestHeader(d); err != nil {
return err
}
case "header":
if err := t.unmarshalResponseHeader(d); err != nil {
return err
}
default:
return d.Errf("unrecognized subdirective %s", subdirective)
}
}
}
return nil
}
func (t *HTTP) unmarshalWebsocketTCPConverter(d *caddyfile.Dispenser) error {
var value string
if !d.Args(&value) { // no arg default is true
t.WebsocketTCPConverter = true
} else if value == "off" {
t.WebsocketTCPConverter = false
} else { // arg was given check it
var err error
t.WebsocketTCPConverter, err = strconv.ParseBool(value)
if err != nil {
return d.Errf(`parsing websocket_tcp_converter value %+v: %w`, value, err)
}
}
return nil
}
func (t *HTTP) unmarshalCompression(d *caddyfile.Dispenser) error {
var value string
if !d.Args(&value) { // no arg default is true
t.Compression = true
} else if value == "off" {
t.Compression = false
} else { // arg was given check it
var err error
t.Compression, err = strconv.ParseBool(value)
if err != nil {
return d.Errf(`parsing compression value %+v: %w`, value, err)
}
}
return nil
}
func (t *HTTP) unmarshalAllowCidr(d *caddyfile.Dispenser) error {
if d.CountRemainingArgs() == 0 {
return d.ArgErr()
}
t.AllowCIDR = append(t.AllowCIDR, d.RemainingArgs()...)
return nil
}
func (t *HTTP) unmarshalDenyCidr(d *caddyfile.Dispenser) error {
if d.CountRemainingArgs() == 0 {
return d.ArgErr()
}
t.DenyCIDR = append(t.DenyCIDR, d.RemainingArgs()...)
return nil
}
func (t *HTTP) unmarshalBasicAuth(d *caddyfile.Dispenser) error {
var (
hasArgs bool
username string
password string
foundBasicAuth bool
)
minLenPassword := 8
if d.NextArg() { // basic_auth is defined inline
username = d.Val()
hasArgs = true
if !d.AllArgs(&password) {
return d.ArgErr()
}
foundBasicAuth = true
if len(password) < minLenPassword {
return d.Err("password must be at least eight characters.")
}
t.BasicAuth = append(t.BasicAuth, basicAuthCred{Username: username, Password: password})
}
for nesting := d.Nesting(); d.NextBlock(nesting); { // block of basic_auth
username := d.Val()
if hasArgs {
return d.Err("cannot specify basic_auth in both arguments and block") // because it would be weird
}
if !d.AllArgs(&password) {
return d.ArgErr()
}
foundBasicAuth = true
if len(password) < minLenPassword {
return d.Err("password must be at least eight characters.")
}
t.BasicAuth = append(t.BasicAuth, basicAuthCred{Username: username, Password: password})
}
if !foundBasicAuth {
return d.ArgErr()
}
return nil
}
func (t *HTTP) unmarshalCircuitBreaker(d *caddyfile.Dispenser) error {
var ratio string
if !d.AllArgs(&ratio) {
return d.ArgErr()
}
circuitBreaker, err := strconv.ParseFloat(ratio, 64)
if err != nil {
return d.ArgErr()
}
t.CircuitBreaker = circuitBreaker
return nil
}
func (t *HTTP) unmarshalOIDC(d *caddyfile.Dispenser) error {
oidc := oidc{}
err := oidc.UnmarshalCaddyfile(d)
if err != nil {
return d.Errf(`parsing oidc %w`, err)
}
t.OIDC = &oidc
return nil
}
func (t *HTTP) unmarshalOAuth(d *caddyfile.Dispenser) error {
oauth := oauth{}
err := oauth.UnmarshalCaddyfile(d)
if err != nil {
return d.Errf(`parsing oauth %w`, err)
}
t.OAuth = &oauth
return nil
}
func (t *HTTP) unmarshalWebhookVerification(d *caddyfile.Dispenser) error {
webhookVerification := webhookVerification{}
err := webhookVerification.UnmarshalCaddyfile(d)
if err != nil {
return d.Errf(`parsing webhook_verification %w`, err)
}
t.WebhookVerification = &webhookVerification
return nil
}
func (t *HTTP) unmarshalRequestHeader(d *caddyfile.Dispenser) error {
requestHeader := httpRequestHeaders{}
err := requestHeader.UnmarshalCaddyfile(d)
if err != nil {
return d.Errf(`parsing request_header %w`, err)
}
t.RequestHeader = &requestHeader
return nil
}
func (t *HTTP) unmarshalResponseHeader(d *caddyfile.Dispenser) error {
responseHeader := httpResponseHeaders{}
err := responseHeader.UnmarshalCaddyfile(d)
if err != nil {
return d.Errf(`parsing header %w`, err)
}
t.ResponseHeader = &responseHeader
return nil
}
var (
_ caddy.Module = (*HTTP)(nil)
_ Tunnel = (*HTTP)(nil)
_ caddy.Provisioner = (*HTTP)(nil)
_ caddyfile.Unmarshaler = (*HTTP)(nil)
)