mirror of
https://git.robbyzambito.me/zaprus
synced 2026-02-04 08:24:52 +00:00
Better error handling and debug logging
This commit is contained in:
@@ -15,7 +15,7 @@ const Ifconf = extern struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub fn init() !RawSocket {
|
pub fn init() !RawSocket {
|
||||||
const socket: i32 = @intCast(std.os.linux.socket(std.os.linux.AF.PACKET, std.os.linux.SOCK.RAW, 0));
|
const socket: i32 = std.math.cast(i32, std.os.linux.socket(std.os.linux.AF.PACKET, std.os.linux.SOCK.RAW, 0)) orelse return error.SocketError;
|
||||||
if (socket < 0) return error.SocketError;
|
if (socket < 0) return error.SocketError;
|
||||||
|
|
||||||
var ifreq_storage: [16]std.os.linux.ifreq = undefined;
|
var ifreq_storage: [16]std.os.linux.ifreq = undefined;
|
||||||
|
|||||||
31
src/main.zig
31
src/main.zig
@@ -1,5 +1,3 @@
|
|||||||
const is_debug = builtin.mode == .Debug;
|
|
||||||
|
|
||||||
const help =
|
const help =
|
||||||
\\-h, --help Display this help and exit.
|
\\-h, --help Display this help and exit.
|
||||||
\\-r, --relay <str> A relay message to send.
|
\\-r, --relay <str> A relay message to send.
|
||||||
@@ -43,7 +41,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
if (to_option.get(args[i])) |opt| {
|
if (to_option.get(args[i])) |opt| {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
.help => {
|
.help => {
|
||||||
std.debug.print("{s}\n", .{help});
|
std.debug.print("{s}", .{help});
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
.relay => {
|
.relay => {
|
||||||
@@ -84,7 +82,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
return error.InvalidArguments;
|
return error.InvalidArguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
var client: SaprusClient = try .init();
|
var client = try SaprusClient.init();
|
||||||
defer client.deinit();
|
defer client.deinit();
|
||||||
|
|
||||||
if (flags.relay != null) {
|
if (flags.relay != null) {
|
||||||
@@ -96,7 +94,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
chunk_writer.end = 0;
|
chunk_writer.end = 0;
|
||||||
try chunk_writer.print("{b64}", .{chunk});
|
try chunk_writer.print("{b64}", .{chunk});
|
||||||
try client.sendRelay(init.io, chunk_writer.buffered(), parseDest(flags.dest));
|
try client.sendRelay(init.io, chunk_writer.buffered(), parseDest(flags.dest));
|
||||||
try init.io.sleep(.fromMilliseconds(40), .real);
|
try init.io.sleep(.fromMilliseconds(40), .boot);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var stdin_file: std.Io.File = .stdin();
|
var stdin_file: std.Io.File = .stdin();
|
||||||
@@ -114,7 +112,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
chunk_writer.end = 0;
|
chunk_writer.end = 0;
|
||||||
try chunk_writer.print("{b64}", .{stdin.buffered()});
|
try chunk_writer.print("{b64}", .{stdin.buffered()});
|
||||||
try client.sendRelay(init.io, chunk_writer.buffered(), parseDest(flags.dest));
|
try client.sendRelay(init.io, chunk_writer.buffered(), parseDest(flags.dest));
|
||||||
try init.io.sleep(.fromMilliseconds(40), .real);
|
try init.io.sleep(.fromMilliseconds(40), .boot);
|
||||||
try stdin.discardAll(stdin.end);
|
try stdin.discardAll(stdin.end);
|
||||||
} else |err| switch (err) {
|
} else |err| switch (err) {
|
||||||
error.EndOfStream => {
|
error.EndOfStream => {
|
||||||
@@ -145,7 +143,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
|
|
||||||
log.debug("Connection started", .{});
|
log.debug("Connection started", .{});
|
||||||
|
|
||||||
while (true) {
|
next_message: while (true) {
|
||||||
var res_buf: [2048]u8 = undefined;
|
var res_buf: [2048]u8 = undefined;
|
||||||
const next = connection.next(init.io, &res_buf) catch {
|
const next = connection.next(init.io, &res_buf) catch {
|
||||||
try init.io.sleep(.fromSeconds(retry_seconds), .boot);
|
try init.io.sleep(.fromSeconds(retry_seconds), .boot);
|
||||||
@@ -156,7 +154,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
var connection_payload_buf: [2048]u8 = undefined;
|
var connection_payload_buf: [2048]u8 = undefined;
|
||||||
const connection_payload = connection_payload_buf[0..try b64d.calcSizeForSlice(next)];
|
const connection_payload = connection_payload_buf[0..try b64d.calcSizeForSlice(next)];
|
||||||
b64d.decode(connection_payload, next) catch {
|
b64d.decode(connection_payload, next) catch {
|
||||||
// TODO: debug log
|
log.debug("Failed to decode message, skipping: '{s}'", .{connection_payload});
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -171,17 +169,24 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
var child_stderr: std.ArrayList(u8) = .empty;
|
var child_stderr: std.ArrayList(u8) = .empty;
|
||||||
defer child_stderr.deinit(init.gpa);
|
defer child_stderr.deinit(init.gpa);
|
||||||
|
|
||||||
try child.collectOutput(init.gpa, &child_stdout, &child_stderr, std.math.maxInt(usize));
|
child.collectOutput(init.gpa, &child_stdout, &child_stderr, std.math.maxInt(usize)) catch |err| {
|
||||||
|
log.debug("Failed to collect output: {t}", .{err});
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
var cmd_output_buf: [2048]u8 = undefined;
|
var cmd_output_buf: [SaprusClient.max_payload_len * 2]u8 = undefined;
|
||||||
var cmd_output: Writer = .fixed(&cmd_output_buf);
|
var cmd_output: Writer = .fixed(&cmd_output_buf);
|
||||||
|
|
||||||
var cmd_output_window_iter = std.mem.window(u8, child_stdout.items, SaprusClient.max_payload_len, SaprusClient.max_payload_len);
|
var cmd_output_window_iter = std.mem.window(u8, child_stdout.items, SaprusClient.max_payload_len, SaprusClient.max_payload_len);
|
||||||
while (cmd_output_window_iter.next()) |chunk| {
|
while (cmd_output_window_iter.next()) |chunk| {
|
||||||
cmd_output.end = 0;
|
cmd_output.end = 0;
|
||||||
try cmd_output.print("{b64}", .{chunk});
|
// Unreachable because the cmd_output_buf is twice the size of the chunk.
|
||||||
try connection.send(init.io, cmd_output.buffered());
|
cmd_output.print("{b64}", .{chunk}) catch unreachable;
|
||||||
try init.io.sleep(.fromMilliseconds(40), .real);
|
connection.send(init.io, cmd_output.buffered()) catch |err| {
|
||||||
|
log.debug("Failed to send connection chunk: {t}", .{err});
|
||||||
|
continue :next_message;
|
||||||
|
};
|
||||||
|
try init.io.sleep(.fromMilliseconds(40), .boot);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user