diff --git a/pagermaid/modules/system.py b/pagermaid/modules/system.py index eba8bfb..4b303be 100644 --- a/pagermaid/modules/system.py +++ b/pagermaid/modules/system.py @@ -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', '') 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 diff --git a/requirements.txt b/requirements.txt index 0d8504e..2a09a35 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ PyYAML>=5.4.1 redis>=3.5.3 coloredlogs>=15.0.1 pytz>=2021.1 +requests>=2.25.1