/
user_agent_filter.go
88 lines (77 loc) · 2.59 KB
/
user_agent_filter.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
package config
import (
"golang.ngrok.com/ngrok/internal/pb"
)
// UserAgentFilter is a pair of strings slices that allow/deny traffic to an endpoint
type userAgentFilter struct {
// slice of regex strings for allowed user agents
Allow []string
// slice of regex strings for denied user agents
Deny []string
}
// WithAllowUserAgentFilter is a deprecated alias for [WithAllowUserAgent].
//
// Deprecated: use [WithAllowUserAgent] instead.
func WithAllowUserAgentFilter(allow ...string) HTTPEndpointOption {
return WithAllowUserAgent(allow...)
}
// WithDenyUserAgentFilter is a deprecated alias for [WithDenyUserAgent].
//
// Deprecated: use [WithDenyUserAgent] instead.
func WithDenyUserAgentFilter(allow ...string) HTTPEndpointOption {
return WithDenyUserAgent(allow...)
}
// WithAllowUserAgent adds user agent filtering to the endpoint.
//
// The allow argument is a regular expressions for the user-agent
// header to allow
//
// Any invalid regular expression will result in an error when creating the tunnel.
//
// https://ngrok.com/docs/http/user-agent-filter/
// ERR_NGROK_2090 for invalid allow/deny on connect.
// ERR_NGROK_3211 The server does not authorize requests from your user-agent
// ERR_NGROK_9022 Your account is not authorized to use user agent filtering.
func WithAllowUserAgent(allow ...string) HTTPEndpointOption {
return &userAgentFilter{
// slice of regex strings for allowed user agents
Allow: allow,
}
}
// WithDenyUserAgent adds user agent filtering to the endpoint.
//
// The deny argument is a regular expressions to
// deny, with allows taking precedence over denies.
//
// Any invalid regular expression will result in an error when creating the tunnel.
//
// https://ngrok.com/docs/http/user-agent-filter/
// ERR_NGROK_2090 for invalid allow/deny on connect.
// ERR_NGROK_3211 The server does not authorize requests from your user-agent
// ERR_NGROK_9022 Your account is not authorized to use user agent filtering.
func WithDenyUserAgent(deny ...string) HTTPEndpointOption {
return &userAgentFilter{
// slice of regex strings for denied user agents
Deny: deny,
}
}
func (b *userAgentFilter) toProtoConfig() *pb.MiddlewareConfiguration_UserAgentFilter {
if b == nil {
return nil
}
return &pb.MiddlewareConfiguration_UserAgentFilter{
Allow: b.Allow,
Deny: b.Deny,
}
}
func (b *userAgentFilter) merge(set userAgentFilter) *userAgentFilter {
if b == nil {
b = &userAgentFilter{}
}
b.Allow = append(b.Allow, set.Allow...)
b.Deny = append(b.Deny, set.Deny...)
return b
}
func (b userAgentFilter) ApplyHTTP(opts *httpOptions) {
opts.UserAgentFilter = opts.UserAgentFilter.merge(b)
}