Skip to content

Created Whitelist Plugin c.f. filter_by_upstream (also fixed latter) #1514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- [Mock Api Plugin](#mockrestapiplugin)
- [Redirect To Custom Server Plugin](#redirecttocustomserverplugin)
- [Filter By Upstream Host Plugin](#filterbyupstreamhostplugin)
- [Whitelist_Upstream_Hosts_Plugin](#whitelistupstreamhostplugin)
- [Cache Responses Plugin](#cacheresponsesplugin)
- [Cache By Response Type](#cachebyresponsetype)
- [Man-In-The-Middle Plugin](#maninthemiddleplugin)
Expand Down Expand Up @@ -729,6 +730,12 @@ Traceback (most recent call last):
... [redacted] ...
... [redacted] ... - access_log:1157 - ::1:49911 - GET None:None/ - None None - 0 bytes
```
### WhitelistUpstreamHostPlugin

Essentially the same as [Filter By Upstream Host Plugin](#filterbyupstreamhostplugin)
except the list of hosts are whitelisted, all others are dropped as being tea-pots that don't serve coffee.

Can take a comma-separated (no-space) list of domains to construct whitelist. Otherwise The whitelist defaults to datasets.datalad.org,singularity-hub.org (because the author needed singularity to run on workernodes in an HPC setting with no NAT to the outside world).

### CacheResponsesPlugin

Expand Down
2 changes: 2 additions & 0 deletions proxy/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from .modify_request_header import ModifyRequestHeaderPlugin
from .redirect_to_custom_server import RedirectToCustomServerPlugin
from .tls_intercept_conditionally import TlsInterceptConditionallyPlugin
from .whitelist_upstream_hosts import WhitelistUpstreamHostsPlugin


__all__ = [
Expand All @@ -57,4 +58,5 @@
'ProgramNamePlugin',
'ModifyRequestHeaderPlugin',
'TlsInterceptConditionallyPlugin',
'WhitelistUpstreamHostsPlugin',
]
4 changes: 2 additions & 2 deletions proxy/plugin/filter_by_upstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'--filtered-upstream-hosts',
type=str,
default='facebook.com,www.facebook.com',
help='Default: Blocks Facebook. Comma separated list of IPv4 and IPv6 addresses.',
help='Default: Blocks Facebook. Comma separated list of fully qualified domain names, e.g. "facebook.com,www.facebook.com".',
)


Expand All @@ -32,7 +32,7 @@ class FilterByUpstreamHostPlugin(HttpProxyBasePlugin):
def before_upstream_connection(
self, request: HttpParser,
) -> Optional[HttpParser]:
if text_(request.host) in self.flags.filtered_upstream_hosts.split(','):
if request.host.decode() in self.flags.filtered_upstream_hosts.split(','):
raise HttpRequestRejected(
status_code=httpStatusCodes.I_AM_A_TEAPOT,
reason=b'I\'m a tea pot',
Expand Down
41 changes: 41 additions & 0 deletions proxy/plugin/whitelist_upstream_hosts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
proxy.py
~~~~~~~~
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
Network monitoring, controls & Application development, testing, debugging.

:copyright: (c) 2013-present by Abhinav Singh and contributors.
:license: BSD, see LICENSE for more details.

Whitelist Plugin modified from (broken) filter_by_upstream plugin by Mike Jones [email protected]

"""
from typing import Optional

from ..http import httpStatusCodes
from ..http.proxy import HttpProxyBasePlugin
from ..common.flag import flags
from ..http.parser import HttpParser
from ..common.utils import text_
from ..http.exception import HttpRequestRejected

flags.add_argument(
'--whitelist-upstream-hosts',
type=str,
default='datasets.datalad.org,singularity-hub.org,galaxy-dev.mcfe.itservices.manchester.ac.uk',
help='Default: Allows Singularity and Datasets. Comma separated list of domains.',
)


class WhitelistUpstreamHostsPlugin(HttpProxyBasePlugin):
"""Drop traffic by inspecting upstream host."""
def before_upstream_connection(
self, request: HttpParser,
) -> Optional[HttpParser]:
if not request.host.decode() in self.flags.whitelist_upstream_hosts.split(','):
raise HttpRequestRejected(
status_code=httpStatusCodes.I_AM_A_TEAPOT,
reason=b'I\'m a tea pot cannot conect to '+request.host,
)
return request