diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-12-16 20:54:09 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-16 20:54:09 -0600 |
| commit | 4f4385475b100c7e86d2281ae685741c21abebc2 (patch) | |
| tree | 65b0d0f70fe0ae1904d61ea9625424eb9c23acb1 /src | |
| parent | 6a3076a0f26d06805eb979951aace79e2d03a6fe (diff) | |
| parent | aacd3a2944c0e68d7c4d5bda209cb3bf84bff4ba (diff) | |
| download | slskd-4f4385475b100c7e86d2281ae685741c21abebc2.tar.gz | |
Merge pull request #1018 from slskd/ip-pinning
Add the ability to specify a local IP address to listen on
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 4 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/IPAddressAttribute.cs | 48 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 9 |
3 files changed, 60 insertions, 1 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index b6d11914..a7483105 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -361,6 +361,7 @@ namespace slskd writeBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer); var patch = new SoulseekClientOptionsPatch( + listenIPAddress: IPAddress.Parse(OptionsAtStartup.Soulseek.ListenIpAddress), listenPort: OptionsAtStartup.Soulseek.ListenPort, enableListener: true, userEndPointCache: new UserEndPointCache(), @@ -386,7 +387,7 @@ namespace slskd await Client.ReconfigureOptionsAsync(patch); Log.Debug("Client configured"); - Log.Information("Listening for incoming connections on port {Port}", OptionsAtStartup.Soulseek.ListenPort); + Log.Information("Listening for incoming connections son {IP}:{Port}", OptionsAtStartup.Soulseek.ListenIpAddress, OptionsAtStartup.Soulseek.ListenPort); if (OptionsAtStartup.Soulseek.Connection.Proxy.Enabled) { @@ -1049,6 +1050,7 @@ namespace slskd } var patch = new SoulseekClientOptionsPatch( + listenIPAddress: old.ListenIpAddress == update.ListenIpAddress ? null : IPAddress.Parse(update.ListenIpAddress), listenPort: old.ListenPort == update.ListenPort ? null : update.ListenPort, enableDistributedNetwork: old.DistributedNetwork.Disabled == update.DistributedNetwork.Disabled ? null : !update.DistributedNetwork.Disabled, distributedChildLimit: old.DistributedNetwork.ChildLimit == update.DistributedNetwork.ChildLimit ? null : update.DistributedNetwork.ChildLimit, diff --git a/src/slskd/Common/Validation/IPAddressAttribute.cs b/src/slskd/Common/Validation/IPAddressAttribute.cs new file mode 100644 index 00000000..6e53df57 --- /dev/null +++ b/src/slskd/Common/Validation/IPAddressAttribute.cs @@ -0,0 +1,48 @@ +// <copyright file="IPAddressAttribute.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> + +namespace slskd.Validation +{ + using System.ComponentModel.DataAnnotations; + using System.Net; + + /// <summary> + /// Validates that the string is a valid IPv4 or IPv6 IP address. + /// </summary> + public class IPAddressAttribute : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + if (value != null) + { + var fail = new ValidationResult($"The {validationContext.DisplayName} field specifies an invalid IPv4 or IPv6 IP address."); + + if (value is not string valueAsString) + { + return fail; + } + + if (!IPAddress.TryParse(valueAsString, out _)) + { + return fail; + } + } + + return ValidationResult.Success; + } + } +}
\ No newline at end of file diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 1d4742da..1eb92449 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1295,6 +1295,15 @@ namespace slskd public string Description { get; init; } = "A slskd user. https://github.com/slskd/slskd"; /// <summary> + /// Gets the local IP address on which to listen for incoming connections. + /// </summary> + [Argument(default, "slsk-listen-ip-address")] + [EnvironmentVariable("SLSK_LISTEN_IP_ADDRESS")] + [Description("local IP address on which to listen for incoming connections")] + [IPAddress] + public string ListenIpAddress { get; init; } = "0.0.0.0"; + + /// <summary> /// Gets the port on which to listen for incoming connections. /// </summary> [Argument(default, "slsk-listen-port")] |
