2018-08-12 21:38:45 +09:00
using System ;
using Microsoft.Extensions.Logging ;
using Microsoft.EntityFrameworkCore ;
using System.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
using BTCPayServer.Data ;
using BTCPayServer.Services ;
using Microsoft.Extensions.Hosting ;
using System.Threading ;
using BTCPayServer.Configuration ;
using BTCPayServer.Logging ;
2018-08-12 23:23:26 +09:00
using NBitcoin.DataEncoders ;
2018-08-12 21:38:45 +09:00
namespace BTCPayServer.HostedServices
{
public class CheckConfigurationHostedService : IHostedService
{
private readonly BTCPayServerOptions _options ;
public CheckConfigurationHostedService ( BTCPayServerOptions options )
{
_options = options ;
}
public Task StartAsync ( CancellationToken cancellationToken )
{
new Thread ( ( ) = >
{
if ( _options . SSHSettings ! = null )
{
Logs . Configuration . LogInformation ( $"SSH settings detected, testing connection to {_options.SSHSettings.Username}@{_options.SSHSettings.Server} on port {_options.SSHSettings.Port} ..." ) ;
var connection = new Renci . SshNet . SshClient ( _options . SSHSettings . CreateConnectionInfo ( ) ) ;
2018-08-12 23:23:26 +09:00
connection . HostKeyReceived + = ( object sender , Renci . SshNet . Common . HostKeyEventArgs e ) = >
{
e . CanTrust = true ;
2018-08-13 09:43:59 +09:00
if ( ! _options . IsTrustedFingerprint ( e . FingerPrint , e . HostKey ) )
2018-08-12 23:23:26 +09:00
{
2018-08-13 09:43:59 +09:00
Logs . Configuration . LogWarning ( $"SSH host fingerprint for {e.HostKeyName} is untrusted, start BTCPay with -sshtrustedfingerprints \" { Encoders . Hex . EncodeData ( e . FingerPrint ) } \ "" ) ;
2018-08-12 23:23:26 +09:00
}
} ;
2018-08-12 21:38:45 +09:00
try
{
connection . Connect ( ) ;
connection . Disconnect ( ) ;
Logs . Configuration . LogInformation ( $"SSH connection succeeded" ) ;
}
catch ( Renci . SshNet . Common . SshAuthenticationException )
{
Logs . Configuration . LogWarning ( $"SSH invalid credentials" ) ;
}
catch ( Exception ex )
{
var message = ex . Message ;
if ( ex is AggregateException aggrEx & & aggrEx . InnerException ? . Message ! = null )
{
message = aggrEx . InnerException . Message ;
}
Logs . Configuration . LogWarning ( $"SSH connection issue: {message}" ) ;
}
finally
{
connection . Dispose ( ) ;
}
}
} )
{ IsBackground = true } . Start ( ) ;
return Task . CompletedTask ;
}
public Task StopAsync ( CancellationToken cancellationToken )
{
return Task . CompletedTask ;
}
}
}