如果主机名与正则表达式匹配,如何使用mitmproxy插件过滤对主机的请求



我想实现一个插件,它过滤并删除对一组域的所有请求。所有域都必须与以下正则表达式匹配:

a-.+.xxxx.com

我不知道如何获得请求主机名:

if re.match("a-.+.xxxx.com", flow.request.hostname):
# Do something

在mitmproxy中,以下类用于封装请求信息:

mitmproxy.net.http.Request

请求类的承包商是:

def __init__(
self,
host: str,
port: int,
method: bytes,
scheme: bytes,
authority: bytes,
path: bytes,
http_version: bytes,
headers: Union[Headers, Tuple[Tuple[bytes, bytes], ...]],
content: Optional[bytes],
trailers: Union[None, Headers, Tuple[Tuple[bytes, bytes], ...]],
timestamp_start: float,
timestamp_end: Optional[float],
):

因此,在平台的某个地方,主机和端口被传递给类实例。

该实现使用一个数据类来存储数据:

@dataclass
class RequestData(message.MessageData):
host: str
port: int
method: bytes
scheme: bytes
authority: bytes
path: bytes

因此,要访问请求主机名:

if re.match("a-.+.xxxx.com", flow.request.data.host):
# Do something

以下方法直接添加到请求类中:

吸气剂

@property
def host(self) -> str:

设置

@host.setter
def host(self, val: Union[str, bytes]) -> None:

因此以下代码也是可以接受的:

if re.match("a-.+.xxxx.com", flow.request.host):
# Do something