From eb53ab89dde320acc10cd31efd863a5277cd23fa Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 1 Feb 2026 14:12:05 -0600 Subject: stub vpn service and gluetun client --- src/slskd/Integrations/VPN/GluetunClient.cs | 116 +++++++++++++++++++++++++++ src/slskd/Integrations/VPN/VPNService.cs | 119 ++++++++++++++++++++++++++++ 2 files changed, 235 insertions(+) create mode 100644 src/slskd/Integrations/VPN/GluetunClient.cs create mode 100644 src/slskd/Integrations/VPN/VPNService.cs diff --git a/src/slskd/Integrations/VPN/GluetunClient.cs b/src/slskd/Integrations/VPN/GluetunClient.cs new file mode 100644 index 00000000..16713963 --- /dev/null +++ b/src/slskd/Integrations/VPN/GluetunClient.cs @@ -0,0 +1,116 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +using Microsoft.Extensions.Options; + +namespace slskd.Integrations.VPN; + +using System; +using System.Net.Http; +using System.Net.Http.Json; +using System.Threading.Tasks; +using Serilog; +using static slskd.Options.IntegrationOptions.VpnOptions; + +public enum GluetunClientAuthOptions +{ + Basic, + ApiKey, +} + +public class GluetunClient : IVPNClient +{ + public GluetunClient(IHttpClientFactory httpClientFactory, IOptionsMonitor optionsMonitor) + { + HttpClientFactory = httpClientFactory; + OptionsMonitor = optionsMonitor; + } + + private ILogger Log { get; } = Serilog.Log.ForContext(); + private IHttpClientFactory HttpClientFactory { get; } + private IOptionsMonitor OptionsMonitor { get; } + private GluetunVpnOptions Options => OptionsMonitor.CurrentValue.Integration.Vpn.Gluetun; + + public async Task GetConnectionStatusAsync() + { + using var http = HttpClientFactory.CreateClient(); + http.Timeout = TimeSpan.FromMilliseconds(1000); + ConfigureAuth(http); + + try + { + using var response = await http.GetAsync($"{Options.Url.TrimEnd('/')}/v1/vpn/status"); + response.EnsureSuccessStatusCode(); + + var status = await response.Content.ReadFromJsonAsync() + ?? throw new Exception($"Failed to deserialize Gluetun status response; got: {await response.Content.ReadAsStringAsync()}"); + + return status.Status.Equals("running", StringComparison.OrdinalIgnoreCase); + } + catch (Exception ex) + { + Log.Warning(ex, "Failed to retrieve status from Gluetun: {Message}", ex.Message); + throw new VPNClientException($"Failed to retrieve status from Gluetun: {ex.Message}", ex); + } + } + + public async Task GetForwardedPortAsync() + { + using var http = HttpClientFactory.CreateClient(); + http.Timeout = TimeSpan.FromMilliseconds(1000); + ConfigureAuth(http); + + try + { + using var response = await http.GetAsync($"{Options.Url.TrimEnd('/')}/v1/portforward"); + response.EnsureSuccessStatusCode(); + + var port = await response.Content.ReadFromJsonAsync() + ?? throw new Exception($"Failed to deserialize Gluetun port forward response; got: {await response.Content.ReadAsStringAsync()}"); + + return port.Port; + } + catch (Exception ex) + { + Log.Warning(ex, "Failed to retrieve status from Gluetun: {Message}", ex.Message); + throw new VPNClientException($"Failed to retrieve status from Gluetun: {ex.Message}", ex); + } + } + + private void ConfigureAuth(HttpClient client) + { + if (Options.Auth.Equals(GluetunClientAuthOptions.Basic.ToString(), StringComparison.OrdinalIgnoreCase)) + { + var creds = $"{Options.Username}:{Options.Password}".ToBase64(); + client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Basic {creds}"); + } + else + { + client.DefaultRequestHeaders.TryAddWithoutValidation("X-API-Key", Options.ApiKey); + } + } + + private class GluetunStatusResponse + { + public string Status { get; init; } + } + + private class GluetunPortForwardResponse + { + public int Port { get; init; } + } +} \ No newline at end of file diff --git a/src/slskd/Integrations/VPN/VPNService.cs b/src/slskd/Integrations/VPN/VPNService.cs new file mode 100644 index 00000000..1b636d62 --- /dev/null +++ b/src/slskd/Integrations/VPN/VPNService.cs @@ -0,0 +1,119 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +using Microsoft.Extensions.Options; + +namespace slskd.Integrations.VPN; + +using System; +using System.Net.Http; +using System.Threading; +using Serilog; +using slskd.Events; +using static slskd.Options.IntegrationOptions; + +public class VPNService : IDisposable +{ + public VPNService( + IOptionsMonitor optionsMonitor, + EventBus eventBus, + IHttpClientFactory httpClientFactory) + { + OptionsMonitor = optionsMonitor; + HttpClientFactory = httpClientFactory; + + Timer = new System.Timers.Timer(interval: 1000) + { + AutoReset = true, + }; + + Timer.Elapsed += Timer_Elapsed; + + EventBus = eventBus; + EventBus.Subscribe(nameof(VPNService), e => + { + Timer.Interval = TimeSpan.FromSeconds(1).TotalMilliseconds; + }); + + if (Options.Enabled) + { + if (!string.IsNullOrEmpty(Options.Gluetun.Url)) + { + Client = new GluetunClient(HttpClientFactory, OptionsMonitor); + Timer.Start(); + return; + } + + Log.Error("VPN integration is enabled, but no VPN client has been configured"); + } + } + + private EventBus EventBus { get; } + private IVPNClient Client { get; set; } + private System.Timers.Timer Timer { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); + private IHttpClientFactory HttpClientFactory { get; } + private IOptionsMonitor OptionsMonitor { get; } + private VpnOptions Options => OptionsMonitor.CurrentValue.Integration.Vpn; + private bool Disposed { get; set; } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (!Disposed) + { + if (disposing) + { + Timer.Dispose(); + } + + Disposed = true; + } + } + + private async void Timer_Elapsed(object sender, EventArgs args) + { + try + { + var isConnected = await Client.GetConnectionStatusAsync(); + + if (isConnected) + { + var port = await Client.GetForwardedPortAsync(); + + Program.ApplyConfigurationOverlay(Program.ConfigurationOverlay with + { + Soulseek = Program.ConfigurationOverlay.Soulseek with + { + ListenPort = 123, + }, + }); + + Timer.Interval = TimeSpan.FromMinutes(5).TotalMilliseconds; + } + } + catch (Exception ex) + { + Log.Error(ex, "Failed to communicate with VPN client: {Message}", ex.Message); + } + } +} \ No newline at end of file -- cgit v1.2.3