| | 1 | | using Microsoft.Extensions.Hosting; |
| | 2 | | using NBitcoin; |
| | 3 | | using NLightning.Domain.Protocol.ValueObjects; |
| | 4 | | using NLightning.Infrastructure.Bitcoin.Managers; |
| | 5 | | using NLightning.Node.Extensions; |
| | 6 | | using NLightning.Node.Helpers; |
| | 7 | | using NLightning.Node.Utilities; |
| | 8 | | using Serilog; |
| | 9 | |
|
| | 10 | | try |
| | 11 | | { |
| | 12 | | // Bootstrap logger for startup messages |
| 0 | 13 | | Log.Logger = new LoggerConfiguration() |
| 0 | 14 | | .WriteTo.Console() |
| 0 | 15 | | .CreateBootstrapLogger(); |
| | 16 | |
|
| 0 | 17 | | AppDomain.CurrentDomain.UnhandledException += (_, e) => |
| 0 | 18 | | { |
| 0 | 19 | | var exception = (Exception)e.ExceptionObject; |
| 0 | 20 | | Log.Logger.Error("An unhandled exception occurred: {exception}", exception); |
| 0 | 21 | | }; |
| | 22 | |
|
| | 23 | | // Get network for the PID file path |
| 0 | 24 | | var network = CommandLineHelper.GetNetwork(args); |
| 0 | 25 | | var pidFilePath = DaemonUtils.GetPidFilePath(network); |
| | 26 | |
|
| | 27 | | // Check for the stop command |
| 0 | 28 | | if (CommandLineHelper.IsStopRequested(args)) |
| | 29 | | { |
| 0 | 30 | | var stopped = DaemonUtils.StopDaemon(pidFilePath, Log.Logger); |
| 0 | 31 | | return stopped ? 0 : 1; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | // Check for status command |
| 0 | 35 | | if (CommandLineHelper.IsStatusRequested(args)) |
| | 36 | | { |
| 0 | 37 | | ReportDaemonStatus(pidFilePath); |
| 0 | 38 | | return 0; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | // Check if help is requested |
| 0 | 42 | | if (CommandLineHelper.IsHelpRequested(args)) |
| | 43 | | { |
| 0 | 44 | | CommandLineHelper.ShowUsage(); |
| 0 | 45 | | return 0; |
| | 46 | | } |
| | 47 | |
|
| | 48 | | // Read the configuration file to check for daemon setting |
| 0 | 49 | | var initialConfig = NodeConfigurationExtensions.ReadInitialConfiguration(args); |
| | 50 | |
|
| 0 | 51 | | string? password = null; |
| | 52 | |
|
| | 53 | | // Try to get password from args or prompt |
| 0 | 54 | | if (args.Contains("--password")) |
| | 55 | | { |
| 0 | 56 | | var idx = Array.IndexOf(args, "--password"); |
| 0 | 57 | | if (idx >= 0 && idx + 1 < args.Length) |
| 0 | 58 | | password = args[idx + 1]; |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | if (string.IsNullOrWhiteSpace(password)) |
| | 62 | | { |
| 0 | 63 | | password = ConsoleUtils.ReadPassword("Enter password for key encryption: "); |
| | 64 | | } |
| | 65 | |
|
| 0 | 66 | | if (string.IsNullOrWhiteSpace(password)) |
| | 67 | | { |
| 0 | 68 | | Log.Error("Password cannot be empty."); |
| 0 | 69 | | return 1; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | SecureKeyManager keyManager; |
| 0 | 73 | | var keyFilePath = SecureKeyManager.GetKeyFilePath(network); |
| 0 | 74 | | if (!File.Exists(keyFilePath)) |
| | 75 | | { |
| | 76 | | // Creates new key |
| 0 | 77 | | var key = new Key(); |
| 0 | 78 | | keyManager = new SecureKeyManager(key.ToBytes(), new BitcoinNetwork(network), keyFilePath); |
| 0 | 79 | | keyManager.SaveToFile(password); |
| 0 | 80 | | Console.WriteLine($"New key created and saved to {keyFilePath}"); |
| | 81 | | } |
| | 82 | | else |
| | 83 | | { |
| | 84 | | // Load the existing key |
| 0 | 85 | | keyManager = SecureKeyManager.FromFilePath(keyFilePath, new BitcoinNetwork(network), password); |
| 0 | 86 | | Console.WriteLine($"Loaded key from {keyFilePath}"); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | // Start as a daemon if requested |
| 0 | 90 | | if (DaemonUtils.StartDaemonIfRequested(args, initialConfig, pidFilePath, Log.Logger)) |
| | 91 | | { |
| | 92 | | // The parent process exits immediately after starting the daemon |
| 0 | 93 | | return 0; |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | Log.Information("Starting NLTG..."); |
| | 97 | |
|
| | 98 | | // Create and run host |
| 0 | 99 | | var host = Host.CreateDefaultBuilder(args) |
| 0 | 100 | | .ConfigureNltg(initialConfig) |
| 0 | 101 | | .ConfigureNltgServices(keyManager) |
| 0 | 102 | | .Build(); |
| | 103 | |
|
| | 104 | | // Run migrations if configured |
| 0 | 105 | | await host.MigrateDatabaseIfConfiguredAsync(); |
| | 106 | |
|
| | 107 | | // Run the host |
| 0 | 108 | | await host.RunAsync(); |
| | 109 | |
|
| 0 | 110 | | return 0; |
| | 111 | | } |
| | 112 | | catch (Exception e) |
| | 113 | | { |
| 0 | 114 | | Log.Fatal(e, "Application terminated unexpectedly"); |
| 0 | 115 | | return 1; |
| | 116 | | } |
| | 117 | | finally |
| | 118 | | { |
| 0 | 119 | | Log.CloseAndFlush(); |
| | 120 | | } |
| | 121 | |
|
| | 122 | | static void ReportDaemonStatus(string pidFilePath) |
| | 123 | | { |
| | 124 | | try |
| | 125 | | { |
| 0 | 126 | | if (!File.Exists(pidFilePath)) |
| | 127 | | { |
| 0 | 128 | | Console.WriteLine("NLTG daemon is not running"); |
| 0 | 129 | | return; |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | var pidText = File.ReadAllText(pidFilePath).Trim(); |
| 0 | 133 | | if (!int.TryParse(pidText, out var pid)) |
| | 134 | | { |
| 0 | 135 | | Console.WriteLine("Invalid PID in file, daemon may not be running"); |
| 0 | 136 | | return; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | try |
| | 140 | | { |
| 0 | 141 | | var process = System.Diagnostics.Process.GetProcessById(pid); |
| 0 | 142 | | var runTime = DateTime.Now - process.StartTime; |
| | 143 | |
|
| 0 | 144 | | Console.WriteLine("NLTG daemon is running"); |
| 0 | 145 | | Console.WriteLine($"PID: {pid}"); |
| 0 | 146 | | Console.WriteLine($"Started: {process.StartTime}"); |
| 0 | 147 | | Console.WriteLine($"Uptime: {runTime.Days}d {runTime.Hours}h {runTime.Minutes}m"); |
| 0 | 148 | | Console.WriteLine($"Memory: {process.WorkingSet64 / (1024 * 1024)} MB"); |
| 0 | 149 | | } |
| 0 | 150 | | catch (ArgumentException) |
| | 151 | | { |
| 0 | 152 | | Console.WriteLine("NLTG daemon is not running (stale PID file)"); |
| 0 | 153 | | } |
| 0 | 154 | | } |
| 0 | 155 | | catch (Exception e) |
| | 156 | | { |
| 0 | 157 | | Console.WriteLine($"Error checking daemon status: {e.Message}"); |
| 0 | 158 | | } |
| 0 | 159 | | } |