//
// 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
{
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using slskd.Validation;
using Soulseek.Diagnostics;
using Utility.CommandLine;
using Utility.EnvironmentVariables;
///
/// Application options.
///
public class Options
{
///
/// Gets a value indicating whether to display the application version.
///
[Argument('v', "version")]
[Description("display version information")]
public bool ShowVersion { get; private set; } = false;
///
/// Gets a value indicating whether to display a list of command line arguments.
///
[Argument('h', "help")]
[Description("display command line usage")]
public bool ShowHelp { get; private set; } = false;
///
/// Gets a value indicating whether to display a list of configuration environment variables.
///
[Argument('e', "envars")]
[Description("display environment variables")]
public bool ShowEnvironmentVariables { get; private set; } = false;
///
/// Gets a value indicating whether to generate an X509 certificate and password.
///
[Argument('g', "generate-cert")]
[Description("generate X509 certificate and password for HTTPs")]
public bool GenerateCertificate { get; private set; } = false;
///
/// Gets the path to the application configuration file.
///
[Argument('c', "config")]
[EnvironmentVariable("CONFIG")]
[Description("path to configuration file")]
public string ConfigurationFile { get; private set; } = Program.DefaultConfigurationFile;
///
/// Gets a value indicating whether the application should run in debug mode.
///
[Argument('d', "debug")]
[EnvironmentVariable("DEBUG")]
[Description("run in debug mode")]
public bool Debug { get; private set; } = Debugger.IsAttached;
///
/// Gets a value indicating whether the logo should be suppressed on startup.
///
[Argument('n', "no-logo")]
[EnvironmentVariable("NO_LOGO")]
[Description("suppress logo on startup")]
public bool NoLogo { get; private set; } = false;
///
/// Gets the unique name for this instance.
///
[Argument('i', "instance-name")]
[EnvironmentVariable("INSTANCE_NAME")]
[Description("optional; a unique name for this instance")]
public string InstanceName { get; private set; } = "default";
///
/// Gets directory options.
///
[Validate]
public DirectoriesOptions Directories { get; private set; } = new DirectoriesOptions();
///
/// Gets options for the web UI.
///
[Validate]
public WebOptions Web { get; private set; } = new WebOptions();
///
/// Gets logger options.
///
[Validate]
public LoggerOptions Logger { get; private set; } = new LoggerOptions();
///
/// Gets feature options.
///
[Validate]
public FeatureOptions Feature { get; private set; } = new FeatureOptions();
///
/// Gets options for the Soulseek client.
///
[Validate]
public SoulseekOptions Soulseek { get; private set; } = new SoulseekOptions();
///
/// Directory options.
///
public class DirectoriesOptions
{
///
/// Gets the path where application data is saved.
///
[Argument(default, "app")]
[EnvironmentVariable("APP_DIR")]
[Description("path where application data is saved")]
[DirectoryExists]
public string App { get; private set; } = Program.DefaultAppDirectory;
///
/// Gets the path where incomplete downloads are saved.
///
[Argument(default, "incomplete")]
[EnvironmentVariable("INCOMPLETE_DIR")]
[Description("path where incomplete downloads are saved")]
[DirectoryExists]
public string Incomplete { get; private set; } = Program.DefaultIncompleteDirectory;
///
/// Gets the path where downloaded files are saved.
///
[Argument('o', "downloads")]
[EnvironmentVariable("DOWNLOADS_DIR")]
[Description("path where downloaded files are saved")]
[DirectoryExists]
public string Downloads { get; private set; } = Program.DefaultDownloadsDirectory;
///
/// Gets the path to shared files.
///
[Argument('s', "shared")]
[EnvironmentVariable("SHARED_DIR")]
[Description("path to shared files")]
[DirectoryExists]
public string Shared { get; private set; } = Program.DefaultSharedDirectory;
}
///
/// Feature options.
///
public class FeatureOptions
{
///
/// Gets a value indicating whether prometheus metrics should be collected and published.
///
[Argument(default, "prometheus")]
[EnvironmentVariable("PROMETHEUS")]
[Description("enable collection and publishing of prometheus metrics")]
public bool Prometheus { get; private set; } = false;
///
/// Gets a value indicating whether swagger documentation and UI should be enabled.
///
[Argument(default, "swagger")]
[EnvironmentVariable("SWAGGER")]
[Description("enable swagger documentation and UI")]
public bool Swagger { get; private set; } = false;
}
///
/// Logger options.
///
public class LoggerOptions
{
///
/// Gets the URL to a Grafana Loki instance to which to log.
///
[Argument(default, "loki")]
[EnvironmentVariable("LOKI")]
[Description("optional; url to a Grafana Loki instance to which to log")]
public string Loki { get; private set; } = null;
}
///
/// Soulseek client options.
///
public class SoulseekOptions
{
///
/// Gets the password for the Soulseek network.
///
[Argument(default, "slsk-password")]
[EnvironmentVariable("SLSK_PASSWORD")]
[Description("password for the Soulseek network")]
[Required]
public string Password { get; private set; } = null;
///
/// Gets the username for the Soulseek network.
///
[Argument(default, "slsk-username")]
[EnvironmentVariable("SLSK_USERNAME")]
[Description("username for the Soulseek network")]
[Required]
public string Username { get; private set; } = null;
///
/// Gets the port on which to listen for incoming connections.
///
[Argument(default, "slsk-listen-port")]
[EnvironmentVariable("SLSK_LISTEN_PORT")]
[Description("port on which to listen for incoming connections")]
[Range(1024, 65535)]
public int ListenPort { get; private set; } = 50000;
///
/// Gets the minimum diagnostic level.
///
[Argument(default, "slsk-diag-level")]
[EnvironmentVariable("SLSK_DIAG_LEVEL")]
[Description("minimum diagnostic level (None, Warning, Info, Debug)")]
public DiagnosticLevel DiagnosticLevel { get; private set; } = DiagnosticLevel.Info;
///
/// Gets options for the distributed network.
///
[Validate]
public DistributedNetworkOptions DistributedNetwork { get; private set; } = new DistributedNetworkOptions();
///
/// Gets connection options.
///
[Validate]
public ConnectionOptions Connection { get; private set; } = new ConnectionOptions();
///
/// Connection options.
///
public class ConnectionOptions
{
///
/// Gets connection timeout options.
///
[Validate]
public TimeoutOptions Timeout { get; private set; } = new TimeoutOptions();
///
/// Gets connection buffer options.
///
[Validate]
public BufferOptions Buffer { get; private set; } = new BufferOptions();
///
/// Gets connection proxy options.
///
[Validate]
public ProxyOptions Proxy { get; private set; } = new ProxyOptions();
///
/// Connection buffer options.
///
public class BufferOptions
{
///
/// Gets the connection read buffer size.
///
[Argument(default, "slsk-read-buffer")]
[EnvironmentVariable("SLSK_READ_BUFFER")]
[Description("read buffer size for connections")]
[Range(1024, int.MaxValue)]
public int Read { get; private set; } = 16384;
///
/// Gets the connection write buffer size.
///
[Argument(default, "slsk-write-buffer")]
[EnvironmentVariable("SLSK_WRITE_BUFFER")]
[Description("write buffer size for connections")]
[Range(1024, int.MaxValue)]
public int Write { get; private set; } = 16384;
}
///
/// Connection timeout options.
///
public class TimeoutOptions
{
///
/// Gets the connection timeout value, in milliseconds.
///
[Argument(default, "slsk-connection-timeout")]
[EnvironmentVariable("SLSK_CONNECTION_TIMEOUT")]
[Description("connection timeout, in milliseconds")]
[Range(1000, int.MaxValue)]
public int Connect { get; private set; } = 10000;
///
/// Gets the connection inactivity timeout, in milliseconds.
///
[Argument(default, "slsk-inactivity-timeout")]
[EnvironmentVariable("SLSK_INACTIVITY_TIMEOUT")]
[Description("connection inactivity timeout, in milliseconds")]
[Range(1000, int.MaxValue)]
public int Inactivity { get; private set; } = 15000;
}
///
/// Connection proxy options.
///
public class ProxyOptions
{
///
/// Gets the proxy address.
///
[Argument(default, "slsk-proxy-address")]
[EnvironmentVariable("SLSK_PROXY_ADDRESS")]
[Description("connection proxy address")]
[StringLength(255, MinimumLength = 1)]
public string Address { get; private set; }
///
/// Gets the proxy port.
///
[Argument(default, "slsk-proxy-port")]
[EnvironmentVariable("SLSK_PROXY_PORT")]
[Description("connection proxy port")]
[Range(1, 65535)]
public int? Port { get; private set; }
///
/// Gets the proxy username, if applicable.
///
[Argument(default, "slsk-proxy-username")]
[EnvironmentVariable("SLSK_PROXY_USERNAME")]
[Description("connection proxy username")]
[StringLength(255, MinimumLength = 1)]
public string Username { get; private set; }
///
/// Gets the proxy password, if applicable.
///
[Argument(default, "slsk-proxy-password")]
[EnvironmentVariable("SLSK_PROXY_PASSWORD")]
[Description("connection proxy password")]
[StringLength(255, MinimumLength = 1)]
public string Password { get; private set; }
}
}
///
/// Distributed network options.
///
public class DistributedNetworkOptions
{
///
/// Gets a value indicating whether the distributed network should be disabled.
///
[Argument(default, "slsk-no-dnet")]
[EnvironmentVariable("SLSK_NO_DNET")]
[Description("disable the distributed network")]
public bool Disabled { get; private set; } = false;
///
/// Gets the distributed child connection limit.
///
[Argument(default, "slsk-dnet-children")]
[EnvironmentVariable("SLSK_DNET_CHILDREN")]
[Description("max number of distributed children")]
[Range(1, int.MaxValue)]
public int ChildLimit { get; private set; } = 25;
}
}
///
/// Web server options.
///
public class WebOptions
{
///
/// Gets the HTTP listen port.
///
[Argument('l', "http-port")]
[EnvironmentVariable("HTTP_PORT")]
[Description("HTTP listen port for web UI")]
[Range(1, 65535)]
public int Port { get; private set; } = 5000;
///
/// Gets HTTPS options.
///
[Validate]
public HttpsOptions Https { get; private set; } = new HttpsOptions();
///
/// Gets the base url for web requests.
///
[Argument(default, "url-base")]
[EnvironmentVariable("URL_BASE")]
[Description("base url for web requests")]
public string UrlBase { get; private set; } = "/";
///
/// Gets the path to static web content.
///
[Argument(default, "content-path")]
[EnvironmentVariable("CONTENT_PATH")]
[Description("path to static web content")]
[StringLength(255, MinimumLength = 1)]
public string ContentPath { get; private set; } = "wwwroot";
///
/// Gets authentication options.
///
[Validate]
public AuthenticationOptions Authentication { get; private set; } = new AuthenticationOptions();
///
/// Authentication options.
///
public class AuthenticationOptions
{
///
/// Gets a value indicating whether authentication should be disabled.
///
[Argument('x', "no-auth")]
[EnvironmentVariable("NO_AUTH")]
[Description("disable authentication for web requests")]
public bool Disable { get; private set; } = false;
///
/// Gets the username for the web UI.
///
[Argument('u', "username")]
[EnvironmentVariable("USERNAME")]
[Description("username for web UI")]
[StringLength(255, MinimumLength = 1)]
public string Username { get; private set; } = Program.AppName;
///
/// Gets the password for the web UI.
///
[Argument('p', "password")]
[EnvironmentVariable("PASSWORD")]
[Description("password for web UI")]
[StringLength(255, MinimumLength = 1)]
public string Password { get; private set; } = Program.AppName;
///
/// Gets JWT options.
///
[Validate]
public JwtOptions Jwt { get; private set; } = new JwtOptions();
///
/// JWT options.
///
public class JwtOptions
{
///
/// Gets the key with which to sign JWTs.
///
[Argument(default, "jwt-key")]
[EnvironmentVariable("JWT_KEY")]
[Description("JWT signing key")]
[StringLength(255, MinimumLength = 1)]
public string Key { get; private set; } = Guid.NewGuid().ToString();
///
/// Gets the TTL for JWTs.
///
[Argument(default, "jwt-ttl")]
[EnvironmentVariable("JWT_TTL")]
[Description("TTL for JWTs")]
[Range(3600, int.MaxValue)]
public int Ttl { get; private set; } = 604800000;
}
}
///
/// HTTPS options.
///
public class HttpsOptions
{
///
/// Gets the HTTPS listen port.
///
[Argument('L', "https-port")]
[EnvironmentVariable("HTTPS_PORT")]
[Description("HTTPS listen port for web UI")]
[Range(1, 65535)]
public int Port { get; private set; } = 5001;
///
/// Gets a value indicating whether HTTP requests should be redirected to HTTPS.
///
[Argument('f', "force-https")]
[EnvironmentVariable("HTTPS_FORCE")]
[Description("redirect HTTP to HTTPS")]
public bool Force { get; private set; } = false;
///
/// Gets certificate options.
///
[Validate]
public CertificateOptions Certificate { get; private set; } = new CertificateOptions();
///
/// Certificate options.
///
[X509Certificate]
public class CertificateOptions
{
///
/// Gets the path to the the X509 certificate .pfx file.
///
[Argument(default, "https-cert-pfx")]
[EnvironmentVariable("HTTPS_CERT_PFX")]
[Description("path to X509 certificate .pfx")]
[FileExists]
public string Pfx { get; private set; }
///
/// Gets the password for the X509 certificate.
///
[Argument(default, "https-cert-password")]
[EnvironmentVariable("HTTPS_CERT_PASSWORD")]
[Description("X509 certificate password")]
public string Password { get; private set; }
}
}
}
}
}