Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved grant local admin to allow all IPs that will loop back #2056

Merged
merged 2 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions Nitrox.Test/Model/Helper/NetHelperTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net;
using System;
using System.Net;
using System.Net.Sockets;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand All @@ -11,11 +13,11 @@ public class NetHelperTest
public void ShouldMatchPrivateIps()
{
// Tested subnet ranges that are reserved for private networks:
// 10.0.0.0/8
// 127.0.0.0/8
// 172.16.0.0/12
// 192.0.0.0/24
// 192.168.0.0/16
// 10.0.0.0/8
// 127.0.0.0/8
// 172.16.0.0/12
// 192.0.0.0/24
// 192.168.0.0/16
// 198.18.0.0/15

IPAddress.Parse("10.0.0.0").IsPrivate().Should().BeTrue();
Expand All @@ -36,4 +38,34 @@ public void ShouldMatchPrivateIps()
IPAddress.Parse("198.17.255.255").IsPrivate().Should().BeFalse();
IPAddress.Parse("198.20.0.0").IsPrivate().Should().BeFalse();
}

[TestMethod]
public void ShouldMatchLocalhostIps()
{
IPAddress GetSlightlyDifferentIp(IPAddress address)
{
if (address.AddressFamily != AddressFamily.InterNetwork)
{
throw new Exception("Only supports IPv4");
}
byte[] bytes = address.GetAddressBytes();
unchecked
{
while (bytes[3] is < 1 or > 253)
{
bytes[3]++;
}
bytes[3]++;
}
return new IPAddress(bytes);
}

IPAddress.Parse("127.0.0.1").IsLocalhost().Should().BeTrue();
IPAddress.Parse("127.0.0.2").IsLocalhost().Should().BeTrue();
IPAddress.Parse("192.168.0.255").IsLocalhost().Should().BeFalse();
NetHelper.GetLanIp().IsLocalhost().Should().BeTrue();
IPAddress differentIp = GetSlightlyDifferentIp(NetHelper.GetLanIp());
differentIp.Should().NotBeEquivalentTo(NetHelper.GetLanIp());
differentIp.IsLocalhost().Should().BeFalse();
}
}
26 changes: 26 additions & 0 deletions NitroxModel/Helper/NetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,31 @@ static bool IsInRange(IPAddress ipAddress, string mask)
}
return false;
}

/// <summary>
/// Returns true if the IP address points to the executing machine.
/// </summary>
public static bool IsLocalhost(this IPAddress address)
{
if (address == null)
{
return false;
}
if (IPAddress.IsLoopback(address))
{
return true;
}
foreach (NetworkInterface ni in GetInternetInterfaces())
{
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if (address.Equals(ip.Address))
{
return true;
}
}
}
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Numerics;
using NitroxModel.DataStructures;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.DataStructures.GameLogic.Entities;
using NitroxModel.DataStructures.Unity;
using NitroxModel.DataStructures.Util;
using NitroxModel.Helper;
using NitroxModel.MultiplayerSession;
using NitroxModel.Packets;
using NitroxServer.Communication.Packets.Processors.Abstract;
Expand Down Expand Up @@ -51,8 +50,9 @@ public override void Process(PlayerJoiningMultiplayerSession packet, NitroxConne
playerManager.SendPacketToOtherPlayers(playerJoinedPacket, player);

// Make players on localhost admin by default.
if (IPAddress.IsLoopback(connection.Endpoint.Address))
if (connection.Endpoint.Address.IsLocalhost())
{
Log.Info($"Granted admin to '{player.Name}' because they're playing on the host machine");
player.Permissions = Perms.ADMIN;
}

Expand Down