PYMUD支持使用socks5代理或者本机的特定网络接口进行网络访问,这本来是没问题的,但是对应北侠fullme的时候,原来提供的websocket插件会无法显示图片。使用浏览器打开时,会提示同一个IP为其他IP的角色显示过fullme图片,因此不能正确显示。此时,就需要让websocket插件通过与会话角色对应的代理或者网络设置来进行显示了。修改原 websocket.py 中的 loadAndSenfFullme 的函数代码,改为如下内容,可以使用与会话相同的网络配置来进行图片获取和下载,再使用websocket传送到web端页面上用于显示。只需要修改此处一个函数即可。
修改后的代码片段如下:
async def loadAndSendFullme(sender, text_msg: str, fullme_url: str, times: int = 3, combine: bool = False):
fmadress = fullme_url.split("robot.php?filename=")[-1]
url = f"http://fullme.pkuxkx.net/robot.php?filename={fmadress}"
images = []
# 使用与会话相同的网络配置进行图片抓取
conn = None
pymudapp = app.pymud
if isinstance(pymudapp, PyMudApp):
session = pymudapp.sessions.get(sender)
if isinstance(session, Session):
if session.proxy:
conn = ProxyConnector.from_url(session.proxy)
elif session.local_address:
conn = TCPConnector(local_addr=(session.local_address, 0))
async with ClientSession(connector=conn) as client:
for i in range(0, times):
async with client.get(url) as response:
if response.status != 200:
continue
text = await response.text()
matches = re.search(r'src="\.([^"]+\.jpg)"', text)
if not matches:
continue
img_url = "http://fullme.pkuxkx.net" + matches.group(1)
# 解析URL获取文件名
parsed_url = urlparse(img_url)
filename = os.path.basename(unquote(parsed_url.path))
# 创建fullme目录
fullme_dir = os.path.join(os.path.dirname(__file__), 'fullme')#cl
os.makedirs(fullme_dir, exist_ok=True)
# 本地文件路径
local_img_path = os.path.join(fullme_dir, filename)
# 下载图片
#async with ClientSession(connector=conn) as session:
async with client.get(img_url) as img_response:
if img_response.status == 200:
with open(local_img_path, 'wb') as f:
f.write(await img_response.read())
# 使用本地路径调用
if not combine:
await send_message_to_clients(sender, text_msg, local_img_path)
else:
images.append(local_img_path)
else:
asyncio.create_task(send_message_to_clients(sender, f"{text_msg} (图片下载失败: HTTP {img_response.status})", ""))
await asyncio.sleep(0.5)
if combine:
await send_message_to_clients(sender, text_msg, images)
以下是我使用本地IP连接了5个账号,同时使用代理后,利用远端云服务器连接了2个账号时,websocket插件的显示部分。
