Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d30ee4af1 | ||
|
|
1db62a8574 | ||
|
|
83801d78c4 | ||
|
|
7db3401799 | ||
|
|
2da1eac443 | ||
|
|
dc92c80b43 | ||
|
|
724644c8eb | ||
|
|
23768dbe0e | ||
|
|
ace23b954e |
+1
-1
@@ -1 +1 @@
|
||||
1.0.20
|
||||
1.0.25
|
||||
|
||||
@@ -23,6 +23,7 @@ git tag -m "Version v%VERSION%: %~1" -a v%VERSION%
|
||||
git push origin v%VERSION%
|
||||
|
||||
git push --tags
|
||||
git push
|
||||
GOTO End
|
||||
pause
|
||||
|
||||
|
||||
@@ -170,8 +170,9 @@ namespace MonsterMonitor.Models
|
||||
|
||||
if (string.IsNullOrWhiteSpace(result.SystemPasswordProtected))
|
||||
{
|
||||
// Значение по умолчанию применяется только в памяти.
|
||||
// Раньше здесь вызывался Save() — лишний дисковый I/O на каждом чтении настроек.
|
||||
result.SetSystemPassword("STerra");
|
||||
result.Save();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<RootNamespace>MonsterMonitor</RootNamespace>
|
||||
<AssemblyName>devenv</AssemblyName>
|
||||
<AssemblyVersion>1.0.20</AssemblyVersion>
|
||||
<Version>1.0.20</Version>
|
||||
<FileVersion>1.0.20</FileVersion>
|
||||
<InformationalVersion>1.0.20</InformationalVersion>
|
||||
<AssemblyVersion>1.0.25</AssemblyVersion>
|
||||
<Version>1.0.25</Version>
|
||||
<FileVersion>1.0.25</FileVersion>
|
||||
<InformationalVersion>1.0.25</InformationalVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Costura.Fody" Version="6.0.0">
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using MonsterMonitor.UI;
|
||||
|
||||
@@ -6,9 +7,21 @@ namespace MonsterMonitor
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
private const string SingleInstanceMutexName = @"Global\MonsterMonitor_v2_SingleInstance";
|
||||
private const string ActivateEventName = @"Global\MonsterMonitor_v2_Activate";
|
||||
|
||||
[STAThread]
|
||||
private static void Main()
|
||||
{
|
||||
var createdNew = false;
|
||||
using (var mutex = new Mutex(true, SingleInstanceMutexName, out createdNew))
|
||||
{
|
||||
if (!createdNew)
|
||||
{
|
||||
TrySignalExistingInstance();
|
||||
return;
|
||||
}
|
||||
|
||||
Application.ThreadException += (sender, args) =>
|
||||
{
|
||||
MessageBox.Show(
|
||||
@@ -29,7 +42,38 @@ namespace MonsterMonitor
|
||||
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new MainForm());
|
||||
using (var activateEvent = new EventWaitHandle(false, EventResetMode.AutoReset, ActivateEventName))
|
||||
{
|
||||
var form = new MainForm();
|
||||
ThreadPool.RegisterWaitForSingleObject(
|
||||
activateEvent,
|
||||
(state, timedOut) =>
|
||||
{
|
||||
var mainForm = state as MainForm;
|
||||
mainForm?.RestoreAndActivateFromExternalSignal();
|
||||
},
|
||||
form,
|
||||
Timeout.Infinite,
|
||||
false);
|
||||
|
||||
Application.Run(form);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void TrySignalExistingInstance()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var activateEvent = EventWaitHandle.OpenExisting(ActivateEventName))
|
||||
{
|
||||
activateEvent.Set();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Если не удалось отправить сигнал, просто завершаем второй инстанс.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MonsterMonitor.Models;
|
||||
|
||||
namespace MonsterMonitor.Services
|
||||
@@ -26,12 +27,14 @@ namespace MonsterMonitor.Services
|
||||
_power.PreventSleep();
|
||||
_processMonitor = new ProcessMonitorService(_log);
|
||||
_sshTunnel = new SshTunnelService(_log);
|
||||
_authMonitor = new AuthMonitor(settings);
|
||||
_authMonitor = new AuthMonitor(settings, _log);
|
||||
|
||||
_ssConfig.EnsureConfig(settings);
|
||||
_processMonitor.Start(settings.SsProcessPath, settings.SsArguments);
|
||||
_sshTunnel.Start(settings);
|
||||
_ = _authMonitor.StartMonitor();
|
||||
_authMonitor.StartMonitor().ContinueWith(
|
||||
t => _log.Error("Ошибка запуска монитора авторизации: " + t.Exception?.GetBaseException().Message),
|
||||
TaskContinuationOptions.OnlyOnFaulted);
|
||||
|
||||
_log.Info("Сервисы приложения запущены.");
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
@@ -9,32 +8,81 @@ using MonsterMonitor.Models;
|
||||
|
||||
namespace MonsterMonitor.Services
|
||||
{
|
||||
public class AuthMonitor: IAuthMonitor
|
||||
public class AuthMonitor : IAuthMonitor
|
||||
{
|
||||
private const string AuthWindowMarker = "XAuth request";
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
static extern IntPtr GetForegroundWindow();
|
||||
private static extern IntPtr GetForegroundWindow();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
||||
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
|
||||
private static extern int GetWindowTextLength(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool IsWindowVisible(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
|
||||
|
||||
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
|
||||
|
||||
private readonly AppSettings _settings;
|
||||
private readonly LogService _log;
|
||||
private CancellationTokenSource _monitorCancellation;
|
||||
private Task _monitorTask;
|
||||
|
||||
public AuthMonitor(AppSettings settings)
|
||||
public AuthMonitor(AppSettings settings, LogService log)
|
||||
{
|
||||
_settings = settings;
|
||||
_log = log;
|
||||
}
|
||||
|
||||
private string GetActiveWindowTitle()
|
||||
private static string GetWindowTitle(IntPtr handle)
|
||||
{
|
||||
const int nChars = 256;
|
||||
var buff = new StringBuilder(nChars);
|
||||
var handle = GetForegroundWindow();
|
||||
return GetWindowText(handle, buff, nChars) > 0 ? buff.ToString() : null;
|
||||
var length = GetWindowTextLength(handle);
|
||||
if (length <= 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var buff = new StringBuilder(length + 1);
|
||||
return GetWindowText(handle, buff, buff.Capacity) > 0 ? buff.ToString() : null;
|
||||
}
|
||||
|
||||
private static string GetActiveWindowTitle()
|
||||
{
|
||||
return GetWindowTitle(GetForegroundWindow());
|
||||
}
|
||||
|
||||
// Ищет окно авторизации через перечисление окон (без перебора всех процессов
|
||||
// и без утечки хендлов Process). Возвращает первое видимое подходящее окно.
|
||||
private static IntPtr FindAuthWindow()
|
||||
{
|
||||
var found = IntPtr.Zero;
|
||||
EnumWindows((hWnd, lParam) =>
|
||||
{
|
||||
if (!IsWindowVisible(hWnd))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var title = GetWindowTitle(hWnd);
|
||||
if (title != null && title.IndexOf(AuthWindowMarker, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
{
|
||||
found = hWnd;
|
||||
return false; // прекращаем перечисление
|
||||
}
|
||||
|
||||
return true;
|
||||
}, IntPtr.Zero);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
public async Task StartMonitor()
|
||||
@@ -75,29 +123,28 @@ namespace MonsterMonitor.Services
|
||||
{
|
||||
try
|
||||
{
|
||||
Process[] processlist = Process.GetProcesses();
|
||||
foreach (Process process in processlist)
|
||||
var authWindow = FindAuthWindow();
|
||||
if (authWindow != IntPtr.Zero)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(process.MainWindowTitle) && process.MainWindowTitle.Contains("XAuth request"))
|
||||
{
|
||||
SetForegroundWindow(process.MainWindowHandle);
|
||||
}
|
||||
}
|
||||
SetForegroundWindow(authWindow);
|
||||
|
||||
var activeWindowTitle = GetActiveWindowTitle();
|
||||
if (activeWindowTitle?.Contains("XAuth request") == true)
|
||||
{
|
||||
var password = _settings.GetSystemPassword();
|
||||
if (!string.IsNullOrEmpty(password))
|
||||
var activeWindowTitle = GetActiveWindowTitle();
|
||||
if (activeWindowTitle?.IndexOf(AuthWindowMarker, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
{
|
||||
SendKeys.SendWait(password);
|
||||
SendKeys.SendWait("{ENTER}");
|
||||
var password = _settings.GetSystemPassword();
|
||||
if (!string.IsNullOrEmpty(password))
|
||||
{
|
||||
SendKeys.SendWait(password);
|
||||
SendKeys.SendWait("{ENTER}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message + ex.StackTrace);
|
||||
// Раньше здесь был MessageBox.Show из фонового потока — при повторяющемся
|
||||
// исключении он заваливал рабочий стол окнами. Теперь просто пишем в лог.
|
||||
_log.Warn("Ошибка мониторинга авторизации: " + ex.Message);
|
||||
}
|
||||
|
||||
try
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace MonsterMonitor.Services
|
||||
@@ -13,6 +14,7 @@ namespace MonsterMonitor.Services
|
||||
private string _path;
|
||||
private string _arguments;
|
||||
private bool _stopping;
|
||||
private string _lastStartError;
|
||||
|
||||
public ProcessMonitorService(LogService log)
|
||||
{
|
||||
@@ -27,6 +29,10 @@ namespace MonsterMonitor.Services
|
||||
_path = path;
|
||||
_arguments = arguments ?? string.Empty;
|
||||
_stopping = false;
|
||||
// Подчищаем ss.exe, оставшийся от предыдущей (аварийно закрытой) копии
|
||||
// приложения — иначе его занятый порт не даст новому 3proxy стартовать,
|
||||
// и watchdog будет бесконечно перезапускать мгновенно умирающий процесс.
|
||||
KillOrphanedProcesses();
|
||||
EnsureStarted();
|
||||
_watchdogTimer.Change(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
|
||||
}
|
||||
@@ -43,7 +49,6 @@ namespace MonsterMonitor.Services
|
||||
{
|
||||
if (_process != null)
|
||||
{
|
||||
_process.Exited -= ProcessOnExited;
|
||||
_process.Dispose();
|
||||
}
|
||||
_process = new Process
|
||||
@@ -57,27 +62,36 @@ namespace MonsterMonitor.Services
|
||||
},
|
||||
EnableRaisingEvents = true
|
||||
};
|
||||
//_process.Exited += ProcessOnExited;
|
||||
_process.Start();
|
||||
|
||||
// ss (3proxy) при занятом порту завершается почти мгновенно.
|
||||
// Если процесс умер сразу после старта — не рапортуем «запущен» и не
|
||||
// крутим бесконечный цикл рестартов, а один раз пишем понятную причину.
|
||||
if (_process.WaitForExit(500))
|
||||
{
|
||||
var exitCode = SafeGetExitCode(_process);
|
||||
var message = "Процесс ss завершился сразу после запуска (код " + exitCode +
|
||||
"). Вероятно, локальный порт уже занят другим процессом ss.";
|
||||
if (_lastStartError != message)
|
||||
{
|
||||
_lastStartError = message;
|
||||
_log.Error(message);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
_lastStartError = null;
|
||||
_log.Info("Процесс ss запущен.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Error("Ошибка запуска ss: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessOnExited(object sender, EventArgs e)
|
||||
{
|
||||
lock (_sync)
|
||||
{
|
||||
if (_stopping)
|
||||
// Watchdog тикает каждые 10с; при неверном пути не флудим лог
|
||||
// одинаковой ошибкой — пишем её только при изменении.
|
||||
if (_lastStartError != ex.Message)
|
||||
{
|
||||
return;
|
||||
_lastStartError = ex.Message;
|
||||
_log.Error("Ошибка запуска ss: " + ex.Message);
|
||||
}
|
||||
|
||||
_log.Warn("Процесс ss завершился, запускаю заново.");
|
||||
EnsureStarted();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +132,6 @@ namespace MonsterMonitor.Services
|
||||
{
|
||||
if (_process != null)
|
||||
{
|
||||
_process.Exited -= ProcessOnExited;
|
||||
_process.Dispose();
|
||||
}
|
||||
_process = null;
|
||||
@@ -143,6 +156,114 @@ namespace MonsterMonitor.Services
|
||||
}
|
||||
}
|
||||
|
||||
// Завершает «осиротевшие» экземпляры ss.exe, оставшиеся от прошлой копии
|
||||
// приложения. Сопоставление по полному пути к исполняемому файлу, чтобы не
|
||||
// задеть посторонние одноимённые процессы.
|
||||
private void KillOrphanedProcesses()
|
||||
{
|
||||
string targetName;
|
||||
string targetFullPath;
|
||||
try
|
||||
{
|
||||
targetName = Path.GetFileNameWithoutExtension(_path);
|
||||
targetFullPath = Path.GetFullPath(_path);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(targetName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var ownPid = _process != null && IsProcessRunning(_process) ? SafeGetId(_process) : -1;
|
||||
|
||||
Process[] candidates;
|
||||
try
|
||||
{
|
||||
candidates = Process.GetProcessesByName(targetName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Warn("Не удалось перечислить процессы ss: " + ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var proc in candidates)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (proc.Id == ownPid)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Убиваем только процессы из нашего каталога ss.exe.
|
||||
if (!PathsEqual(SafeGetProcessPath(proc), targetFullPath))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
proc.Kill();
|
||||
proc.WaitForExit(3000);
|
||||
_log.Warn("Завершен осиротевший процесс ss (PID " + proc.Id + ") от предыдущего запуска.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Warn("Не удалось завершить осиротевший ss: " + ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
proc.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool PathsEqual(string a, string b)
|
||||
{
|
||||
return !string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b) &&
|
||||
string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private static string SafeGetProcessPath(Process process)
|
||||
{
|
||||
try
|
||||
{
|
||||
return process.MainModule?.FileName;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Доступ к MainModule может быть запрещён (другой пользователь/разрядность).
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static int SafeGetId(Process process)
|
||||
{
|
||||
try
|
||||
{
|
||||
return process.Id;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private static int SafeGetExitCode(Process process)
|
||||
{
|
||||
try
|
||||
{
|
||||
return process.ExitCode;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Stop();
|
||||
|
||||
@@ -12,16 +12,21 @@ namespace MonsterMonitor.Services
|
||||
{
|
||||
private readonly LogService _log;
|
||||
private readonly object _sync = new object();
|
||||
|
||||
// Сериализует реконнект: перекрывающиеся запросы (из событий и из монитора)
|
||||
// отбрасываются, а не выполняются параллельно и не копятся в очередь.
|
||||
private readonly SemaphoreSlim _reconnectGate = new SemaphoreSlim(1, 1);
|
||||
|
||||
private SshClient _client;
|
||||
private ForwardedPortRemote _forwardedPort;
|
||||
private SshCommand _heartbeatCommand;
|
||||
private CancellationTokenSource _heartbeatReadTokenSource;
|
||||
private Task _heartbeatReadTask;
|
||||
private CancellationTokenSource _monitorTokenSource;
|
||||
private Task _monitorTask;
|
||||
private AppSettings _settings;
|
||||
private bool _disposed;
|
||||
private bool _reconnectInProgress;
|
||||
private DateTime _lastHeartbeatUtc = DateTime.MinValue;
|
||||
private volatile bool _disposed;
|
||||
private long _lastHeartbeatTicks;
|
||||
private bool _remoteIsWindows;
|
||||
|
||||
public SshTunnelService(LogService log)
|
||||
@@ -55,6 +60,10 @@ namespace MonsterMonitor.Services
|
||||
var authMethod = new PasswordAuthenticationMethod(_settings.SshUsername, password);
|
||||
var connectionInfo = new ConnectionInfo(_settings.SshHost, _settings.SshPort, _settings.SshUsername, authMethod);
|
||||
|
||||
// Ограничиваем блокирующий Connect(), чтобы он не висел бесконечно.
|
||||
var timeoutSec = Math.Min(60, Math.Max(5, _settings.ReconnectTimeoutSec));
|
||||
connectionInfo.Timeout = TimeSpan.FromSeconds(timeoutSec);
|
||||
|
||||
_client = new SshClient(connectionInfo);
|
||||
_client.ErrorOccurred += ClientOnErrorOccurred;
|
||||
_client.Connect();
|
||||
@@ -85,7 +94,7 @@ namespace MonsterMonitor.Services
|
||||
private void StartMonitor()
|
||||
{
|
||||
_monitorTokenSource = new CancellationTokenSource();
|
||||
Task.Run(() => MonitorLoop(_monitorTokenSource.Token));
|
||||
_monitorTask = Task.Run(() => MonitorLoop(_monitorTokenSource.Token));
|
||||
}
|
||||
|
||||
private async Task MonitorLoop(CancellationToken token)
|
||||
@@ -94,18 +103,23 @@ namespace MonsterMonitor.Services
|
||||
{
|
||||
try
|
||||
{
|
||||
var silenceThresholdSec = Math.Max(
|
||||
5,
|
||||
_settings.MaxPingFailures);
|
||||
|
||||
var lastHeartbeat = _lastHeartbeatUtc;
|
||||
var isHeartbeatAlive = lastHeartbeat != DateTime.MinValue &&
|
||||
(DateTime.UtcNow - lastHeartbeat).TotalSeconds <= silenceThresholdSec;
|
||||
|
||||
if (!IsConnected() || !isHeartbeatAlive)
|
||||
// Пропускаем проверку, пока идёт реконнект — иначе получаем лог-флуд
|
||||
// и лишние пробуждения, дёргающие уже занятый шлюз реконнекта.
|
||||
if (_reconnectGate.CurrentCount > 0)
|
||||
{
|
||||
_log.Warn("Нет живого вывода heartbeat-команды на удаленном сервере. Переподключаю SSH.");
|
||||
await Reconnect().ConfigureAwait(false);
|
||||
// heartbeat присылается раз в секунду; MaxPingFailures трактуем
|
||||
// как допустимое число пропущенных ответов (≈ секунд тишины).
|
||||
var silenceThresholdSec = Math.Max(10, _settings.MaxPingFailures);
|
||||
|
||||
var lastHeartbeatTicks = Interlocked.Read(ref _lastHeartbeatTicks);
|
||||
var isHeartbeatAlive = lastHeartbeatTicks != 0 &&
|
||||
(DateTime.UtcNow - new DateTime(lastHeartbeatTicks, DateTimeKind.Utc)).TotalSeconds <= silenceThresholdSec;
|
||||
|
||||
if (!IsConnected() || !isHeartbeatAlive)
|
||||
{
|
||||
_log.Warn("Нет живого вывода heartbeat-команды на удаленном сервере. Переподключаю SSH.");
|
||||
await Reconnect().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -115,9 +129,9 @@ namespace MonsterMonitor.Services
|
||||
|
||||
try
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(2), token).ConfigureAwait(false);
|
||||
await Task.Delay(TimeSpan.FromSeconds(3), token).ConfigureAwait(false);
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -136,39 +150,51 @@ namespace MonsterMonitor.Services
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(1000).ConfigureAwait(false);
|
||||
await Reconnect().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await Task.Delay(1000).ConfigureAwait(false);
|
||||
await Reconnect().ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// fire-and-forget: гарантированно не роняем процесс необработанным исключением.
|
||||
_log.Warn("Ошибка отложенного переподключения SSH: " + ex.Message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async Task Reconnect()
|
||||
{
|
||||
if (_reconnectInProgress || _disposed)
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Неблокирующая попытка захватить шлюз: если реконнект уже идёт — выходим,
|
||||
// не создавая второй параллельный Connect() и не накапливая очередь.
|
||||
if (!await _reconnectGate.WaitAsync(0).ConfigureAwait(false))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_reconnectInProgress = true;
|
||||
try
|
||||
{
|
||||
var timeout = Math.Min(60, Math.Max(5, _settings.ReconnectTimeoutSec));
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_log.Warn("Перезапуск SSH-соединения...");
|
||||
|
||||
var reconnectTask = Task.Run(() =>
|
||||
// Connect() ограничен ConnectionInfo.Timeout, поэтому не зависнет навсегда.
|
||||
// Ждём завершения задачи (без брошенного WhenAny) — нет орфанных SshClient.
|
||||
await Task.Run(() =>
|
||||
{
|
||||
DisconnectCore();
|
||||
Connect();
|
||||
});
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
var completed = await Task.WhenAny(reconnectTask, Task.Delay(TimeSpan.FromSeconds(timeout))).ConfigureAwait(false);
|
||||
if (completed != reconnectTask)
|
||||
{
|
||||
_log.Error("Переподключение превысило таймаут " + timeout + "с.");
|
||||
}
|
||||
else
|
||||
{
|
||||
_log.Info("SSH-соединение восстановлено.");
|
||||
}
|
||||
_log.Info("SSH-соединение восстановлено.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -176,46 +202,51 @@ namespace MonsterMonitor.Services
|
||||
}
|
||||
finally
|
||||
{
|
||||
_reconnectInProgress = false;
|
||||
_reconnectGate.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private void DisconnectCore()
|
||||
{
|
||||
StopRemoteHeartbeatNoLock();
|
||||
lock (_sync)
|
||||
{
|
||||
StopRemoteHeartbeatNoLock();
|
||||
|
||||
try
|
||||
{
|
||||
if (_forwardedPort != null)
|
||||
try
|
||||
{
|
||||
if (_forwardedPort.IsStarted)
|
||||
if (_forwardedPort != null)
|
||||
{
|
||||
_forwardedPort.Stop();
|
||||
if (_forwardedPort.IsStarted)
|
||||
{
|
||||
_forwardedPort.Stop();
|
||||
}
|
||||
_forwardedPort.Exception -= ForwardedPortOnException;
|
||||
_forwardedPort.Dispose();
|
||||
_forwardedPort = null;
|
||||
}
|
||||
_forwardedPort.Dispose();
|
||||
_forwardedPort = null;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore errors on shutdown.
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore errors on shutdown.
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (_client != null)
|
||||
try
|
||||
{
|
||||
if (_client.IsConnected)
|
||||
if (_client != null)
|
||||
{
|
||||
_client.Disconnect();
|
||||
_client.ErrorOccurred -= ClientOnErrorOccurred;
|
||||
if (_client.IsConnected)
|
||||
{
|
||||
_client.Disconnect();
|
||||
}
|
||||
_client.Dispose();
|
||||
_client = null;
|
||||
}
|
||||
_client.Dispose();
|
||||
_client = null;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore errors on shutdown.
|
||||
catch
|
||||
{
|
||||
// Ignore errors on shutdown.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,7 +266,7 @@ namespace MonsterMonitor.Services
|
||||
|
||||
_heartbeatCommand = _client.CreateCommand(heartbeatCommand);
|
||||
_heartbeatReadTokenSource = new CancellationTokenSource();
|
||||
_lastHeartbeatUtc = DateTime.UtcNow;
|
||||
Interlocked.Exchange(ref _lastHeartbeatTicks, DateTime.UtcNow.Ticks);
|
||||
_heartbeatCommand.BeginExecute();
|
||||
|
||||
_heartbeatReadTask = Task.Run(() =>
|
||||
@@ -287,7 +318,7 @@ namespace MonsterMonitor.Services
|
||||
continue;
|
||||
}
|
||||
|
||||
_lastHeartbeatUtc = DateTime.UtcNow;
|
||||
Interlocked.Exchange(ref _lastHeartbeatTicks, DateTime.UtcNow.Ticks);
|
||||
_log.Debug("HB: " + line);
|
||||
}
|
||||
}
|
||||
@@ -305,9 +336,11 @@ namespace MonsterMonitor.Services
|
||||
{
|
||||
try
|
||||
{
|
||||
var command = _client.CreateCommand("cmd /c ver");
|
||||
var output = command.Execute() ?? string.Empty;
|
||||
return output.IndexOf("windows", StringComparison.OrdinalIgnoreCase) >= 0;
|
||||
using (var command = _client.CreateCommand("cmd /c ver"))
|
||||
{
|
||||
var output = command.Execute() ?? string.Empty;
|
||||
return output.IndexOf("windows", StringComparison.OrdinalIgnoreCase) >= 0;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -317,19 +350,35 @@ namespace MonsterMonitor.Services
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_monitorTokenSource?.Cancel();
|
||||
_monitorTokenSource?.Dispose();
|
||||
var monitorTokenSource = _monitorTokenSource;
|
||||
var monitorTask = _monitorTask;
|
||||
_monitorTokenSource = null;
|
||||
lock (_sync)
|
||||
_monitorTask = null;
|
||||
|
||||
try
|
||||
{
|
||||
DisconnectCore();
|
||||
monitorTokenSource?.Cancel();
|
||||
// Дожидаемся завершения цикла монитора до освобождения CTS,
|
||||
// иначе Task.Delay(token) может словить ObjectDisposedException.
|
||||
monitorTask?.Wait(TimeSpan.FromSeconds(2));
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore shutdown errors.
|
||||
}
|
||||
finally
|
||||
{
|
||||
monitorTokenSource?.Dispose();
|
||||
}
|
||||
|
||||
DisconnectCore();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_disposed = true;
|
||||
Stop();
|
||||
_reconnectGate.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,31 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using MonsterMonitor.Models;
|
||||
using MonsterMonitor.Services;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace MonsterMonitor.UI
|
||||
{
|
||||
public sealed class MainForm : Form
|
||||
{
|
||||
private const int EmGetFirstVisibleLine = 0x00CE;
|
||||
private const int EmLineScroll = 0x00B6;
|
||||
private const int WmSetRedraw = 0x000B;
|
||||
|
||||
// Ограничение размера буфера консоли, чтобы текст не рос бесконечно.
|
||||
private const int ConsoleMaxChars = 100000;
|
||||
private const int ConsoleTrimToChars = 80000;
|
||||
|
||||
private readonly RichTextBox _console = new RichTextBox();
|
||||
private readonly Button _btnSettings = new Button();
|
||||
private readonly Button _btnExit = new Button();
|
||||
private readonly NotifyIcon _notifyIcon = new NotifyIcon();
|
||||
private readonly Timer _updateTimer = new Timer();
|
||||
private readonly Timer _logFlushTimer = new Timer();
|
||||
private readonly ConcurrentQueue<LogEntry> _pendingLogs = new ConcurrentQueue<LogEntry>();
|
||||
private readonly Icon _trayIcon;
|
||||
private readonly LogService _log = new LogService();
|
||||
private readonly PowerManagementService _power = new PowerManagementService();
|
||||
@@ -30,6 +42,7 @@ namespace MonsterMonitor.UI
|
||||
Height = 620;
|
||||
StartPosition = FormStartPosition.CenterScreen;
|
||||
_trayIcon = LoadTrayIcon();
|
||||
Icon = _trayIcon;
|
||||
|
||||
BuildUi();
|
||||
BindEvents();
|
||||
@@ -38,12 +51,11 @@ namespace MonsterMonitor.UI
|
||||
protected override void OnLoad(EventArgs e)
|
||||
{
|
||||
base.OnLoad(e);
|
||||
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
|
||||
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
|
||||
|
||||
_settings = AppSettings.Load();
|
||||
_controller = new AppController(_log, _power);
|
||||
_updateService = new GitHubUpdateService(_log, _settings);
|
||||
ConfigureLogFlushTimer();
|
||||
RestartServices();
|
||||
ConfigureUpdateTimer();
|
||||
_ = RunUpdateCheckAsync(true);
|
||||
@@ -135,6 +147,33 @@ namespace MonsterMonitor.UI
|
||||
Activate();
|
||||
}
|
||||
|
||||
public void RestoreAndActivateFromExternalSignal()
|
||||
{
|
||||
if (InvokeRequired)
|
||||
{
|
||||
BeginInvoke(new Action(RestoreAndActivateFromExternalSignal));
|
||||
return;
|
||||
}
|
||||
|
||||
_log.Info("Получен сигнал активации от второго инстанса.");
|
||||
|
||||
if (!Visible)
|
||||
{
|
||||
Show();
|
||||
}
|
||||
|
||||
if (WindowState == FormWindowState.Minimized)
|
||||
{
|
||||
WindowState = FormWindowState.Normal;
|
||||
}
|
||||
|
||||
// Небольшой трюк для надежного вывода окна на передний план.
|
||||
TopMost = true;
|
||||
Activate();
|
||||
TopMost = false;
|
||||
Focus();
|
||||
}
|
||||
|
||||
private void ExitApplication()
|
||||
{
|
||||
_allowClose = true;
|
||||
@@ -150,29 +189,117 @@ namespace MonsterMonitor.UI
|
||||
return;
|
||||
}
|
||||
|
||||
_notifyIcon.Visible = false;
|
||||
// Останавливаем таймеры заранее, чтобы не было тиков во время разрушения формы.
|
||||
_logFlushTimer.Stop();
|
||||
_updateTimer.Stop();
|
||||
_updateTimer.Dispose();
|
||||
_controller?.Dispose();
|
||||
_trayIcon?.Dispose();
|
||||
_notifyIcon.Visible = false;
|
||||
}
|
||||
|
||||
private void ConfigureLogFlushTimer()
|
||||
{
|
||||
// Логи копятся в очереди и выводятся пачкой ~10 раз в секунду,
|
||||
// а не по одной строке за событие — это снимает нагрузку на CPU и убирает мерцание.
|
||||
_logFlushTimer.Interval = 100;
|
||||
_logFlushTimer.Tick += (_, __) => FlushLogs();
|
||||
_logFlushTimer.Start();
|
||||
}
|
||||
|
||||
// Вызывается из фоновых потоков — только кладём запись в очередь, без обращения к UI.
|
||||
private void AppendLog(LogEntry entry)
|
||||
{
|
||||
if (InvokeRequired)
|
||||
_pendingLogs.Enqueue(entry);
|
||||
}
|
||||
|
||||
// Выполняется всегда в UI-потоке (таймер WinForms).
|
||||
private void FlushLogs()
|
||||
{
|
||||
if (_pendingLogs.IsEmpty || !_console.IsHandleCreated)
|
||||
{
|
||||
BeginInvoke(new Action<LogEntry>(AppendLog), entry);
|
||||
return;
|
||||
}
|
||||
|
||||
var wasNearBottom = IsConsoleNearBottom();
|
||||
var firstVisibleLineBeforeAppend = GetFirstVisibleLine(_console);
|
||||
|
||||
// Замораживаем отрисовку на время пакетного добавления — одна перерисовка вместо десятков.
|
||||
SendMessage(_console.Handle, WmSetRedraw, IntPtr.Zero, IntPtr.Zero);
|
||||
try
|
||||
{
|
||||
while (_pendingLogs.TryDequeue(out var entry))
|
||||
{
|
||||
_console.SelectionStart = _console.TextLength;
|
||||
_console.SelectionLength = 0;
|
||||
_console.SelectionColor = GetColor(entry.Level);
|
||||
_console.AppendText($"[{entry.Timestamp:HH:mm:ss}] [{entry.Level}] {entry.Message}{Environment.NewLine}");
|
||||
_console.SelectionColor = _console.ForeColor;
|
||||
}
|
||||
|
||||
TrimConsole();
|
||||
}
|
||||
finally
|
||||
{
|
||||
SendMessage(_console.Handle, WmSetRedraw, (IntPtr)1, IntPtr.Zero);
|
||||
_console.Invalidate();
|
||||
}
|
||||
|
||||
if (wasNearBottom)
|
||||
{
|
||||
_console.SelectionStart = _console.TextLength;
|
||||
_console.SelectionLength = 0;
|
||||
_console.ScrollToCaret();
|
||||
return;
|
||||
}
|
||||
|
||||
var firstVisibleLineAfterAppend = GetFirstVisibleLine(_console);
|
||||
var linesToRestore = firstVisibleLineBeforeAppend - firstVisibleLineAfterAppend;
|
||||
if (linesToRestore != 0)
|
||||
{
|
||||
SendMessage(_console.Handle, EmLineScroll, IntPtr.Zero, (IntPtr)linesToRestore);
|
||||
}
|
||||
}
|
||||
|
||||
// Обрезаем старые строки, а не сбрасываем весь буфер — плавно и без рывка скролла.
|
||||
private void TrimConsole()
|
||||
{
|
||||
if (_console.TextLength <= ConsoleMaxChars)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var removeUpTo = _console.TextLength - ConsoleTrimToChars;
|
||||
var line = _console.GetLineFromCharIndex(removeUpTo);
|
||||
var cut = _console.GetFirstCharIndexFromLine(line + 1);
|
||||
if (cut <= 0)
|
||||
{
|
||||
cut = removeUpTo;
|
||||
}
|
||||
|
||||
_console.Select(0, cut);
|
||||
_console.SelectedText = string.Empty;
|
||||
_console.SelectionStart = _console.TextLength;
|
||||
_console.SelectionLength = 0;
|
||||
_console.SelectionColor = GetColor(entry.Level);
|
||||
_console.AppendText($"[{entry.Timestamp:HH:mm:ss}] [{entry.Level}] {entry.Message}{Environment.NewLine}");
|
||||
_console.SelectionColor = _console.ForeColor;
|
||||
_console.ScrollToCaret();
|
||||
}
|
||||
|
||||
private bool IsConsoleNearBottom()
|
||||
{
|
||||
if (_console.TextLength == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var bottomLeftPoint = new Point(1, Math.Max(0, _console.ClientSize.Height - 1));
|
||||
var lastVisibleCharIndex = _console.GetCharIndexFromPosition(bottomLeftPoint);
|
||||
return lastVisibleCharIndex >= _console.TextLength - 2;
|
||||
}
|
||||
|
||||
private static int GetFirstVisibleLine(RichTextBox richTextBox)
|
||||
{
|
||||
return SendMessage(richTextBox.Handle, EmGetFirstVisibleLine, IntPtr.Zero, IntPtr.Zero).ToInt32();
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
||||
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
private void ConfigureUpdateTimer()
|
||||
{
|
||||
_updateTimer.Interval = (int)TimeSpan.FromHours(1).TotalMilliseconds;
|
||||
@@ -203,6 +330,11 @@ namespace MonsterMonitor.UI
|
||||
|
||||
await _updateService.CheckAndPrepareUpdateAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// async void (Timer.Tick) — необработанное исключение уронило бы приложение.
|
||||
_log.Error("Ошибка проверки обновлений: " + ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isUpdateCheckRunning = false;
|
||||
@@ -230,18 +362,25 @@ namespace MonsterMonitor.UI
|
||||
return ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// MainForm
|
||||
//
|
||||
this.ClientSize = new System.Drawing.Size(284, 261);
|
||||
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
|
||||
this.Name = "MainForm";
|
||||
this.ResumeLayout(false);
|
||||
if (disposing)
|
||||
{
|
||||
_log.LogReceived -= AppendLog;
|
||||
|
||||
_logFlushTimer.Stop();
|
||||
_logFlushTimer.Dispose();
|
||||
_updateTimer.Stop();
|
||||
_updateTimer.Dispose();
|
||||
|
||||
_controller?.Dispose();
|
||||
|
||||
_notifyIcon.Visible = false;
|
||||
_notifyIcon.Dispose();
|
||||
_trayIcon?.Dispose();
|
||||
}
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,14 +194,14 @@ namespace MonsterMonitor.UI
|
||||
private void LoadValues()
|
||||
{
|
||||
_txtHost.Text = _settings.SshHost;
|
||||
_numSshPort.Value = _settings.SshPort;
|
||||
_numSshPort.Value = Clamp(_numSshPort, _settings.SshPort);
|
||||
_txtUser.Text = _settings.SshUsername;
|
||||
_txtPassword.Text = _settings.GetPassword();
|
||||
_chkSavePassword.Checked = _settings.SavePassword;
|
||||
_numRemotePort.Value = _settings.RemotePort;
|
||||
_numLocalPort.Value = _settings.LocalPort;
|
||||
_numMaxFailures.Value = _settings.MaxPingFailures;
|
||||
_numReconnectTimeout.Value = _settings.ReconnectTimeoutSec;
|
||||
_numRemotePort.Value = Clamp(_numRemotePort, _settings.RemotePort);
|
||||
_numLocalPort.Value = Clamp(_numLocalPort, _settings.LocalPort);
|
||||
_numMaxFailures.Value = Clamp(_numMaxFailures, _settings.MaxPingFailures);
|
||||
_numReconnectTimeout.Value = Clamp(_numReconnectTimeout, _settings.ReconnectTimeoutSec);
|
||||
_txtProxy.Text = _settings.Proxy;
|
||||
_txtSsPath.Text = _settings.SsProcessPath;
|
||||
_txtSsArgs.Text = _settings.SsArguments;
|
||||
@@ -209,6 +209,23 @@ namespace MonsterMonitor.UI
|
||||
_txtThreeProxyPassword.Text = _settings.GetThreeProxyPassword();
|
||||
}
|
||||
|
||||
// Приводит значение из настроек к допустимому диапазону контрола,
|
||||
// иначе NumericUpDown.Value бросит ArgumentOutOfRangeException и окно не откроется.
|
||||
private static decimal Clamp(NumericUpDown control, int value)
|
||||
{
|
||||
if (value < control.Minimum)
|
||||
{
|
||||
return control.Minimum;
|
||||
}
|
||||
|
||||
if (value > control.Maximum)
|
||||
{
|
||||
return control.Maximum;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private void SaveAndClose()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_txtHost.Text))
|
||||
|
||||
Reference in New Issue
Block a user