Skip to content

Commit 1980a09

Browse files
LetMeR00tpre-commit-ci[bot]abhinavsingh
authored
[FilterByClientIpPlugin] Implement the whitelist logic (#1127)
* Implement the "whitelist" logic for the plugin "FilterByClientIpPlugin" * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix lint errors for the plugin FilterByClientIpPlugin Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Abhinav Singh <[email protected]>
1 parent 798f428 commit 1980a09

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,8 @@ options:
24692469
--filtered-upstream-hosts FILTERED_UPSTREAM_HOSTS
24702470
Default: Blocks Facebook. Comma separated list of IPv4
24712471
and IPv6 addresses.
2472+
--filtered-client-ips-mode FILTERED_CLIENT_IPS_MODE
2473+
Default: "blacklist". Can be either "whitelist" (restrict access to specific IPs) or "blacklist" (allow everything except specific IPs).
24722474
--filtered-client-ips FILTERED_CLIENT_IPS
24732475
Default: 127.0.0.1,::1. Comma separated list of IPv4
24742476
and IPv6 addresses.

proxy/plugin/filter_by_client_ip.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
from ..http.exception import HttpRequestRejected
2222

2323

24+
flags.add_argument(
25+
'--filtered-client-ips-mode',
26+
type=str,
27+
default='blacklist',
28+
help='Default: blacklist. Can be either "whitelist" (restrict access to specific IPs)'
29+
'or "blacklist" (allow everything except specific IPs).',
30+
)
31+
2432
flags.add_argument(
2533
'--filtered-client-ips',
2634
type=str,
@@ -30,15 +38,23 @@
3038

3139

3240
class FilterByClientIpPlugin(HttpProxyBasePlugin):
33-
"""Drop traffic by inspecting incoming client IP address."""
41+
"""Allow only (whitelist) or Drop only (blacklist) traffic by inspecting incoming client IP address."""
3442

3543
def before_upstream_connection(
3644
self, request: HttpParser,
3745
) -> Optional[HttpParser]:
3846
assert not self.flags.unix_socket_path and self.client.addr
39-
if self.client.addr[0] in self.flags.filtered_client_ips.split(','):
40-
raise HttpRequestRejected(
41-
status_code=httpStatusCodes.I_AM_A_TEAPOT,
42-
reason=b'I\'m a tea pot',
43-
)
47+
assert self.flags.filtered_client_ips_mode in ('blacklist', 'whitelist')
48+
if self.flags.filtered_client_ips_mode == 'blacklist':
49+
if self.client.addr[0] in self.flags.filtered_client_ips.split(','):
50+
raise HttpRequestRejected(
51+
status_code=httpStatusCodes.I_AM_A_TEAPOT,
52+
reason=b'I\'m a tea pot',
53+
)
54+
elif self.flags.filtered_client_ips_mode == 'whitelist':
55+
if self.client.addr[0] not in self.flags.filtered_client_ips.split(','):
56+
raise HttpRequestRejected(
57+
status_code=httpStatusCodes.I_AM_A_TEAPOT,
58+
reason=b'I\'m a tea pot',
59+
)
4460
return request

0 commit comments

Comments
 (0)