writing /web checking, added nodes file

This commit is contained in:
kiriharu 2021-01-03 16:38:47 +03:00
parent 3f6c966b5f
commit 94487be5e4
6 changed files with 68 additions and 1 deletions

View File

@ -8,6 +8,7 @@ authors = ["kiriharu <kiriharu@yandex.ru>"]
python = "^3.8"
aiogram = "^2.11.2"
core = {path = "../core"}
httpx = "^0.16.1"
[tool.poetry.dev-dependencies]

View File

@ -1,7 +1,9 @@
from aiogram import Dispatcher
from .start import start_cmd
from .web import web_cmd
def setup(dp: Dispatcher):
dp.register_message_handler(start_cmd, commands=['start'])
dp.register_message_handler(web_cmd, commands=['web'])

View File

@ -2,4 +2,4 @@ from aiogram.types import Message
async def start_cmd(msg: Message):
await msg.answer("Basic reply")
await msg.answer("Тут нужно будет хелп мессадж написать")

View File

@ -0,0 +1,51 @@
from aiogram.types import Message
from typing import Optional
from tgbot.handlers.helpers import check_int
from tgbot.nodes import nodes
import httpx
help_message = """
Использование:
/web <hostname> <port>
/web <hostname>
Производит проверку хоста по протоколу HTTP.
"""
invalid_port = """Неправильный порт!"""
async def check_web(message: Message, host: str, port: Optional[int]):
if port is None:
port = 80
responses = []
for node in nodes:
async with httpx.AsyncClient() as client:
result = await client.get(
f"{node.address}/http", params=dict(
target=host,
port=port,
token=node.token
)
)
await message.answer(result.json())
responses.append(result)
await message.answer(f"{host}:{port}")
async def web_cmd(msg: Message):
port = None
# TODO: Maybe check it in separated function?
args = msg.text.split(" ")
if len(args) < 2:
return await msg.answer(help_message)
if len(args) == 3:
port = args[2]
if not check_int(port):
return await msg.answer(invalid_port)
host = args[1]
await check_web(msg, host, port)

View File

@ -0,0 +1,7 @@
def check_int(value) -> bool:
try:
int(value)
except ValueError:
return False
else:
return True

View File

@ -0,0 +1,6 @@
from core.coretypes import APINode
from typing import List
nodes: List[APINode] = [
APINode("http://localhost:8080", "CHANGE_TOKEN_BY_ENV"),
]