Skip to content

Commit

Permalink
Merge pull request #1853 from dartasen/1.6.1.0-cherrypick
Browse files Browse the repository at this point in the history
1.6.1.0 hotfix with cherrypicked commits
  • Loading branch information
Measurity authored Sep 4, 2022
2 parents 7bdea41 + cf310fe commit 74fe702
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project InitialTargets="PrepareForModding">
<!-- Set default properties for all projects (can be overridden per project) -->
<PropertyGroup>
<Version>1.6.0.0</Version>
<Version>1.6.1.0</Version>
<LangVersion>10</LangVersion>
<TestLibrary>false</TestLibrary>
<NitroxLibrary>false</NitroxLibrary>
Expand Down
39 changes: 39 additions & 0 deletions Nitrox.Test/Model/Helper/NetHelperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Net;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NitroxModel.Helper;

[TestClass]
public class NetHelperTest
{
[TestMethod]
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
// 198.18.0.0/15

IPAddress.Parse("10.0.0.0").IsPrivate().Should().BeTrue();
IPAddress.Parse("10.0.0.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("172.31.255.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("172.31.255.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("192.0.0.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("192.168.2.1").IsPrivate().Should().BeTrue();
IPAddress.Parse("192.168.2.254").IsPrivate().Should().BeTrue();
IPAddress.Parse("192.168.2.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("198.18.0.1").IsPrivate().Should().BeTrue();
IPAddress.Parse("198.19.255.255").IsPrivate().Should().BeTrue();

IPAddress.Parse("9.255.255.255").IsPrivate().Should().BeFalse();
IPAddress.Parse("91.63.176.12").IsPrivate().Should().BeFalse();
IPAddress.Parse("172.32.0.1").IsPrivate().Should().BeFalse();
IPAddress.Parse("192.0.1.0").IsPrivate().Should().BeFalse();
IPAddress.Parse("198.17.255.255").IsPrivate().Should().BeFalse();
IPAddress.Parse("198.20.0.0").IsPrivate().Should().BeFalse();
}
}
2 changes: 1 addition & 1 deletion NitroxModel/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public static IEnumerable<T> TakeUntilLast<T>(this IEnumerable<T> source)
}
}
}
}
}
40 changes: 39 additions & 1 deletion NitroxModel/Helper/NetHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
Expand All @@ -13,6 +15,16 @@ namespace NitroxModel.Helper
{
public static class NetHelper
{
private static readonly string[] privateNetworks =
{
"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",
};

private static IPAddress wanIpCache;
private static IPAddress lanIpCache;
private static readonly object wanIpLock = new();
Expand Down Expand Up @@ -69,7 +81,7 @@ public static async Task<IPAddress> GetWanIpAsync()

IPAddress ip = await NatHelper.GetExternalIpAsync();
#if RELEASE
if (ip == null)
if (ip == null || ip.IsPrivate())
{
Regex regex = new(@"(?:[0-2]??[0-9]{1,2}\.){3}[0-2]??[0-9]+", RegexOptions.Compiled);
string[] sites =
Expand Down Expand Up @@ -124,5 +136,31 @@ public static IPAddress GetHamachiIp()
}
return null;
}

/// <summary>
/// Returns true if the given IP address is reserved for private networks.
/// </summary>
public static bool IsPrivate(this IPAddress address)
{
static bool IsInRange(IPAddress ipAddress, string mask)
{
string[] parts = mask.Split('/');

int ipNum = BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0);
int cidrAddress = BitConverter.ToInt32(IPAddress.Parse(parts[0]).GetAddressBytes(), 0);
int cidrMask = IPAddress.HostToNetworkOrder(-1 << (32 - int.Parse(parts[1])));

return (ipNum & cidrMask) == (cidrAddress & cidrMask);
}

foreach (string privateSubnet in privateNetworks)
{
if (IsInRange(address, privateSubnet))
{
return true;
}
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ private IEnumerable<Entity> SpawnEntitiesUsingRandomDistribution(EntitySpawnPoin
yield break;
}

double randomNumber = deterministicBatchGenerator.NextDouble();
float randomNumber = (float)deterministicBatchGenerator.NextDouble();
if (rollingProbabilityDensity > 1f)
{
randomNumber *= rollingProbabilityDensity;
}

double rollingProbability = 0;
float rollingProbability = 0;

UwePrefab selectedPrefab = allowedPrefabs.FirstOrDefault(prefab =>
{
Expand Down Expand Up @@ -394,6 +394,11 @@ private List<Entity> ConvertComponentPrefabsToEntities(List<PrefabAsset> prefabs
{
parent.ChildEntities.Add(possibleEntity);
}
else
{
// Without the "continue;" lots of entities as fragments will stop spawning (#1779)
continue;
}
}

// Setup any children this object may have attached to it.
Expand Down

0 comments on commit 74fe702

Please sign in to comment.