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] [build-system]
requires = ["poetry-core>=1.0.0"] requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api" 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 aiogram.bot import Bot
from datetime import datetime from datetime import datetime
from core.coretypes import APINodeInfo 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 .errors import NotEnoughArgs, InvalidPort, LocalhostForbidden
from .validators import BaseValidator, LocalhostValidator from .validators import BaseValidator, LocalhostValidator
from tgbot.middlewares.throttling import rate_limit 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: def process_args_for_host_port(text: str, default_port: int) -> list:
port = None
args = text.split()
if len(args) < 2:
raise NotEnoughArgs()
if len(args) == 2:
port = default_port port = default_port
if len(args) == 3: args = text.split(' ', 1)
port = args[2] if len(args) != 2:
if not check_int(port): raise NotEnoughArgs()
raise InvalidPort()
host = args[1] host = args[1]
if ":" in host:
host, port = host.rsplit(":", 1)
elif " " in host:
host, port = host.rsplit(" ", 1)
return [host, port] return [host, port]

View File

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

View File

@ -12,6 +12,7 @@ tcp_help_message = """
Использование: Использование:
`/tcp <hostname> <port>` `/tcp <hostname> <port>`
`/tcp <hostname>:<port>`
""" """
invalid_port = """❗Неправильный порт. Напишите /tcp чтобы увидеть справку к данному способу проверки.""" invalid_port = """❗Неправильный порт. Напишите /tcp чтобы увидеть справку к данному способу проверки."""
@ -29,15 +30,18 @@ class TCPCheckerHandler(CheckerTargetPortHandler):
await super(TCPCheckerHandler, self).handler(message) await super(TCPCheckerHandler, self).handler(message)
async def process_args(self, text: str) -> list: async def process_args(self, text: str) -> list:
port = None args = text.split(' ', 1)
args = text.split() if len(args) != 2:
if len(args) < 3: raise NotEnoughArgs()
host = args[1]
if ":" in host:
host, port = host.rsplit(":", 1)
elif " " in host:
host, port = host.split(maxsplit=1)
else:
raise NotEnoughArgs() raise NotEnoughArgs()
if len(args) >= 3:
port = args[2]
if not check_int(port): if not check_int(port):
raise InvalidPort() raise InvalidPort()
host = args[1]
return [host, port] return [host, port]
async def prepare_message(self, res: Response): async def prepare_message(self, res: Response):

View File

@ -8,6 +8,7 @@ web_help_message = """
Использование: Использование:
`/web <hostname> <port>` `/web <hostname> <port>`
`/web <hostname>:<port>`
`/web <hostname>` - автоматически выставит 80 порт `/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())