diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-02-14 17:55:58 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-02-14 17:55:58 -0600 |
| commit | 7f0312a1ea28e611a126ddb07117aa1072f1c36a (patch) | |
| tree | c78c5372d6bb23464480a509e3aee1789b5b78a1 /src/slskd/Core | |
| parent | 8d30845f59d693aa6909c9fe8291493422dc10aa (diff) | |
| parent | 9f3b7217dd4554482c904becc101a4cf3074544e (diff) | |
| download | slskd-7f0312a1ea28e611a126ddb07117aa1072f1c36a.tar.gz | |
Merge pull request #1632 from slskd/gluetun
Add VPN integration and gluetun client
Diffstat (limited to 'src/slskd/Core')
| -rw-r--r-- | src/slskd/Core/ConnectionWatchdog.cs | 30 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 120 | ||||
| -rw-r--r-- | src/slskd/Core/OptionsOverlay.cs | 4 | ||||
| -rw-r--r-- | src/slskd/Core/State.cs | 13 |
4 files changed, 162 insertions, 5 deletions
diff --git a/src/slskd/Core/ConnectionWatchdog.cs b/src/slskd/Core/ConnectionWatchdog.cs index b7a4fa34..d2f7caa8 100644 --- a/src/slskd/Core/ConnectionWatchdog.cs +++ b/src/slskd/Core/ConnectionWatchdog.cs @@ -23,6 +23,7 @@ namespace slskd using System.Threading; using System.Threading.Tasks; using Serilog; + using slskd.Integrations.VPN; using Soulseek; /// <summary> @@ -41,15 +42,21 @@ namespace slskd /// Initializes a new instance of the <see cref="ConnectionWatchdog"/> class. /// </summary> /// <param name="soulseekClient"></param> + /// <param name="vpnService"></param> /// <param name="optionsMonitor"></param> + /// <param name="optionsAtStartup"></param> /// <param name="applicationState"></param> public ConnectionWatchdog( ISoulseekClient soulseekClient, + VPNService vpnService, IOptionsMonitor<Options> optionsMonitor, + OptionsAtStartup optionsAtStartup, IManagedState<State> applicationState) { Client = soulseekClient; + VPN = vpnService; OptionsMonitor = optionsMonitor; + OptionsAtStartup = optionsAtStartup; ApplicationState = applicationState; WatchdogTimer = new System.Timers.Timer() @@ -76,10 +83,17 @@ namespace slskd /// </summary> public bool IsAttemptingConnection => SyncRoot.CurrentCount == 0; + /// <summary> + /// Gets a value indicating whether the watchdog is waiting for the VPN client to connect. + /// </summary> + public bool IsAwaitingVpn { get; private set; } + private ISoulseekClient Client { get; } + private VPNService VPN { get; } private bool Disposed { get; set; } private ILogger Log { get; } = Serilog.Log.ForContext<Application>(); private IOptionsMonitor<Options> OptionsMonitor { get; set; } + private OptionsAtStartup OptionsAtStartup { get; set; } private IManagedState<State> ApplicationState { get; } private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); private object StateLock { get; } = new object(); @@ -101,6 +115,11 @@ namespace slskd /// <remarks>This should be called at application startup.</remarks> public virtual void Start() { + if (OptionsAtStartup.Integration.Vpn.Enabled) + { + VPN.StartPolling(); + } + WatchdogTimer.Enabled = true; UpdateApplicationState(); @@ -184,6 +203,7 @@ namespace slskd { IsEnabled = IsEnabled, IsAttemptingConnection = IsAttemptingConnection, + IsAwaitingVpn = IsAwaitingVpn, NextAttemptAt = IsAttemptingConnection ? nextAttemptAt ?? state.ConnectionWatchdog.NextAttemptAt : null, // only null this if we're not attempting, otherwise it sticks }, }); @@ -209,8 +229,6 @@ namespace slskd { try { - Log.Debug("AttemptConnection initiated by {Source}", source); - /* go until we connect and break, or something stops the watchdog. why don't we just use the timer here? good question. we could, but a loop gives us more control and precision. at the expensive of needing to @@ -232,6 +250,14 @@ namespace slskd return; } + if (OptionsAtStartup.Integration.Vpn.Enabled && !VPN.IsReady) + { + IsAwaitingVpn = true; + return; + } + + IsAwaitingVpn = false; + CancellationTokenSource = new CancellationTokenSource(); if (attempts > 0) diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 200b0ee8..644fb885 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -33,6 +33,7 @@ namespace slskd using slskd.Authentication; using slskd.Configuration; using slskd.Events; + using slskd.Integrations.VPN; using slskd.Relay; using slskd.Shares; using slskd.Validation; @@ -2275,6 +2276,12 @@ namespace slskd public class IntegrationOptions { /// <summary> + /// Gets VPN options. + /// </summary> + [Validate] + public VpnOptions Vpn { get; init; } = new(); + + /// <summary> /// Gets webhook configuration. /// </summary> [Validate] @@ -2299,6 +2306,119 @@ namespace slskd public PushbulletOptions Pushbullet { get; init; } = new PushbulletOptions(); /// <summary> + /// VPN options. + /// </summary> + public class VpnOptions : IValidatableObject + { + /// <summary> + /// Gets a value indicating whether the VPN integration is enabled. + /// </summary> + [Argument(default, "vpn")] + [EnvironmentVariable("VPN")] + [Description("enable VPN integration")] + [RequiresRestart] + public bool Enabled { get; init; } = false; + + /// <summary> + /// Gets a value indicating whether the configured VPN supports dynamic port forwarding. + /// </summary> + [Argument(default, "vpn-port-forwarding")] + [EnvironmentVariable("VPN_PORT_FORWARDING")] + [Description("VPN supports dynamic port forwarding")] + public bool PortForwarding { get; init; } = false; + + /// <summary> + /// Gets the rate at which to poll the configured VPN client for status updates, in milliseconds. + /// </summary> + [Argument(default, "vpn-polling-interval")] + [EnvironmentVariable("VPN_POLLING_INTERVAL")] + [Description("VPN client status polling interval")] + [Range(500, int.MaxValue)] + [RequiresRestart] + public int PollingInterval { get; init; } = 2500; + + /// <summary> + /// Gets Gluetun options. + /// </summary> + [Validate] + public GluetunVpnOptions Gluetun { get; init; } = new GluetunVpnOptions(); + + public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) + { + if (Enabled) + { + // as/if more VPN clients are added, update this logic to ensure that exactly one is configured + if (string.IsNullOrWhiteSpace(Gluetun?.Url)) + { + yield return new ValidationResult("VPN is enabled but no client is configured"); + yield break; + } + + if (!Uri.TryCreate(Gluetun.Url, UriKind.Absolute, out _)) + { + yield return new ValidationResult("The gluetun URL must be absolute, e.g. 'http://127.0.0.1:8000'"); + } + } + } + + /// <summary> + /// Gluetun options. + /// </summary> + public class GluetunVpnOptions + { + /// <summary> + /// Gets the Gluetun integration version. + /// </summary> + /// <remarks> + /// Only v1 of the Gluetun API exists right now; this is to ease a transition if there's ever a v2. + /// </remarks> + [Range(1, 1)] + public int Version { get; init; } = 1; + + /// <summary> + /// Gets the fully qualified root URL for the Gluetun control server. + /// </summary> + [Argument(default, "vpn-gluetun-url")] + [EnvironmentVariable("VPN_GLUETUN_URL")] + [Description("URL for gluetun control server")] + public string Url { get; init; } + + /// <summary> + /// Gets the timeout for HTTP requests to the Gluetun control server, in milliseconds. + /// </summary> + [Argument(default, "vpn-gluetun-timeout")] + [EnvironmentVariable("VPN_GLUETUN_TIMEOUT")] + [Description("timeout for HTTP requests to gluetun control server")] + [Range(500, 10_000)] + public int Timeout { get; init; } = 1000; + + /// <summary> + /// Gets the username used for basic auth. + /// </summary> + [Argument(default, "vpn-gluetun-username")] + [EnvironmentVariable("VPN_GLUETUN_USERNAME")] + [Description("username for gluetun control server")] + public string Username { get; init; } + + /// <summary> + /// Gets the password used for basic auth. + /// </summary> + [Argument(default, "vpn-gluetun-password")] + [EnvironmentVariable("VPN_GLUETUN_PASSWORD")] + [Description("password for gluetun control server")] + public string Password { get; init; } + + /// <summary> + /// Gets the API key used for API key auth. + /// </summary> + [Argument(default, "vpn-gluetun-api-key")] + [EnvironmentVariable("VPN_GLUETUN_API_KEY")] + [Description("API key for gluetun control server")] + public string ApiKey { get; init; } + } + } + + /// <summary> /// Webhook configuration. /// </summary> public class WebhookOptions diff --git a/src/slskd/Core/OptionsOverlay.cs b/src/slskd/Core/OptionsOverlay.cs index 11167da5..4edd3b02 100644 --- a/src/slskd/Core/OptionsOverlay.cs +++ b/src/slskd/Core/OptionsOverlay.cs @@ -37,7 +37,7 @@ namespace slskd /// selectively applies the patch overlay only to information that is explicitly supplied. /// </para> /// </remarks> - public class OptionsOverlay + public record OptionsOverlay { /// <summary> /// Gets options for the Soulseek client. @@ -48,7 +48,7 @@ namespace slskd /// <summary> /// Soulseek client options. /// </summary> - public class SoulseekOptionsPatch + public record SoulseekOptionsPatch { /// <summary> /// Gets the local IP address on which to listen for incoming connections. diff --git a/src/slskd/Core/State.cs b/src/slskd/Core/State.cs index 4f7876de..cd742348 100644 --- a/src/slskd/Core/State.cs +++ b/src/slskd/Core/State.cs @@ -36,6 +36,7 @@ namespace slskd public bool PendingRestart { get; init; } public ServerState Server { get; init; } = new ServerState(); public ServerConnectionWatchdogState ConnectionWatchdog { get; init; } = new ServerConnectionWatchdogState(); + public VpnState Vpn { get; init; } = new VpnState(); public RelayState Relay { get; init; } = new RelayState(); public HealthState Health { get; init; } = new HealthState(); public UserState User { get; init; } = new UserState(); @@ -90,7 +91,17 @@ namespace slskd { public bool IsEnabled { get; init; } = false; public bool IsAttemptingConnection { get; init; } = false; - public DateTime? NextAttemptAt { get; init; } + public bool IsAwaitingVpn { get; init; } = false; + public DateTime? NextAttemptAt { get; init; } = null; + } + + public record VpnState + { + public bool IsReady { get; init; } = false; + public bool IsConnected { get; init; } = false; + public IPAddress PublicIPAddress { get; init; } = null; + public string Location { get; init; } = null; + public int? ForwardedPort { get; init; } = null; } public record RelayState |
