2021-01-25 17:28:59 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
from discord.ext import commands
|
|
|
|
|
|
|
|
from tuxbot.cogs.Network.functions.exceptions import InvalidIp
|
|
|
|
|
|
|
|
|
|
|
|
def _(x):
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
2021-01-26 09:43:25 +01:00
|
|
|
DOMAIN_PATTERN = r"^([A-Za-z0-9]\.|[A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9]\.){1,3}[A-Za-z]{2,6}$"
|
|
|
|
IP_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])$"
|
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://", "")
|
|
|
|
|
|
|
|
check_domain = re.match(DOMAIN_PATTERN, argument)
|
|
|
|
check_ip = re.match(IP_PATTERN, argument)
|
|
|
|
|
|
|
|
if check_domain or check_ip:
|
|
|
|
return argument
|
|
|
|
|
|
|
|
raise InvalidIp(_("Invalid ip or domain"))
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
argument = argument.replace("-", "").replace("p", "").replace("v", "")
|
|
|
|
|
|
|
|
if argument not in ["4", "6"]:
|
|
|
|
raise InvalidIp(_("Invalid ip version"))
|
|
|
|
|
|
|
|
return argument
|