2017-09-22 18:31:29 +02:00
using Microsoft.AspNetCore.Hosting ;
using Microsoft.Extensions.Configuration ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using Microsoft.Extensions.Logging ;
using System.Net ;
using System.Threading.Tasks ;
using NBitcoin ;
using System.Text ;
using CommandLine ;
2018-01-12 03:54:57 +01:00
using NBXplorer ;
2017-09-22 18:31:29 +02:00
namespace BTCPayServer.Configuration
{
2017-10-27 10:53:04 +02:00
public class DefaultConfiguration : StandardConfiguration . DefaultConfiguration
{
protected override CommandLineApplication CreateCommandLineApplicationCore ( )
{
2018-04-19 09:54:25 +02:00
var provider = new BTCPayNetworkProvider ( NetworkType . Mainnet ) ;
2018-01-12 14:08:20 +01:00
var chains = string . Join ( "," , provider . GetAll ( ) . Select ( n = > n . CryptoCode . ToLowerInvariant ( ) ) . ToArray ( ) ) ;
2017-10-27 10:53:04 +02:00
CommandLineApplication app = new CommandLineApplication ( true )
{
2017-12-02 15:22:23 +01:00
FullName = "BTCPay\r\nOpen source, self-hosted payment processor." ,
Name = "BTCPay"
2017-10-27 10:53:04 +02:00
} ;
app . HelpOption ( "-? | -h | --help" ) ;
2018-01-11 14:52:28 +01:00
app . Option ( "-n | --network" , $"Set the network among (mainnet,testnet,regtest) (default: mainnet)" , CommandOptionType . SingleValue ) ;
2018-07-13 19:00:03 +02:00
app . Option ( "--testnet | -testnet" , $"Use testnet (deprecated, use --network instead)" , CommandOptionType . BoolValue ) ;
app . Option ( "--regtest | -regtest" , $"Use regtest (deprecated, use --network instead)" , CommandOptionType . BoolValue ) ;
app . Option ( "--chains | -c" , $"Chains to support as a comma separated (default: btc; available: {chains})" , CommandOptionType . SingleValue ) ;
app . Option ( "--postgres" , $"Connection string to a PostgreSQL database (default: SQLite)" , CommandOptionType . SingleValue ) ;
2018-10-27 16:15:21 +02:00
app . Option ( "--mysql" , $"Connection string to a MySQL database (default: SQLite)" , CommandOptionType . SingleValue ) ;
2018-07-13 19:00:03 +02:00
app . Option ( "--externalurl" , $"The expected external URL of this service, to use if BTCPay is behind a reverse proxy (default: empty, use the incoming HTTP request to figure out)" , CommandOptionType . SingleValue ) ;
app . Option ( "--bundlejscss" , $"Bundle JavaScript and CSS files for better performance (default: true)" , CommandOptionType . SingleValue ) ;
2018-04-05 08:50:23 +02:00
app . Option ( "--rootpath" , "The root path in the URL to access BTCPay (default: /)" , CommandOptionType . SingleValue ) ;
2018-08-12 14:38:45 +02:00
app . Option ( "--sshconnection" , "SSH server to manage BTCPay under the form user@server:port (default: root@externalhost or empty)" , CommandOptionType . SingleValue ) ;
app . Option ( "--sshpassword" , "SSH password to manage BTCPay (default: empty)" , CommandOptionType . SingleValue ) ;
app . Option ( "--sshkeyfile" , "SSH private key file to manage BTCPay (default: empty)" , CommandOptionType . SingleValue ) ;
app . Option ( "--sshkeyfilepassword" , "Password of the SSH keyfile (default: empty)" , CommandOptionType . SingleValue ) ;
2018-08-13 02:43:59 +02:00
app . Option ( "--sshtrustedfingerprints" , "SSH Host public key fingerprint or sha256 (default: empty, it will allow untrusted connections)" , CommandOptionType . SingleValue ) ;
2018-10-15 17:37:42 +02:00
app . Option ( "--debuglog" , "A rolling log file for debug messages." , CommandOptionType . SingleValue ) ;
2018-01-11 14:52:28 +01:00
foreach ( var network in provider . GetAll ( ) )
2018-01-07 18:36:41 +01:00
{
2018-01-12 03:54:57 +01:00
var crypto = network . CryptoCode . ToLowerInvariant ( ) ;
2018-07-13 19:00:03 +02:00
app . Option ( $"--{crypto}explorerurl" , $"URL of the NBXplorer for {network.CryptoCode} (default: {network.NBXplorerNetwork.DefaultSettings.DefaultUrl})" , CommandOptionType . SingleValue ) ;
2018-01-12 03:54:57 +01:00
app . Option ( $"--{crypto}explorercookiefile" , $"Path to the cookie file (default: {network.NBXplorerNetwork.DefaultSettings.DefaultCookieFile})" , CommandOptionType . SingleValue ) ;
2018-07-13 19:00:03 +02:00
app . Option ( $"--{crypto}lightning" , $"Easy configuration of lightning for the server administrator: Must be a UNIX socket of c-lightning (lightning-rpc) or URL to a charge server (default: empty)" , CommandOptionType . SingleValue ) ;
2018-07-23 04:53:39 +02:00
app . Option ( $"--{crypto}externallndgrpc" , $"The LND gRPC configuration BTCPay will expose to easily connect to the internal lnd wallet from Zap wallet (default: empty)" , CommandOptionType . SingleValue ) ;
2018-01-07 18:36:41 +01:00
}
2017-10-27 10:53:04 +02:00
return app ;
}
2017-09-22 18:31:29 +02:00
2017-10-27 10:53:04 +02:00
public override string EnvironmentVariablePrefix = > "BTCPAY_" ;
2017-09-28 04:02:21 +02:00
2017-10-27 10:53:04 +02:00
protected override string GetDefaultDataDir ( IConfiguration conf )
{
2018-04-19 09:54:25 +02:00
return BTCPayDefaultSettings . GetDefaultSettings ( GetNetworkType ( conf ) ) . DefaultDataDirectory ;
2017-10-27 10:53:04 +02:00
}
2017-09-22 18:31:29 +02:00
2017-10-27 10:53:04 +02:00
protected override string GetDefaultConfigurationFile ( IConfiguration conf )
{
2018-04-19 09:54:25 +02:00
var network = BTCPayDefaultSettings . GetDefaultSettings ( GetNetworkType ( conf ) ) ;
2017-10-27 10:53:04 +02:00
var dataDir = conf [ "datadir" ] ;
if ( dataDir = = null )
return network . DefaultConfigurationFile ;
var fileName = Path . GetFileName ( network . DefaultConfigurationFile ) ;
2018-01-12 05:45:25 +01:00
var chainDir = Path . GetFileName ( Path . GetDirectoryName ( network . DefaultConfigurationFile ) ) ;
chainDir = Path . Combine ( dataDir , chainDir ) ;
try
{
if ( ! Directory . Exists ( chainDir ) )
Directory . CreateDirectory ( chainDir ) ;
}
catch { }
return Path . Combine ( chainDir , fileName ) ;
2017-10-27 10:53:04 +02:00
}
2017-09-22 18:31:29 +02:00
2018-04-19 09:54:25 +02:00
public static NetworkType GetNetworkType ( IConfiguration conf )
2017-10-27 10:53:04 +02:00
{
var network = conf . GetOrDefault < string > ( "network" , null ) ;
if ( network ! = null )
{
2018-01-11 14:52:28 +01:00
var n = Network . GetNetwork ( network ) ;
2018-01-12 14:08:20 +01:00
if ( n = = null )
{
throw new ConfigException ( $"Invalid network parameter '{network}'" ) ;
}
2018-04-19 09:54:25 +02:00
return n . NetworkType ;
2017-10-27 10:53:04 +02:00
}
2018-04-19 09:54:25 +02:00
var net = conf . GetOrDefault < bool > ( "regtest" , false ) ? NetworkType . Regtest :
conf . GetOrDefault < bool > ( "testnet" , false ) ? NetworkType . Testnet : NetworkType . Mainnet ;
2017-09-22 18:31:29 +02:00
2018-01-11 14:52:28 +01:00
return net ;
2017-10-27 10:53:04 +02:00
}
2017-09-22 18:31:29 +02:00
2017-10-27 10:53:04 +02:00
protected override string GetDefaultConfigurationFileTemplate ( IConfiguration conf )
{
2018-04-19 09:54:25 +02:00
var networkType = GetNetworkType ( conf ) ;
var defaultSettings = BTCPayDefaultSettings . GetDefaultSettings ( networkType ) ;
2017-10-27 10:53:04 +02:00
StringBuilder builder = new StringBuilder ( ) ;
builder . AppendLine ( "### Global settings ###" ) ;
2018-01-11 14:52:28 +01:00
builder . AppendLine ( "#network=mainnet" ) ;
2017-10-27 10:53:04 +02:00
builder . AppendLine ( ) ;
builder . AppendLine ( "### Server settings ###" ) ;
2018-01-11 14:52:28 +01:00
builder . AppendLine ( "#port=" + defaultSettings . DefaultPort ) ;
2017-10-27 10:53:04 +02:00
builder . AppendLine ( "#bind=127.0.0.1" ) ;
builder . AppendLine ( ) ;
builder . AppendLine ( "### Database ###" ) ;
builder . AppendLine ( "#postgres=User ID=root;Password=myPassword;Host=localhost;Port=5432;Database=myDataBase;" ) ;
2018-10-27 16:15:21 +02:00
builder . AppendLine ( "#mysql=User ID=root;Password=myPassword;Host=localhost;Port=3306;Database=myDataBase;" ) ;
2017-10-27 10:53:04 +02:00
builder . AppendLine ( ) ;
builder . AppendLine ( "### NBXplorer settings ###" ) ;
2018-04-19 09:54:25 +02:00
foreach ( var n in new BTCPayNetworkProvider ( networkType ) . GetAll ( ) )
2018-01-07 18:36:41 +01:00
{
2018-01-12 03:54:57 +01:00
builder . AppendLine ( $"#{n.CryptoCode}.explorer.url={n.NBXplorerNetwork.DefaultSettings.DefaultUrl}" ) ;
builder . AppendLine ( $"#{n.CryptoCode}.explorer.cookiefile={ n.NBXplorerNetwork.DefaultSettings.DefaultCookieFile}" ) ;
2018-03-20 18:09:25 +01:00
builder . AppendLine ( $"#{n.CryptoCode}.lightning=/root/.lightning/lightning-rpc" ) ;
builder . AppendLine ( $"#{n.CryptoCode}.lightning=https://apitoken:API_TOKEN_SECRET@charge.example.com/" ) ;
2018-01-07 18:36:41 +01:00
}
2017-10-27 10:53:04 +02:00
return builder . ToString ( ) ;
}
2017-09-22 18:31:29 +02:00
2017-10-27 10:53:04 +02:00
protected override IPEndPoint GetDefaultEndpoint ( IConfiguration conf )
{
2018-04-19 09:54:25 +02:00
return new IPEndPoint ( IPAddress . Parse ( "127.0.0.1" ) , BTCPayDefaultSettings . GetDefaultSettings ( GetNetworkType ( conf ) ) . DefaultPort ) ;
2017-10-27 10:53:04 +02:00
}
}
2017-09-22 18:31:29 +02:00
}