Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions ProtectionScan/Features/MainFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ internal sealed class MainFeature : Feature
private const string _fileOnlyName = "file-only";
internal readonly FlagInput FileOnlyInput = new(_fileOnlyName, ["-f", "--file"], "Print to file only");

#if NETCOREAPP
private const string _jsonName = "json";
internal readonly FlagInput JsonInput = new(_jsonName, ["-j", "--json"], "Output to json file");
#endif

private const string _noArchivesName = "no-archives";
internal readonly FlagInput NoArchivesInput = new(_noArchivesName, ["-na", "--no-archives"], "Disable scanning archives");

Expand All @@ -47,6 +52,11 @@ internal sealed class MainFeature : Feature
/// Output information to file only, skip printing to console
/// </summary>
public bool FileOnly { get; private set; }

/// <summary>
/// Output information to json
/// </summary>
public bool JsonFlag { get; private set; }

public MainFeature()
: base(DisplayName, _flags, _description)
Expand All @@ -55,6 +65,9 @@ public MainFeature()

Add(DebugInput);
Add(FileOnlyInput);
#if NETCOREAPP
Add(JsonInput);
#endif
Add(NoContentsInput);
Add(NoArchivesInput);
Add(NoPathsInput);
Expand All @@ -70,6 +83,9 @@ public override bool Execute()

// Get the options from the arguments
FileOnly = GetBoolean(_fileOnlyName);
#if NETCOREAPP
JsonFlag = GetBoolean(_jsonName);
#endif

// Create scanner for all paths
var scanner = new Scanner(
Expand Down Expand Up @@ -127,7 +143,12 @@ private void GetAndWriteProtections(Scanner scanner, string path)
try
{
var protections = scanner.GetProtections(path);

WriteProtectionResultFile(path, protections);

#if NETCOREAPP
WriteProtectionResultJson(path, protections);
#endif
}
catch (Exception ex)
{
Expand Down Expand Up @@ -199,5 +220,45 @@ private void WriteProtectionResultFile(string path, Dictionary<string, List<stri
// Dispose of the writer
sw?.Dispose();
}

#if NETCOREAPP
/// <summary>
/// Write the protection results from a single path to a json file, if possible
/// </summary>
/// <param name="path">File or directory path</param>
/// <param name="protections">Dictionary of protections found, if any</param>
private static void WriteProtectionResultJson(string path, Dictionary<string, List<string>> protections)
{
if (protections == null)
{
Console.WriteLine($"No protections found for {path}");
return;
}

// Attempt to open a protection file for writing
StreamWriter? jsw = null;
try
{
jsw = new StreamWriter(File.OpenWrite($"protection-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.json"));
// Create the output data
string serializedData = System.Text.Json.JsonSerializer.Serialize(protections, JsonSerializerOptions);

// Write the output data
// TODO: this prints plus symbols wrong, probably some other things too
jsw?.WriteLine(serializedData);
jsw?.Flush();

// Dispose of the writer
jsw?.Dispose();
}
catch { }
}

/// <summary>
/// JSON serializer options for output printing
/// </summary>
private static System.Text.Json.JsonSerializerOptions JsonSerializerOptions
=> new System.Text.Json.JsonSerializerOptions { WriteIndented = true };
#endif
}
}