Skip to content

Commit f804615

Browse files
committed
Cleanup of Server Access & Nuget Updates
1 parent 5b97cfd commit f804615

File tree

8 files changed

+125
-76
lines changed

8 files changed

+125
-76
lines changed

Zolian.GameServer/Zolian.GameServer.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141
</PropertyGroup>
4242

4343
<ItemGroup>
44-
<PackageReference Include="Chaos-Networking" Version="2.7.6" />
44+
<PackageReference Include="Chaos-Networking" Version="2.8.0" />
4545
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.0-preview2.25289.6" />
4646
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
4747
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.1">
4848
<TreatAsUsed>true</TreatAsUsed>
4949
</PackageReference>
50-
<PackageReference Include="Sentry" Version="5.16.2" />
51-
<PackageReference Include="Sentry.Extensions.Logging" Version="5.16.2" />
52-
<PackageReference Include="Sentry.Profiling" Version="5.16.2" />
50+
<PackageReference Include="Sentry" Version="6.0.0" />
51+
<PackageReference Include="Sentry.Extensions.Logging" Version="6.0.0" />
52+
<PackageReference Include="Sentry.Profiling" Version="6.0.0" />
5353
<PackageReference Include="Serilog.Extensions.Logging" Version="10.0.0" />
5454
<PackageReference Include="Serilog.Sinks.Async" Version="2.1.0" />
5555
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />

Zolian.Server.Base/Network/Server/BadActor.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Newtonsoft.Json;
66
using RestSharp;
77
using Darkages.Models;
8+
using ServiceStack.Text;
89

910
namespace Darkages.Network.Server;
1011

@@ -17,7 +18,7 @@ public class ReportInfo
1718

1819
public static class BadActor
1920
{
20-
private static readonly IMemoryCache IpCache = new MemoryCache(new MemoryCacheOptions());
21+
public static readonly IMemoryCache IpCache = new MemoryCache(new MemoryCacheOptions());
2122

2223
// Good actors: cache “clean” for 30 days
2324
private static readonly TimeSpan CacheDurationGoodActor = TimeSpan.FromDays(30);
@@ -380,5 +381,21 @@ private static bool IsVpnBotUsageType(string? usageType)
380381
_ => false
381382
};
382383

384+
private static readonly HashSet<string> _bannedIPs = [];
385+
386+
public static bool BannedIpCheck(string ip)
387+
{
388+
if (ip.IsNullOrEmpty()) return true;
389+
390+
// Check against known bad actors cache
391+
var knownBadActor = BadActor.IpCache.TryGetValue(ip, out _);
392+
if (knownBadActor) return true;
393+
394+
// Add banned player IPs to the _bannedIPs HashSet
395+
_bannedIPs.Add("0.0.0.0");
396+
397+
return _bannedIPs.Contains(ip);
398+
}
399+
383400
private static bool IsKeyCodeValid(string? keyCode) => !string.IsNullOrWhiteSpace(keyCode);
384401
}

Zolian.Server.Base/Network/Server/LobbyServer.cs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,30 @@ protected override void IndexHandlers()
134134
ClientHandlers[(byte)ClientOpCode.ServerTableRequest] = OnServerTableRequest;
135135
}
136136

