From e4849bcd1312d914f9b84886d28c112b47b27380 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Wed, 5 Jan 2022 17:46:08 -0800 Subject: [PATCH] partial windows support/external curl zfetch (or iguana) is triggering some Crowdstrike heuristic simply from the inclusion of the library, so we need to add an option to compile with included zfetch or with external curl --- build.zig | 17 +++++++++++++--- config/curl.zig | 1 + config/nocurl.zig | 1 + src/clipboard.zig | 10 ++++++++++ src/download.zig | 51 ++++++++++++++++++++++++++++++++++++----------- 5 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 config/curl.zig create mode 100644 config/nocurl.zig diff --git a/build.zig b/build.zig index eeb4a2b..209a2ff 100644 --- a/build.zig +++ b/build.zig @@ -23,7 +23,12 @@ pub fn build(b: *std.build.Builder) void { else => std.os.exit(1), }; - configureExe(uploadexe, b, target, mode, zfetch_repo); + const path = if (b.option(bool, "curl", "use external curl command") orelse false) + "config/curl.zig" + else + "config/nocurl.zig"; + + configureExe(uploadexe, b, target, mode, zfetch_repo, path); const run_cmd = uploadexe.run(); run_cmd.step.dependOn(b.getInstallStep()); @@ -36,7 +41,7 @@ pub fn build(b: *std.build.Builder) void { const downloadexe = b.addExecutable("clipboard-download", "src/download.zig"); - configureExe(downloadexe, b, target, mode, zfetch_repo); + configureExe(downloadexe, b, target, mode, zfetch_repo, path); const run_download_cmd = downloadexe.run(); run_download_cmd.step.dependOn(b.getInstallStep()); @@ -48,7 +53,7 @@ pub fn build(b: *std.build.Builder) void { run_download_step.dependOn(&run_download_cmd.step); } -fn configureExe(exe: *std.build.LibExeObjStep, b: *std.build.Builder, target: std.zig.CrossTarget, mode: std.builtin.Mode, zfetch_repo: anytype) void { +fn configureExe(exe: *std.build.LibExeObjStep, b: *std.build.Builder, target: std.zig.CrossTarget, mode: std.builtin.Mode, zfetch_repo: anytype, config_path: []const u8) void { exe.setTarget(target); exe.setBuildMode(mode); if (target.getOs().tag == .linux) { @@ -102,6 +107,12 @@ fn configureExe(exe: *std.build.LibExeObjStep, b: *std.build.Builder, target: st .name = "iguanaTLS", .path = .{ .path = "libs/zfetch/libs/iguanaTLS/src/main.zig" }, }); + + exe.addPackage(.{ + .name = "config", + .path = .{ .path = config_path }, + }); + exe.install(); } diff --git a/config/curl.zig b/config/curl.zig new file mode 100644 index 0000000..02906e5 --- /dev/null +++ b/config/curl.zig @@ -0,0 +1 @@ +pub const curl: ?[]const u8 = "d:\\d\\bin\\curl"; diff --git a/config/nocurl.zig b/config/nocurl.zig new file mode 100644 index 0000000..0ef6c61 --- /dev/null +++ b/config/nocurl.zig @@ -0,0 +1 @@ +pub const curl: ?[]const u8 = null; diff --git a/src/clipboard.zig b/src/clipboard.zig index 5291aca..533b8c2 100644 --- a/src/clipboard.zig +++ b/src/clipboard.zig @@ -1,6 +1,7 @@ const std = @import("std"); const zfetch = @import("zfetch"); const crypt = @import("crypt.zig"); +const config = @import("config"); // const tls = @import("iguanaTLS"); // NGINX config isn't allowing ECDHE-RSA-CHACHA20-POLY1305 on TLS 1.2 @@ -91,6 +92,9 @@ fn getKey(allocator: std.mem.Allocator) !*[crypt.key_size]u8 { } fn get(allocator: std.mem.Allocator) ![]const u8 { + if (config.curl) |curl| + return getCurl(allocator, curl); + // TODO: Windows // var cert_reader = std.io.fixedBufferStream( // @embedFile("/etc/ssl/certs/ca-certificates.crt"), @@ -134,6 +138,12 @@ fn get(allocator: std.mem.Allocator) ![]const u8 { return data.toOwnedSlice(); } +fn getCurl(allocator: std.mem.Allocator, curl_path: []const u8) ![]const u8 { + _ = allocator; + _ = curl_path; + return "hello"; +} + fn post(allocator: std.mem.Allocator, data: []const u8) !void { // TODO: Windows // var cert_reader = std.io.fixedBufferStream( diff --git a/src/download.zig b/src/download.zig index d969544..59f2dc8 100644 --- a/src/download.zig +++ b/src/download.zig @@ -1,3 +1,4 @@ +const builtin = @import("builtin"); const std = @import("std"); const Clipboard = @import("clipboard.zig"); @@ -34,7 +35,6 @@ pub fn main() !u8 { return 0; } - fn addToClipboardNoError(allocator: std.mem.Allocator, data: []const u8) void { addToClipboard(allocator, data) catch |e| { std.log.err("Could not add data to clipboard: {}", .{e}); @@ -46,18 +46,16 @@ fn addToClipboardNoError(allocator: std.mem.Allocator, data: []const u8) void { fn addToClipboard(allocator: std.mem.Allocator, data: []const u8) !void { // We're going to cheat here and just run xclip - const xclip_cmd = try std.fmt.allocPrint(allocator, "echo '{s}' | xclip -selection c", .{data}); - defer allocator.free(xclip_cmd); + const result = os: { + if (builtin.os.tag == .linux) { + break :os try execLinux(allocator, data); + } else if (builtin.os.tag == .windows) { + break :os try execWindows(allocator, data); + } else { + return error.OsUnsupported; + } + }; - const result = try std.ChildProcess.exec(.{ - .allocator = allocator, - .argv = &[_][]const u8{ - "/usr/bin/env", - "sh", - "-c", - xclip_cmd, - }, - }); try std.io.getStdErr().writer().writeAll(result.stderr); switch (result.term) { .Exited => |code| if (code != 0) return error.NonZeroExit, @@ -66,3 +64,32 @@ fn addToClipboard(allocator: std.mem.Allocator, data: []const u8) !void { .Unknown => return error.Failed, } } + +fn execLinux(allocator: std.mem.Allocator, data: []const u8) !std.ChildProcess.ExecResult { + const xclip_cmd = try std.fmt.allocPrint(allocator, "echo -n '{s}'| xclip -selection c", .{data}); + defer allocator.free(xclip_cmd); + + return std.ChildProcess.exec(.{ + .allocator = allocator, + .argv = &[_][]const u8{ + "/usr/bin/env", + "sh", + "-c", + xclip_cmd, + }, + }); +} + +fn execWindows(allocator: std.mem.Allocator, data: []const u8) !std.ChildProcess.ExecResult { + const clip_cmd = try std.fmt.allocPrint(allocator, "echo '{s}'| clip", .{data}); + defer allocator.free(clip_cmd); + + return std.ChildProcess.exec(.{ + .allocator = allocator, + .argv = &[_][]const u8{ + "c:\\windows\\system32\\cmd.exe", // TODO: use Comspec + "/c", + clip_cmd, + }, + }); +}