Fix more warnings and upgrade CommandLineParser

This commit is contained in:
Ben Olden-Cooligan 2022-06-19 10:51:40 -07:00
parent 9e239f3301
commit 88ae3938de
9 changed files with 28 additions and 56 deletions

View File

@ -0,0 +1,5 @@
// https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb
// ReSharper disable once CheckNamespace
namespace System.Runtime.CompilerServices;
public static class IsExternalInit {}

View File

@ -2,7 +2,7 @@
namespace NAPS2.Automation;
public class AutomatedScanningOptions : CommandLineOptions
public class AutomatedScanningOptions
{
#region General Options
@ -38,10 +38,10 @@ public class AutomatedScanningOptions : CommandLineOptions
" If not specified, no output is displayed if the scan is successful.")]
public bool Verbose { get; set; }
[Option('n', "number", DefaultValue = 1, HelpText = "The number of scans to perform.")]
[Option('n', "number", Default = 1, HelpText = "The number of scans to perform.")]
public int Number { get; set; }
[Option('d', "delay", DefaultValue = 0, HelpText = "The delay (in milliseconds) between each scan.")]
[Option('d', "delay", Default = 0, HelpText = "The delay (in milliseconds) between each scan.")]
public int Delay { get; set; }
[Option('f', "force", HelpText = "Overwrite existing files." +
@ -174,7 +174,7 @@ public class AutomatedScanningOptions : CommandLineOptions
#region Image Options
[Option("jpegquality", DefaultValue = 75, HelpText = "The quality of saved JPEG files (0-100, default 75).")]
[Option("jpegquality", Default = 75, HelpText = "The quality of saved JPEG files (0-100, default 75).")]
public int JpegQuality { get; set; }
[Option("tiffcomp", HelpText = "The compression to use for TIFF files. Possible values: auto, lzw, ccitt4, none")]

View File

@ -1,16 +0,0 @@
using CommandLine;
using CommandLine.Text;
namespace NAPS2.Automation;
public class CommandLineOptions
{
[ParserState]
public IParserState LastParserState { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this, current => HelpText.DefaultParsingErrorsHandler(this, current));
}
}

View File

@ -1,4 +1,5 @@
using NAPS2.Automation;
using CommandLine;
using NAPS2.Automation;
using NAPS2.Modules;
using NAPS2.Remoting.Worker;
using Ninject;
@ -19,12 +20,12 @@ public static class ConsoleEntryPoint
Paths.ClearTemp();
// Parse the command-line arguments (and display help text if appropriate)
var options = new AutomatedScanningOptions();
if (!CommandLine.Parser.Default.ParseArguments(args, options))
var options = Parser.Default.ParseArguments<AutomatedScanningOptions>(args).Value;
if (options == null)
{
return;
}
// Start a pending worker process
kernel.Get<IWorkerFactory>().Init();

View File

@ -18,7 +18,7 @@
<!-- TODO: Do we want to remove this reference? -->
<ProjectReference Include="..\NAPS2.Lib.WinForms\NAPS2.Lib.WinForms.csproj" />
<Reference Include="System.Windows.Forms" />
<PackageReference Include="CommandLineParser" Version="1.9.71" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,5 @@
using System.Security.Cryptography.X509Certificates;
using NAPS2.Remoting.Network.Internal;
using Org.BouncyCastle.OpenSsl;
using Xunit;
@ -9,7 +10,7 @@ public class SslHelperTests
[Fact]
public void GenerateRootCertificate()
{
var (cert, privateKey) = SslHelper.GenerateRootCertificate();
var cert = SslHelper.GeneratePublicKeyString();
var reader = new PemReader(new StringReader(cert));
var obj = reader.ReadPemObject();

View File

@ -6,24 +6,30 @@ using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
namespace NAPS2.Util;
namespace NAPS2.Remoting.Network.Internal;
public static class SslHelper
{
private static readonly SecureRandom Random = new SecureRandom();
private static readonly SecureRandom Random = new();
private static readonly Lazy<RsaKeyPairGenerator> RsaKeyPairGenerator = new Lazy<RsaKeyPairGenerator>(() =>
private static readonly Lazy<RsaKeyPairGenerator> RsaKeyPairGenerator = new(() =>
{
var gen = new RsaKeyPairGenerator();
gen.Init(new KeyGenerationParameters(Random, 2048));
return gen;
});
public static (string cert, string privateKey) GenerateRootCertificate()
public static string GeneratePublicKeyString()
{
var keyPair = RsaKeyPairGenerator.Value.GenerateKeyPair();
return (GetPemString(GenerateCert(keyPair)), GetPemString(keyPair.Private));
return GetPemString(GenerateCert(keyPair));
}
// public static (string cert, string privateKey) GenerateRootCertificate()
// {
// var keyPair = RsaKeyPairGenerator.Value.GenerateKeyPair();
// return (GetPemString(GenerateCert(keyPair)), GetPemString(keyPair.Private));
// }
//public static (string cert, string privateKey) GenerateCertificateChain(string rootCert, string rootPrivateKey)
//{

View File

@ -15,10 +15,6 @@
<HintPath>..\NAPS2.Setup\lib\PdfSharp.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms" />
<ProjectReference Include="..\NAPS2.Sdk.Network\NAPS2.Sdk.Network.csproj" />
<ProjectReference Include="..\NAPS2.Sdk\NAPS2.Sdk.csproj" />
<PackageReference Include="Moq" Version="4.18.1" />

View File

@ -1,21 +0,0 @@
using System.Security.Cryptography.X509Certificates;
using Org.BouncyCastle.OpenSsl;
using Xunit;
namespace NAPS2.Sdk.Tests.Util;
public class SslHelperTests
{
[Fact]
public void GenerateRootCertificate()
{
var (cert, privateKey) = SslHelper.GenerateRootCertificate();
var reader = new PemReader(new StringReader(cert));
var obj = reader.ReadPemObject();
Assert.Equal("CERTIFICATE", obj.Type);
var certObj = new X509Certificate(obj.Content);
Assert.Equal("CN=localhost", certObj.Issuer);
Assert.Equal("CN=localhost", certObj.Subject);
}
}