## 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.
133 lines
3.4 KiB
C#
133 lines
3.4 KiB
C#
using System;
|
|
using System.Buffers;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Runtime.CompilerServices;
|
|
using Server.Logging;
|
|
using Server.Network;
|
|
|
|
namespace Server;
|
|
|
|
public static class AdminFirewall
|
|
{
|
|
private static readonly ILogger logger = LogFactory.GetLogger(typeof(AdminFirewall));
|
|
|
|
private static readonly HashSet<IFirewallEntry> _firewallSet = [];
|
|
private const string firewallConfigPath = "firewall.cfg";
|
|
|
|
public static void Configure()
|
|
{
|
|
if (File.Exists(firewallConfigPath))
|
|
{
|
|
var searchValues = SearchValues.Create("*Xx?");
|
|
|
|
using var ip = new StreamReader(firewallConfigPath);
|
|
|
|
while (ip.ReadLine() is { } line)
|
|
{
|
|
line = line.Trim();
|
|
|
|
if (line.Length == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (line.AsSpan().ContainsAny(searchValues))
|
|
{
|
|
logger.Warning("Legacy firewall entry \"{Entry}\" ignored", line);
|
|
continue;
|
|
}
|
|
|
|
Add(ToFirewallEntry(line), false);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Note: This is not optimized, so do not use this in hot paths
|
|
public static IReadOnlySet<IFirewallEntry> Set => _firewallSet;
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static IFirewallEntry ToFirewallEntry(object entry)
|
|
{
|
|
return entry switch
|
|
{
|
|
IFirewallEntry firewallEntry => firewallEntry,
|
|
IPAddress address => new SingleIpFirewallEntry(address),
|
|
string s => ToFirewallEntry(s),
|
|
_ => null
|
|
};
|
|
}
|
|
|
|
public static IFirewallEntry ToFirewallEntry(string entry)
|
|
{
|
|
if (entry == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
var rangeSeparator = entry.IndexOf('-');
|
|
if (rangeSeparator > -1)
|
|
{
|
|
return new CidrFirewallEntry(
|
|
IPAddress.Parse(entry.AsSpan(0, rangeSeparator)),
|
|
IPAddress.Parse(entry.AsSpan(rangeSeparator + 1))
|
|
);
|
|
}
|
|
|
|
// CIDR notation
|
|
if (entry.IndexOf('/') > -1)
|
|
{
|
|
return new CidrFirewallEntry(entry);
|
|
}
|
|
|
|
return new SingleIpFirewallEntry(entry);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static void Remove(object obj, bool save = true)
|
|
{
|
|
var entry = ToFirewallEntry(obj);
|
|
|
|
if (entry != null)
|
|
{
|
|
_firewallSet.Remove(entry);
|
|
Firewall.RequestRemoveEntry(entry); // Request that the TcpServer also remove the entry
|
|
|
|
if (save)
|
|
{
|
|
Save();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static bool Add(object obj) => Add(ToFirewallEntry(obj));
|
|
|
|
public static bool Add(IFirewallEntry entry, bool save = true)
|
|
{
|
|
var added = _firewallSet.Add(entry);
|
|
Firewall.RequestAddEntry(entry); // Request that the TcpServer also add the entry
|
|
|
|
if (save)
|
|
{
|
|
Save();
|
|
}
|
|
|
|
return added;
|
|
}
|
|
|
|
public static void Save()
|
|
{
|
|
using var op = new StreamWriter(firewallConfigPath);
|
|
foreach (var entry in Set)
|
|
{
|
|
op.WriteLine(entry);
|
|
}
|
|
}
|
|
}
|