-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathUwnSpeedTestService.cs
More file actions
541 lines (472 loc) · 22.3 KB
/
UwnSpeedTestService.cs
File metadata and controls
541 lines (472 loc) · 22.3 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore;
using NetworkOptimizer.Alerts.Events;
using NetworkOptimizer.Storage;
using NetworkOptimizer.Storage.Models;
using NetworkOptimizer.UniFi;
using NetworkOptimizer.Web.Services.Ssh;
namespace NetworkOptimizer.Web.Services;
/// <summary>
/// Service for running WAN speed tests via UWN's distributed HTTP speed test network.
/// Executes the local uwnspeedtest Go binary and parses its JSON output.
/// </summary>
public class UwnSpeedTestService : WanSpeedTestServiceBase
{
private readonly IConfiguration _configuration;
private readonly UniFiConnectionService _connectionService;
private readonly IGatewaySshService _gatewaySsh;
private readonly IServiceScopeFactory _scopeFactory;
protected override SpeedTestDirection Direction => SpeedTestDirection.UwnWan;
// Include historical Cloudflare WAN results so the UI shows all server-side WAN test history
protected override SpeedTestDirection[] OwnedDirections =>
[SpeedTestDirection.UwnWan, SpeedTestDirection.CloudflareWan];
private int Streams => MaxMode ? 48 : 20;
private int ServerCount => MaxMode ? 12 : 4;
public UwnSpeedTestService(
ILogger<UwnSpeedTestService> logger,
IDbContextFactory<NetworkOptimizerDbContext> dbFactory,
INetworkPathAnalyzer pathAnalyzer,
IConfiguration configuration,
Iperf3ServerService iperf3ServerService,
UniFiConnectionService connectionService,
IGatewaySshService gatewaySsh,
IServiceScopeFactory scopeFactory,
IAlertEventBus? alertEventBus = null)
: base(dbFactory, pathAnalyzer, logger, iperf3ServerService, alertEventBus)
{
_configuration = configuration;
_connectionService = connectionService;
_gatewaySsh = gatewaySsh;
_scopeFactory = scopeFactory;
}
protected override async Task<Iperf3Result?> RunTestCoreAsync(
Action<string, int, string?> report,
CancellationToken cancellationToken)
{
var binaryPath = GetLocalBinaryPath();
if (!File.Exists(binaryPath))
throw new InvalidOperationException(
$"UWN speed test binary not found at {binaryPath}. " +
"Ensure the binary is built for this platform.");
Logger.LogInformation(
"Starting UWN WAN speed test via local binary ({Streams} streams, {Servers} servers, binary: {Binary})",
Streams, ServerCount, Path.GetFileName(binaryPath));
report("Starting", 0, null);
var args = $"-streams {Streams} -servers {ServerCount} -duration 8 -timeout 90";
var psi = new ProcessStartInfo
{
FileName = binaryPath,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
using var process = Process.Start(psi)
?? throw new InvalidOperationException("Failed to start uwnspeedtest process");
// Track metadata from stderr for early UI display
string? serverInfo = null;
string? wanIp = null;
string? isp = null;
// Parse stderr lines for progress reporting
var stderrTask = Task.Run(async () =>
{
try
{
while (await process.StandardError.ReadLineAsync(cancellationToken) is { } line)
{
Logger.LogDebug("uwnspeedtest: {Line}", line);
if (line.StartsWith("Acquiring"))
report("Acquiring token", 2, "Getting test token...");
else if (line.StartsWith("IP: "))
{
// Parse "IP: 1.2.3.4 (ISP Name)"
var content = line[4..];
var parenIdx = content.IndexOf(" (", StringComparison.Ordinal);
if (parenIdx >= 0)
{
wanIp = content[..parenIdx].Trim();
isp = content[(parenIdx + 2)..].TrimEnd(')');
}
else
{
wanIp = content.Trim();
}
}
else if (line.StartsWith("Discovering"))
report("Discovering servers", 5, "Finding nearby servers...");
else if (line.StartsWith("Found"))
report("Selecting servers", 7, line);
else if (line.StartsWith("Servers: "))
{
serverInfo = line[9..].Trim();
SetMetadata(new WanTestMetadata(
ServerInfo: serverInfo,
Location: isp ?? "",
WanIp: wanIp));
report("Servers selected", 8, serverInfo);
}
else if (line.StartsWith("Measuring latency"))
report("Testing latency", 10, null);
else if (line.StartsWith("Latency: "))
report("Latency measured", 15, line);
else if (line.StartsWith("Testing download"))
report("Testing download", 20, null);
else if (line.StartsWith("Download: "))
report("Download complete", 55, "Down: " + line[10..].Trim());
else if (line.StartsWith("Testing upload"))
report("Testing upload", 60, null);
else if (line.StartsWith("Upload: "))
report("Upload complete", 95, null);
}
}
catch (OperationCanceledException) { /* expected */ }
}, CancellationToken.None);
// Read all stdout (JSON output)
var stdoutTask = process.StandardOutput.ReadToEndAsync(cancellationToken);
// Wait for process with timeout
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeoutCts.CancelAfter(TimeSpan.FromSeconds(120));
try
{
await process.WaitForExitAsync(timeoutCts.Token);
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
try { process.Kill(entireProcessTree: true); } catch { /* best effort */ }
throw new TimeoutException("UWN speed test timed out after 120 seconds");
}
catch (OperationCanceledException)
{
try { process.Kill(entireProcessTree: true); } catch { /* best effort */ }
throw;
}
await stderrTask;
var stdout = await stdoutTask;
if (string.IsNullOrWhiteSpace(stdout))
throw new InvalidOperationException(
$"UWN speed test binary produced no output (exit code: {process.ExitCode})");
// Parse JSON output
report("Processing", 96, null);
var json = JsonSerializer.Deserialize<WanSpeedTestResult>(stdout, JsonOptions);
if (json == null)
throw new InvalidOperationException("Failed to parse speed test JSON output");
if (!json.Success)
throw new InvalidOperationException($"Speed test failed: {json.Error}");
// Build result
var primaryServerHost = !string.IsNullOrEmpty(json.Metadata?.ServerHost)
? json.Metadata.ServerHost : "UWN Test";
var deviceName = !string.IsNullOrEmpty(json.Metadata?.Colo)
? json.Metadata.Colo : serverInfo ?? "UWN";
var downloadMbps = (json.Download?.Bps ?? 0) / 1_000_000.0;
var uploadMbps = (json.Upload?.Bps ?? 0) / 1_000_000.0;
// Update metadata with final values from JSON
var finalWanIp = !string.IsNullOrEmpty(json.Metadata?.Ip) ? json.Metadata.Ip : wanIp;
var finalIsp = !string.IsNullOrEmpty(json.Metadata?.Country) ? json.Metadata.Country : isp;
var finalServerInfo = !string.IsNullOrEmpty(json.Metadata?.Colo) ? json.Metadata.Colo : serverInfo;
SetMetadata(new WanTestMetadata(
ServerInfo: finalServerInfo ?? "UWN",
Location: finalIsp ?? "",
WanIp: finalWanIp));
var serverIp = _configuration["HOST_IP"];
var result = new Iperf3Result
{
Direction = SpeedTestDirection.UwnWan,
DeviceHost = primaryServerHost,
DeviceName = deviceName,
DeviceType = "WAN",
LocalIp = serverIp,
DownloadBitsPerSecond = json.Download?.Bps ?? 0,
UploadBitsPerSecond = json.Upload?.Bps ?? 0,
DownloadBytes = json.Download?.Bytes ?? 0,
UploadBytes = json.Upload?.Bytes ?? 0,
PingMs = json.Latency?.UnloadedMs ?? 0,
JitterMs = json.Latency?.JitterMs ?? 0,
DownloadLatencyMs = json.Download?.LoadedLatencyMs > 0 ? json.Download.LoadedLatencyMs : null,
DownloadJitterMs = json.Download?.LoadedJitterMs > 0 ? json.Download.LoadedJitterMs : null,
UploadLatencyMs = json.Upload?.LoadedLatencyMs > 0 ? json.Upload.LoadedLatencyMs : null,
UploadJitterMs = json.Upload?.LoadedJitterMs > 0 ? json.Upload.LoadedJitterMs : null,
TestTime = DateTime.UtcNow,
Success = true,
ParallelStreams = json.Streams,
DurationSeconds = json.DurationSeconds,
};
// Identify WAN connection
try
{
var isMultiWan = false;
List<NetworkInfo>? wanNetworks = null;
if (_connectionService.IsConnected)
{
var networks = await _connectionService.GetNetworksAsync();
// Use device-level WAN interface detection to determine which WANs
// are actually active on the gateway. The network config's "enabled"
// field is unreliable - UniFi reports disabled WANs as enabled=true.
HashSet<string>? activeWanGroups = null;
using (var scope = _scopeFactory.CreateScope())
{
var sqmService = scope.ServiceProvider.GetRequiredService<ISqmService>();
var activeWans = await sqmService.GetWanInterfacesFromControllerAsync();
if (activeWans.Count > 0)
{
activeWanGroups = new HashSet<string>(
activeWans.Where(w => !string.IsNullOrEmpty(w.NetworkGroup))
.Select(w => w.NetworkGroup!),
StringComparer.OrdinalIgnoreCase);
}
else
{
Logger.LogWarning("No active WAN interfaces detected on gateway - falling back to network config filter");
}
}
wanNetworks = networks.Where(n => n.IsWan && n.Enabled
&& (activeWanGroups == null || activeWanGroups.Contains(n.WanNetworkgroup ?? "WAN")))
.ToList();
isMultiWan = wanNetworks.Count > 1;
}
if (isMultiWan)
{
// Try SSH route lookup to identify which WANs were actually used
var combo = await IdentifyWanComboViaSshAsync(
json.Metadata?.ServerIps, serverIp, wanNetworks!, cancellationToken);
if (combo != null)
{
result.WanNetworkGroup = combo.Value.Group;
result.WanName = combo.Value.Name;
}
else
{
// Fallback: if measured speed exceeds 125% of any single WAN's
// configured speed, assume multiple WANs are bonded. The 25% margin
// accounts for ISP overprovisioning and burst headroom.
var maxSingleDown = wanNetworks!.Max(n => n.WanDownloadMbps ?? 0);
var maxSingleUp = wanNetworks!.Max(n => n.WanUploadMbps ?? 0);
const double fudgeFactor = 1.25;
if (downloadMbps > maxSingleDown * fudgeFactor || uploadMbps > maxSingleUp * fudgeFactor)
{
var groups = wanNetworks!
.Select(n => n.WanNetworkgroup ?? "WAN")
.Distinct().OrderBy(g => g);
result.WanNetworkGroup = string.Join("+", groups);
var names = wanNetworks!
.Select(n => !string.IsNullOrEmpty(n.Name) ? n.Name : n.WanNetworkgroup ?? "WAN")
.Distinct().OrderBy(n => n);
result.WanName = string.Join(" + ", names);
}
else
{
var (wanGroup, wanName) = await PathAnalyzer.IdentifyWanConnectionAsync(
finalWanIp ?? "", downloadMbps, uploadMbps, cancellationToken);
result.WanNetworkGroup = wanGroup;
result.WanName = wanName;
}
}
}
else
{
var (wanGroup, wanName) = await PathAnalyzer.IdentifyWanConnectionAsync(
finalWanIp ?? "", downloadMbps, uploadMbps, cancellationToken);
result.WanNetworkGroup = wanGroup;
result.WanName = wanName;
}
}
catch (Exception ex)
{
Logger.LogDebug(ex, "Could not identify WAN connection");
}
Logger.LogInformation(
"UWN WAN speed test complete: Down {Download:F1} Mbps, Up {Upload:F1} Mbps, Latency {Latency:F1} ms",
downloadMbps, uploadMbps, json.Latency?.UnloadedMs ?? 0);
report("Complete", 100, $"Down: {downloadMbps:F1} / Up: {uploadMbps:F1} Mbps");
return result;
}
protected override Iperf3Result CreateFailedResult(string errorMessage) => new()
{
Direction = SpeedTestDirection.UwnWan,
DeviceHost = "UWN Test",
DeviceName = "UWN",
DeviceType = "WAN",
TestTime = DateTime.UtcNow,
Success = false,
ErrorMessage = errorMessage,
};
/// <summary>
/// Use SSH route lookup on the gateway to determine which WAN interfaces
/// traffic to the test servers traverses. Returns the combo group and name,
/// or null if SSH is unavailable or route lookup fails.
/// </summary>
private async Task<(string Group, string Name)?> IdentifyWanComboViaSshAsync(
List<string>? serverIps,
string? nasIp,
List<NetworkInfo> wanNetworks,
CancellationToken cancellationToken)
{
if (serverIps == null || serverIps.Count == 0)
return null;
try
{
var settings = await _gatewaySsh.GetSettingsAsync();
if (string.IsNullOrEmpty(settings.Host) || !settings.HasCredentials || !settings.Enabled)
return null;
// Get interface→group mapping from SqmService (scoped)
Dictionary<string, (string? Group, string Name)> ifToWan;
List<string> wanIfaceNames;
using (var scope = _scopeFactory.CreateScope())
{
var sqmService = scope.ServiceProvider.GetRequiredService<ISqmService>();
var wanInterfaces = await sqmService.GetWanInterfacesFromControllerAsync();
if (wanInterfaces.Count == 0)
return null;
ifToWan = wanInterfaces.ToDictionary(
w => w.Interface,
w => (w.NetworkGroup, w.Name));
wanIfaceNames = wanInterfaces.Select(w => w.Interface).ToList();
}
var validIps = serverIps.Distinct()
.Where(ip => System.Net.IPAddress.TryParse(ip, out _))
.ToList();
if (validIps.Count == 0)
return null;
// Single SSH command:
// 1. Get WAN interface public IPs (to map conntrack reply dst → WAN)
// 2. Query conntrack for connections from NAS to speed test servers
var ipAddrCmds = string.Join("; ", wanIfaceNames.Select(iface => $"ip -4 -o addr show {iface}"));
var escapedIps = string.Join("|", validIps.Select(ip => Regex.Escape(ip)));
string conntrackCmd;
if (!string.IsNullOrEmpty(nasIp) && System.Net.IPAddress.TryParse(nasIp, out _))
conntrackCmd = $"conntrack -L -s {nasIp} 2>/dev/null | grep -E '{escapedIps}'";
else
conntrackCmd = $"conntrack -L 2>/dev/null | grep -E '{escapedIps}'";
var fullCmd = $"{ipAddrCmds}; echo '---CONNTRACK---'; {conntrackCmd}";
var (success, output) = await _gatewaySsh.RunCommandAsync(
fullCmd, TimeSpan.FromSeconds(10), cancellationToken);
if (!success || string.IsNullOrEmpty(output))
return null;
// Split output into ip addr section and conntrack section
var separatorIdx = output.IndexOf("---CONNTRACK---", StringComparison.Ordinal);
if (separatorIdx < 0)
return null;
var ipAddrOutput = output[..separatorIdx];
var conntrackOutput = output[(separatorIdx + "---CONNTRACK---".Length)..];
// Build WAN public IP → interface mapping
// Format: "2: eth2 inet 67.209.42.120/25 ..."
var wanIpToIface = new Dictionary<string, string>();
foreach (Match match in Regex.Matches(ipAddrOutput, @"\d+:\s+(\S+)\s+inet\s+(\d+\.\d+\.\d+\.\d+)/"))
{
var iface = match.Groups[1].Value;
var ip = match.Groups[2].Value;
if (ifToWan.ContainsKey(iface))
wanIpToIface[ip] = iface;
}
Logger.LogDebug("WAN IP mapping: {Mapping}",
string.Join(", ", wanIpToIface.Select(kv => $"{kv.Value}={kv.Key}")));
// Parse conntrack: each line has two dst= values
// Original: src=<nas> dst=<server> ... Reply: src=<server> dst=<wan_public_ip>
var wanGroups = new HashSet<string>();
var wanNames = new HashSet<string>();
foreach (var line in conntrackOutput.Split('\n', StringSplitOptions.RemoveEmptyEntries))
{
var dstMatches = Regex.Matches(line, @"dst=(\d+\.\d+\.\d+\.\d+)");
if (dstMatches.Count >= 2)
{
// Second dst= is the reply direction → WAN public IP
var replyDstIp = dstMatches[1].Groups[1].Value;
if (wanIpToIface.TryGetValue(replyDstIp, out var iface) &&
ifToWan.TryGetValue(iface, out var wan))
{
wanGroups.Add(wan.Group ?? "WAN");
wanNames.Add(wan.Name);
}
}
}
if (wanGroups.Count == 0)
{
Logger.LogDebug("Conntrack found no WAN matches for {Count} server IPs", validIps.Count);
return null;
}
var sortedGroups = wanGroups.OrderBy(g => g).ToList();
var combo = string.Join("+", sortedGroups);
// Build name in same order as groups
var nameMap = ifToWan.Values
.Where(w => wanGroups.Contains(w.Group ?? "WAN"))
.DistinctBy(w => w.Group)
.OrderBy(w => w.Group)
.Select(w => w.Name);
var comboName = string.Join(" + ", nameMap);
Logger.LogInformation(
"Conntrack identified WAN combo: {Combo} ({Name}) from {ServerCount} server IPs",
combo, comboName, validIps.Count);
return (combo, comboName);
}
catch (Exception ex)
{
Logger.LogDebug(ex, "SSH-based WAN identification failed, falling back");
return null;
}
}
#region Binary Resolution
/// <summary>
/// Resolves the local uwnspeedtest binary path based on the current platform.
/// Binary naming convention: uwnspeedtest-{os}-{arch}[.exe]
/// </summary>
private static string GetLocalBinaryPath()
{
var os = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "windows"
: RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "darwin"
: "linux";
var arch = RuntimeInformation.OSArchitecture switch
{
Architecture.X64 => "amd64",
Architecture.Arm64 => "arm64",
Architecture.X86 => "386",
_ => "amd64"
};
var ext = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : "";
var binaryName = $"uwnspeedtest-{os}-{arch}{ext}";
return Path.Combine(AppContext.BaseDirectory, "tools", binaryName);
}
#endregion
#region JSON Models
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
Converters = { new JsonStringEnumConverter() }
};
private sealed class WanSpeedTestResult
{
public bool Success { get; set; }
public string? Error { get; set; }
public WanMetadata? Metadata { get; set; }
public WanLatency? Latency { get; set; }
public WanThroughput? Download { get; set; }
public WanThroughput? Upload { get; set; }
public int Streams { get; set; }
public int DurationSeconds { get; set; }
}
private sealed class WanMetadata
{
public string Ip { get; set; } = "";
public string Colo { get; set; } = "";
public string Country { get; set; } = "";
public string ServerHost { get; set; } = "";
public List<string>? ServerIps { get; set; }
}
private sealed class WanLatency
{
public double UnloadedMs { get; set; }
public double JitterMs { get; set; }
}
private sealed class WanThroughput
{
public double Bps { get; set; }
public long Bytes { get; set; }
public double LoadedLatencyMs { get; set; }
public double LoadedJitterMs { get; set; }
}
#endregion
}