Exemplo n.º 1
0
        private IActionResult RunSSH(MaintenanceViewModel vm, string ssh)
        {
            ssh = $"sudo bash -c '. /etc/profile.d/btcpay-env.sh && nohup {ssh} > /dev/null 2>&1 & disown'";
            var sshClient = vm.CreateSSHClient(this.Request.Host.Host);

            try
            {
                sshClient.Connect();
            }
            catch (Renci.SshNet.Common.SshAuthenticationException)
            {
                ModelState.AddModelError(nameof(vm.Password), "Invalid credentials");
                sshClient.Dispose();
                return(View(vm));
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                if (ex is AggregateException aggrEx && aggrEx.InnerException?.Message != null)
                {
                    message = aggrEx.InnerException.Message;
                }
                ModelState.AddModelError(nameof(vm.UserName), $"Connection problem ({message})");
                sshClient.Dispose();
                return(View(vm));
            }

            var sshCommand = sshClient.CreateCommand(ssh);

            sshCommand.CommandTimeout = TimeSpan.FromMinutes(1.0);
            sshCommand.BeginExecute(ar =>
            {
                try
                {
                    Logs.PayServer.LogInformation("Running SSH command: " + ssh);
                    var result = sshCommand.EndExecute(ar);
                    Logs.PayServer.LogInformation("SSH command executed: " + result);
                }
                catch (Exception ex)
                {
                    Logs.PayServer.LogWarning("Error while executing SSH command: " + ex.Message);
                }
                sshClient.Dispose();
            });
            return(null);
        }
Exemplo n.º 2
0
        private IActionResult RunSSH(MaintenanceViewModel vm, string ssh)
        {
            ssh = $"sudo bash -c '. /etc/profile.d/btcpay-env.sh && nohup {ssh} > /dev/null 2>&1 & disown'";
            var sshClient = _Options.SSHSettings == null?vm.CreateSSHClient(this.Request.Host.Host)
                                : new SshClient(_Options.SSHSettings.CreateConnectionInfo());

            if (_Options.TrustedFingerprints.Count != 0)
            {
                sshClient.HostKeyReceived += (object sender, Renci.SshNet.Common.HostKeyEventArgs e) =>
                {
                    if (_Options.TrustedFingerprints.Count == 0)
                    {
                        Logs.Configuration.LogWarning($"SSH host fingerprint for {e.HostKeyName} is untrusted, start BTCPay with -sshtrustedfingerprints \"{Encoders.Hex.EncodeData(e.FingerPrint)}\"");
                        e.CanTrust = true; // Not a typo, we want the connection to succeed with a warning
                    }
                    else
                    {
                        e.CanTrust = _Options.IsTrustedFingerprint(e.FingerPrint, e.HostKey);
                        if (!e.CanTrust)
                        {
                            Logs.Configuration.LogError($"SSH host fingerprint for {e.HostKeyName} is untrusted, start BTCPay with -sshtrustedfingerprints \"{Encoders.Hex.EncodeData(e.FingerPrint)}\"");
                        }
                    }
                };
            }
            else
            {
            }

            try
            {
                sshClient.Connect();
            }
            catch (Renci.SshNet.Common.SshAuthenticationException)
            {
                ModelState.AddModelError(nameof(vm.Password), "Invalid credentials");
                sshClient.Dispose();
                return(View(vm));
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                if (ex is AggregateException aggrEx && aggrEx.InnerException?.Message != null)
                {
                    message = aggrEx.InnerException.Message;
                }
                ModelState.AddModelError(nameof(vm.UserName), $"Connection problem ({message})");
                sshClient.Dispose();
                return(View(vm));
            }

            var sshCommand = sshClient.CreateCommand(ssh);

            sshCommand.CommandTimeout = TimeSpan.FromMinutes(1.0);
            sshCommand.BeginExecute(ar =>
            {
                try
                {
                    Logs.PayServer.LogInformation("Running SSH command: " + ssh);
                    var result = sshCommand.EndExecute(ar);
                    Logs.PayServer.LogInformation("SSH command executed: " + result);
                }
                catch (Exception ex)
                {
                    Logs.PayServer.LogWarning("Error while executing SSH command: " + ex.Message);
                }
                sshClient.Dispose();
            });
            return(null);
        }