Skip to content

Commit 17c662d

Browse files
committed
Bug fixes and spam prevention
1 parent a3169c8 commit 17c662d

File tree

10 files changed

+167
-80
lines changed

10 files changed

+167
-80
lines changed

.idea/.idea.Alibi/.idea/contentModel.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.Alibi/.idea/workspace.xml

Lines changed: 84 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Alibi.Plugins.Webhook/DiscordWebhook.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ public override void OnModCall(IClient caller, string reason)
6464
if (_validConfig && _enabled)
6565
{
6666
string decodedMessage = Configuration.ModMessage;
67-
decodedMessage = decodedMessage.Replace("%ch", caller.CharacterName);
68-
decodedMessage = decodedMessage.Replace("%a", caller.Area.Name);
67+
decodedMessage = decodedMessage.Replace("%ch", caller.CharacterName ?? "Spectator");
68+
decodedMessage = decodedMessage.Replace("%a", caller.Area!.Name);
6969
decodedMessage = decodedMessage.Replace("%r", reason);
7070
decodedMessage = decodedMessage.Replace("%ip", caller.IpAddress.ToString());
71-
decodedMessage = decodedMessage.Replace("%hwid", caller.HardwareId);
72-
decodedMessage = decodedMessage.Replace("%lsm", caller.LastSentMessage);
71+
decodedMessage = decodedMessage.Replace("%hwid", caller.HardwareId ?? "");
72+
decodedMessage = decodedMessage.Replace("%lsm", caller.LastSentMessage ?? "");
7373
_hook.SendMessage(decodedMessage);
7474
}
7575
}
@@ -79,13 +79,13 @@ public override void OnBan(IClient banned, string reason, TimeSpan? expires = nu
7979
if (_validConfig && _enabled)
8080
{
8181
string decodedMessage = Configuration.BanMessage;
82-
decodedMessage = decodedMessage.Replace("%ch", banned.CharacterName);
82+
decodedMessage = decodedMessage.Replace("%ch", banned.CharacterName ?? "Spectator");
8383
decodedMessage = decodedMessage.Replace("%e",
8484
expires != null ? expires.Value.LargestIntervalWithUnits() : "Never.");
8585
decodedMessage = decodedMessage.Replace("%r", reason);
8686
decodedMessage = decodedMessage.Replace("%ip", banned.IpAddress.ToString());
87-
decodedMessage = decodedMessage.Replace("%hwid", banned.HardwareId);
88-
decodedMessage = decodedMessage.Replace("%lsm", banned.LastSentMessage);
87+
decodedMessage = decodedMessage.Replace("%hwid", banned.HardwareId ?? "");
88+
decodedMessage = decodedMessage.Replace("%lsm", banned.LastSentMessage ?? "");
8989
_hook.SendMessage(decodedMessage);
9090
}
9191
}

Alibi/Client.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ public void BanHwid(string reason, TimeSpan? expireDate)
139139

140140
public void BanIp(string reason, TimeSpan? expireDate)
141141
{
142-
foreach (var hdid in Server.Database.GetHwidsfromIp(IpAddress.ToString()))
142+
foreach (var hwid in Server.Database.GetHwidsfromIp(IpAddress.ToString()))
143143
{
144-
BanHwid(reason, expireDate);
144+
ServerRef.FindUser(hwid)?.BanHwid(reason, expireDate);
145145
}
146146
}
147147

