|
21 | 21 | from ..http.exception import HttpRequestRejected
|
22 | 22 |
|
23 | 23 |
|
| 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 | + |
24 | 32 | flags.add_argument(
|
25 | 33 | '--filtered-client-ips',
|
26 | 34 | type=str,
|
|
30 | 38 |
|
31 | 39 |
|
32 | 40 | 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.""" |
34 | 42 |
|
35 | 43 | def before_upstream_connection(
|
36 | 44 | self, request: HttpParser,
|
37 | 45 | ) -> Optional[HttpParser]:
|
38 | 46 | 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 | + ) |
44 | 60 | return request
|
0 commit comments