Skip to content

Commit 554517b

Browse files
Conntrack (#3901)
<!-- IT'S NOT WIZDENS REPO, IF YOU WANT TO ADD YOUR CHANGES ON ALL SERVERS, CREATE PR TO WIZDENS REPO --> ## Short description A fix for a few more places that still use the old IP. ## Why we need to add this ## Media (Video/Screenshots) <!-- If your PR contains in-game changes you must provide screenshots/videos of the changes. --> ## Checks <!-- check boxes for faster reviewing of your PR --> - [x] I do not require assistance to complete the PR. - [x] Before posting/requesting review of a PR, I have verified that the changes work. - [x] I have added screenshots/videos of the changes, or this PR does not change in-game mechanics. - [x] I affirm that my changes are licensed under the [MIT License](https://github.com/ss14Starlight/space-station-14/blob/Starlight/LICENSE.TXT) and grant permission for use in this repository under its conditions. **Changelog** <!-- If you want the players to know about changes made in this PR, specify them using the template outside the comment. Short and informative. :cl: STARLIGHT TEAM - add: Added Starlight. - remove: Removed SS13. - tweak: Changed SS14. - fix: Fixed Rinary. --> --------- Co-authored-by: Walker Fowlkes <47339836+walksanatora@users.noreply.github.com>
1 parent 8816207 commit 554517b

4 files changed

Lines changed: 38 additions & 7 deletions

File tree

Content.Server/Administration/Managers/BanManager.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using System.Text.Json;
3131
using System.Text.RegularExpressions;
3232
using Content.Server.Discord;
33+
using Content.Server.Connection;
3334
using Content.Server._NullLink.Core;
3435
using Content.Server._NullLink.Helpers;
3536
using Content.Shared.Starlight.CCVar;
@@ -42,6 +43,7 @@ namespace Content.Server.Administration.Managers;
4243
public sealed partial class BanManager : IBanManager, IPostInjectInit
4344
{
4445
[Dependency] private readonly IActorRouter _actor = default!; // nulllink
46+
[Dependency] private readonly IConnectionManager _connectionManager = default!; // Starlight
4547
[Dependency] private readonly IConfigurationManager _cfg = default!;
4648
[Dependency] private readonly IChatManager _chat = default!;
4749
[Dependency] private readonly IServerDbManager _db = default!;
@@ -100,7 +102,9 @@ private async Task CachePlayerData(ICommonSession player, CancellationToken canc
100102
var netChannel = player.Channel;
101103
ImmutableArray<byte>? hwId = netChannel.UserData.HWId.Length == 0 ? null : netChannel.UserData.HWId;
102104
var modernHwids = netChannel.UserData.ModernHWIds;
103-
var roleBans = await _db.GetServerRoleBansAsync(netChannel.RemoteEndPoint.Address, player.UserId, hwId, modernHwids, false);
105+
var addr = _connectionManager.GetResolvedAddress(player.UserId)
106+
?? netChannel.RemoteEndPoint.Address; // Starlight: prefer resolved IP
107+
var roleBans = await _db.GetServerRoleBansAsync(addr, player.UserId, hwId, modernHwids, false);
104108

105109
var userRoleBans = new List<ServerRoleBanDef>();
106110
foreach (var ban in roleBans)
@@ -221,7 +225,8 @@ private bool BanMatchesPlayer(ICommonSession player, ServerBanDef ban)
221225
var playerInfo = new BanMatcher.PlayerInfo
222226
{
223227
UserId = player.UserId,
224-
Address = player.Channel.RemoteEndPoint.Address,
228+
Address = _connectionManager.GetResolvedAddress(player.UserId)
229+
?? player.Channel.RemoteEndPoint.Address, // Starlight: prefer resolved IP
225230
HWId = player.Channel.UserData.HWId,
226231
ModernHWIds = player.Channel.UserData.ModernHWIds,
227232
// It's possible for the player to not have cached data loading yet due to coincidental timing.

Content.Server/Administration/PlayerLocator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ internal sealed class PlayerLocator : IPlayerLocator, IDisposable, IPostInjectIn
7676
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
7777
[Dependency] private readonly IServerDbManager _db = default!;
7878
[Dependency] private readonly ILogManager _logManager = default!;
79+
[Dependency] private readonly IConnectionManager _connectionManager = default!; // Starlight
7980

8081
private readonly HttpClient _httpClient = new();
8182
private ISawmill _sawmill = default!;
@@ -148,10 +149,11 @@ public PlayerLocator()
148149
return new LocatedPlayerData(new NetUserId(responseData.UserId), null, null, responseData.UserName, null, []);
149150
}
150151

151-
private static LocatedPlayerData ReturnForSession(ICommonSession session)
152+
private LocatedPlayerData ReturnForSession(ICommonSession session) // Starlight: non-static, uses resolved IP
152153
{
153154
var userId = session.UserId;
154-
var address = session.Channel.RemoteEndPoint.Address;
155+
var address = _connectionManager.GetResolvedAddress(userId)
156+
?? session.Channel.RemoteEndPoint.Address; // Starlight: prefer resolved IP
155157
var hwId = session.Channel.UserData.GetModernHwid();
156158
return new LocatedPlayerData(
157159
userId,

Content.Server/Administration/PlayerPanelEui.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Content.Server.Administration.Logs;
33
using Content.Server.Administration.Managers;
44
using Content.Server.Administration.Notes;
5+
using Content.Server.Connection;
56
using Content.Server.Database;
67
using Content.Server.EUI;
78
using Content.Shared.Administration;
@@ -17,6 +18,7 @@ namespace Content.Server.Administration;
1718
public sealed class PlayerPanelEui : BaseEui
1819
{
1920
[Dependency] private readonly IAdminManager _admins = default!;
21+
[Dependency] private readonly IConnectionManager _connectionManager = default!; // Starlight
2022
[Dependency] private readonly IServerDbManager _db = default!;
2123
[Dependency] private readonly IAdminNotesManager _notesMan = default!;
2224
[Dependency] private readonly IEntityManager _entity = default!;
@@ -179,7 +181,10 @@ public async void SetPlayerState()
179181
_notes = null;
180182
}
181183

182-
_sharedConnections = _player.Sessions.Count(s => s.Channel.RemoteEndPoint.Address.Equals(_targetPlayer.LastAddress) && s.UserId != _targetPlayer.UserId);
184+
_sharedConnections = _player.Sessions.Count(s =>
185+
(_connectionManager.GetResolvedAddress(s.UserId) ?? s.Channel.RemoteEndPoint.Address) // Starlight: prefer resolved IP
186+
.Equals(_targetPlayer.LastAddress)
187+
&& s.UserId != _targetPlayer.UserId);
183188

184189
// Apparently the Bans flag is also used for whitelists
185190
if (_admins.HasAdminFlag(Player, AdminFlags.Ban))

Content.Server/Connection/ConnectionManager.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ public interface IConnectionManager
5555
void AddTemporaryConnectBypass(NetUserId user, TimeSpan duration);
5656

5757
void Update();
58+
59+
/// <summary>
60+
/// Gets the resolved real client IP for a connected user.
61+
/// When conntrack resolution is active, this returns the real IP behind SNAT.
62+
/// Returns <c>null</c> if the user has no cached address.
63+
/// </summary>
64+
IPAddress? GetResolvedAddress(NetUserId user); // Starlight
5865
}
5966

6067
/// <summary>
@@ -82,6 +89,7 @@ public sealed partial class ConnectionManager : IConnectionManager
8289

8390
private ISawmill _sawmill = default!;
8491
private readonly Dictionary<NetUserId, TimeSpan> _temporaryBypasses = [];
92+
private readonly Dictionary<NetUserId, IPAddress> _resolvedAddresses = []; // Starlight
8593
private IPIntel.IPIntel _ipintel = default!;
8694
private ConntrackResolver _conntrack = default!; // Starlight
8795

@@ -116,6 +124,12 @@ public void AddTemporaryConnectBypass(NetUserId user, TimeSpan duration)
116124
time = newTime;
117125
}
118126

127+
// Starlight: resolved IP cache
128+
public IPAddress? GetResolvedAddress(NetUserId user)
129+
{
130+
return _resolvedAddresses.GetValueOrDefault(user);
131+
}
132+
119133
public async void Update()
120134
{
121135
try
@@ -181,6 +195,7 @@ private async Task NetMgrOnConnecting(NetConnectingArgs e)
181195
}
182196
else
183197
{
198+
_resolvedAddresses[userId] = addr; // Starlight: cache resolved IP for later lookups
184199
await _db.AddConnectionLogAsync(userId, e.UserName, addr, hwid, trust, null, serverId);
185200

186201
if (!ServerPreferencesManager.ShouldStorePrefs(e.AuthType))
@@ -196,6 +211,8 @@ private async void PlayerStatusChanged(object? sender, SessionStatusEventArgs ar
196211
{
197212
AdminAlertIfSharedConnection(args.Session);
198213
}
214+
else if (args.NewStatus == SessionStatus.Disconnected) // Starlight
215+
_resolvedAddresses.Remove(args.Session.UserId); // Starlight
199216
}
200217

201218
private void AdminAlertIfSharedConnection(ICommonSession newSession)
@@ -204,11 +221,13 @@ private void AdminAlertIfSharedConnection(ICommonSession newSession)
204221
if (playerThreshold < 0)
205222
return;
206223

207-
var addr = newSession.Channel.RemoteEndPoint.Address;
224+
var addr = _resolvedAddresses.GetValueOrDefault(newSession.UserId)
225+
?? newSession.Channel.RemoteEndPoint.Address; // Starlight: use resolved IP
208226

209227
var otherConnectionsFromAddress = _plyMgr.Sessions.Where(session =>
210228
session.Status is SessionStatus.Connected or SessionStatus.InGame
211-
&& session.Channel.RemoteEndPoint.Address.Equals(addr)
229+
&& (_resolvedAddresses.GetValueOrDefault(session.UserId)
230+
?? session.Channel.RemoteEndPoint.Address).Equals(addr) // Starlight: use resolved IP
212231
&& session.UserId != newSession.UserId)
213232
.ToList();
214233

0 commit comments

Comments
 (0)