Skip to content

Commit

Permalink
Applied IntelliSense suggestions to simplify the code.
Browse files Browse the repository at this point in the history
Added script to update mbedTLS binaries
  • Loading branch information
DmitriySalnikov committed Jan 19, 2024
1 parent 5822446 commit 410110b
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 158 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dotnet-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: 🛠️ .NET Desktop

on:
push:
paths-ignore: [HexEditorStructures/**, Images/**, mbedTLS_AES/**, Bruteforcer/**, Tests/**, "*.*"]
paths-ignore: [HexEditorStructures/**, Images/**, Core/mbedTLS_AES/**, "*.*", ".github/**/util_*"]
pull_request:
paths-ignore: [HexEditorStructures/**, Images/**, mbedTLS_AES/**, Bruteforcer/**, Tests/**, "*.*"]
paths-ignore: [HexEditorStructures/**, Images/**, Core/mbedTLS_AES/**, "*.*", ".github/**/util_*"]

# Stop the same workflow actions
concurrency:
Expand Down
111 changes: 111 additions & 0 deletions .github/workflows/util_update_mbed_tls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: ↗️ Update mbedTLS

on:
workflow_dispatch:

# Stop the same workflow actions
concurrency:
group: ${{github.workflow}}-${{github.ref}}
cancel-in-progress: true

env:
OUTPUT_DIR: Core/GodotPCKExplorer/mbedTLS
PROJECT_DIR: Core/mbedTLS_AES

jobs:
build:
name: Build mbedTLS (${{matrix.os}})
runs-on: ${{matrix.runner}}

strategy:
fail-fast: false
matrix:
include:
- runner: windows-latest
os: win
sciprt: build_mbedTLS_AES.bat
- runner: ubuntu-20.04
os: linux
sciprt: build_mbedTLS_AES_linux.sh
- runner: macos-latest
os: mac
sciprt: build_mbedTLS_AES_mac.sh

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: recursive

- name: Clean up old binaries
shell: bash
run: rm -rf ${{env.OUTPUT_DIR}}/**/*

# Add MSBuild to the PATH: https://github.com/microsoft/setup-msbuild
- name: Setup MSBuild.exe
if: matrix.os == 'win'
uses: microsoft/setup-msbuild@v1.3.1

- name: Setup 32-bit libs
if: matrix.os == 'linux'
run: |
sudo apt install gcc-multilib g++-multilib
- name: Build mbedTLS_AES
shell: bash
run: |
cd ${{env.PROJECT_DIR}}
sh_files=$(find . -mindepth 1 -maxdepth 1 -type f)
for file in $sh_files; do
folder_basename=$(basename "$file")
if [[ "$folder_basename" == *".sh" ]]; then
if [ "${{matrix.os}}" == "linux" ]; then
chmod +x "$file"
elif [ "${{matrix.os}}" == "mac" ]; then
chmod 755 "$file"
fi
fi
done
./${{matrix.sciprt}}
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: lib_${{matrix.os}}
path: ${{env.OUTPUT_DIR}}/${{matrix.os}}*/

update:
name: Update binaries
needs: build
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Remove old binaries
shell: bash
run: rm -rf ${{env.OUTPUT_DIR}}/**/*

- name: Download Binaries
uses: actions/download-artifact@v4
with:
path: ${{env.OUTPUT_DIR}}/
merge-multiple: true

- name: Commit and report
shell: bash
run: |
git config --global user.name 'github-actions-auto-updater[bot]'
git config --global user.email 'github-actions-auto-updater[bot]@users.noreply.github.com'
git diff
git add -A
git commit -am "[CI] ↗️ Updated mbedTLS Binaries: ${{github.sha}}"
git push
echo "## Changed files:" >> $GITHUB_STEP_SUMMARY
codeblock_tmp=$'```\nSTATS\n```'
echo "${codeblock_tmp//STATS/$(git diff --stat HEAD~)}" >> $GITHUB_STEP_SUMMARY
5 changes: 3 additions & 2 deletions Bruteforcer/Console/ConsoleCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace PCKBruteforcer.Cmd
public static class ConsoleCommands
{
static bool runWithArgs = false;
private static readonly string[] yesVariants = ["y", "yes", "true"];

public static bool RunCommand(string[] args)
{
Expand All @@ -17,7 +18,7 @@ public static bool RunCommand(string[] args)
try
{
if (args.Length > 0)
if (Path.GetFullPath(args[0]) == AppContext.BaseDirectory)
if (Path.GetFullPath(args[0]) == Path.GetFullPath(AppContext.BaseDirectory))
args = args.Skip(1).ToArray();
}
catch (Exception ex)
Expand Down Expand Up @@ -113,7 +114,7 @@ static void BruteforceCommand(string[] args)
threads = int.Parse(args[5]);
if (args.Length > 6)
{
inMem = new string[] { "y", "yes", "true" }.Contains(args[6].ToLower());
inMem = yesVariants.Contains(args[6].ToLower());
}
}
}
Expand Down
93 changes: 28 additions & 65 deletions Bruteforcer/PCKBruteforcer/Bruteforcer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,78 +14,41 @@ public class Bruteforcer

public double ReportUpdateInterval = 0.25;

struct StartData
struct StartData(string exe, string pck, bool encIndex, long startPos, long endPos, CancellationTokenSource ct, int threadIdx, string? testFileName, PCKFile? testFile, MemoryStream? memStr)
{
public string exe;
public string pck;
public bool encIndex;
public long startPos;
public long endPos;
public CancellationTokenSource ct;
public Action<int, long>? reportProgress;
public int threadIdx;
public string? testFileName = null;
public PCKFile? testFile = null;
public MemoryStream? memStr = null;

public StartData(string exe, string pck, bool encIndex, long startPos, long endPos, CancellationTokenSource ct, int threadIdx, string? testFileName, PCKFile? testFile, MemoryStream? memStr)
{
this.exe = exe;
this.pck = pck;
this.encIndex = encIndex;
this.startPos = startPos;
this.endPos = endPos;
this.ct = ct;
this.reportProgress = null;
this.threadIdx = threadIdx;
this.testFileName = testFileName;
this.testFile = testFile;
this.memStr = memStr;
}
public string exe = exe;
public string pck = pck;
public bool encIndex = encIndex;
public long startPos = startPos;
public long endPos = endPos;
public CancellationTokenSource ct = ct;
public Action<int, long>? reportProgress = null;
public int threadIdx = threadIdx;
public string? testFileName = testFileName;
public PCKFile? testFile = testFile;
public MemoryStream? memStr = memStr;
}

public struct ResultData
public struct ResultData(bool result, string key, long address)
{
public bool found;
public string key;
public long address;

public ResultData(bool result, string key, long address)
{
this.found = result;
this.key = key;
this.address = address;
}
public bool found = result;
public string key = key;
public long address = address;
}

public struct ReportData
public struct ReportData(TimeSpan elapsedTime, TimeSpan remainingTime, double progressPercent, ThreadProgressData[] threadsData)
{
public TimeSpan ElapsedTime;
public TimeSpan RemainingTime;
public double ProgressPercent;
public ThreadProgressData[] ThreadsData;

public ReportData(TimeSpan elapsedTime, TimeSpan remainingTime, double progressPercent, ThreadProgressData[] threadsData)
{
ElapsedTime = elapsedTime;
RemainingTime = remainingTime;
ProgressPercent = progressPercent;
ThreadsData = threadsData;
}
public TimeSpan ElapsedTime = elapsedTime;
public TimeSpan RemainingTime = remainingTime;
public double ProgressPercent = progressPercent;
public ThreadProgressData[] ThreadsData = threadsData;
}

public struct ThreadProgressData
public struct ThreadProgressData(int progress, string text, Color color)
{
public int progress;
public string text;
public Color color;

public ThreadProgressData(int progress, string text, Color color)
{
this.progress = progress;
this.text = text;
this.color = color;
}
public int progress = progress;
public string text = text;
public Color color = color;
}

readonly Action? disablePCKLogs_cb;
Expand Down Expand Up @@ -272,7 +235,7 @@ public void Start(string exe, string pck, long startAdr, long endAdr, int thread
fileReader.BaseStream.Position = pck_in_memory_file.Offset;

// Read encrypted header
using var encReader = new PCKEncryptedReader(fileReader, Array.Empty<byte>());
using var encReader = new PCKEncryptedReader(fileReader, []);
// Restore position to header start
fileReader.BaseStream.Position = pck_in_memory_file.Offset;

Expand All @@ -287,7 +250,7 @@ public void Start(string exe, string pck, long startAdr, long endAdr, int thread
return;
}

List<Thread> threads = new();
List<Thread> threads = [];
ResultData[] thread_results = new ResultData[threadsCount];
ThreadProgressData[] thread_progress = new ThreadProgressData[threadsCount];
object task_progress_mutex = new();
Expand Down Expand Up @@ -414,7 +377,7 @@ public void Start(string exe, string pck, long startAdr, long endAdr, int thread
prev_percent = perc;
prev_time = DateTime.UtcNow;
safe_thread_progress = thread_progress.ToArray();
safe_thread_progress = [.. thread_progress];
ReportProgress(new ReportData(elapsed, remaining_time, perc, safe_thread_progress));
Expand Down
4 changes: 2 additions & 2 deletions Bruteforcer/UI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ internal partial class BruteforcerMainForm : Form
[DllImport("user32.dll")]
static extern int FlashWindow(IntPtr Hwnd, bool Revert);

readonly List<Control> browse_buttons = new();
readonly List<Control> browse_buttons = [];
CancellationTokenSource? cancellationToken = null;
Bruteforcer? bruteforcer;
Task? bgTask = null;

readonly ProgressReporterBrute progress;
readonly List<ProgressBarEx> progressBars = new();
readonly List<ProgressBarEx> progressBars = [];
static readonly int ProgressRowHeight = 20;

public BruteforcerMainForm()
Expand Down
4 changes: 2 additions & 2 deletions Core/Global.Shared/DeferredAction.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace GodotPCKExplorer.GlobalShared
{
public class DeferredAction : IDisposable
public sealed class DeferredAction : IDisposable
{
System.Threading.Timer? close_timer = null;
Action action;
readonly Action action;
readonly int delay = 1000;

public DeferredAction(Action action, int delay = 1000)
Expand Down
2 changes: 1 addition & 1 deletion Core/Global.Shared/GlobalConstants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace GodotPCKExplorer.GlobalShared
{
public class GlobalConstants
public static class GlobalConstants
{
public static readonly string ProjectName = "GodotPCKExplorer";
public static readonly string AppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ProjectName);
Expand Down
6 changes: 4 additions & 2 deletions Core/GodotPCKExplorer/PCKActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ public static void Init(IPCKProgressReporter progressReporter)
/// <param name="logPrinter">Custom action for printing log texts.</param>
public static void Init(Action<string> logPrinter)
{
var rep = new BasicPCKProgressReporter();
rep.PrintLogText = logPrinter;
var rep = new BasicPCKProgressReporter
{
PrintLogText = logPrinter
};
progress = rep;
LoadNativeLibs();
}
Expand Down
2 changes: 1 addition & 1 deletion Core/GodotPCKExplorer/PCKEncryptedReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace GodotPCKExplorer
{
public class PCKEncryptedReader : IDisposable
public sealed class PCKEncryptedReader : IDisposable
{
[ThreadStatic]
static byte[]? temp_encryption_buffer;
Expand Down
2 changes: 1 addition & 1 deletion Core/GodotPCKExplorer/PCKFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace GodotPCKExplorer
{
public class PCKFile
public sealed class PCKFile
{
private readonly BinaryReader reader;
/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions Core/GodotPCKExplorer/PCKPacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;

Expand Down Expand Up @@ -34,7 +33,7 @@ public virtual IEnumerable<ReadOnlyMemory<byte>> ReadMemoryBlocks()
}
}

public class PCKPackerRegularFile : PCKPackerFile
public sealed class PCKPackerRegularFile : PCKPackerFile
{
public string OriginalPath;

Expand Down
4 changes: 2 additions & 2 deletions Core/GodotPCKExplorer/PCKReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct PCKReaderEncryptionKeyResult
public bool IsCancelled;
}

public class PCKReader : IDisposable
public sealed class PCKReader : IDisposable
{
BinaryReader? binReader = null;
public Dictionary<string, PCKFile> Files = new Dictionary<string, PCKFile>();
Expand All @@ -33,7 +33,7 @@ public class PCKReader : IDisposable
public long PCK_EndPosition = 0;
public bool PCK_Embedded = false;

public byte[]? ReceivedEncryptionKey { get; protected set; } = null;
public byte[]? ReceivedEncryptionKey { get; set; } = null;

public PCKVersion PCK_Version { get { return new PCKVersion(PCK_VersionPack, PCK_VersionMajor, PCK_VersionMinor, PCK_VersionRevision); } }
public bool IsOpened { get { return binReader != null; } }
Expand Down
2 changes: 1 addition & 1 deletion Core/GodotPCKExplorer/mbedTLS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GodotPCKExplorer
{
public class mbedTLS : IDisposable
public sealed class mbedTLS : IDisposable
{
const string LIB_NAME = "mbedTLS_AES";
public const int CHUNK_SIZE = 16;
Expand Down
Loading

0 comments on commit 410110b

Please sign in to comment.