Merge pull request #26 from Termonoid/main

Port can be defined as host:port
This commit is contained in:
kiriharu 2021-02-18 23:42:27 +03:00 committed by GitHub
commit 0649a1c3c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 87 additions and 21 deletions

View File

@ -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'

View File

@ -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
@ -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]

View File

@ -8,8 +8,9 @@ minecraft_help_message = """
Получает статистику о Minecraft сервере
Использование:
`/minecraft <hostname> <port>`
`/minecraft <hostname>` - автоматически выставит порт 25565
`/minecraft <hostname> <port>`
`/minecraft <hostname>:<port>`
`/minecraft <hostname>` - автоматически выставит порт 25565
"""

View File

@ -11,7 +11,8 @@ tcp_help_message = """
Производит проверку TCP порта, открыт ли он или нет
Использование:
`/tcp <hostname> <port>`
`/tcp <hostname> <port>`
`/tcp <hostname>:<port>`
"""
invalid_port = """❗Неправильный порт. Напишите /tcp чтобы увидеть справку к данному способу проверки."""
@ -29,15 +30,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):

View File

@ -7,7 +7,8 @@ web_help_message = """
Производит проверку хоста по протоколу HTTP.
Использование:
`/web <hostname> <port>`
`/web <hostname> <port>`
`/web <hostname>:<port>`
`/web <hostname>` - автоматически выставит 80 порт
"""

View File

@ -0,0 +1,2 @@
def run_all():
from . import port_parsers

View File

@ -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())