From a54a938c2f3ecf68005c528fe19cfc48d29c2e92 Mon Sep 17 00:00:00 2001 From: Termonoid Date: Sun, 31 Jan 2021 10:27:22 +0300 Subject: [PATCH 1/3] Port can be defined as host:port --- apps/tgbot/tgbot/handlers/base.py | 16 +++++++--------- apps/tgbot/tgbot/handlers/default/tcp.py | 17 ++++++++++------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/apps/tgbot/tgbot/handlers/base.py b/apps/tgbot/tgbot/handlers/base.py index 623e850..c880ab0 100644 --- a/apps/tgbot/tgbot/handlers/base.py +++ b/apps/tgbot/tgbot/handlers/base.py @@ -103,15 +103,13 @@ class CheckerTargetPortHandler(CheckerBaseHandler): def process_args_for_host_port(text: str, default_port: int) -> list: - port = None - args = text.split() - if len(args) < 2: + port = default_port + args = text.split(' ', 1) + if len(args) != 2: raise NotEnoughArgs() - if len(args) == 2: - port = default_port - if len(args) == 3: - port = args[2] - if not check_int(port): - raise InvalidPort() host = args[1] + if ":" in host: + host, port = host.rsplit(":", 1) + elif " " in host: + host, port = host.rsplit(" ", 1) return [host, port] diff --git a/apps/tgbot/tgbot/handlers/default/tcp.py b/apps/tgbot/tgbot/handlers/default/tcp.py index a2aa1fc..14edd65 100644 --- a/apps/tgbot/tgbot/handlers/default/tcp.py +++ b/apps/tgbot/tgbot/handlers/default/tcp.py @@ -29,15 +29,18 @@ class TCPCheckerHandler(CheckerTargetPortHandler): await super(TCPCheckerHandler, self).handler(message) async def process_args(self, text: str) -> list: - port = None - args = text.split() - if len(args) < 3: + args = text.split(' ', 1) + if len(args) != 2: raise NotEnoughArgs() - if len(args) >= 3: - port = args[2] - if not check_int(port): - raise InvalidPort() host = args[1] + if ":" in host: + host, port = host.rsplit(":", 1) + elif " " in host: + host, port = host.split(maxsplit=1) + else: + raise NotEnoughArgs() + if not check_int(port): + raise InvalidPort() return [host, port] async def prepare_message(self, res: Response): From 766198ad818097e5cecf26d653d444109cd1955a Mon Sep 17 00:00:00 2001 From: Termonoid Date: Sun, 31 Jan 2021 12:42:22 +0300 Subject: [PATCH 2/3] Update help messages for host:port form of command --- apps/tgbot/tgbot/handlers/default/minecraft.py | 5 +++-- apps/tgbot/tgbot/handlers/default/tcp.py | 3 ++- apps/tgbot/tgbot/handlers/default/web.py | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/apps/tgbot/tgbot/handlers/default/minecraft.py b/apps/tgbot/tgbot/handlers/default/minecraft.py index 790acaa..4811c39 100644 --- a/apps/tgbot/tgbot/handlers/default/minecraft.py +++ b/apps/tgbot/tgbot/handlers/default/minecraft.py @@ -8,8 +8,9 @@ minecraft_help_message = """ ❓ Получает статистику о Minecraft сервере Использование: - `/minecraft ` - `/minecraft ` - автоматически выставит порт 25565 + `/minecraft ` + `/minecraft :` + `/minecraft ` - автоматически выставит порт 25565 """ diff --git a/apps/tgbot/tgbot/handlers/default/tcp.py b/apps/tgbot/tgbot/handlers/default/tcp.py index 14edd65..c3bf612 100644 --- a/apps/tgbot/tgbot/handlers/default/tcp.py +++ b/apps/tgbot/tgbot/handlers/default/tcp.py @@ -11,7 +11,8 @@ tcp_help_message = """ ❓ Производит проверку TCP порта, открыт ли он или нет Использование: - `/tcp ` + `/tcp ` + `/tcp :` """ invalid_port = """❗Неправильный порт. Напишите /tcp чтобы увидеть справку к данному способу проверки.""" diff --git a/apps/tgbot/tgbot/handlers/default/web.py b/apps/tgbot/tgbot/handlers/default/web.py index dafc369..b0ca9aa 100644 --- a/apps/tgbot/tgbot/handlers/default/web.py +++ b/apps/tgbot/tgbot/handlers/default/web.py @@ -7,7 +7,8 @@ web_help_message = """ ❓ Производит проверку хоста по протоколу HTTP. Использование: - `/web ` + `/web ` + `/web :` `/web ` - автоматически выставит 80 порт """ From ff8143ccf40aab4fd043625f42a69ba7c6476dbf Mon Sep 17 00:00:00 2001 From: Termonoid Date: Wed, 3 Feb 2021 20:40:02 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D1=8B=3F=20=D0=A2?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D1=8B.=20=D0=9C=D0=B5=D1=80=D0=B6=D0=B8=20?= =?UTF-8?q?=D1=83=D0=B6=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/tgbot/pyproject.toml | 3 ++ apps/tgbot/tgbot/handlers/base.py | 2 +- apps/tgbot/tgbot/test/__init__.py | 2 + apps/tgbot/tgbot/test/port_parsers.py | 57 +++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 apps/tgbot/tgbot/test/__init__.py create mode 100644 apps/tgbot/tgbot/test/port_parsers.py diff --git a/apps/tgbot/pyproject.toml b/apps/tgbot/pyproject.toml index bec37b6..e2893b7 100644 --- a/apps/tgbot/pyproject.toml +++ b/apps/tgbot/pyproject.toml @@ -21,3 +21,6 @@ aiomysql = "^0.0.21" [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" + +[tool.poetry.scripts] +test = 'tgbot.test:run_all' diff --git a/apps/tgbot/tgbot/handlers/base.py b/apps/tgbot/tgbot/handlers/base.py index c880ab0..ee77eb8 100644 --- a/apps/tgbot/tgbot/handlers/base.py +++ b/apps/tgbot/tgbot/handlers/base.py @@ -6,7 +6,7 @@ from httpx import Response from aiogram.bot import Bot from datetime import datetime from core.coretypes import APINodeInfo -from .helpers import send_api_requests, check_int +from .helpers import send_api_requests from .errors import NotEnoughArgs, InvalidPort, LocalhostForbidden from .validators import BaseValidator, LocalhostValidator from tgbot.middlewares.throttling import rate_limit diff --git a/apps/tgbot/tgbot/test/__init__.py b/apps/tgbot/tgbot/test/__init__.py new file mode 100644 index 0000000..1ba86c5 --- /dev/null +++ b/apps/tgbot/tgbot/test/__init__.py @@ -0,0 +1,2 @@ +def run_all(): + from . import port_parsers diff --git a/apps/tgbot/tgbot/test/port_parsers.py b/apps/tgbot/tgbot/test/port_parsers.py new file mode 100644 index 0000000..7f770f4 --- /dev/null +++ b/apps/tgbot/tgbot/test/port_parsers.py @@ -0,0 +1,57 @@ +import asyncio + +from ..handlers.default.tcp import TCPCheckerHandler +from ..handlers.base import process_args_for_host_port,\ + NotEnoughArgs, InvalidPort + + +try: + args = "/cmd" + process_args_for_host_port(args, 443) +except NotEnoughArgs: + pass +args = "/cmd example.com" +host, port = process_args_for_host_port(args, 443) +assert port == 443 + +args = "/cmd example.com 42" +host, port = process_args_for_host_port(args, 443) +assert port == "42" # TODO: FIX THIS SHIT + +args = "/cmd example.com:42" +host, port = process_args_for_host_port(args, 443) +assert port == "42" + +try: + args = "/cmd example.com fucktests" +except InvalidPort: + pass + +method = TCPCheckerHandler().process_args + + +async def test(): + try: + args = "/cmd" + await method(args) + args = "/cmd example.com" + await method(args) + except NotEnoughArgs: + pass + + args = "/cmd example.com 42" + host, port = await method(args) + assert port == "42" + + args = "/cmd example.com:42" + host, port = await method(args) + assert port == "42" + + try: + args = "/cmd example.com jdbnjsbndjsd" + await method(args) + except InvalidPort: + pass + + +asyncio.run(test())