137+
protected override bool ShouldAcceptConnection(Socket clientSocket, out string? rejectReason)
138+
{
139+
rejectReason = null;
140+
141+
string ipAddress;
142+
try
143+
{
144+
ipAddress = (clientSocket.RemoteEndPoint as IPEndPoint)?.Address.ToString() ?? "unknown";
145+
}
146+
catch
147+
{
148+
rejectReason = "Rejecting connection: unable to read RemoteEndPoint.";
149+
return false;
150+
}
151+
152+
if (!IsConnectionAllowed(ipAddress))
153+
{
154+
rejectReason = $"Too many connection attempts - throttled {ipAddress}";
155+
return false;
156+
}
157+
158+
return true;
159+
}
160+
137161
protected override void OnConnected(Socket clientSocket)
138162
{
139163
ServerSetup.ConnectionLogger($"Lobby connection from {clientSocket.RemoteEndPoint as IPEndPoint}");
@@ -149,54 +173,56 @@ protected override void OnConnected(Socket clientSocket)
149173
client.OnDisconnected += OnDisconnect;
150174
var safe = false;
151175

152-
var banned = BannedIpCheck(ipAddress.ToString());
176+
// Check for banned IPs
177+
var banned = BadActor.BannedIpCheck(ipAddress.ToString());
153178
if (banned)
154179
{
155-
client.Disconnect();
156-
ServerSetup.ConnectionLogger($"Banned connection attempt from {ip}");
180+
try
181+
{
182+
ServerSetup.ConnectionLogger($"Banned connection attempt on Lobby Server from {ip}");
183+
client.Disconnect();
184+
}
185+
catch { }
186+
157187
return;
158188
}
159189

190+
// Check against known good actors cache
160191
foreach (var _ in ServerSetup.Instance.GlobalKnownGoodActorsCache.Values.Where(savedIp => savedIp == ipAddress.ToString()))
161192
safe = true;
162193

163194
if (!safe)
164195
{
196+
// Check against bad actor service
165197
var isBadActor = Task.Run(() => BadActor.ClientOnBlackListAsync(ipAddress.ToString())).Result;
166198

167199
if (isBadActor)
168200
{
169201
try
170202
{
171-
client.Disconnect();
172203
ServerSetup.ConnectionLogger($"Disconnected Bad Actor from {ip}");
204+
client.Disconnect();
173205
}
174-
catch
175-
{
176-
// ignored
177-
}
206+
catch { }
178207

179208
return;
180209
}
181-
182-
ServerSetup.ConnectionLogger($"Good Actor! {ip}");
183210
}
184211

212+
// Register client and check for ID collisions
185213
if (!ClientRegistry.TryAdd(client))
186214
{
187-
ServerSetup.ConnectionLogger("Two clients ended up with the same id - newest client disconnected");
188215
try
189216
{
217+
ServerSetup.ConnectionLogger("ID Collision - Lobby Server");
190218
client.Disconnect();
191219
}
192-
catch
193-
{
194-
// ignored
195-
}
220+
catch { }
196221

197222
return;
198223
}
199224

225+
// Add to passed checks cache for Lobby Server
200226
ServerSetup.Instance.GlobalLobbyConnection.TryAdd(ipAddress, ipAddress);
201227
client.BeginReceive();
202228
// 0x7E - Handshake
@@ -209,17 +235,5 @@ private void OnDisconnect(object sender, EventArgs e)
209235
ClientRegistry.TryRemove(client.Id, out _);
210236
}
211237

212-
private readonly HashSet<string> _bannedIPs = [];
213-
214-
private bool BannedIpCheck(string ip)
215-
{
216-
if (ip.IsNullOrEmpty()) return true;
217-
218-
// Add banned player IPs to the _bannedIPs HashSet
219-
_bannedIPs.Add("0.0.0.0");
220-
221-
return _bannedIPs.Contains(ip);
222-
}
223-
224238
#endregion
225239
}

Zolian.Server.Base/Network/Server/LoginServer.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ async ValueTask InnerOnLogin(ILoginClient localClient, LoginArgs localArgs)
264264
{
265265
var ipLocal = IPAddress.Parse(ServerSetup.Instance.InternalAddress);
266266

267-
if (GameMastersIPs.Any(ip => localClient.RemoteIp.Equals(IPAddress.Parse(ip)))
267+
if (GameMastersIPs.Any(ip => localClient.RemoteIp.Equals(IPAddress.Parse(ip)))
268268
|| IPAddress.IsLoopback(localClient.RemoteIp) || localClient.RemoteIp.Equals(ipLocal))
269269
{
270270
_ = Login(result, redirect, localClient);
@@ -275,16 +275,16 @@ async ValueTask InnerOnLogin(ILoginClient localClient, LoginArgs localArgs)
275275
return;
276276
}
277277
default:
278-
{
279-
if (result.Hacked)
280278
{
281-
localClient.SendLoginMessage(LoginMessageType.CharacterDoesntExist, "Bruteforce detected, we've locked the account to protect it; If this is your account, please contact the GM.");
282-
return;
283-
}
279+
if (result.Hacked)
280+
{
281+
localClient.SendLoginMessage(LoginMessageType.CharacterDoesntExist, "Bruteforce detected, we've locked the account to protect it; If this is your account, please contact the GM.");
282+
return;
283+
}
284284

285-
_ = Login(result, redirect, localClient);
286-
break;
287-
}
285+
_ = Login(result, redirect, localClient);
286+
break;
287+
}
288288
}
289289
}
290290
}
@@ -484,41 +484,52 @@ protected override void OnConnected(Socket clientSocket)
484484
var client = _clientProvider.CreateClient(clientSocket);
485485
client.OnDisconnected += OnDisconnect;
486486

