Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Jun 4, 2024
2 parents 6094e0e + 9033e28 commit 6bb9e35
Show file tree
Hide file tree
Showing 126 changed files with 1,382 additions and 1,328 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# v4.5.522
### Client
* Update: Disable "Exclude My Country" when unable to load the IP location database
* Update: Automatically disconnect client after changing exclude my country settings
* Improve: Performance & Speed on the connection when the user has selected exclude my country
* Fix: Freeze network on some situation

### Server
* Update: Improve performance

# v4.5.520
### Client
* Feature: Allow to select servers by country if the server supports it
Expand Down
4 changes: 2 additions & 2 deletions Pub/PubVersion.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Version": "4.5.520",
"BumpTime": "2024-05-29T06:25:48.3640756Z",
"Version": "4.5.522",
"BumpTime": "2024-06-04T09:05:15.4355193Z",
"Prerelease": false,
"DeprecatedVersion": "4.0.00"
}
13 changes: 10 additions & 3 deletions Tests/VpnHood.Test/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public static string CreateAccessManagerWorkingDir()
return Path.Combine(WorkingPath, $"AccessManager_{Guid.NewGuid()}");
}

public static FileAccessManager CreateFileAccessManager(FileAccessManagerOptions? options = null, string? storagePath = null,
public static FileAccessManager CreateFileAccessManager(FileAccessManagerOptions? options = null, string? storagePath = null,
string? serverLocation = null)
{
storagePath ??= CreateAccessManagerWorkingDir();
Expand Down Expand Up @@ -332,7 +332,7 @@ public static async Task<VpnHoodClient> CreateClient(Token token,
clientId ??= Guid.NewGuid();
clientOptions ??= CreateClientOptions();
if (clientOptions.ConnectTimeout == new ClientOptions().ConnectTimeout) clientOptions.ConnectTimeout = TimeSpan.FromSeconds(3);
clientOptions.PacketCaptureIncludeIpRanges = TestIpAddresses.Select(x => new IpRange(x)).ToArray();
clientOptions.PacketCaptureIncludeIpRanges = TestIpAddresses.Select(IpRange.FromIpAddress).ToOrderedList();
clientOptions.IncludeLocalNetwork = true;

var client = new VpnHoodClient(
Expand Down Expand Up @@ -362,7 +362,7 @@ public static AppOptions CreateClientAppOptions()
{
StorageFolderPath = Path.Combine(WorkingPath, "AppData_" + Guid.NewGuid()),
SessionTimeout = TimeSpan.FromSeconds(2),
UseIpGroupManager = false,
UseInternalLocationService = false,
UseExternalLocationService = false,
LogVerbose = LogVerbose
};
Expand Down Expand Up @@ -435,4 +435,11 @@ internal static void Init()
JobRunner.Default.Interval = TimeSpan.FromMilliseconds(200);
JobSection.DefaultInterval = TimeSpan.FromMilliseconds(200);
}
public static string GetParentDirectory(string path, int level = 1)
{
for (var i = 0; i < level; i++)
path = Path.GetDirectoryName(path) ?? throw new Exception("Invalid path");

return path;
}
}
4 changes: 2 additions & 2 deletions Tests/VpnHood.Test/TestNullPacketCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void SendPacketToInbound(IPPacket ipPacket)
// nothing
}

public void SendPacketToInbound(IEnumerable<IPPacket> packets)
public void SendPacketToInbound(IList<IPPacket> packets)
{
// nothing
}
Expand All @@ -57,7 +57,7 @@ public void SendPacketToOutbound(IPPacket ipPacket)
// nothing
}

public void SendPacketToOutbound(IEnumerable<IPPacket> ipPackets)
public void SendPacketToOutbound(IList<IPPacket> ipPackets)
{
// nothing
}
Expand Down
54 changes: 37 additions & 17 deletions Tests/VpnHood.Test/Tests/ClientAppTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Net;
using System.IO.Compression;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.Json;
using EmbedIO;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -13,8 +15,6 @@
using VpnHood.Common.Net;
using VpnHood.Common.Utils;

// ReSharper disable DisposeOnUsingVariable

namespace VpnHood.Test.Tests;

[TestClass]
Expand Down Expand Up @@ -72,25 +72,45 @@ public async Task BuiltIn_AccessKeys_initialization()
}
}

