Skip to content

Commit

Permalink
feat(mc): add proper server IP validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ZRunner committed Jan 27, 2024
1 parent 75076d3 commit e85a594
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
47 changes: 28 additions & 19 deletions fcts/minecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
from typing import Any, Optional, Union

import aiohttp
import discord
from asyncache import cached
from cachetools import TTLCache
import discord
from dateutil.parser import isoparse
from discord import app_commands
from discord.ext import commands
from fcts.rss import can_use_rss

from fcts.rss import can_use_rss
from libs.bot_classes import Axobot, MyContext
from libs.checks import checks
from libs.formatutils import FormatUtils
from libs.rss.rss_general import FeedObject

SERVER_ADDRESS_REGEX = re.compile(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|"
r"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|"
r"[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$")

def _similar(input_1: str, input_2: str):
"Compare two strings and output the similarity ratio"
Expand Down Expand Up @@ -242,16 +245,11 @@ async def mc_server(self, ctx: MyContext, ip: str, port: Optional[int] = None):
..Example minecraft server play.gunivers.net
..Doc minecraft.html#mc"""
if ":" in ip and port is None:
ip, port_str = ip.split(":")
if not port_str.isnumeric():
await ctx.send(await self.bot._(ctx.guild, "minecraft.invalid-port"))
return
port = int(port_str)
elif port is None:
port_str = None
else:
port_str = str(port)
if (validation := await self.validate_server_ip(ip, port)) is None:
await ctx.send(await self.bot._(ctx.guild.id, "minecraft.invalid-ip"))
return
ip, port = validation
port_str = str(port) if port else ''
await ctx.defer()
obj = await self.create_server_1(ctx.guild, ip, port_str)
embed = await self.form_msg_server(obj, ctx.guild, (ip, port_str))
Expand All @@ -271,13 +269,10 @@ async def mc_follow_server(self, ctx: MyContext, ip: str, port: Optional[int] =
if not ctx.bot.database_online:
await ctx.send(await self.bot._(ctx.guild.id, "cases.no_database"))
return
if ":" in ip and port is None:
ip, port_str = ip.split(":")
if not port_str.isnumeric():
await ctx.send(await self.bot._(ctx.guild, "minecraft.invalid-port"))
return
port = int(port_str)
# TODO: add proper ip validation and testing
if (validation := await self.validate_server_ip(ip, port)) is None:
await ctx.send(await self.bot._(ctx.guild.id, "minecraft.invalid-ip"))
return
ip, port = validation
await ctx.defer()
is_over, flow_limit = await self.bot.get_cog('Rss').is_overflow(ctx.guild)
if is_over:
Expand All @@ -300,6 +295,20 @@ async def mc_follow_server(self, ctx: MyContext, ip: str, port: Optional[int] =
await ctx.send(await self.bot._(ctx.guild, "errors.unknown2", about=cmd))
self.bot.dispatch("error", err, ctx)

async def validate_server_ip(self, ip: str, port: Optional[int] = None):
"Validate a server IP and port"
if ":" in ip and port is None:
ip, port_str = ip.split(":")
if not port_str.isnumeric():
return None
port = int(port_str)
if port is not None:
if not 0 < port < 65536:
return None
if not SERVER_ADDRESS_REGEX.match(ip):
return None
return ip, port

async def create_server_1(self, guild: discord.Guild, ip: str, port: Optional[str]=None) -> Union[str, 'MCServer']:
"Collect and serialize server data from a given IP, using minetools.eu"
if port is None:
Expand Down
2 changes: 1 addition & 1 deletion lang/minecraft/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"cant-embed": "Cannot send embed. Please make sure the \"Embed links\" permission is enabled.",
"invalid-port": "Invalid server port",
"invalid-ip": "Invalid server IP",
"mod-fields": {
"author": "Author",
"authors": "Authors",
Expand Down
2 changes: 1 addition & 1 deletion lang/minecraft/fr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"cant-embed": "Impossible d'envoyez l'embed. Vérifiez que la permission \"Embed links\" est bien activée",
"invalid-port": "Numéro de port invalide",
"invalid-ip": "IP de serveur invalide",
"mod-fields": {
"author": "Auteur",
"authors": "Auteurs",
Expand Down

0 comments on commit e85a594

Please sign in to comment.