Skip to content

Commit 74fe702

Browse files
authored
Merge pull request #1853 from dartasen/1.6.1.0-cherrypick
1.6.1.0 hotfix with cherrypicked commits
2 parents 7bdea41 + cf310fe commit 74fe702

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project InitialTargets="PrepareForModding">
22
<!-- Set default properties for all projects (can be overridden per project) -->
33
<PropertyGroup>
4-
<Version>1.6.0.0</Version>
4+
<Version>1.6.1.0</Version>
55
<LangVersion>10</LangVersion>
66
<TestLibrary>false</TestLibrary>
77
<NitroxLibrary>false</NitroxLibrary>
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Net;
2+
using FluentAssertions;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace NitroxModel.Helper;
6+
7+
[TestClass]
8+
public class NetHelperTest
9+
{
10+
[TestMethod]
11+
public void ShouldMatchPrivateIps()
12+
{
13+
// Tested subnet ranges that are reserved for private networks:
14+
// 10.0.0.0/8
15+
// 127.0.0.0/8
16+
// 172.16.0.0/12
17+
// 192.0.0.0/24
18+
// 192.168.0.0/16
19+
// 198.18.0.0/15
20+
21+
IPAddress.Parse("10.0.0.0").IsPrivate().Should().BeTrue();
22+
IPAddress.Parse("10.0.0.255").IsPrivate().Should().BeTrue();
23+
IPAddress.Parse("172.31.255.255").IsPrivate().Should().BeTrue();
24+
IPAddress.Parse("172.31.255.255").IsPrivate().Should().BeTrue();
25+
IPAddress.Parse("192.0.0.255").IsPrivate().Should().BeTrue();
26+
IPAddress.Parse("192.168.2.1").IsPrivate().Should().BeTrue();
27+
IPAddress.Parse("192.168.2.254").IsPrivate().Should().BeTrue();
28+
IPAddress.Parse("192.168.2.255").IsPrivate().Should().BeTrue();
29+
IPAddress.Parse("198.18.0.1").IsPrivate().Should().BeTrue();
30+
IPAddress.Parse("198.19.255.255").IsPrivate().Should().BeTrue();
31+
32+
IPAddress.Parse("9.255.255.255").IsPrivate().Should().BeFalse();
33+
IPAddress.Parse("91.63.176.12").IsPrivate().Should().BeFalse();
34+
IPAddress.Parse("172.32.0.1").IsPrivate().Should().BeFalse();
35+
IPAddress.Parse("192.0.1.0").IsPrivate().Should().BeFalse();
36+
IPAddress.Parse("198.17.255.255").IsPrivate().Should().BeFalse();
37+
IPAddress.Parse("198.20.0.0").IsPrivate().Should().BeFalse();
38+
}
39+
}

NitroxModel/Extensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ public static IEnumerable<T> TakeUntilLast<T>(this IEnumerable<T> source)
3535
}
3636
}
3737
}
38-
}
38+
}

NitroxModel/Helper/NetHelper.cs

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
24
using System.Linq;
35
using System.Net;
46
using System.Net.NetworkInformation;
@@ -13,6 +15,16 @@ namespace NitroxModel.Helper
1315
{
1416
public static class NetHelper
1517
{
18+
private static readonly string[] privateNetworks =
19+
{
20+
"10.0.0.0/8",
21+
"127.0.0.0/8",
22+
"172.16.0.0/12",
23+
"192.0.0.0/24 ",
24+
"192.168.0.0/16",
25+
"198.18.0.0/15",
26+
};
27+
1628
private static IPAddress wanIpCache;
1729
private static IPAddress lanIpCache;
1830
private static readonly object wanIpLock = new();
@@ -69,7 +81,7 @@ public static async Task<IPAddress> GetWanIpAsync()
6981

7082
IPAddress ip = await NatHelper.GetExternalIpAsync();
7183
#if RELEASE
72-
if (ip == null)
84+
if (ip == null || ip.IsPrivate())
7385
{
7486
Regex regex = new(@"(?:[0-2]??[0-9]{1,2}\.){3}[0-2]??[0-9]+", RegexOptions.Compiled);
7587
string[] sites =
@@ -124,5 +136,31 @@ public static IPAddress GetHamachiIp()
124136
}
125137
return null;
126138
}
139+
140+
/// <summary>
141+
/// Returns true if the given IP address is reserved for private networks.
142+
/// </summary>
143+
public static bool IsPrivate(this IPAddress address)
144+
{
145+
static bool IsInRange(IPAddress ipAddress, string mask)
146+
{
147+
string[] parts = mask.Split('/');
148+
149+
int ipNum = BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0);
150+
int cidrAddress = BitConverter.ToInt32(IPAddress.Parse(parts[0]).GetAddressBytes(), 0);
151+
int cidrMask = IPAddress.HostToNetworkOrder(-1 << (32 - int.Parse(parts[1])));
152+
153+
return (ipNum & cidrMask) == (cidrAddress & cidrMask);
154+
}
155+
156+
foreach (string privateSubnet in privateNetworks)
157+
{
158+
if (IsInRange(address, privateSubnet))
159+
{
160+
return true;
161+
}
162+
}
163+
return false;
164+
}
127165
}
128166
}

NitroxServer/GameLogic/Entities/Spawning/BatchEntitySpawner.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ private IEnumerable<Entity> SpawnEntitiesUsingRandomDistribution(EntitySpawnPoin
121121
yield break;
122122
}
123123

124-
double randomNumber = deterministicBatchGenerator.NextDouble();
124+
float randomNumber = (float)deterministicBatchGenerator.NextDouble();
125125
if (rollingProbabilityDensity > 1f)
126126
{
127127
randomNumber *= rollingProbabilityDensity;
128128
}
129129

130-
double rollingProbability = 0;
130+
float rollingProbability = 0;
131131

132132
UwePrefab selectedPrefab = allowedPrefabs.FirstOrDefault(prefab =>
133133
{
@@ -394,6 +394,11 @@ private List<Entity> ConvertComponentPrefabsToEntities(List<PrefabAsset> prefabs
394394
{
395395
parent.ChildEntities.Add(possibleEntity);
396396
}
397+
else
398+
{
399+
// Without the "continue;" lots of entities as fragments will stop spawning (#1779)
400+
continue;
401+
}
397402
}
398403

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

0 commit comments

Comments
 (0)