private static async Task UpdateIp2LocationFile()
{
// update current ipLocation in app project after a week
var solutionFolder = TestHelper.GetParentDirectory(Directory.GetCurrentDirectory(), 5);
var ipLocationFile = Path.Combine(solutionFolder, "VpnHood.Client.App", "Resources", "IpLocations.zip");
if (File.GetCreationTime(ipLocationFile) >= DateTime.Now - TimeSpan.FromDays(7))
return;

// find token
var userSecretFile = Path.Combine(Path.GetDirectoryName(solutionFolder)!, ".user", "credentials.json");
var document = JsonDocument.Parse(await File.ReadAllTextAsync(userSecretFile));
var ip2LocationToken = document.RootElement.GetProperty("Ip2LocationToken").GetString();

// copy zip to memory
var httpClient = new HttpClient();
// ReSharper disable once StringLiteralTypo
await using var ipLocationZipNetStream = await httpClient.GetStreamAsync(
$"https://www.ip2location.com/download/?token={ip2LocationToken}&file=DB1LITECSVIPV6");
using var ipLocationZipStream = new MemoryStream();
await ipLocationZipNetStream.CopyToAsync(ipLocationZipStream);
ipLocationZipStream.Position = 0;

// build new ipLocation file
using var ipLocationZipArchive = new ZipArchive(ipLocationZipStream, ZipArchiveMode.Read);
await using var crvStream = ipLocationZipArchive.GetEntry("IP2LOCATION-LITE-DB1.IPV6.CSV")!.Open();
await IpGroupBuilder.BuildIpGroupArchiveFromIp2Location(crvStream, ipLocationFile);
}

[TestMethod]
public async Task Load_country_ip_groups()
public async Task IpLocations_must_be_loaded()
{
// ************
// *** TEST ***:
await using var app1 = TestHelper.CreateClientApp();
var ipGroups = await app1.GetIpGroups();
Assert.IsFalse(ipGroups.Any(x => x.IpGroupId == "us"),
"Countries should not be extracted in test due to performance.");
await app1.DisposeAsync();
await UpdateIp2LocationFile();

// ************
// *** TEST ***:
var appOptions = TestHelper.CreateClientAppOptions();
appOptions.UseIpGroupManager = true;
await using var app2 = TestHelper.CreateClientApp(appOptions: appOptions);
var ipGroups2 = await app2.GetIpGroups();
Assert.IsTrue(ipGroups2.Any(x => x.IpGroupId == "us"),
appOptions.UseInternalLocationService = true;
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
var ipGroupsManager = await app.GetIpGroupManager();
var ipGroupIds = await ipGroupsManager.GetIpGroupIds();
Assert.IsTrue(ipGroupIds.Any(x => x == "us"),
"Countries has not been extracted.");
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/VpnHood.Test/Tests/DnsConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task Server_should_not_block_own_dns_servers()
await using var client = await TestHelper.CreateClient(token, clientOptions: clientOptions);

foreach (var serverDnsServer in serverDnsServers)
Assert.IsNull(server.SessionManager.NetFilter.BlockedIpRanges.FindInSortedRanges(serverDnsServer));
Assert.IsFalse(server.SessionManager.NetFilter.BlockedIpRanges.IsInRange(serverDnsServer));
CollectionAssert.AreEqual(fileAccessManagerOptions.DnsServers, client.DnsServers);
}
}
44 changes: 21 additions & 23 deletions Tests/VpnHood.Test/Tests/IpNetworkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,23 @@ public void Invert_Unify_Convert()
IpRange.Parse("192.168.10.0 - 192.168.255.255"),
IpRange.Parse("127.0.0.0 - 127.255.255.255"),
IpRange.Parse("127.0.0.0 - 127.255.255.254") //extra
};
CollectionAssert.AreEqual(ipRangesSorted, ipRanges.Sort().ToArray());
}.ToOrderedList();
CollectionAssert.AreEqual(ipRangesSorted, ipRanges.ToArray());

var inverted = ipRanges.Invert();
var expected = new[]
{
IpRange.Parse("0.0.0.0 - 126.255.255.255"),
IpRange.Parse("128.0.0.0 - 192.167.255.255"),
IpRange.Parse("192.169.0.0 - 255.255.255.255"),
IpRange.Parse(":: - 99:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"),
IpRange.Parse("AA::01:0000 - FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")
};
}.ToOrderedList();

CollectionAssert.AreEqual(expected, inverted.ToArray());
CollectionAssert.AreEqual(expected.ToArray(), ipRanges.Invert().ToArray());

// check network
CollectionAssert.AreEqual(expected.ToIpNetworks().ToArray(), ipRanges.ToIpNetworks().Invert().ToArray());
CollectionAssert.AreEqual(ipRangesSorted, ipRanges.ToIpNetworks().ToIpRanges().Sort().ToArray());
CollectionAssert.AreEqual(expected.ToIpNetworks().ToArray(), ipRanges.Invert().ToIpNetworks().ToArray());
CollectionAssert.AreEqual(ipRangesSorted, ipRanges.ToArray());
}

