Windows 0.3.1 clean monitor source delta; no private history or other platform changes
This commit is contained in:
+26
-3
@@ -7,7 +7,7 @@ namespace UCVL.VPNPlus;
|
||||
|
||||
internal sealed class VpnApiClient : IDisposable
|
||||
{
|
||||
internal const string BaseAddress = "https://vpn.toplc.cn/";
|
||||
internal const string BaseAddress = "https://vpn.example.invalid/";
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
@@ -65,6 +65,14 @@ internal sealed class VpnApiClient : IDisposable
|
||||
return await GitReleaseUpdater.GetLatestAsync(cancellationToken);
|
||||
}
|
||||
|
||||
internal async Task<NodeTrafficSnapshot> GetNodeTrafficAsync(string token, CancellationToken cancellationToken)
|
||||
{
|
||||
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
deadline.CancelAfter(TimeSpan.FromSeconds(3));
|
||||
using var request = AuthorizedRequest(HttpMethod.Get, "api/v1/client/node-traffic/", token);
|
||||
return await SendAsync<NodeTrafficSnapshot>(request, deadline.Token, maximumBodyBytes: 32 * 1024);
|
||||
}
|
||||
|
||||
internal async Task<ConfigResponse> GetConfigAsync(string token, string nodeSlug, string routeMode, CancellationToken cancellationToken, IEnumerable<string>? excluded = null)
|
||||
{
|
||||
var url = $"api/v1/client/config/?node={Uri.EscapeDataString(nodeSlug)}&route={Uri.EscapeDataString(routeMode)}";
|
||||
@@ -126,7 +134,7 @@ internal sealed class VpnApiClient : IDisposable
|
||||
return request;
|
||||
}
|
||||
|
||||
private async Task<T> SendAsync<T>(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
private async Task<T> SendAsync<T>(HttpRequestMessage request, CancellationToken cancellationToken, int? maximumBodyBytes = null)
|
||||
{
|
||||
var uri = request.RequestUri ?? throw new InvalidOperationException("管理接口地址为空。");
|
||||
PlatformBootstrapTransport.ValidateRequest(uri.IsAbsoluteUri ? uri : new Uri(new Uri(BaseAddress), uri), request.Headers.Host);
|
||||
@@ -136,7 +144,22 @@ internal sealed class VpnApiClient : IDisposable
|
||||
deadline.CancelAfter(TimeSpan.FromSeconds(20));
|
||||
cancellationToken = deadline.Token;
|
||||
using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
||||
var payload = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
string payload;
|
||||
if (maximumBodyBytes is { } maximum)
|
||||
{
|
||||
if (response.Content.Headers.ContentLength > maximum) throw new IOException("监控响应过大。");
|
||||
using var body = new MemoryStream();
|
||||
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
|
||||
var buffer = new byte[4096];
|
||||
int read;
|
||||
while ((read = await stream.ReadAsync(buffer, cancellationToken)) != 0)
|
||||
{
|
||||
if (body.Length + read > maximum) throw new IOException("监控响应过大。");
|
||||
body.Write(buffer, 0, read);
|
||||
}
|
||||
payload = new UTF8Encoding(false, true).GetString(body.ToArray());
|
||||
}
|
||||
else payload = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
string message;
|
||||
|
||||
Reference in New Issue
Block a user