0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-16 08:35:07 +00:00

add command: ban ip <ipv4-address>

To add an IPv4 address to the ban list.
This commit is contained in:
Robin C. Ladiges 2023-03-22 21:39:47 +01:00
parent e13840d543
commit 5dcb85eb68
No known key found for this signature in database
GPG key ID: B494D3DF92661B99
2 changed files with 33 additions and 2 deletions

View file

@ -1,4 +1,5 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using Shared.Packet.Packets;
@ -27,6 +28,14 @@ public static class BanLists {
}
private static bool IsIPv4(string str) {
return IPAddress.TryParse(str, out IPAddress? ip)
&& ip != null
&& ip.AddressFamily == AddressFamily.InterNetwork;
;
}
public static bool IsIPv4Banned(Client user) {
IPEndPoint? ipv4 = (IPEndPoint?) user.Socket?.RemoteEndPoint;
if (ipv4 == null) { return false; }
@ -102,10 +111,16 @@ public static class BanLists {
});
}
private static void CrashMultiple(string[] args, MUCH much) {
foreach (Client user in much(args).toActUpon) {
Crash(user, true);
}
}
public static string HandleBanCommand(string[] args, MUCH much) {
if (args.Length == 0) {
return "Usage: ban player <* | !* (usernames to not ban...) | (usernames to ban...)>";
return "Usage: ban {player|ip} ...";
}
string cmd = args[0];
@ -113,7 +128,7 @@ public static class BanLists {
switch (cmd) {
default:
return "Usage: ban player <* | !* (usernames to not ban...) | (usernames to ban...)>";
return "Usage: ban {player|ip} ...";
case "player":
if (args.Length == 0) {
@ -138,6 +153,21 @@ public static class BanLists {
Save();
return sb.ToString();
case "ip":
if (args.Length != 1) {
return "Usage: ban ip <ipv4-address>";
}
if (!IsIPv4(args[0])) {
return "Invalid IPv4 address!";
}
if (IsIPv4Banned(args[0])) {
return "IP " + args[0] + " is already banned.";
}
BanIPv4(args[0]);
CrashMultiple(args, much);
Save();
return "Banned ip: " + args[0];
}
}
}

View file

@ -228,6 +228,7 @@ server.PacketHandler = (c, p) => {
IEnumerable<Client> search = server.Clients.Where(c => c.Connected && (
c.Name.ToLower().StartsWith(arg.ToLower())
|| (Guid.TryParse(arg, out Guid res) && res == c.Id)
|| (IPAddress.TryParse(arg, out IPAddress? ip) && ip.Equals(((IPEndPoint) c.Socket!.RemoteEndPoint!).Address))
));
if (!search.Any()) {
failToFind.Add(arg); //none found