aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Integrations/VPN
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-02-01 14:12:05 -0600
committerJP Dillingham <jp@dillingham.me>2026-02-01 14:12:05 -0600
commiteb53ab89dde320acc10cd31efd863a5277cd23fa (patch)
treece0a7f0422b6180b386713cd59ff6a28117790bd /src/slskd/Integrations/VPN
parentf5365181c88885eaba3ad69511a5a2748f3481ed (diff)
downloadslskd-eb53ab89dde320acc10cd31efd863a5277cd23fa.tar.gz
stub vpn service and gluetun client
Diffstat (limited to 'src/slskd/Integrations/VPN')
-rw-r--r--src/slskd/Integrations/VPN/GluetunClient.cs116
-rw-r--r--src/slskd/Integrations/VPN/VPNService.cs119
2 files changed, 235 insertions, 0 deletions
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 file="GluetunClient.cs" company="slskd Team">
+// 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/.
+// </copyright>
+
+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<Options> optionsMonitor)
+ {
+ HttpClientFactory = httpClientFactory;
+ OptionsMonitor = optionsMonitor;
+ }
+
+ private ILogger Log { get; } = Serilog.Log.ForContext<GluetunClient>();
+ private IHttpClientFactory HttpClientFactory { get; }
+ private IOptionsMonitor<Options> OptionsMonitor { get; }
+ private GluetunVpnOptions Options => OptionsMonitor.CurrentValue.Integration.Vpn.Gluetun;
+
+ public async Task<bool> 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<GluetunStatusResponse>()
+ ?? 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<int> 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<GluetunPortForwardResponse>()
+ ?? 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 file="VPNService.cs" company="slskd Team">
+// 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/.
+// </copyright>
+
+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<Options> 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<SoulseekClientDisconnectedEvent>(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<VPNService>();
+ private IHttpClientFactory HttpClientFactory { get; }
+ private IOptionsMonitor<Options> 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