487-
if (!ClientRegistry.TryAdd(client))
487+
// Check for banned IPs
488+
var banned = BadActor.BannedIpCheck(ipAddress.ToString());
489+
if (banned)
488490
{
489-
ServerSetup.ConnectionLogger("Two clients ended up with the same id - newest client disconnected");
490491
try
491492
{
493+
ServerSetup.ConnectionLogger($"Banned connection attempt on Login Server from {ip}");
492494
client.Disconnect();
493495
}
494-
catch
496+
catch { }
497+
498+
return;
499+
}
500+
501+
// Register client and check for ID collisions
502+
if (!ClientRegistry.TryAdd(client))
503+
{
504+
try
495505
{
496-
// ignored
506+
ServerSetup.ConnectionLogger("ID Collision - Login Server");
507+
client.Disconnect();
497508
}
509+
catch { }
498510

499511
return;
500512
}
501513

514+
// Verify connection passed through appropriate port access
502515
var lobbyCheck = ServerSetup.Instance.GlobalLobbyConnection.TryGetValue(ipAddress, out _);
503516

504517
if (!lobbyCheck)
505518
{
506519
try
507520
{
508521
client.Disconnect();
522+
ServerSetup.ConnectionLogger("---------Login-Server---------");
523+
var comment = $"{ipAddress} has been blocked for violating security protocols through improper port access.";
524+
ServerSetup.ConnectionLogger(comment, LogLevel.Warning);
525+
Task.Run(() => BadActor.ReportMaliciousEndpoint(ipAddress.ToString(), comment));
509526
}
510-
catch
511-
{
512-
// ignored
513-
}
527+
catch { }
514528

515-
ServerSetup.ConnectionLogger("---------Login-Server---------");
516-
var comment = $"{ipAddress} has been blocked for violating security protocols through improper port access.";
517-
ServerSetup.ConnectionLogger(comment, LogLevel.Warning);
518-
Task.Run(() => BadActor.ReportMaliciousEndpoint(ipAddress.ToString(), comment));
519529
return;
520530
}
521531

532+
// Add to passed checks cache for Login Server
522533
ServerSetup.Instance.GlobalLoginConnection.TryAdd(ipAddress, ipAddress);
523534
client.BeginReceive();
524535
// 0x7E - Handshake

Zolian.Server.Base/Network/Server/ServerSetup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public class ServerSetup : IServerContext
8787
public ConcurrentDictionary<long, ConcurrentDictionary<string, KillRecord>> GlobalKillRecordCache { get; set; } = [];
8888
public ConcurrentDictionary<IPAddress, IPAddress> GlobalLobbyConnection { get; set; } = [];
8989
public ConcurrentDictionary<IPAddress, IPAddress> GlobalLoginConnection { get; set; } = [];
90-
public ConcurrentDictionary<IPAddress, IPAddress> GlobalWorldConnection { get; set; } = [];
9190
public ConcurrentDictionary<IPAddress, byte> GlobalCreationCount { get; set; } = [];
9291
public ConcurrentDictionary<IPAddress, byte> GlobalPasswordAttempt { get; set; } = [];
9392

Zolian.Server.Base/Network/Server/WorldServer.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3138,22 +3138,34 @@ protected override void OnConnected(Socket clientSocket)
31383138
var client = _clientProvider.CreateClient(clientSocket);
31393139
client.OnDisconnected += OnDisconnect;
31403140

