From 6155ff6cabf276ea084e5641b8a8ea1abe07b81c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 4 Oct 2022 19:36:10 -0500 Subject: add network options --- src/slskd/Core/Options.cs | 85 ++++++++++++++++++++++++++++ src/slskd/Core/Types/NetworkOperationMode.cs | 35 ++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/slskd/Core/Types/NetworkOperationMode.cs (limited to 'src') 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 @@ -200,6 +200,12 @@ namespace slskd [YamlIgnore] public string ConfigurationFile { get; init; } = Program.DefaultConfigurationFile; + /// + /// Gets network options. + /// + [Validate] + public NetworkOptions Network { get; init; } = new NetworkOptions(); + /// /// Gets directory options. /// @@ -347,6 +353,85 @@ namespace slskd public bool LogSQL { get; init; } = false; } + /// + /// Network options. + /// + public class NetworkOptions : IValidatableObject + { + /// + /// Gets the application operation mode. + /// + [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(); + + /// + /// Gets the controller configuration. + /// + public NetworkControllerConfigurationOptions Controller { get; init; } = new NetworkControllerConfigurationOptions(); + + /// + /// Gets the agent configuration. + /// + public Dictionary Agents { get; init; } = new(); + + public IEnumerable Validate(ValidationContext validationContext) + { + var results = new List(); + var controllerResults = new List(); + var agentResults = new List(); + + if (OperationMode.ToEnum() == NetworkOperationMode.Agent && !Validator.TryValidateObject(Controller, validationContext, controllerResults)) + { + results.AddRange(controllerResults); + } + else if (!Validator.TryValidateObject(Agents, validationContext, results)) + { + results.AddRange(agentResults); + } + + return results; + } + + /// + /// Network controller configuration options. + /// + public class NetworkControllerConfigurationOptions + { + /// + /// Gets the controller address. + /// + [Argument(default, "controller-address")] + [EnvironmentVariable("CONTROLLER_ADDRESS")] + public string Address { get; init; } + + /// + /// Gets the controller secret. + /// + [Argument(default, "controller-secret")] + [EnvironmentVariable("CONTROLLER_SECRET")] + [StringLength(255, MinimumLength = 32)] + [Secret] + public string Secret { get; init; } + } + + /// + /// Network agent configuration options. + /// + public class NetworkAgentConfigurationOptions + { + /// + /// Gets the agent secret. + /// + [StringLength(255, MinimumLength = 32)] + [Secret] + public string Secret { get; init; } + } + } + /// /// Directory options. /// 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 (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/. +// + +namespace slskd +{ + /// + /// The network operation mode. + /// + public enum NetworkOperationMode + { + /// + /// Controller operation mode. + /// + Controller = 0, + + /// + /// Agent operation mode. + /// + Agent = 1, + } +} -- cgit v1.2.3