From d59e882ce4ffec5ad14de842811000f05f088afd Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 25 Jul 2021 17:02:41 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=94=AF=E6=8C=81=E8=B7=9F?= =?UTF-8?q?=E8=B8=AA=E9=87=8D=E5=AE=9A=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagermaid/modules/system.py | 55 +++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 56 insertions(+) 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