feat: Moves TcpServer to another thread. Rewrites Firewall (#1660)

## Breaking Changes
* The Firewall and IP Limiter have been rewritten. Please read the notes carefully!
* `TcpServer.Instances` moved back to `NetState.Instances` - sorry - it was stupid to move it to begin with.

> [!Note]
> Sockets that fail the IP Limiter or Firewall will be immediately and forcibly disconnected.
> This means they will be stuck at "Verifying account..." if it was a real client.

### Summary
- Removes firewall wildcard support.
- Removes `AccessRestrictions`.
- Moves Firewall/IPLimiter to the core.
- Moves `TcpServer` to its own thread.
- Removes the `SocketConnect` and `SocketDisconnect` event sinks.
- Moves `Instances` back to `NetState.Instances`.
- Fixes a long standing bug with bad handling of duplicate listener addresses.

#### Firewall
The firewall has been completely rewritten. There is now an "Admin Firewall" which saves to the config file. Secondarily, there is an internal firewall used exclusively by the TcpServer while processing sockets. The Admin firewall mirrors it's additions/deletions to the internal firewall by adding requests to a queue.

> [!IMPORTANT]  
> **Wildcard firewall entries, such as `X`, `*`, `?` are not allowed.**
> **Ranges in between IP classes or sextets are not allowed.**
> **Please make sure to use one of the following:**
> * IP Address - `192.168.1.1`
> * CIDR - `192.168.1.0/24`
> * Range - `192.168.1.1-192.168.1.100`

#### IP Limiter
The IP Limiter has been completely rewritten. The available configurations are:
```json
"ipLimiter.enable": "True",
"ipLimiter.maxConnectionsPerIP": 10,
"ipLimiter.clearConnectionAttemptsDuration": "00:00:00:10",
"ipLimiter.clearThrottledDuration": "00:00:02:00",
```

The IP Limiter is set up to prevent spamming connections from the same IP. Every time an IP connects, it is added to a connection list. After 10 attempts, the IP is added to the throttle list. To keep the system fast, the connection list is entirely wiped every 10 seconds, and the throttle list is entirely wiped every 2 minutes.
This commit is contained in:
Kamron Batman 2024-01-20 14:25:12 -08:00 committed by GitHub
parent 5f3de6537b
commit 4cd668ef61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 1117 additions and 1030 deletions

View file

@ -15,6 +15,7 @@
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Net;
@ -53,12 +54,15 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
private static readonly IPollGroup _pollGroup = PollGroup.Create();
private static readonly Queue<NetState> _flushPending = new(2048);
private static readonly Queue<NetState> _flushedPartials = new(256);
private static readonly Queue<NetState> _disposed = new(256);
private static readonly ConcurrentQueue<NetState> _disposed = new();
private static readonly Queue<NetState> _throttled = new(256);
private static readonly Queue<NetState> _throttledPending = new(256);
public static NetStateCreatedCallback CreatedCallback { get; set; }
private static readonly HashSet<NetState> _instances = new(2048);
public static IReadOnlySet<NetState> Instances => _instances;
private readonly string _toString;
private ClientVersion _version;
private long _nextActivityCheck;
@ -152,8 +156,6 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
TraceException(ex);
Disconnect("Unable to add socket to poll group");
}
CreatedCallback?.Invoke(this);
}
// Sectors
@ -965,6 +967,16 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
public static void Slice()
{
const int maxEntriesPerLoop = 32;
var count = 0;
while (++count <= maxEntriesPerLoop && TcpServer.ConnectedQueue.TryDequeue(out var ns))
{
CreatedCallback?.Invoke(ns);
_instances.Add(ns);
ns.LogInfo($"Connected. [{Instances.Count} Online]");
}
while (_throttled.Count > 0)
{
var ns = _throttled.Dequeue();
@ -980,7 +992,7 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
_throttled.Enqueue(_throttledPending.Dequeue());
}
int count = _pollGroup.Poll(_polledStates);
count = _pollGroup.Poll(_polledStates);
if (count > 0)
{
@ -1037,7 +1049,7 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
{
long curTicks = Core.TickCount;
foreach (var ns in TcpServer.Instances)
foreach (var ns in Instances)
{
ns.CheckAlive(curTicks);
}
@ -1116,7 +1128,7 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
public static void TraceDisconnect(string reason, string ip)
{
if (reason == string.Empty)
if (string.IsNullOrWhiteSpace(reason))
{
return;
}
@ -1140,6 +1152,12 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
private void Dispose()
{
// It's possible we could queue for dispose multiple times
if (Connection == null)
{
return;
}
TraceDisconnect(_disconnectReason, _toString);
if (_running)
@ -1164,7 +1182,8 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
m.NetState = null;
}
TcpServer.Instances.Remove(this);
_instances.Remove(this);
try
{
_pollGroup.Remove(Connection, _handle);
@ -1191,7 +1210,7 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
CityInfo = null;
Connection = null;
var count = TcpServer.Instances.Count;
var count = Instances.Count;
LogInfo(a != null ? $"Disconnected. [{count} Online] [{a}]" : $"Disconnected. [{count} Online]");
}