Alibi/ClientSession.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,38 @@ protected override void OnConnected()
2727
{
2828
if (((Server)Server).ConnectedPlayers >= Alibi.Server.ServerConfiguration.MaxPlayers)
2929
{
30-
Send(new AOPacket("BD", "Max players has been reached."));
30+
Send(new AOPacket("BD", "Not a real ban: Max players has been reached."));
3131
Task.Delay(500);
3232
Disconnect();
33+
return;
3334
}
3435

3536
_banCheckTime = DateTime.Now;
3637

3738
var ip = ((IPEndPoint)Socket.RemoteEndPoint).Address;
3839
if (Alibi.Server.ServerConfiguration.Advertise && ip.Equals(Alibi.Server.MasterServerIp))
3940
Alibi.Server.Logger.Log(LogSeverity.Info, " Probed by master server.", true);
40-
if (((Server) Server).ClientsConnected.Count(c => Equals(c.IpAddress, ip))
41-
> Alibi.Server.ServerConfiguration.MaxMultiClients)
41+
if (!IPAddress.IsLoopback(ip) && ((Server) Server).ClientsConnected.Count(c => ip.ToString() == c.IpAddress.ToString())
42+
>= Alibi.Server.ServerConfiguration.MaxMultiClients)
4243
{
43-
Send(new AOPacket("BD", $"Can't have more than " +
44+
Send(new AOPacket("BD", $"Not a real ban: Can't have more than " +
4445
$"{Alibi.Server.ServerConfiguration.MaxMultiClients} clients at a time."));
4546
Task.Delay(500);
4647
Disconnect();
48+
return;
4749
}
4850
Client = new Client((Server)Server, this, ip);
4951
Client.LastAlive = DateTime.Now;
5052
Client.KickIfBanned();
51-
53+
5254
// fuck fantaencrypt
53-
SendAsync(new AOPacket("decryptor", "NOENCRYPT"));
55+
Send(new AOPacket("decryptor", "NOENCRYPT"));
5456
}
5557

5658
protected override void OnDisconnected()
5759
{
5860
((Server)Server).ClientsConnected.Remove(Client);
59-
if (Client.Connected)
61+
if (Client != null && Client.Connected)
6062
{
6163
((Server)Server).ConnectedPlayers--;
6264
((Area)Client.Area)!.PlayerCount--;
@@ -79,7 +81,9 @@ protected override void OnReceived(byte[] buffer, long offset, long size)
7981
string[] packets = msg.Split("%", StringSplitOptions.RemoveEmptyEntries);
8082
foreach (var packet in packets)
8183
{
82-
if (Client.HardwareId == null && !packet.StartsWith("HI#"))
84+
if (Client.HardwareId == null
85+
&& !packet.StartsWith("HI#")
86+
&& !packet.StartsWith("WSIP#"))
8387
return;
8488
if (DateTime.Now.CompareTo(_banCheckTime.AddSeconds
8589
(Alibi.Server.ServerConfiguration.RateLimitResetTime)) >= 0)

Alibi/Configuration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class Configuration
2727
public int MaxPlayers = 100;
2828
public int MaxMultiClients = 4;
2929
public int TimeoutSeconds = 60;
30+
public int MaxMessageSize = 256;
31+
public int MaxShownameSize = 16;
3032
public int RateLimit = 50;
3133
public int RateLimitResetTime = 1;
3234
public TimeSpan RateLimitBanLength = new TimeSpan(0, 5, 0);

Alibi/Protocol/IcValidator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ internal static AOPacket ValidateIcPacket(IAOPacket packet, IClient client)
1212
{
1313
if (client.Character == null || !client.Connected)
1414
throw new IcValidationException("Client does not have a character or isn't connected.");
15+
if(packet.Objects.Length < 16)
16+
throw new IcValidationException("Didn't provide a full Ic Message.");
1517

1618
List<string> validatedObjects = new List<string>(packet.Objects.Length);
1719

@@ -41,6 +43,8 @@ internal static AOPacket ValidateIcPacket(IAOPacket packet, IClient client)
4143
// Make sure message is sanitized(eventually) and prevent double messages
4244
// TODO: Sanitization and zalgo cleaning
4345
string sentMessage = packet.Objects[4].Trim();
46+
if(sentMessage.Length > Server.ServerConfiguration.MaxMessageSize)
47+
throw new IcValidationException("Message was too long.");
4448
if (!Server.ServerConfiguration.AllowDoublePostsIfDifferentAnim && sentMessage == client.LastSentMessage)
4549
throw new IcValidationException("Cannot double post.");
4650
if (Server.ServerConfiguration.AllowDoublePostsIfDifferentAnim
@@ -116,6 +120,8 @@ internal static AOPacket ValidateIcPacket(IAOPacket packet, IClient client)
116120
if (packet.Objects.Length > 15)
117121
{
118122
// Showname
123+
if(packet.Objects[15].Length > Server.ServerConfiguration.MaxShownameSize)
124+
throw new IcValidationException($"Showname is longer than {Server.ServerConfiguration.MaxShownameSize}");
119125
validatedObjects.Add(packet.Objects[15]);
120126

121127
// First object is the charID, second is whether or not they're in front

Alibi/Protocol/MessageHandler.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@ public static void HandleMessage(IClient client, IAOPacket packet)
1919
{
2020
if (Handlers.ContainsKey(packet.Type))
2121
{
22-
var stateAttr = Handlers[packet.Type].Method.GetCustomAttribute<RequireStateAttribute>();
22+
try
23+
{
24+
var stateAttr = Handlers[packet.Type].Method.GetCustomAttribute<RequireStateAttribute>();
2325

24-
if (stateAttr != null)
25-
if (client.CurrentState != stateAttr.State)
26-
{
27-
client.Kick("Protocol violation.");
28-
return;
29-
}
30-
Handlers[packet.Type].Method.Invoke(Handlers[packet.Type].Target, new object[] { client, packet });
26+
if (stateAttr != null)
27+
if (client.CurrentState != stateAttr.State)
28+
{
29+
client.Kick("Protocol violation.");
30+
return;
31+
}
32+
33+
Handlers[packet.Type].Method.Invoke(Handlers[packet.Type].Target, new object[] {client, packet});
34+
}
35+
catch (TargetInvocationException e)
36+
{
37+
Server.Logger.Log(LogSeverity.Error, $" Error handling message: {e.Message}\n{e.StackTrace}");
38+
}
3139
}
3240
else
3341
Server.Logger.Log(LogSeverity.Warning, $" Unknown client message: '{packet.Type}'", true);

Alibi/Protocol/Messages.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using System.Linq;
99
using System.Net;
10+
using System.Threading.Tasks;
1011

1112
// ReSharper disable UnusedType.Global
1213
// ReSharper disable UnusedParameter.Global
@@ -156,7 +157,7 @@ internal static void Ready(IClient client, IAOPacket packet)
156157
if (client.Connected)
157158
return;
158159

159-
((Client)client).Area = client.ServerRef.Areas.First();
160+
((Client)client).Area = client.ServerRef.Areas.First(a => a.Locked == "FREE");
160161
((Client)client).Connected = true;
161162
client.CurrentState = ClientState.InArea;
162163
client.ServerRef.ConnectedPlayers++;
@@ -297,9 +298,17 @@ internal static void IcMessage(IClient client, IAOPacket packet)
297298
[RequireState(ClientState.InArea)]
298299
internal static void OocMessage(IClient client, IAOPacket packet)
299300
{
301+
if (packet.Objects.Length < 2)
302+
return;
303+
300304
// TODO: Sanitization and cleaning (especially Zalgo)
301305
// maybe put this into anti-spam plugin
302306
string message = packet.Objects[1];
307+
if (message.Length > Server.ServerConfiguration.MaxMessageSize)
308+
{
309+
client.SendOocMessage("Message was too long.");
310+
return;
311+
}
303312
((Client)client).OocName = packet.Objects[0];
304313
if (message.StartsWith("/"))
305314
{
@@ -359,7 +368,7 @@ internal static void ModCall(IClient client, IAOPacket packet)
359368
}
360369

361370
[MessageHandler("WSIP")]
362-
[RequireState(ClientState.NewClient)]
371+
[RequireState(ClientState.Identified)]
363372
internal static void UpdateWebsocketIp(IClient client, IAOPacket packet)
364373
{
365374
IPAddress ip = IPAddress.Parse(packet.Objects[0]);
@@ -369,6 +378,11 @@ internal static void UpdateWebsocketIp(IClient client, IAOPacket packet)
369378
((Client)client).IpAddress = ip;
370379
client.KickIfBanned();
371380
}
381+
if (((Server) client.ServerRef).ClientsConnected.Count(c => ip.ToString() == c.IpAddress.ToString())
382+
>= Alibi.Server.ServerConfiguration.MaxMultiClients)
383+
{
384+
client.Kick($"Cannot have more than {Server.ServerConfiguration.MaxMultiClients} clients at the same");
385+
}
372386
}
373387

374388
private static bool CanModifyEvidence(IClient client)

Alibi/Server.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,32 @@ public void OnAllPluginsLoaded()
247247

248248
public void OnModCall(IClient client, IAOPacket packet)
249249
{
250-
_pluginManager.GetAllPlugins().ForEach(p => p.OnModCall(client, packet.Objects[0]));
250+
_pluginManager.GetAllPlugins().ForEach(p =>
251+
{
252+
try
253+
{
254+
p.OnModCall(client, packet.Objects[0]);
255+
}
256+
catch (Exception e)
257+
{
258+
p.Log(LogSeverity.Error, $"Error occured during OnModCall(), {e.Message}\n{e.StackTrace}");
259+
}
260+
});
251261
}
252262

253263
public void OnBan(IClient client, string reason, TimeSpan? expires = null)
254264
{
255-
_pluginManager.GetAllPlugins().ForEach(p => p.OnBan(client, reason, expires));
265+
_pluginManager.GetAllPlugins().ForEach(p =>
266+
{
267+
try
268+
{
269+
p.OnBan(client, reason, expires);
270+
}
271+
catch (Exception e)
272+
{
273+
p.Log(LogSeverity.Error, $"Error occured during OnBan(), {e.Message}\n{e.StackTrace}");
274+
}
275+
});
256276
}
257277

258278
protected override TcpSession CreateSession()

0 commit comments

Comments
 (0)