3141-
if (!ClientRegistry.TryAdd(client))
3141+
// Check for banned IPs
3142+
var banned = BadActor.BannedIpCheck(ipAddress.ToString());
3143+
if (banned)
31423144
{
3143-
ServerSetup.ConnectionLogger("Two clients ended up with the same id - newest client disconnected");
3144-
31453145
try
31463146
{
3147+
ServerSetup.ConnectionLogger($"Banned connection attempt on World Server from {ip}");
31473148
client.Disconnect();
31483149
}
3149-
catch
3150+
catch { }
3151+
3152+
return;
3153+
}
3154+
3155+
// Register client and check for ID collisions
3156+
if (!ClientRegistry.TryAdd(client))
3157+
{
3158+
try
31503159
{
3151-
// ignored
3160+
ServerSetup.ConnectionLogger("ID Collision - World Server");
3161+
client.Disconnect();
31523162
}
3163+
catch { }
31533164

31543165
return;
31553166
}
31563167

3168+
// Verify connection passed through appropriate port access
31573169
var lobbyCheck = ServerSetup.Instance.GlobalLobbyConnection.TryGetValue(ipAddress, out _);
31583170
var loginCheck = ServerSetup.Instance.GlobalLoginConnection.TryGetValue(ipAddress, out _);
31593171

@@ -3162,20 +3174,16 @@ protected override void OnConnected(Socket clientSocket)
31623174
try
31633175
{
31643176
client.Disconnect();
3177+
ServerSetup.ConnectionLogger("---------World-Server---------");
3178+
var comment = $"{ipAddress} has been blocked for violating security protocols through improper port access.";
3179+
ServerSetup.ConnectionLogger(comment, LogLevel.Warning);
3180+
Task.Run(() => BadActor.ReportMaliciousEndpoint(ipAddress.ToString(), comment));
31653181
}
3166-
catch
3167-
{
3168-
// ignored
3169-
}
3182+
catch { }
31703183

3171-
ServerSetup.ConnectionLogger("---------World-Server---------");
3172-
var comment = $"{ipAddress} has been blocked for violating security protocols through improper port access.";
3173-
ServerSetup.ConnectionLogger(comment, LogLevel.Warning);
3174-
Task.Run(() => BadActor.ReportMaliciousEndpoint(ipAddress.ToString(), comment));
31753184
return;
31763185
}
31773186

3178-
ServerSetup.Instance.GlobalWorldConnection.TryAdd(ipAddress, ipAddress);
31793187
client.BeginReceive();
31803188
}
31813189

Zolian.Server.Base/Zolian.Server.Base.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@
4242
</PropertyGroup>
4343

4444
<ItemGroup>
45-
<PackageReference Include="Chaos-Networking" Version="2.7.6" />
45+
<PackageReference Include="Chaos-Networking" Version="2.8.0" />
4646
<PackageReference Include="Dapper.StrongName" Version="2.1.66" />
4747
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.0-preview2.25289.6" />
4848
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
4949
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
5050
<PackageReference Include="RestSharp" Version="113.0.0" />
51-
<PackageReference Include="Sentry" Version="5.16.2" />
52-
<PackageReference Include="Sentry.Extensions.Logging" Version="5.16.2" />
53-
<PackageReference Include="Sentry.Profiling" Version="5.16.2" />
51+
<PackageReference Include="Sentry" Version="6.0.0" />
52+
<PackageReference Include="Sentry.Extensions.Logging" Version="6.0.0" />
53+
<PackageReference Include="Sentry.Profiling" Version="6.0.0" />
5454
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
5555
<PackageReference Include="ServiceStack" Version="10.0.4" />
5656
<PackageReference Include="ServiceStack.Text" Version="10.0.4" />

ZolianTest/ZolianTest.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="Chaos-Networking" Version="2.7.6" />
22+
<PackageReference Include="Chaos-Networking" Version="2.8.0" />
2323
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.0-preview2.25289.6" />
2424
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
2525
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
2626
<PackageReference Include="NUnit" Version="4.4.0" />
27-
<PackageReference Include="Sentry" Version="5.16.2" />
28-
<PackageReference Include="Sentry.Extensions.Logging" Version="5.16.2" />
29-
<PackageReference Include="Sentry.Profiling" Version="5.16.2" />
27+
<PackageReference Include="Sentry" Version="6.0.0" />
28+
<PackageReference Include="Sentry.Extensions.Logging" Version="6.0.0" />
29+
<PackageReference Include="Sentry.Profiling" Version="6.0.0" />
3030
<PackageReference Include="ServiceStack" Version="10.0.4" />
3131
<PackageReference Include="ServiceStack.Text" Version="10.0.4" />
3232
</ItemGroup>

0 commit comments

Comments
 (0)