forked from vpnhood/VpnHood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IpGroupManager.cs
194 lines (164 loc) · 6.95 KB
/
IpGroupManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System.IO.Compression;
using System.Net;
using System.Net.Sockets;
using System.Numerics;
using System.Text.Json;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using VpnHood.Common.Logging;
using VpnHood.Common.Net;
using VpnHood.Common.Utils;
namespace VpnHood.Client.App;
public class IpGroupManager
{
private IpGroup[]? _ipGroups;
private string IpGroupsFolderPath => Path.Combine(StorageFolder, "ipgroups");
private string IpGroupsFilePath => Path.Combine(StorageFolder, "ipgroups.json");
private string VersionFilePath => Path.Combine(StorageFolder, "version.txt");
private string GetIpGroupFilePath(string ipGroup) => Path.Combine(IpGroupsFolderPath, ipGroup + ".json");
public string StorageFolder { get; }
private IpGroupManager(string storageFolder)
{
StorageFolder = storageFolder;
}
public static Task<IpGroupManager> Create(string storageFolder)
{
var ret = new IpGroupManager(storageFolder);
return Task.FromResult(ret);
}
public async Task InitByIp2LocationZipStream(ZipArchiveEntry archiveEntry)
{
var newVersion = archiveEntry.LastWriteTime.ToUniversalTime().ToString("u");
var oldVersion = "NotFound";
// check is version changed
if (File.Exists(VersionFilePath))
{
try
{
oldVersion = await File.ReadAllTextAsync(VersionFilePath);
if (oldVersion == newVersion)
return;
}
catch (Exception ex)
{
VhLogger.Instance.LogError(ex, "Could not read last version file. File: {File}", VersionFilePath);
}
}
// Build new structure
VhLogger.Instance.LogInformation("Building IPLocation. OldVersion: {OldVersion}, NewVersion {NewVersion},", oldVersion, newVersion);
// delete all files and other versions if any
if (Directory.Exists(IpGroupsFolderPath))
{
VhLogger.Instance.LogTrace("Deleting the old IpGroups...");
Directory.Delete(IpGroupsFolderPath, true);
}
// Loading the ip2Location stream
VhLogger.Instance.LogTrace("Loading the ip2Location stream...");
await using var ipLocationsStream = archiveEntry.Open();
var ipGroupNetworks = await LoadIp2Location(ipLocationsStream);
// Building the IpGroups directory structure
VhLogger.Instance.LogTrace("Building the IpGroups directory structure...");
Directory.CreateDirectory(IpGroupsFolderPath);
foreach (var ipGroupNetwork in ipGroupNetworks)
{
ipGroupNetwork.Value.IpRanges = ipGroupNetwork.Value.IpRanges.ToArray().Sort().ToList();
await File.WriteAllTextAsync(GetIpGroupFilePath(ipGroupNetwork.Key), JsonSerializer.Serialize(ipGroupNetwork.Value.IpRanges));
}
// write IpGroups file
var ipGroups = ipGroupNetworks.Select(x =>
new IpGroup
{
IpGroupId = x.Value.IpGroupId,
IpGroupName = x.Value.IpGroupName
})
.OrderBy(x => x.IpGroupName)
.ToArray();
await File.WriteAllTextAsync(IpGroupsFilePath, JsonSerializer.Serialize(ipGroups));
// write version
await File.WriteAllTextAsync(VersionFilePath, newVersion);
_ipGroups = null; // clear cache
}
private static async Task<Dictionary<string, IpGroupNetwork>> LoadIp2Location(Stream ipLocationsStream)
{
// extract IpGroups
var ipGroupNetworks = new Dictionary<string, IpGroupNetwork>();
using var streamReader = new StreamReader(ipLocationsStream);
while (!streamReader.EndOfStream)
{
var line = await streamReader.ReadLineAsync();
var items = line.Replace("\"", "").Split(',');
if (items.Length != 4)
continue;
var ipGroupId = items[2].ToLower();
if (ipGroupId == "-") continue;
if (ipGroupId == "um") ipGroupId = "us";
if (!ipGroupNetworks.TryGetValue(ipGroupId, out var ipGroupNetwork))
{
var ipGroupName = ipGroupId switch
{
"us" => "United States",
"gb" => "United Kingdom",
_ => items[3]
};
ipGroupName = Regex.Replace(ipGroupName, @"\(.*?\)", "").Replace(" ", " ");
ipGroupNetwork = new IpGroupNetwork
{
IpGroupId = ipGroupId,
IpGroupName = ipGroupName
};
ipGroupNetworks.Add(ipGroupId, ipGroupNetwork);
}
var addressFamily = items[0].Length > 10 || items[1].Length > 10 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
var ipRange = new IpRange(
IPAddressUtil.FromBigInteger(BigInteger.Parse(items[0]), addressFamily),
IPAddressUtil.FromBigInteger(BigInteger.Parse(items[1]), addressFamily));
ipGroupNetwork.IpRanges.Add(ipRange.IsIPv4MappedToIPv6 ? ipRange.MapToIPv4() : ipRange);
}
return ipGroupNetworks;
}
public async Task<IpGroup[]> GetIpGroups()
{
if (_ipGroups != null)
return _ipGroups;
// no countries if there is no import
if (!File.Exists(IpGroupsFilePath))
return Array.Empty<IpGroup>();
var json = await File.ReadAllTextAsync(IpGroupsFilePath);
_ipGroups = VhUtil.JsonDeserialize<IpGroup[]>(json);
return _ipGroups;
}
public async Task<IEnumerable<IpRange>> GetIpRanges(string ipGroupId)
{
var filePath = GetIpGroupFilePath(ipGroupId);
var json = await File.ReadAllTextAsync(filePath);
var ipRanges = JsonSerializer.Deserialize<IpRange[]>(json) ?? throw new Exception($"Could not deserialize {filePath}!");
var ip4MappedRanges = ipRanges.Where(x => x.AddressFamily==AddressFamily.InterNetwork).Select(x => x.MapToIPv6());
var ret = ipRanges.Concat(ip4MappedRanges);
return ret;
}
// it is sequential search
public async Task<IpGroup?> FindIpGroup(IPAddress ipAddress, string? lastIpGroupId)
{
var ipGroups = await GetIpGroups();
var lastIpGroup = ipGroups.FirstOrDefault(x => x.IpGroupId == lastIpGroupId);
// IpGroup
if (lastIpGroup != null)
{
var ipRanges = await GetIpRanges(lastIpGroup.IpGroupId);
if (ipRanges.Any(x => x.IsInRange(ipAddress)))
return lastIpGroup;
}
// iterate through all groups
foreach (var ipGroup in ipGroups)
{
var ipRanges = await GetIpRanges(ipGroup.IpGroupId);
if (ipRanges.Any(x => x.IsInRange(ipAddress)))
return ipGroup;
}
return null;
}
private class IpGroupNetwork : IpGroup
{
public List<IpRange> IpRanges { get; set; } = [];
}
}