diff options
| -rw-r--r-- | config/slskd.example.yml | 12 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 85 | ||||
| -rw-r--r-- | src/slskd/Core/Types/NetworkOperationMode.cs | 35 |
3 files changed, 132 insertions, 0 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index ec2504a0..7afeb5c8 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -10,6 +10,18 @@ # force_share_scan: false # no_version_check: false # log_sql: false +# network: +# mode: controller +# # controller config is required when running in 'agent' mode +# # this specifies the network controller that will be controlling this agent +# controller: +# address: +# secret: <a 32-255 character string that matches the controller> +# # agent config is optional when running in 'controller' mode +# # this specifies all of the agents on the network +# agents: +# my_agent: +# secret: <a 32-255 character string unique to this agent> # directories: # incomplete: ~ # downloads: ~ diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 4c146d32..7debb525 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -201,6 +201,12 @@ namespace slskd public string ConfigurationFile { get; init; } = Program.DefaultConfigurationFile; /// <summary> + /// Gets network options. + /// </summary> + [Validate] + public NetworkOptions Network { get; init; } = new NetworkOptions(); + + /// <summary> /// Gets directory options. /// </summary> [Validate] @@ -348,6 +354,85 @@ namespace slskd } /// <summary> + /// Network options. + /// </summary> + public class NetworkOptions : IValidatableObject + { + /// <summary> + /// Gets the application operation mode. + /// </summary> + [Argument('m', "network-operation-mode")] + [EnvironmentVariable("NETWORK_OPERATION_MODE")] + [Description("network operation mode; controller, agent")] + [RequiresRestart] + [Enum(typeof(NetworkOperationMode))] + public string OperationMode { get; init; } = NetworkOperationMode.Controller.ToString().ToLowerInvariant(); + + /// <summary> + /// Gets the controller configuration. + /// </summary> + public NetworkControllerConfigurationOptions Controller { get; init; } = new NetworkControllerConfigurationOptions(); + + /// <summary> + /// Gets the agent configuration. + /// </summary> + public Dictionary<string, NetworkAgentConfigurationOptions> Agents { get; init; } = new(); + + public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) + { + var results = new List<ValidationResult>(); + var controllerResults = new List<ValidationResult>(); + var agentResults = new List<ValidationResult>(); + + if (OperationMode.ToEnum<NetworkOperationMode>() == NetworkOperationMode.Agent && !Validator.TryValidateObject(Controller, validationContext, controllerResults)) + { + results.AddRange(controllerResults); + } + else if (!Validator.TryValidateObject(Agents, validationContext, results)) + { + results.AddRange(agentResults); + } + + return results; + } + + /// <summary> + /// Network controller configuration options. + /// </summary> + public class NetworkControllerConfigurationOptions + { + /// <summary> + /// Gets the controller address. + /// </summary> + [Argument(default, "controller-address")] + [EnvironmentVariable("CONTROLLER_ADDRESS")] + public string Address { get; init; } + + /// <summary> + /// Gets the controller secret. + /// </summary> + [Argument(default, "controller-secret")] + [EnvironmentVariable("CONTROLLER_SECRET")] + [StringLength(255, MinimumLength = 32)] + [Secret] + public string Secret { get; init; } + } + + /// <summary> + /// Network agent configuration options. + /// </summary> + public class NetworkAgentConfigurationOptions + { + /// <summary> + /// Gets the agent secret. + /// </summary> + [StringLength(255, MinimumLength = 32)] + [Secret] + public string Secret { get; init; } + } + } + + /// <summary> /// Directory options. /// </summary> public class DirectoriesOptions diff --git a/src/slskd/Core/Types/NetworkOperationMode.cs b/src/slskd/Core/Types/NetworkOperationMode.cs new file mode 100644 index 00000000..4b190275 --- /dev/null +++ b/src/slskd/Core/Types/NetworkOperationMode.cs @@ -0,0 +1,35 @@ +// <copyright file="NetworkOperationMode.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 +{ + /// <summary> + /// The network operation mode. + /// </summary> + public enum NetworkOperationMode + { + /// <summary> + /// Controller operation mode. + /// </summary> + Controller = 0, + + /// <summary> + /// Agent operation mode. + /// </summary> + Agent = 1, + } +} |
