public static bool TryParse(string str, out SparkConnectionString result, out string error)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }
            error  = null;
            result = null;
            var resultTemp = new SparkConnectionString();

            foreach (var kv in str.Split(';')
                     .Select(part => part.Split('='))
                     .Where(kv => kv.Length == 2))
            {
                switch (kv[0].ToLowerInvariant())
                {
                case "server":
                    if (resultTemp.Server != null)
                    {
                        error = "Duplicated server attribute";
                        return(false);
                    }
                    if (!Uri.IsWellFormedUriString(kv[1], UriKind.Absolute))
                    {
                        error = "Invalid URI";
                        return(false);
                    }
                    resultTemp.Server = new Uri(kv[1], UriKind.Absolute);
                    if (resultTemp.Server.Scheme == "http")
                    {
                        error = "Insecure transport protocol (http)";
                        return(false);
                    }
                    break;

                case "cookiefile":
                case "cookiefilepath":
                    if (resultTemp.CookeFile != null)
                    {
                        error = "Duplicated cookiefile attribute";
                        return(false);
                    }

                    resultTemp.CookeFile = kv[1];
                    break;

                default:
                    return(false);
                }
            }
            result = resultTemp;
            return(true);
        }
Esempio n. 2
0
        public static bool TryParse(string str, out SparkConnectionString result)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            result = null;
            var resultTemp = new SparkConnectionString();

            foreach (var kv in str.Split(';')
                     .Select(part => part.Split('='))
                     .Where(kv => kv.Length == 2))
            {
                switch (kv[0].ToLowerInvariant())
                {
                case "server":
                    if (resultTemp.Server != null)
                    {
                        return(false);
                    }
                    if (!Uri.IsWellFormedUriString(kv[1], UriKind.Absolute))
                    {
                        return(false);
                    }
                    resultTemp.Server = new Uri(kv[1], UriKind.Absolute);
                    break;

                case "cookiefile":
                case "cookiefilepath":
                    if (resultTemp.CookeFile != null)
                    {
                        return(false);
                    }
                    resultTemp.CookeFile = kv[1];
                    break;

                default:
                    return(false);
                }
            }
            result = resultTemp;
            return(true);
        }
        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))
                        {
                            Logs.Configuration.LogWarning($"Invalid setting {net.CryptoCode}.lightning, " + Environment.NewLine +
                                                          $"If you have a c-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: {error}" + Environment.NewLine +
                                                          "This service will not be exposed through BTCPay Server");
                        }
                        else
                        {
                            if (connectionString.IsLegacy)
                            {
                                Logs.Configuration.LogWarning($"Setting {net.CryptoCode}.lightning is a deprecated format, it will work now, but please replace it for future versions with '{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))
                        {
                            Logs.Configuration.LogWarning($"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: {error}" + Environment.NewLine +
                                                          "This service will not be exposed through BTCPay Server");
                        }
                        else
                        {
                            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");

                {
                    var spark = conf.GetOrDefault <string>($"{net.CryptoCode}.external.spark", string.Empty);
                    if (spark.Length != 0)
                    {
                        if (!SparkConnectionString.TryParse(spark, out var connectionString, out var error))
                        {
                            Logs.Configuration.LogWarning($"Invalid setting {net.CryptoCode}.external.spark, " + Environment.NewLine +
                                                          $"Valid example: 'server=https://btcpay.example.com/spark/btc/;cookiefile=/etc/clightning_bitcoin_spark/.cookie'" + Environment.NewLine +
                                                          $"Error: {error}" + Environment.NewLine +
                                                          "This service will not be exposed through BTCPay Server");
                        }
                        else
                        {
                            ExternalServicesByCryptoCode.Add(net.CryptoCode, new ExternalSpark(connectionString));
                        }
                    }
                }
        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 c-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 is a deprecated format, it will work now, but please replace it for future versions with '{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");

                var spark = conf.GetOrDefault <string>($"{net.CryptoCode}.external.spark", string.Empty);
                if (spark.Length != 0)
                {
                    if (!SparkConnectionString.TryParse(spark, out var connectionString))
                    {
                        throw new ConfigException($"Invalid setting {net.CryptoCode}.external.spark, " + Environment.NewLine +
                                                  $"Valid example: 'server=https://btcpay.example.com/spark/btc/;cookiefile=/etc/clightning_bitcoin_spark/.cookie'");
                    }
                    ExternalServicesByCryptoCode.Add(net.CryptoCode, new ExternalSpark(connectionString));
                }

                var charge = conf.GetOrDefault <string>($"{net.CryptoCode}.external.charge", string.Empty);
                if (charge.Length != 0)
                {
                    if (!LightningConnectionString.TryParse(charge, false, out var chargeConnectionString, out var chargeError))
                    {
                        LightningConnectionString.TryParse("type=charge;" + charge, false, out chargeConnectionString, out chargeError);
                    }

                    if (chargeConnectionString == null || chargeConnectionString.ConnectionType != LightningConnectionType.Charge)
                    {
                        throw new ConfigException($"Invalid setting {net.CryptoCode}.external.charge, " + Environment.NewLine +
                                                  $"lightning charge server: 'type=charge;server=https://charge.example.com;api-token=2abdf302...'" + Environment.NewLine +
                                                  $"lightning charge server: 'type=charge;server=https://charge.example.com;cookiefilepath=/root/.charge/.cookie'" + Environment.NewLine +
                                                  chargeError ?? string.Empty);
                    }
                    ExternalServicesByCryptoCode.Add(net.CryptoCode, new ExternalCharge(chargeConnectionString));
                }
            }

            Logs.Configuration.LogInformation("Supported chains: " + String.Join(',', supportedChains.ToArray()));

            var services = conf.GetOrDefault <string>("externalservices", null);

            if (services != null)
            {
                foreach (var service in services.Split(new[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries)
                         .Select(p => (p, SeparatorIndex: p.IndexOf(':', StringComparison.OrdinalIgnoreCase)))
                         .Where(p => p.SeparatorIndex != -1)
                         .Select(p => (Name: p.p.Substring(0, p.SeparatorIndex),
                                       Link: p.p.Substring(p.SeparatorIndex + 1))))
                {
                    ExternalServices.AddOrReplace(service.Name, service.Link);
                }
            }

            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 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);
        }