/// <summary> /// Builds up a relative path based on specified segment parts. /// </summary> /// <param name="path">Path segments</param> /// <returns>Returns a relative path.</returns> public string GetPath(string[] path) { string[] childPath = path ?? new string[] { }; // create result path string resultPath = PathUtil.Combine(childPath); resultPath = resultPath.Trim('/', '\\'); // prepend root path with corrected separators // remove all leading and trailing separator chars string cleansedRoot = RootPath.Trim('/', '\\'); // replace all slashes with directory separator cleansedRoot = cleansedRoot.Replace('/', PathUtil.DirectorySeparatorChar); // replace all backslashes with directory separator cleansedRoot = cleansedRoot.Replace('\\', PathUtil.DirectorySeparatorChar); if (!resultPath.StartsWith(cleansedRoot)) { resultPath = Path.Join(cleansedRoot, resultPath); } // if root path is relative, add relative separator if (RootPath.StartsWith(PathUtil.DirectorySeparatorChar) || RootPath.StartsWith(PathUtil.AltDirectorySeparatorChar)) { resultPath = PathUtil.DirectorySeparatorChar + resultPath; } return(resultPath); }
private string BuildResourcePath(string resourcePath) { string fullPath; string assemblyPath = SourceAssembly.GetName().Name; if (!string.IsNullOrWhiteSpace(RootPath)) { if (!RootPath.StartsWith(assemblyPath, StringComparison.Ordinal)) { fullPath = $"{assemblyPath}.{RootPath}.{resourcePath}"; } else { fullPath = $"{RootPath}.{resourcePath}"; } } else { if (resourcePath.StartsWith(assemblyPath, StringComparison.Ordinal)) { fullPath = resourcePath; } else { fullPath = $"{assemblyPath}.{resourcePath}"; } } return(fullPath); }
public virtual void Initialize(Action <DavServerOptions> setupAction, IDictionary <string, string> properties) { if (properties == null) { throw new ArgumentNullException(nameof(properties)); } var path = properties.GetNullifiedValue(nameof(RootPath)); if (string.IsNullOrWhiteSpace(path)) { throw new DavServerException("0002: Configuration is missing parameter '" + nameof(RootPath) + "'."); } if (!Path.IsPathRooted(path)) { throw new DavServerException("0004: Parameter '" + nameof(RootPath) + "' must be rooted."); } // make sure we use long file names RootPath = path; if (!RootPath.StartsWith(@"\\?\")) { RootPath = LongFileNamePrefix + RootPath; } if (!Extensions.DirectoryExists(RootPath)) { throw new DavServerException("0006: Directory root path '" + path + "' cannot be found, please check the appsettings.json file."); } setupAction?.Invoke(Options); }
public void LoadArgs(IConfiguration conf) { NetworkType = DefaultConfiguration.GetNetworkType(conf); var defaultSettings = BTCPayDefaultSettings.GetDefaultSettings(NetworkType); DataDir = conf.GetOrDefault <string>("datadir", defaultSettings.DefaultDataDirectory); Logs.Configuration.LogInformation("Network: " + NetworkType.ToString()); var supportedChains = conf.GetOrDefault <string>("chains", "btc") .Split(',', StringSplitOptions.RemoveEmptyEntries) .Select(t => t.ToUpperInvariant()); NetworkProvider = new BTCPayNetworkProvider(NetworkType).Filter(supportedChains.ToArray()); foreach (var chain in supportedChains) { if (NetworkProvider.GetNetwork(chain) == null) { throw new ConfigException($"Invalid chains \"{chain}\""); } } var validChains = new List <string>(); foreach (var net in NetworkProvider.GetAll()) { NBXplorerConnectionSetting setting = new NBXplorerConnectionSetting(); setting.CryptoCode = net.CryptoCode; setting.ExplorerUri = conf.GetOrDefault <Uri>($"{net.CryptoCode}.explorer.url", net.NBXplorerNetwork.DefaultSettings.DefaultUrl); setting.CookieFile = conf.GetOrDefault <string>($"{net.CryptoCode}.explorer.cookiefile", net.NBXplorerNetwork.DefaultSettings.DefaultCookieFile); NBXplorerConnectionSettings.Add(setting); var lightning = conf.GetOrDefault <string>($"{net.CryptoCode}.lightning", string.Empty); if (lightning.Length != 0) { if (!LightningConnectionString.TryParse(lightning, true, out var connectionString, out var error)) { throw new ConfigException($"Invalid setting {net.CryptoCode}.lightning, " + Environment.NewLine + $"If you have a lightning server use: 'type=clightning;server=/root/.lightning/lightning-rpc', " + Environment.NewLine + $"If you have a lightning charge server: 'type=charge;server=https://charge.example.com;api-token=yourapitoken'" + Environment.NewLine + $"If you have a lnd server: 'type=lnd-rest;server=https://lnd:[email protected];macaron=abf239...;certthumbprint=2abdf302...'"); } if (connectionString.IsLegacy) { Logs.Configuration.LogWarning($"Setting {net.CryptoCode}.lightning will work but use an deprecated format, please replace it by '{connectionString.ToString()}'"); } InternalLightningByCryptoCode.Add(net.CryptoCode, connectionString); } } Logs.Configuration.LogInformation("Supported chains: " + String.Join(',', supportedChains.ToArray())); PostgresConnectionString = conf.GetOrDefault <string>("postgres", null); BundleJsCss = conf.GetOrDefault <bool>("bundlejscss", true); ExternalUrl = conf.GetOrDefault <Uri>("externalurl", null); RootPath = conf.GetOrDefault <string>("rootpath", "/"); if (!RootPath.StartsWith("/", StringComparison.InvariantCultureIgnoreCase)) { RootPath = "/" + RootPath; } var old = conf.GetOrDefault <Uri>("internallightningnode", null); if (old != null) { throw new ConfigException($"internallightningnode should not be used anymore, use btclightning instead"); } }
public void LoadArgs(IConfiguration conf) { NetworkType = DefaultConfiguration.GetNetworkType(conf); var defaultSettings = BTCPayDefaultSettings.GetDefaultSettings(NetworkType); DataDir = conf.GetOrDefault <string>("datadir", defaultSettings.DefaultDataDirectory); Logs.Configuration.LogInformation("Network: " + NetworkType.ToString()); var supportedChains = conf.GetOrDefault <string>("chains", "btc") .Split(',', StringSplitOptions.RemoveEmptyEntries) .Select(t => t.ToUpperInvariant()); NetworkProvider = new BTCPayNetworkProvider(NetworkType).Filter(supportedChains.ToArray()); foreach (var chain in supportedChains) { if (NetworkProvider.GetNetwork(chain) == null) { throw new ConfigException($"Invalid chains \"{chain}\""); } } var validChains = new List <string>(); foreach (var net in NetworkProvider.GetAll()) { NBXplorerConnectionSetting setting = new NBXplorerConnectionSetting(); setting.CryptoCode = net.CryptoCode; setting.ExplorerUri = conf.GetOrDefault <Uri>($"{net.CryptoCode}.explorer.url", net.NBXplorerNetwork.DefaultSettings.DefaultUrl); setting.CookieFile = conf.GetOrDefault <string>($"{net.CryptoCode}.explorer.cookiefile", net.NBXplorerNetwork.DefaultSettings.DefaultCookieFile); NBXplorerConnectionSettings.Add(setting); { var lightning = conf.GetOrDefault <string>($"{net.CryptoCode}.lightning", string.Empty); if (lightning.Length != 0) { if (!LightningConnectionString.TryParse(lightning, true, out var connectionString, out var error)) { throw new ConfigException($"Invalid setting {net.CryptoCode}.lightning, " + Environment.NewLine + $"If you have a lightning server use: 'type=clightning;server=/root/.lightning/lightning-rpc', " + Environment.NewLine + $"If you have a lightning charge server: 'type=charge;server=https://charge.example.com;api-token=yourapitoken'" + Environment.NewLine + $"If you have a lnd server: 'type=lnd-rest;server=https://lnd:[email protected];macaroon=abf239...;certthumbprint=2abdf302...'" + Environment.NewLine + $" lnd server: 'type=lnd-rest;server=https://lnd:[email protected];macaroonfilepath=/root/.lnd/admin.macaroon;certthumbprint=2abdf302...'" + Environment.NewLine + error); } if (connectionString.IsLegacy) { Logs.Configuration.LogWarning($"Setting {net.CryptoCode}.lightning will work but use an deprecated format, please replace it by '{connectionString.ToString()}'"); } InternalLightningByCryptoCode.Add(net.CryptoCode, connectionString); } } void externalLnd <T>(string code, string lndType) { var lightning = conf.GetOrDefault <string>(code, string.Empty); if (lightning.Length != 0) { if (!LightningConnectionString.TryParse(lightning, false, out var connectionString, out var error)) { throw new ConfigException($"Invalid setting {code}, " + Environment.NewLine + $"lnd server: 'type={lndType};server=https://lnd.example.com;macaroon=abf239...;certthumbprint=2abdf302...'" + Environment.NewLine + $"lnd server: 'type={lndType};server=https://lnd.example.com;macaroonfilepath=/root/.lnd/admin.macaroon;certthumbprint=2abdf302...'" + Environment.NewLine + error); } var instanceType = typeof(T); ExternalServicesByCryptoCode.Add(net.CryptoCode, (ExternalService)Activator.CreateInstance(instanceType, connectionString)); } }; externalLnd <ExternalLndGrpc>($"{net.CryptoCode}.external.lnd.grpc", "lnd-grpc"); externalLnd <ExternalLndRest>($"{net.CryptoCode}.external.lnd.rest", "lnd-rest"); } Logs.Configuration.LogInformation("Supported chains: " + String.Join(',', supportedChains.ToArray())); PostgresConnectionString = conf.GetOrDefault <string>("postgres", null); MySQLConnectionString = conf.GetOrDefault <string>("mysql", null); BundleJsCss = conf.GetOrDefault <bool>("bundlejscss", true); ExternalUrl = conf.GetOrDefault <Uri>("externalurl", null); var sshSettings = ParseSSHConfiguration(conf); if ((!string.IsNullOrEmpty(sshSettings.Password) || !string.IsNullOrEmpty(sshSettings.KeyFile)) && !string.IsNullOrEmpty(sshSettings.Server)) { int waitTime = 0; while (!string.IsNullOrEmpty(sshSettings.KeyFile) && !File.Exists(sshSettings.KeyFile)) { if (waitTime++ < 5) { System.Threading.Thread.Sleep(1000); } else { throw new ConfigException($"sshkeyfile does not exist"); } } if (sshSettings.Port > ushort.MaxValue || sshSettings.Port < ushort.MinValue) { throw new ConfigException($"ssh port is invalid"); } if (!string.IsNullOrEmpty(sshSettings.Password) && !string.IsNullOrEmpty(sshSettings.KeyFile)) { throw new ConfigException($"sshpassword or sshkeyfile should be provided, but not both"); } try { sshSettings.CreateConnectionInfo(); } catch { throw new ConfigException($"sshkeyfilepassword is invalid"); } SSHSettings = sshSettings; } var fingerPrints = conf.GetOrDefault <string>("sshtrustedfingerprints", ""); if (!string.IsNullOrEmpty(fingerPrints)) { foreach (var fingerprint in fingerPrints.Split(';', StringSplitOptions.RemoveEmptyEntries)) { if (!SSHFingerprint.TryParse(fingerprint, out var f)) { throw new ConfigException($"Invalid ssh fingerprint format {fingerprint}"); } TrustedFingerprints.Add(f); } } RootPath = conf.GetOrDefault <string>("rootpath", "/"); if (!RootPath.StartsWith("/", StringComparison.InvariantCultureIgnoreCase)) { RootPath = "/" + RootPath; } var old = conf.GetOrDefault <Uri>("internallightningnode", null); if (old != null) { throw new ConfigException($"internallightningnode should not be used anymore, use btclightning instead"); } LogFile = GetDebugLog(conf); if (!string.IsNullOrEmpty(LogFile)) { Logs.Configuration.LogInformation("LogFile: " + LogFile); Logs.Configuration.LogInformation("Log Level: " + GetDebugLogLevel(conf)); } }
public void LoadArgs(IConfiguration conf) { NetworkType = DefaultConfiguration.GetNetworkType(conf); Logs.Configuration.LogInformation("Network: " + NetworkType.ToString()); if (conf.GetOrDefault <bool>("launchsettings", false) && NetworkType != NetworkType.Regtest) { throw new ConfigException($"You need to run BTCPayServer with the run.sh or run.ps1 script"); } BundleJsCss = conf.GetOrDefault <bool>("bundlejscss", true); DockerDeployment = conf.GetOrDefault <bool>("dockerdeployment", true); AllowAdminRegistration = conf.GetOrDefault <bool>("allow-admin-registration", false); TorrcFile = conf.GetOrDefault <string>("torrcfile", null); var socksEndpointString = conf.GetOrDefault <string>("socksendpoint", null); if (!string.IsNullOrEmpty(socksEndpointString)) { if (!Utils.TryParseEndpoint(socksEndpointString, 9050, out var endpoint)) { throw new ConfigException("Invalid value for socksendpoint"); } SocksEndpoint = endpoint; } UpdateUrl = conf.GetOrDefault <Uri>("updateurl", null); var sshSettings = ParseSSHConfiguration(conf); if ((!string.IsNullOrEmpty(sshSettings.Password) || !string.IsNullOrEmpty(sshSettings.KeyFile)) && !string.IsNullOrEmpty(sshSettings.Server)) { int waitTime = 0; while (!string.IsNullOrEmpty(sshSettings.KeyFile) && !File.Exists(sshSettings.KeyFile)) { if (waitTime++ < 5) { System.Threading.Thread.Sleep(1000); } else { throw new ConfigException($"sshkeyfile does not exist"); } } if (sshSettings.Port > ushort.MaxValue || sshSettings.Port < ushort.MinValue) { throw new ConfigException($"ssh port is invalid"); } if (!string.IsNullOrEmpty(sshSettings.Password) && !string.IsNullOrEmpty(sshSettings.KeyFile)) { throw new ConfigException($"sshpassword or sshkeyfile should be provided, but not both"); } try { sshSettings.CreateConnectionInfo(); SSHSettings = sshSettings; } catch (NotSupportedException ex) { Logs.Configuration.LogWarning($"The SSH key is not supported ({ex.Message}), try to generate the key with ssh-keygen using \"-m PEM\". Skipping SSH configuration..."); } catch { throw new ConfigException($"sshkeyfilepassword is invalid"); } } var fingerPrints = conf.GetOrDefault <string>("sshtrustedfingerprints", ""); if (!string.IsNullOrEmpty(fingerPrints)) { foreach (var fingerprint in fingerPrints.Split(';', StringSplitOptions.RemoveEmptyEntries)) { if (!SSHFingerprint.TryParse(fingerprint, out var f)) { throw new ConfigException($"Invalid ssh fingerprint format {fingerprint}"); } SSHSettings?.TrustedFingerprints.Add(f); } } RootPath = conf.GetOrDefault <string>("rootpath", "/"); if (!RootPath.StartsWith("/", StringComparison.InvariantCultureIgnoreCase)) { RootPath = "/" + RootPath; } var old = conf.GetOrDefault <Uri>("internallightningnode", null); if (old != null) { throw new ConfigException($"internallightningnode is deprecated and should not be used anymore, use btclightning instead"); } LogFile = GetDebugLog(conf); if (!string.IsNullOrEmpty(LogFile)) { Logs.Configuration.LogInformation("LogFile: " + LogFile); Logs.Configuration.LogInformation("Log Level: " + GetDebugLogLevel(conf)); } DisableRegistration = conf.GetOrDefault <bool>("disable-registration", true); PluginRemote = conf.GetOrDefault("plugin-remote", "btcpayserver/btcpayserver-plugins"); RecommendedPlugins = conf.GetOrDefault("recommended-plugins", "").ToLowerInvariant().Split('\r', '\n', '\t', ' ').Where(s => !string.IsNullOrEmpty(s)).Distinct().ToArray(); }
public void LoadArgs(IConfiguration conf) { NetworkType = DefaultConfiguration.GetNetworkType(conf); var defaultSettings = BTCPayDefaultSettings.GetDefaultSettings(NetworkType); DataDir = conf.GetOrDefault <string>("datadir", defaultSettings.DefaultDataDirectory); Logs.Configuration.LogInformation("Network: " + NetworkType.ToString()); var supportedChains = conf.GetOrDefault <string>("chains", "btc") .Split(',', StringSplitOptions.RemoveEmptyEntries) .Select(t => t.ToUpperInvariant()); NetworkProvider = new BTCPayNetworkProvider(NetworkType).Filter(supportedChains.ToArray()); foreach (var chain in supportedChains) { if (NetworkProvider.GetNetwork(chain) == null) { throw new ConfigException($"Invalid chains \"{chain}\""); } } var validChains = new List <string>(); foreach (var net in NetworkProvider.GetAll()) { NBXplorerConnectionSetting setting = new NBXplorerConnectionSetting(); setting.CryptoCode = net.CryptoCode; setting.ExplorerUri = conf.GetOrDefault <Uri>($"{net.CryptoCode}.explorer.url", net.NBXplorerNetwork.DefaultSettings.DefaultUrl); setting.CookieFile = conf.GetOrDefault <string>($"{net.CryptoCode}.explorer.cookiefile", net.NBXplorerNetwork.DefaultSettings.DefaultCookieFile); NBXplorerConnectionSettings.Add(setting); var lightning = conf.GetOrDefault <string>($"{net.CryptoCode}.lightning", string.Empty); if (lightning.Length != 0) { if (!LightningConnectionString.TryParse(lightning, out var connectionString, out var error)) { throw new ConfigException($"Invalid setting {net.CryptoCode}.lightning, you need to pass either " + $"the absolute path to the unix socket of a running CLightning instance (eg. /root/.lightning/lightning-rpc), " + $"or the url to a charge server with crendetials (eg. https://apitoken@API_TOKEN_SECRET:charge.example.com/)"); } InternalLightningByCryptoCode.Add(net.CryptoCode, connectionString); } } Logs.Configuration.LogInformation("Supported chains: " + String.Join(',', supportedChains.ToArray())); PostgresConnectionString = conf.GetOrDefault <string>("postgres", null); BundleJsCss = conf.GetOrDefault <bool>("bundlejscss", true); ExternalUrl = conf.GetOrDefault <Uri>("externalurl", null); RootPath = conf.GetOrDefault <string>("rootpath", "/"); if (!RootPath.StartsWith("/", StringComparison.InvariantCultureIgnoreCase)) { RootPath = "/" + RootPath; } var old = conf.GetOrDefault <Uri>("internallightningnode", null); if (old != null) { throw new ConfigException($"internallightningnode should not be used anymore, use btclightning instead"); } }