complete webcheck

This commit is contained in:
kiriharu 2021-01-03 21:19:46 +03:00
parent ee422dce33
commit e5045c76ad
2 changed files with 37 additions and 10 deletions

View File

@ -2,12 +2,13 @@ from aiogram.types import Message
from typing import Optional from typing import Optional
from tgbot.handlers.helpers import check_int from tgbot.handlers.helpers import check_int
from tgbot.nodes import nodes from tgbot.nodes import nodes
import httpx from httpx import AsyncClient, Response
from core.coretypes import ResponseStatus, HTTP_EMOJI
help_message = """ help_message = """
Использование: Использование:
/web <hostname> <port> /web <hostname> <port>
/web <hostname> /web <hostname> - автоматически выставит 80 порт
Производит проверку хоста по протоколу HTTP. Производит проверку хоста по протоколу HTTP.
""" """
@ -15,12 +16,24 @@ help_message = """
invalid_port = """Неправильный порт!""" invalid_port = """Неправильный порт!"""
async def check_web(message: Message, host: str, port: Optional[int]): async def prepare_webcheck_message(response: Response) -> str:
if port is None: # TODO: Use types from core!
port = 80 message = ""
responses = [] json_rsp = response.json()
status = json_rsp.get("status")
if status == ResponseStatus.OK:
status_code = json_rsp['payload']['status_code']
time = round(json_rsp['payload']['time'], 3)
message = f"Location, Town: {HTTP_EMOJI.get(status_code, None)} {status_code}, {time} сек."
if status == ResponseStatus.ERROR:
message = json_rsp['payload']['message']
message = f"Location, Town: ❌ {message}"
return message
async def send_check_requests(host: str, port: int):
for node in nodes: for node in nodes:
async with httpx.AsyncClient() as client: async with AsyncClient() as client:
result = await client.get( result = await client.get(
f"{node.address}/http", params=dict( f"{node.address}/http", params=dict(
target=host, target=host,
@ -28,10 +41,22 @@ async def check_web(message: Message, host: str, port: Optional[int]):
token=node.token token=node.token
) )
) )
await message.answer(result.json()) yield result
responses.append(result)
await message.answer(f"{host}:{port}")
async def check_web(message: Message, host: str, port: Optional[int]):
if port is None:
port = 80
rsp_msg = await message.answer(f"Отчет о проверке хоста {host}:{port}...\n\n")
iter_keys = 1
async for res in send_check_requests(host, port):
# set typing status...
await message.bot.send_chat_action(message.chat.id, 'typing')
node_formatted_response = await prepare_webcheck_message(res)
rsp_msg = await rsp_msg.edit_text(rsp_msg.text + f"\n{iter_keys}. {node_formatted_response}")
iter_keys = iter_keys + 1
await rsp_msg.edit_text(rsp_msg.text + f"\n\nПроверка завершена!")
async def web_cmd(msg: Message): async def web_cmd(msg: Message):

View File

@ -3,4 +3,6 @@ from typing import List
nodes: List[APINode] = [ nodes: List[APINode] = [
APINode("http://localhost:8080", "CHANGE_TOKEN_BY_ENV"), APINode("http://localhost:8080", "CHANGE_TOKEN_BY_ENV"),
APINode("http://localhost:8080", "CHANGE_TOKEN_BY_ENV"),
APINode("http://localhost:8080", "CHANGE_TOKEN_BY_ENV"),
] ]