-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from ngrok/mo/bot_filter_mw
config: add user agent module
- Loading branch information
Showing
10 changed files
with
510 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.4.0 | ||
1.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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 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/cloud-edge/modules/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 WithAllowUserAgentFilter(allow ...string) HTTPEndpointOption { | ||
return userAgentFilter{ | ||
// slice of regex strings for allowed user agents | ||
Allow: allow, | ||
} | ||
} | ||
|
||
// WithDenyUserAgentFilter 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/cloud-edge/modules/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 WithDenyUserAgentFilter(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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"golang.ngrok.com/ngrok/internal/pb" | ||
"golang.ngrok.com/ngrok/internal/tunnel/proto" | ||
) | ||
|
||
func testUserAgentFilter[T tunnelConfigPrivate, O any, OT any](t *testing.T, | ||
makeOpts func(...OT) Tunnel, | ||
getUserAgentFilter func(*O) *pb.MiddlewareConfiguration_UserAgentFilter, | ||
) { | ||
optsFunc := func(opts ...any) Tunnel { | ||
return makeOpts(assertSlice[OT](opts)...) | ||
} | ||
cases := testCases[T, O]{ | ||
{ | ||
name: "test empty", | ||
opts: optsFunc(), | ||
expectOpts: func(t *testing.T, opts *O) { | ||
actual := getUserAgentFilter(opts) | ||
require.Nil(t, actual) | ||
}, | ||
}, | ||
{ | ||
name: "test allow", | ||
opts: optsFunc( | ||
WithAllowUserAgentFilter(`(Pingdom\.com_bot_version_)(\d+)\.(\d+)`), | ||
), | ||
expectOpts: func(t *testing.T, opts *O) { | ||
actual := getUserAgentFilter(opts) | ||
require.NotNil(t, actual) | ||
require.Nil(t, actual.Deny) | ||
require.NotNil(t, actual.Allow) | ||
require.Len(t, actual.Allow, 1) | ||
require.Len(t, actual.Deny, 0) | ||
require.Contains(t, actual.Allow, `(Pingdom\.com_bot_version_)(\d+)\.(\d+)`) | ||
}, | ||
}, | ||
{ | ||
name: "test deny", | ||
opts: optsFunc( | ||
WithDenyUserAgentFilter(`(Pingdom\.com_bot_version_)(\d+)\.(\d+)`), | ||
), | ||
expectOpts: func(t *testing.T, opts *O) { | ||
actual := getUserAgentFilter(opts) | ||
require.NotNil(t, actual) | ||
require.Nil(t, actual.Allow) | ||
require.Len(t, actual.Allow, 0) | ||
require.NotNil(t, actual.Deny) | ||
require.Len(t, actual.Deny, 1) | ||
require.Contains(t, actual.Deny, `(Pingdom\.com_bot_version_)(\d+)\.(\d+)`) | ||
}, | ||
}, | ||
{ | ||
name: "test allow and deny", | ||
opts: optsFunc( | ||
WithAllowUserAgentFilter(`(Pingdom\.com_bot_version_)(\d+)\.(\d+)`), | ||
WithDenyUserAgentFilter(`(Pingdom\.com_bot_version_)(\d+)\.(\d+)`), | ||
), | ||
expectOpts: func(t *testing.T, opts *O) { | ||
actual := getUserAgentFilter(opts) | ||
require.NotNil(t, actual) | ||
require.Len(t, actual.Allow, 1) | ||
require.Len(t, actual.Deny, 1) | ||
require.Contains(t, actual.Allow, `(Pingdom\.com_bot_version_)(\d+)\.(\d+)`) | ||
require.Contains(t, actual.Deny, `(Pingdom\.com_bot_version_)(\d+)\.(\d+)`) | ||
}, | ||
}, | ||
{ | ||
name: "test multiple", | ||
opts: optsFunc( | ||
WithAllowUserAgentFilter(`(Pingdom\.com_bot_version_)(\d+)\.(\d+)`), | ||
WithDenyUserAgentFilter(`(Pingdom\.com_bot_version_)(\d+)\.(\d+)`), | ||
WithAllowUserAgentFilter(`(Pingdom2\.com_bot_version_)(\d+)\.(\d+)`), | ||
WithDenyUserAgentFilter(`(Pingdom2\.com_bot_version_)(\d+)\.(\d+)`), | ||
), | ||
expectOpts: func(t *testing.T, opts *O) { | ||
actual := getUserAgentFilter(opts) | ||
require.NotNil(t, actual) | ||
require.Len(t, actual.Allow, 2) | ||
require.Len(t, actual.Deny, 2) | ||
require.Contains(t, actual.Allow, `(Pingdom\.com_bot_version_)(\d+)\.(\d+)`) | ||
require.Contains(t, actual.Deny, `(Pingdom\.com_bot_version_)(\d+)\.(\d+)`) | ||
require.Contains(t, actual.Allow, `(Pingdom2\.com_bot_version_)(\d+)\.(\d+)`) | ||
require.Contains(t, actual.Deny, `(Pingdom2\.com_bot_version_)(\d+)\.(\d+)`) | ||
}, | ||
}, | ||
} | ||
|
||
cases.runAll(t) | ||
} | ||
|
||
func TestUserAgentFilter(t *testing.T) { | ||
testUserAgentFilter[httpOptions](t, HTTPEndpoint, | ||
func(h *proto.HTTPEndpoint) *pb.MiddlewareConfiguration_UserAgentFilter { | ||
return h.UserAgentFilter | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.