diff --git a/apps/tgbot/tgbot/bot.py b/apps/tgbot/tgbot/bot.py index bf2a7fc..954a8ee 100644 --- a/apps/tgbot/tgbot/bot.py +++ b/apps/tgbot/tgbot/bot.py @@ -1,12 +1,14 @@ +from asyncio import sleep + from aiogram import Bot, Dispatcher, executor from aiogram.contrib.fsm_storage.memory import MemoryStorage -from .middlewares import UserMiddleware, WriteCommandMetric, LoggingMiddleware, ThrottlingMiddleware +from loguru import logger from tortoise import Tortoise from tortoise.exceptions import DBConnectionError -from loguru import logger -from asyncio import sleep -from . import config -from . import handlers + +from . import config, handlers +from .middlewares import (LoggingMiddleware, ThrottlingMiddleware, + UserMiddleware, WriteCommandMetric) storage = MemoryStorage() telegram_bot = Bot(token=config.TELEGRAM_BOT_TOKEN) diff --git a/apps/tgbot/tgbot/handlers/base.py b/apps/tgbot/tgbot/handlers/base.py index 141a7e1..27de800 100644 --- a/apps/tgbot/tgbot/handlers/base.py +++ b/apps/tgbot/tgbot/handlers/base.py @@ -1,18 +1,19 @@ -from aiogram.types import Message -from typing import Tuple, Any, List - -from ..nodes import nodes as all_nodes -from httpx import Response -from aiogram.bot import Bot from datetime import datetime -from core.coretypes import APINodeInfo -from .helpers import send_api_requests -from .errors import NotEnoughArgs, InvalidPort, LocalhostForbidden -from .validators import BaseValidator, LocalhostValidator -from ..middlewares.throttling import rate_limit -from loguru import logger -from uuid import uuid4 from time import time +from typing import Any, List, Tuple +from uuid import uuid4 + +from aiogram.bot import Bot +from aiogram.types import Message +from core.coretypes import APINodeInfo +from httpx import Response +from loguru import logger + +from ..middlewares.throttling import rate_limit +from ..nodes import nodes as all_nodes +from .errors import InvalidPort, LocalhostForbidden, NotEnoughArgs +from .helpers import send_api_requests +from .validators import BaseValidator, LocalhostValidator header = "Отчет о проверке хоста:" \ "\n\n— Хост: {target_fq}"\ diff --git a/apps/tgbot/tgbot/handlers/default/__init__.py b/apps/tgbot/tgbot/handlers/default/__init__.py index 149d615..dbdfc8d 100644 --- a/apps/tgbot/tgbot/handlers/default/__init__.py +++ b/apps/tgbot/tgbot/handlers/default/__init__.py @@ -1,12 +1,12 @@ from aiogram import Dispatcher +from .icmp import ICMPCheckerHandler +from .ipcalc import IPCalcCommandHandler +from .minecraft import MinecraftCheckerHandler from .start import start_cmd +from .tcp import TCPCheckerHandler from .web import WebCheckerHandler from .whois import WhoisCommandHandler -from .icmp import ICMPCheckerHandler -from .tcp import TCPCheckerHandler -from .minecraft import MinecraftCheckerHandler -from .ipcalc import IPCalcCommandHandler def setup(dp: Dispatcher): diff --git a/apps/tgbot/tgbot/handlers/default/icmp.py b/apps/tgbot/tgbot/handlers/default/icmp.py index 47a8e69..1ee495d 100644 --- a/apps/tgbot/tgbot/handlers/default/icmp.py +++ b/apps/tgbot/tgbot/handlers/default/icmp.py @@ -1,9 +1,10 @@ from aiogram.types import Message -from httpx import Response from core.coretypes import ErrorPayload, ICMPCheckerResponse, ResponseStatus -from ..base import CheckerBaseHandler, NotEnoughArgs, LocalhostForbidden -from ..metrics import push_status_metric +from httpx import Response + from ...middlewares.throttling import rate_limit +from ..base import CheckerBaseHandler, LocalhostForbidden, NotEnoughArgs +from ..metrics import push_status_metric icmp_help_message = """ ❓ Производит проверку хоста по протоколу ICMP. diff --git a/apps/tgbot/tgbot/handlers/default/ipcalc.py b/apps/tgbot/tgbot/handlers/default/ipcalc.py index 79a45fc..d40a7ef 100644 --- a/apps/tgbot/tgbot/handlers/default/ipcalc.py +++ b/apps/tgbot/tgbot/handlers/default/ipcalc.py @@ -1,9 +1,10 @@ -from aiogram.types import Message -from typing import Union import ipaddress +from typing import Union + +from aiogram.types import Message -from ..base import SimpleCommandHandler, NotEnoughArgs from ...middlewares.throttling import rate_limit +from ..base import NotEnoughArgs, SimpleCommandHandler ipcalc_help_message = """ ❓ Калькулятор IP подсетей. diff --git a/apps/tgbot/tgbot/handlers/default/minecraft.py b/apps/tgbot/tgbot/handlers/default/minecraft.py index 4e75cab..3e76070 100644 --- a/apps/tgbot/tgbot/handlers/default/minecraft.py +++ b/apps/tgbot/tgbot/handlers/default/minecraft.py @@ -1,4 +1,4 @@ -from core.coretypes import ResponseStatus, ErrorPayload, MinecraftResponse +from core.coretypes import ErrorPayload, MinecraftResponse, ResponseStatus from httpx import Response from ..base import CheckerTargetPortHandler, process_args_for_host_port diff --git a/apps/tgbot/tgbot/handlers/default/start.py b/apps/tgbot/tgbot/handlers/default/start.py index f6e8a1e..978636e 100644 --- a/apps/tgbot/tgbot/handlers/default/start.py +++ b/apps/tgbot/tgbot/handlers/default/start.py @@ -1,7 +1,8 @@ from aiogram.types import Message -from ...models.user import User + from ...middlewares.throttling import rate_limit from ...middlewares.userdata import userdata_required +from ...models.user import User start_message = """ diff --git a/apps/tgbot/tgbot/handlers/default/tcp.py b/apps/tgbot/tgbot/handlers/default/tcp.py index a71e35c..d3797ee 100644 --- a/apps/tgbot/tgbot/handlers/default/tcp.py +++ b/apps/tgbot/tgbot/handlers/default/tcp.py @@ -1,11 +1,13 @@ from typing import Tuple + from aiogram.types import Message -from core.coretypes import ResponseStatus, ErrorPayload, PortResponse +from core.coretypes import ErrorPayload, PortResponse, ResponseStatus from httpx import Response -from ..base import CheckerTargetPortHandler, NotEnoughArgs, InvalidPort, parse_host_port -from ..metrics import push_status_metric from ...middlewares.throttling import rate_limit +from ..base import (CheckerTargetPortHandler, InvalidPort, NotEnoughArgs, + parse_host_port) +from ..metrics import push_status_metric tcp_help_message = """ ❓ Производит проверку TCP порта, открыт ли он или нет diff --git a/apps/tgbot/tgbot/handlers/default/web.py b/apps/tgbot/tgbot/handlers/default/web.py index 7fce4a3..83b4ef4 100644 --- a/apps/tgbot/tgbot/handlers/default/web.py +++ b/apps/tgbot/tgbot/handlers/default/web.py @@ -1,5 +1,7 @@ +from core.coretypes import (HTTP_EMOJI, ErrorPayload, HttpCheckerResponse, + ResponseStatus) from httpx import Response -from core.coretypes import ResponseStatus, HTTP_EMOJI, HttpCheckerResponse, ErrorPayload + from ..base import CheckerTargetPortHandler, process_args_for_host_port from ..metrics import push_status_metric diff --git a/apps/tgbot/tgbot/handlers/default/whois.py b/apps/tgbot/tgbot/handlers/default/whois.py index 687ac3c..da3e298 100644 --- a/apps/tgbot/tgbot/handlers/default/whois.py +++ b/apps/tgbot/tgbot/handlers/default/whois.py @@ -1,15 +1,16 @@ +from dataclasses import dataclass from typing import Optional -from whois import whois, parser from aiogram.types import Message -from dataclasses import dataclass from whois_vu.api import WhoisSource from whois_vu.errors import IncorrectZone, QueryNotMatchRegexp -from ..whois_zones import ZONES -from ..base import SimpleCommandHandler -from ..errors import NotEnoughArgs, LocalhostForbidden +from whois import parser, whois + from ...middlewares.throttling import rate_limit +from ..base import SimpleCommandHandler +from ..errors import LocalhostForbidden, NotEnoughArgs +from ..whois_zones import ZONES whois_help_message = """ ❓ Вернёт информацию о домене. diff --git a/apps/tgbot/tgbot/handlers/helpers.py b/apps/tgbot/tgbot/handlers/helpers.py index 63b4d92..3d41ee7 100644 --- a/apps/tgbot/tgbot/handlers/helpers.py +++ b/apps/tgbot/tgbot/handlers/helpers.py @@ -1,14 +1,16 @@ -from httpx import AsyncClient, Timeout, Response -from typing import List, Callable -from core.coretypes import APINode -from ipaddress import ip_address -from contextlib import suppress -from loguru import logger -from aiogram.bot import Bot -from .metrics import push_api_request_status -from ..config import NOTIFICATION_BOT_TOKEN, NOTIFICATION_USERS -from traceback import format_exc import asyncio +from contextlib import suppress +from ipaddress import ip_address +from traceback import format_exc +from typing import Callable, List + +from aiogram.bot import Bot +from core.coretypes import APINode +from httpx import AsyncClient, Response, Timeout +from loguru import logger + +from ..config import NOTIFICATION_BOT_TOKEN, NOTIFICATION_USERS +from .metrics import push_api_request_status async def send_api_request(client: AsyncClient, endpoint: str, data: dict, node: APINode): diff --git a/apps/tgbot/tgbot/handlers/metrics.py b/apps/tgbot/tgbot/handlers/metrics.py index 3943be7..42559a1 100644 --- a/apps/tgbot/tgbot/handlers/metrics.py +++ b/apps/tgbot/tgbot/handlers/metrics.py @@ -1,6 +1,9 @@ -from aioinflux import InfluxDBClient from typing import Dict -from ..config import INFLUX_DB, INFLUX_HOST, INFLUX_PORT, INFLUX_PASSWORD, INFLUX_USERNAME + +from aioinflux import InfluxDBClient + +from ..config import (INFLUX_DB, INFLUX_HOST, INFLUX_PASSWORD, INFLUX_PORT, + INFLUX_USERNAME) async def push_metric(measurement, tags: Dict, fields: Dict): diff --git a/apps/tgbot/tgbot/handlers/validators.py b/apps/tgbot/tgbot/handlers/validators.py index 160317e..44d7f6c 100644 --- a/apps/tgbot/tgbot/handlers/validators.py +++ b/apps/tgbot/tgbot/handlers/validators.py @@ -1,6 +1,7 @@ -from .errors import LocalhostForbidden -from ipaddress import ip_address from contextlib import suppress +from ipaddress import ip_address + +from .errors import LocalhostForbidden class BaseValidator: diff --git a/apps/tgbot/tgbot/middlewares/__init__.py b/apps/tgbot/tgbot/middlewares/__init__.py index 498c6e6..641ad65 100644 --- a/apps/tgbot/tgbot/middlewares/__init__.py +++ b/apps/tgbot/tgbot/middlewares/__init__.py @@ -1,4 +1,4 @@ -from .write_command_metric import WriteCommandMetric from .logging import LoggingMiddleware from .throttling import ThrottlingMiddleware from .userdata import UserMiddleware +from .write_command_metric import WriteCommandMetric diff --git a/apps/tgbot/tgbot/middlewares/throttling.py b/apps/tgbot/tgbot/middlewares/throttling.py index 462cb90..0cfed7d 100644 --- a/apps/tgbot/tgbot/middlewares/throttling.py +++ b/apps/tgbot/tgbot/middlewares/throttling.py @@ -1,9 +1,10 @@ +import asyncio + from aiogram import Dispatcher, types from aiogram.dispatcher import DEFAULT_RATE_LIMIT from aiogram.dispatcher.handler import CancelHandler, current_handler from aiogram.dispatcher.middlewares import BaseMiddleware from aiogram.utils.exceptions import Throttled -import asyncio def rate_limit(func): diff --git a/apps/tgbot/tgbot/middlewares/userdata.py b/apps/tgbot/tgbot/middlewares/userdata.py index 2a6e559..72a1b99 100644 --- a/apps/tgbot/tgbot/middlewares/userdata.py +++ b/apps/tgbot/tgbot/middlewares/userdata.py @@ -1,6 +1,6 @@ from aiogram.dispatcher.handler import current_handler from aiogram.dispatcher.middlewares import BaseMiddleware -from aiogram.types import Message, CallbackQuery +from aiogram.types import CallbackQuery, Message from ..models import User diff --git a/apps/tgbot/tgbot/middlewares/write_command_metric.py b/apps/tgbot/tgbot/middlewares/write_command_metric.py index 13b0ac5..2729fa2 100644 --- a/apps/tgbot/tgbot/middlewares/write_command_metric.py +++ b/apps/tgbot/tgbot/middlewares/write_command_metric.py @@ -1,5 +1,6 @@ from aiogram.dispatcher.middlewares import BaseMiddleware from aiogram.types import Message + from ..handlers.metrics import push_metric from ..models import User, UserCheckRequests diff --git a/apps/tgbot/tgbot/nodes.py b/apps/tgbot/tgbot/nodes.py index ba5208d..1c02cab 100644 --- a/apps/tgbot/tgbot/nodes.py +++ b/apps/tgbot/tgbot/nodes.py @@ -1,6 +1,7 @@ -from core.coretypes import APINode from typing import List +from core.coretypes import APINode + nodes: List[APINode] = [ APINode("http://localhost:8080", "CHANGE_TOKEN_BY_ENV"), APINode("http://localhost:8080", "CHANGE_TOKEN_BY_ENV"),