starting rewriting whois

This commit is contained in:
kiriharu 2021-01-13 22:36:05 +03:00
parent 8afcb2e087
commit 09fd459a1f
3 changed files with 34 additions and 18 deletions

View File

@ -21,6 +21,7 @@ header = "Отчет о проверке хоста:" \
class SimpleCommandHandler: class SimpleCommandHandler:
help_message = "Set help message in class!" help_message = "Set help message in class!"
localhost_forbidden_message = "❗ Локальные адреса запрещены"
validators: List[BaseValidator] = [LocalhostValidator()] validators: List[BaseValidator] = [LocalhostValidator()]
@rate_limit @rate_limit
@ -39,7 +40,6 @@ class SimpleCommandHandler:
class CheckerBaseHandler(SimpleCommandHandler): class CheckerBaseHandler(SimpleCommandHandler):
localhost_forbidden_message = "❗ Локальные адреса запрещены"
invalid_port_message = "Invalid port!" invalid_port_message = "Invalid port!"
header_message = header header_message = header
api_endpoint = "Set api endpoint in class!" api_endpoint = "Set api endpoint in class!"

View File

@ -2,7 +2,7 @@ from aiogram import Dispatcher
from .start import start_cmd from .start import start_cmd
from .web import WebCheckerHandler from .web import WebCheckerHandler
from .whois import whois_cmd from .whois import WhoisCommandHandler
from .icmp import ICMPCheckerHandler from .icmp import ICMPCheckerHandler
from .tcp import TCPCheckerHandler from .tcp import TCPCheckerHandler
from .minecraft import MinecraftCheckerHandler from .minecraft import MinecraftCheckerHandler
@ -12,7 +12,7 @@ from .ipcalc import IPCalcCommandHandler
def setup(dp: Dispatcher): def setup(dp: Dispatcher):
dp.register_message_handler(start_cmd, is_forwarded=False, commands=['start']) dp.register_message_handler(start_cmd, is_forwarded=False, commands=['start'])
dp.register_message_handler(WebCheckerHandler().handler, is_forwarded=False, commands=['web', 'http']) dp.register_message_handler(WebCheckerHandler().handler, is_forwarded=False, commands=['web', 'http'])
dp.register_message_handler(whois_cmd, is_forwarded=False, commands=['whois']) dp.register_message_handler(WhoisCommandHandler().handler, is_forwarded=False, commands=['whois'])
dp.register_message_handler(ICMPCheckerHandler().handler, is_forwarded=False, commands=['icmp', 'ping']) dp.register_message_handler(ICMPCheckerHandler().handler, is_forwarded=False, commands=['icmp', 'ping'])
dp.register_message_handler(TCPCheckerHandler().handler, is_forwarded=False, commands=['tcp']) dp.register_message_handler(TCPCheckerHandler().handler, is_forwarded=False, commands=['tcp'])
dp.register_message_handler(MinecraftCheckerHandler().handler, is_forwarded=False, commands=['minecraft']) dp.register_message_handler(MinecraftCheckerHandler().handler, is_forwarded=False, commands=['minecraft'])

View File

@ -2,7 +2,8 @@ from whois import whois, parser
from aiogram.types import Message from aiogram.types import Message
from aiogram.utils.markdown import quote_html from aiogram.utils.markdown import quote_html
from tgbot.handlers.helpers import validate_local from tgbot.handlers.base import SimpleCommandHandler
from tgbot.handlers.errors import NotEnoughArgs, LocalhostForbidden
from tgbot.middlewares.throttling import rate_limit from tgbot.middlewares.throttling import rate_limit
whois_help_message = """ whois_help_message = """
@ -17,10 +18,7 @@ no_domain_text = """
Напишите /whois чтобы посмотреть справку. Напишите /whois чтобы посмотреть справку.
""" """
localhost_exception = "❗Локальные адреса запрещены!"
# TODO: Very shitty code. I should rewrite this.
def create_whois_message(domain: str) -> str: def create_whois_message(domain: str) -> str:
try: try:
domain_info = whois(domain) domain_info = whois(domain)
@ -80,14 +78,32 @@ def create_whois_message(domain: str) -> str:
return message return message
@rate_limit class WhoisCommandHandler(SimpleCommandHandler):
async def whois_cmd(msg: Message):
args = msg.text.split() help_message = whois_help_message
if len(args) == 1:
return await msg.answer(no_domain_text) def __init__(self):
if len(args) >= 2: super().__init__()
host = args[1]
if validate_local(args[0]): @rate_limit
return await msg.answer(localhost_exception, parse_mode="Markdown") async def handler(self, message: Message):
await msg.bot.send_chat_action(msg.chat.id, 'typing') try:
await msg.answer(create_whois_message(host), parse_mode='html') args = await self.process_args(message.text)
except NotEnoughArgs:
await message.answer(no_domain_text, parse_mode='Markdown')
except LocalhostForbidden:
await message.answer(self.localhost_forbidden_message, parse_mode='Markdown')
else:
await message.answer(create_whois_message(args[0]), parse_mode='html')
async def process_args(self, text: str) -> list:
args = text.split()
if len(args) == 1:
raise NotEnoughArgs
if len(args) >= 2:
host = args[1]
await self.validate_target(host)
return [host] # only domain name
async def prepare_message(self) -> str:
pass