Skip to content

Commit

Permalink
Add keygen
Browse files Browse the repository at this point in the history
  • Loading branch information
l0go committed Jul 24, 2024
1 parent 980368e commit 8badf54
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 4 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ jobs:
working-directory: .
run: |
sudo apt install -y podman
- name: Build container
- name: Build HaxeSandbox container
working-directory: .
run: |
podman manifest create haxesandbox
podman build --platform linux/amd64 --manifest haxesandbox .
podman build --platform linux/amd64 -t=haxesandbox .
- name: Build keygen container
working-directory: ./tools/keygen/
run: |
podman build --platform linux/amd64 -t=haxesandbox-keygen .
- name: Log in to Github Packages
uses: redhat-actions/podman-login@v1
with:
Expand All @@ -33,3 +36,8 @@ jobs:
with:
image: haxesandbox
registry: ghcr.io/l0go
- name: Push keygen to Github Packages
uses: redhat-actions/push-to-registry@v2
with:
image: haxesandbox-keygen
registry: ghcr.io/l0go
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bin/
.env
.zig-cache/
zig-out/
2 changes: 1 addition & 1 deletion Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ RUN microdnf install -y haxe
WORKDIR /app/src
COPY src ./src/
COPY vendor ./vendor/
copy build.hxml ./
COPY build.hxml ./
RUN haxe build.hxml -w -WDeprecated

FROM quay.io/fedora/fedora-minimal
Expand Down
2 changes: 2 additions & 0 deletions tools/keygen/.containerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Containerfile
zig-out/
10 changes: 10 additions & 0 deletions tools/keygen/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM quay.io/fedora/fedora-minimal AS build
WORKDIR /app/src
COPY . ./
RUN microdnf install -y zig binutils
RUN zig build -Doptimize=ReleaseSmall
RUN strip ./zig-out/bin/haxesandbox-keygen

FROM scratch
COPY --from=build /app/src/zig-out/bin/haxesandbox-keygen /
CMD ["/haxesandbox-keygen"]
27 changes: 27 additions & 0 deletions tools/keygen/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const chameleon = b.dependency("chameleon", .{}).module("chameleon");
const exe = b.addExecutable(.{
.name = "haxesandbox-keygen",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("chameleon", chameleon);

b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());

if (b.args) |args| {
run_cmd.addArgs(args);
}

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
18 changes: 18 additions & 0 deletions tools/keygen/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.{
.name = "haxesandbox-keygen",
.version = "0.0.0",
.dependencies = .{
.chameleon = .{
.url = "https://github.com/tr1ckydev/chameleon/archive/e94bf213e542dbfa932469e921a4bd2b8f7bb8b1.zip",
.hash = "1220a497e4583061a32a500d6886bc440f34dc54ccd9c08b27769c583baff1c2954f",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
// For example...
//"LICENSE",
//"README.md",
},
}
50 changes: 50 additions & 0 deletions tools/keygen/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const std = @import("std");
const Chameleon = @import("chameleon").Chameleon;
const base64 = std.base64.standard;
const sha256 = std.crypto.hash.sha2.Sha256;

const length = 24;

pub fn main() !void {
// Create allocator
var ally = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer ally.deinit();

// Initialize stdout
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();

// Generate some random bytes to generate the base64 from
var bytes: [length]u8 = undefined;
std.crypto.random.bytes(&bytes);

// Generate the base64
var b64 = try ally.allocator().alloc(u8, base64.Encoder.calcSize(length));
b64 = @constCast(base64.Encoder.encode(b64, &bytes));

// And the sha256 hash
var hash: [32]u8 = undefined;
sha256.hash(b64, &hash, .{});

// Now print everything to the user
comptime var cham = Chameleon.init(.Auto);
try stdout.print(cham.redBright().fmt(
\\
\\ _ _ _____ _ _
\\| | | | / ___| | || |
\\| |_| | __ _ __ __ ___ \ `--. __ _ _ __ __| || |__ ___ __ __
\\| _ | / _` |\ \/ / / _ \ `--. \ / _` || '_ \ / _` || '_ \ / _ \ \ \/ /
\\| | | || (_| | > < | __//\__/ /| (_| || | | || (_| || |_) || (_) | > <
\\\_| |_/ \__,_|/_/\_\ \___|\____/ \__,_||_| |_| \__,_||_.__/ \___/ /_/\_\
\\
\\
\\
), .{});
try stdout.print(cham.yellow().fmt("Run this command to save the hashed key:\n"), .{});
try stdout.print(cham.grey().fmt("printf \"{s}\" | podman secret create --replace haxe_authkey -\n\n"), .{std.fmt.fmtSliceHexLower(&hash)});
try stdout.print(cham.yellow().fmt("The following is the base64 key you should provide to HaxeSandbox during requests. Keep this secret!\n"), .{});
try stdout.print(cham.grey().fmt("{s}\n"), .{b64});

try bw.flush();
}

0 comments on commit 8badf54

Please sign in to comment.