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