2021-01-25 17:28:59 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
from discord.ext import commands
|
|
|
|
|
2021-01-26 17:11:30 +01:00
|
|
|
from tuxbot.cogs.Network.functions.exceptions import (
|
|
|
|
InvalidIp,
|
|
|
|
InvalidDomain,
|
|
|
|
InvalidQueryType,
|
|
|
|
)
|
2021-01-25 17:28:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _(x):
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
2021-02-16 19:38:42 +01:00
|
|
|
DOMAIN_PATTERN = r"^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$"
|
2021-01-26 11:59:36 +01:00
|
|
|
IPV4_PATTERN = r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
|
|
|
|
IPV6_PATTERN = r"^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"
|
2021-01-25 17:28:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
class IPConverter(commands.Converter):
|
2021-01-26 09:43:25 +01:00
|
|
|
async def convert(self, ctx, argument): # skipcq: PYL-W0613
|
2021-01-25 17:28:59 +01:00
|
|
|
argument = argument.replace("http://", "").replace("https://", "")
|
2021-02-11 18:21:50 +01:00
|
|
|
argument = argument.rstrip("/")
|
2021-01-25 17:28:59 +01:00
|
|
|
|
|
|
|
check_domain = re.match(DOMAIN_PATTERN, argument)
|
2021-01-26 11:59:36 +01:00
|
|
|
check_ipv4 = re.match(IPV4_PATTERN, argument)
|
|
|
|
check_ipv6 = re.match(IPV6_PATTERN, argument)
|
2021-01-25 17:28:59 +01:00
|
|
|
|
2021-01-26 11:59:36 +01:00
|
|
|
if check_domain or check_ipv4 or check_ipv6:
|
2021-01-25 17:28:59 +01:00
|
|
|
return argument
|
|
|
|
|
|
|
|
raise InvalidIp(_("Invalid ip or domain"))
|
|
|
|
|
|
|
|
|
2021-01-26 15:24:10 +01:00
|
|
|
class IPCheckerConverter(commands.Converter):
|
|
|
|
async def convert(self, ctx, argument): # skipcq: PYL-W0613
|
2021-01-26 17:11:30 +01:00
|
|
|
if not argument.startswith("http"):
|
|
|
|
return f"http://{argument}"
|
|
|
|
|
|
|
|
return argument
|
|
|
|
|
|
|
|
|
|
|
|
class DomainCheckerConverter(commands.Converter):
|
|
|
|
async def convert(self, ctx, argument): # skipcq: PYL-W0613
|
2021-01-26 15:24:10 +01:00
|
|
|
argument = argument.replace("http://", "").replace("https://", "")
|
|
|
|
|
|
|
|
check_domain = re.match(DOMAIN_PATTERN, argument)
|
|
|
|
|
2021-01-26 17:11:30 +01:00
|
|
|
if check_domain:
|
|
|
|
return argument
|
2021-01-26 15:24:10 +01:00
|
|
|
|
2021-01-26 17:11:30 +01:00
|
|
|
raise InvalidDomain(_("Invalid domain"))
|
2021-01-26 15:24:10 +01:00
|
|
|
|
2021-01-26 17:11:30 +01:00
|
|
|
|
|
|
|
class QueryTypeConverter(commands.Converter):
|
|
|
|
async def convert(self, ctx, argument): # skipcq: PYL-W0613
|
|
|
|
argument = argument.lower()
|
|
|
|
query_types = [
|
|
|
|
"a",
|
|
|
|
"aaaa",
|
|
|
|
"cname",
|
|
|
|
"ns",
|
|
|
|
"ds",
|
|
|
|
"dnskey",
|
|
|
|
"soa",
|
|
|
|
"txt",
|
|
|
|
"ptr",
|
|
|
|
"mx",
|
|
|
|
]
|
|
|
|
|
|
|
|
if argument in query_types:
|
|
|
|
return argument
|
|
|
|
|
|
|
|
raise InvalidQueryType(
|
|
|
|
_(
|
|
|
|
"Supported queries : A, AAAA, CNAME, NS, DS, DNSKEY, SOA, TXT, PTR, MX"
|
|
|
|
)
|
|
|
|
)
|
2021-01-26 15:24:10 +01:00
|
|
|
|
|
|
|
|
2021-01-25 17:28:59 +01:00
|
|
|
class IPVersionConverter(commands.Converter):
|
2021-01-26 09:43:25 +01:00
|
|
|
async def convert(self, ctx, argument): # skipcq: PYL-W0613
|
2021-01-25 17:28:59 +01:00
|
|
|
if not argument:
|
|
|
|
return argument
|
|
|
|
|
2021-01-30 13:44:34 +01:00
|
|
|
argument = argument.replace("-", "").replace("ip", "").replace("v", "")
|
2021-01-25 17:28:59 +01:00
|
|
|
|
|
|
|
if argument not in ["4", "6"]:
|
|
|
|
raise InvalidIp(_("Invalid ip version"))
|
|
|
|
|
|
|
|
return argument
|