[TestMethod]
Expand All @@ -54,15 +53,15 @@ public void IpNetwork_Unit()
var ipNetwork = IpNetwork.Parse("192.168.23.23/32");
var inverted = ipNetwork.Invert().ToArray();
Assert.AreEqual(32, inverted.Length);
CollectionAssert.AreEqual(new[] { ipNetwork }, inverted.Invert(true, false).ToArray());
CollectionAssert.AreEqual(new[] { ipNetwork }, inverted.ToIpRanges().Invert(true, false).ToIpNetworks().ToArray());

ipNetwork = IpNetwork.AllV4;
Assert.AreEqual(0, ipNetwork.Invert().ToArray().Length);

ipNetwork = IpNetwork.AllV6;
Assert.AreEqual(0, ipNetwork.Invert().ToArray().Length);

CollectionAssert.AreEqual(IpNetwork.All, Array.Empty<IpNetwork>().Invert().ToArray());
CollectionAssert.AreEqual(IpNetwork.All, Array.Empty<IpNetwork>().ToIpRanges().Invert().ToIpNetworks().ToArray());
}

[TestMethod]
Expand All @@ -87,14 +86,14 @@ public void IpRange_IsInRange()
IpRange.Parse("5.5.5.5-5.5.5.10")
};

ipRanges = ipRanges.Sort().ToArray();
Assert.IsFalse(ipRanges.IsInSortedRanges(IPAddress.Parse("9.9.9.7")));
Assert.IsTrue(ipRanges.IsInSortedRanges(IPAddress.Parse("8.8.8.8")));
Assert.IsTrue(ipRanges.IsInSortedRanges(IPAddress.Parse("9.9.9.9")));
Assert.IsFalse(ipRanges.IsInSortedRanges(IPAddress.Parse("4.4.4.5")));
Assert.IsTrue(ipRanges.IsInSortedRanges(IPAddress.Parse("4.4.4.3")));
Assert.IsTrue(ipRanges.IsInSortedRanges(IPAddress.Parse("FF::F0")));
Assert.IsFalse(ipRanges.IsInSortedRanges(IPAddress.Parse("AF::F0")));
var ipRangeOrderedList = ipRanges.ToOrderedList();
Assert.IsFalse(ipRangeOrderedList.IsInRange(IPAddress.Parse("9.9.9.7")));
Assert.IsTrue(ipRangeOrderedList.IsInRange(IPAddress.Parse("8.8.8.8")));
Assert.IsTrue(ipRangeOrderedList.IsInRange(IPAddress.Parse("9.9.9.9")));
Assert.IsFalse(ipRangeOrderedList.IsInRange(IPAddress.Parse("4.4.4.5")));
Assert.IsTrue(ipRangeOrderedList.IsInRange(IPAddress.Parse("4.4.4.3")));
Assert.IsTrue(ipRangeOrderedList.IsInRange(IPAddress.Parse("FF::F0")));
Assert.IsFalse(ipRangeOrderedList.IsInRange(IPAddress.Parse("AF::F0")));
}

[TestMethod]
Expand All @@ -110,7 +109,7 @@ public void IpRange_Intersect(bool swap)
IpRange.Parse("30.30.10.50 - 30.30.10.100"),
IpRange.Parse("20.20.10.50 - 20.20.10.55"),
IpRange.Parse("20.20.10.60 - 20.20.10.100")
};
}.ToOrderedList();

var ipRanges2 = new[]
{
Expand All @@ -119,22 +118,21 @@ public void IpRange_Intersect(bool swap)
IpRange.Parse("190.190.11.1 - 190.190.11.50"), //ignore
IpRange.Parse("30.30.10.70 - 30.30.10.110"),
IpRange.Parse("20.20.10.0 - 20.20.10.90")
};
}.ToOrderedList();

// Expected
// AA::FFF5 - AA::FFF6

var ranges = swap
? ipRanges2.Intersect(ipRanges1).ToArray()
: ipRanges1.Intersect(ipRanges2).ToArray();
? ipRanges2.Intersect(ipRanges1)
: ipRanges1.Intersect(ipRanges2);

var i = 0;
Assert.AreEqual("20.20.10.50-20.20.10.55", ranges[i++].ToString().ToUpper());
Assert.AreEqual("20.20.10.60-20.20.10.90", ranges[i++].ToString().ToUpper());
Assert.AreEqual("30.30.10.70-30.30.10.100", ranges[i++].ToString().ToUpper());
Assert.AreEqual("192.168.10.0-192.168.12.12", ranges[i++].ToString().ToUpper());
Assert.AreEqual("AA::FFF5-AA::FFF6", ranges[i++].ToString().ToUpper());
Assert.AreEqual(i, ranges.Length);
Assert.AreEqual(i, ranges.Count);

}
}
Loading

0 comments on commit 6bb9e35

Please sign in to comment.