使用7.0.x版本的mitmproxy,去除外置mitmproxy依赖,可简化http网络开销

This commit is contained in:
2022-06-06 12:25:42 +08:00
parent 77d4854530
commit 8211068215
12 changed files with 119 additions and 28 deletions

0
proxy/__init__.py Normal file
View File

0
proxy/addon/__init__.py Normal file
View File

24
proxy/addon/danmaku_ws.py Normal file
View File

@ -0,0 +1,24 @@
import re
from proxy.common import MessagePayload
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mitmproxy import http
from queue import SimpleQueue
class DanmakuWebsocketAddon:
def __init__(self, queue: "SimpleQueue[MessagePayload]"):
self._queue = queue
def websocket_message(self, flow: "http.HTTPFlow"):
re_c = re.search('webcast\d-ws-web-.*\.douyin\.com', flow.request.host)
if re_c:
message = flow.websocket.messages[-1]
if message.from_client:
return
payload = MessagePayload(message.content)
payload.request_url = flow.request.url
payload.request_query = flow.request.query
self._queue.put(payload)

View File

@ -0,0 +1,11 @@
from mitmproxy import http
class UserInfoAddon:
def __init__(self):
...
def response(self, flow: http.HTTPFlow):
# /aweme/v1/web/user/profile/other/ 他人主页获取他人信息
if '/aweme/v1/web/user/profile/other' in flow.request.path:
content = flow.response.content

9
proxy/common.py Normal file
View File

@ -0,0 +1,9 @@
import time
class MessagePayload(object):
def __init__(self, body: bytes):
self.body = body
self.timestamp: float = time.time()
self.request_url: str = ""
self.request_query: dict[str, str] = {}

59
proxy/manager.py Normal file
View File

@ -0,0 +1,59 @@
import asyncio
import threading
from typing import TYPE_CHECKING
from mitmproxy.options import Options
from mitmproxy.tools.dump import DumpMaster
from config.helper import config
from proxy.addon.danmaku_ws import DanmakuWebsocketAddon
from proxy.queues import MESSAGE_QUEUE
if TYPE_CHECKING:
from typing import Optional
_manager: "Optional[ProxyManager]" = None
class ProxyManager:
def __init__(self):
self._mitm_instance = None
opts = Options(
listen_host=config()['mitm']['host'],
listen_port=config()['mitm']['port'],
)
self._mitm_instance = DumpMaster(options=opts)
self._load_addon()
opts.update_defer(
flow_detail=0,
termlog_verbosity="error",
)
self._thread = None
def _load_addon(self):
self._mitm_instance.addons.add(DanmakuWebsocketAddon(MESSAGE_QUEUE))
def _start(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self._mitm_instance.run()
def start_loop(self):
self._thread = threading.Thread(target=self._start)
self._thread.start()
def join(self):
if self._thread:
self._thread.join()
def init_manager():
global _manager
_manager = ProxyManager()
return _manager
def get_manager():
if _manager is None:
return init_manager()
return _manager

4
proxy/queues.py Normal file
View File

@ -0,0 +1,4 @@
from queue import SimpleQueue
from proxy.common import MessagePayload
MESSAGE_QUEUE: "SimpleQueue[MessagePayload]" = SimpleQueue()