Skip to content

Commit fc1ff26

Browse files
committed
Keep hostnames lowercase in Styx
1 parent dea7f6c commit fc1ff26

3 files changed

Lines changed: 11 additions & 9 deletions

File tree

Styx/Services/ClientRegistry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public async ValueTask Unregister(string connectionId)
4040
using var clients = await _clients.WaitForDisposable();
4141
foreach (var (connectionId, identity) in clients.Value)
4242
{
43-
if (identity.NetworkId == networkId && identity.HostName.EqualsIgnoreCase(hostName))
43+
if (identity.NetworkId == networkId && identity.HostName.EqualsOrdinal(hostName))
4444
return connectionId;
4545
}
4646
return null;
@@ -58,7 +58,7 @@ public async ValueTask<IReadOnlyList<string>> KickDuplicates(Guid networkId, str
5858
using var clients = await _clients.WaitForDisposable();
5959
var found = clients.Value
6060
.Where(kv => kv.Value.NetworkId == networkId
61-
&& kv.Value.HostName.EqualsIgnoreCase(hostName)
61+
&& kv.Value.HostName.EqualsOrdinal(hostName)
6262
&& kv.Key != newConnectionId)
6363
.Select(kv => kv.Key)
6464
.ToList();

Styx/Services/PeerBroadcastService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ private async Task BroadcastPeers(Guid networkId)
4848
try
4949
{
5050
var clients = await registry.GetNetworkClients(networkId);
51-
var allHostNames = clients.Select(c => c.HostName).OrderBy(h => h, StringComparer.OrdinalIgnoreCase).ToArray();
51+
var allHostNames = clients.Select(c => c.HostName).OrderBy(h => h, StringComparer.Ordinal).ToArray();
5252
var peerList = allHostNames.Length > 0 ? string.Join(", ", allHostNames) : "<none>";
5353
log.LogInformation("Network {NetworkId} peers: {Peers}", networkId, peerList);
5454
foreach (var (connectionId, hostName) in clients)
5555
{
56-
var peers = allHostNames.Where(h => !h.EqualsIgnoreCase(hostName)).ToArray();
56+
var peers = allHostNames.Where(h => !h.EqualsOrdinal(hostName)).ToArray();
5757
await hubContext.Clients.Client(connectionId).Peers(peers);
5858
}
5959
}

Styx/StyxHub.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,20 @@ public async Task<RelayLoginResponse> Authenticate(RelayLogin login)
3636
}
3737
catch (Exception ex)
3838
{
39-
log.LogWarning(ex, "Authentication failed for \"{HostName}\" from {RemoteIp}", login.HostName, remoteIp);
39+
log.LogWarning(ex, "Authentication failed for \"{HostName}\" from {RemoteIp}", login.HostName.ToLowerInvariant(), remoteIp);
4040
await throttle;
4141
return new RelayLoginResponse { Authenticated = false, Message = "Invalid authorization" };
4242
}
4343

44+
var hostName = login.HostName.ToLowerInvariant();
45+
4446
// kick any existing connections with the same network+hostname (stale entries can accumulate on unclean disconnect)
45-
var kicked = await registry.KickDuplicates(networkId, login.HostName, Context.ConnectionId);
47+
var kicked = await registry.KickDuplicates(networkId, hostName, Context.ConnectionId);
4648
foreach (var connectionId in kicked)
4749
await Clients.Client(connectionId).Kicked("duplicate hostname");
4850

49-
await registry.Register(Context.ConnectionId, networkId, login.HostName, remoteIp);
50-
log.LogInformation("Authentication accepted for \"{HostName}\" (connectionId:{ConnectionId}) from {RemoteIp} on network {NetworkId}", login.HostName, Context.ConnectionId, remoteIp, networkId);
51+
await registry.Register(Context.ConnectionId, networkId, hostName, remoteIp);
52+
log.LogInformation("Authentication accepted for \"{HostName}\" (connectionId:{ConnectionId}) from {RemoteIp} on network {NetworkId}", hostName, Context.ConnectionId, remoteIp, networkId);
5153
await throttle;
5254

5355
// queue after throttle so Authenticated=true is sent to the caller before Peers arrives
@@ -81,7 +83,7 @@ public async Task Send(string[] targetHosts, byte[] payload)
8183
continue;
8284
}
8385

84-
var targetConnectionId = await registry.GetConnectionId(identity.NetworkId, targetHost);
86+
var targetConnectionId = await registry.GetConnectionId(identity.NetworkId, targetHost.ToLowerInvariant());
8587
if (targetConnectionId == null)
8688
{
8789
log.LogDebug("Target {TargetHost} not found on network {NetworkId}", targetHost, identity.NetworkId);

0 commit comments

Comments
 (0)