支持跟踪重定向

This commit is contained in:
xtaodada 2021-07-25 17:02:41 +08:00
parent 862d01a475
commit d59e882ce4
No known key found for this signature in database
GPG Key ID: EE4DC37B55E24736
2 changed files with 56 additions and 0 deletions

View File

@ -5,7 +5,12 @@ from platform import uname
import discord
from discord.ext import commands
from subprocess import run, PIPE
from requests import head
from requests.exceptions import MissingSchema, InvalidURL, ConnectionError
from pagermaid import color, des_handler, par_handler, redis_status
from pagermaid.utils import process_command
class Info(commands.Cog):
@ -42,10 +47,60 @@ class Info(commands.Cog):
embed.add_field(name="交流群", value='[点击加入](https://discord.gg/A4mWpa83e6)')
await ctx.send(embed=embed)
@commands.command()
async def trace(self, context):
message = process_command(context)
if len(message.parameters) == 1:
url = message.arguments
if url.startswith("https://") or url.startswith("http://"):
pass
else:
url = "https://" + url
msg = await context.reply('跟踪重定向中...')
result = str("")
for url in url_tracer(url):
count = 0
if result:
result += "\n" + url
else:
result = url
if count == 128:
result += f"\n\n重定向超过 128 次,已停止追踪。"
break
if result:
if len(result) > 2000:
result = result[:2000]
await msg.edit(content=f"跟踪重定向:\n"
f"{result}")
else:
await msg.edit(content='服务器连接失败。')
else:
await context.reply('您好像输入了一个无效的参数。')
des_handler('info', '查看程序信息。')
par_handler('info', '')
des_handler('trace', '追踪重定向。')
par_handler('info', '<url>')
def setup(bot):
bot.add_cog(Info(bot))
def url_tracer(url):
""" Method to trace URL redirects. """
while True:
yield url
try:
response = head(url)
except MissingSchema:
break
except InvalidURL:
break
except ConnectionError:
break
if 300 < response.status_code < 400:
url = response.headers['location']
else:
break

View File

@ -4,3 +4,4 @@ PyYAML>=5.4.1
redis>=3.5.3
coloredlogs>=15.0.1
pytz>=2021.1
requests>=2.25.1