|
2 | 2 | using System.Application.Services; |
3 | 3 | using System.CommandLine; |
4 | 4 | using System.CommandLine.NamingConventionBinder; |
| 5 | +using System.Runtime.InteropServices; |
5 | 6 | using LogLevel = Microsoft.Extensions.Logging.LogLevel; |
| 7 | +using System.Runtime.Versioning; |
| 8 | +using System.Application.Settings; |
| 9 | +using System.Security.Cryptography.X509Certificates; |
| 10 | +using System.Security.Cryptography; |
6 | 11 |
|
7 | 12 | namespace System.Application.CommandLine; |
8 | 13 |
|
@@ -227,6 +232,67 @@ void MainHandlerByCLT_(Action? onInitStartuped) |
227 | 232 | }); |
228 | 233 | rootCommand.AddCommand(run_SteamApp); |
229 | 234 |
|
| 235 | + // -clt show -config -cert |
| 236 | + var show = new Command("show", "显示信息"); |
| 237 | + show.AddOption(new Option<bool>("-config", "显示 Config.mpo 值")); |
| 238 | + show.AddOption(new Option<bool>("-cert", "显示当前根证书信息")); |
| 239 | + show.Handler = CommandHandler.Create((bool config, bool cert) => |
| 240 | + { |
| 241 | + if (OperatingSystem.IsWindows()) if (!AttachConsole()) AllocConsole(); |
| 242 | + |
| 243 | + if (config) |
| 244 | + { |
| 245 | + Console.WriteLine("Config: "); |
| 246 | + try |
| 247 | + { |
| 248 | + SettingsHost.Load(); |
| 249 | + var configValue = SettingsHostBase.Local.ToJsonString(); |
| 250 | + Console.WriteLine(configValue); |
| 251 | + } |
| 252 | + catch (Exception e) |
| 253 | + { |
| 254 | + Console.WriteLine(e.ToString()); |
| 255 | + } |
| 256 | + Console.WriteLine(); |
| 257 | + } |
| 258 | + |
| 259 | + if (cert) |
| 260 | + { |
| 261 | + Console.WriteLine("RootCertificate: "); |
| 262 | + try |
| 263 | + { |
| 264 | + using X509Certificate2 rootCert = new(ICertificateManager.DefaultPfxFilePath, (string?)null, X509KeyStorageFlags.Exportable); |
| 265 | + Console.WriteLine("Subject:"); |
| 266 | + Console.WriteLine(rootCert.Subject); |
| 267 | + Console.WriteLine("SerialNumber:"); |
| 268 | + Console.WriteLine(rootCert.SerialNumber); |
| 269 | + Console.WriteLine("PeriodValidity:"); |
| 270 | + Console.Write(rootCert.GetEffectiveDateString()); |
| 271 | + Console.Write(" ~ "); |
| 272 | + Console.Write(rootCert.GetExpirationDateString()); |
| 273 | + Console.WriteLine(); |
| 274 | + Console.WriteLine("SHA256:"); |
| 275 | + Console.WriteLine(rootCert.GetCertHashStringCompat(HashAlgorithmName.SHA256)); |
| 276 | + Console.WriteLine("SHA1:"); |
| 277 | + Console.WriteLine(rootCert.GetCertHashStringCompat(HashAlgorithmName.SHA1)); |
| 278 | + } |
| 279 | + catch (Exception e) |
| 280 | + { |
| 281 | + Console.WriteLine(e.ToString()); |
| 282 | + } |
| 283 | + Console.WriteLine(); |
| 284 | + } |
| 285 | + |
| 286 | + Console.WriteLine("Press any key to exit"); |
| 287 | + Console.ReadKey(); |
| 288 | + |
| 289 | + if (OperatingSystem.IsWindows()) FreeConsole(); |
| 290 | + }); |
| 291 | + rootCommand.AddCommand(show); |
| 292 | + |
| 293 | + // TODO |
| 294 | + //var proxy = new Command("proxy", "仅启用代理服务,无 GUI 窗口"); |
| 295 | + |
230 | 296 | var r = rootCommand.InvokeAsync(args).GetAwaiter().GetResult(); |
231 | 297 | return r; |
232 | 298 | } |
@@ -261,4 +327,16 @@ public void Dispose() |
261 | 327 | Dispose(disposing: true); |
262 | 328 | GC.SuppressFinalize(this); |
263 | 329 | } |
| 330 | + |
| 331 | + [SupportedOSPlatform("Windows")] |
| 332 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 333 | + static extern bool AttachConsole(int dwProcessId = -1); |
| 334 | + |
| 335 | + [SupportedOSPlatform("Windows")] |
| 336 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 337 | + static extern bool FreeConsole(); |
| 338 | + |
| 339 | + [SupportedOSPlatform("Windows")] |
| 340 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 341 | + static extern bool AllocConsole(); |
264 | 342 | } |
0 commit comments