mirror of
https://git.robbyzambito.me/zaprus
synced 2026-02-04 08:24:52 +00:00
Compare commits
4 Commits
a81c4b3175
...
19d4e88c33
| Author | SHA1 | Date | |
|---|---|---|---|
| 19d4e88c33 | |||
| 3577d538b8 | |||
| fc9c5bcd5d | |||
| 157afa13b1 |
@@ -5,6 +5,8 @@ const Client = @This();
|
||||
|
||||
const max_message_size = 2048;
|
||||
|
||||
pub const max_payload_len = RawSocket.max_payload_len;
|
||||
|
||||
socket: RawSocket,
|
||||
|
||||
pub fn init() !Client {
|
||||
@@ -95,12 +97,15 @@ pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
|
||||
},
|
||||
};
|
||||
|
||||
log.debug("Setting bpf filter to port {}", .{connection.connection.src});
|
||||
try self.socket.attachSaprusPortFilter(connection.connection.src);
|
||||
log.debug("bpf set", .{});
|
||||
|
||||
var connection_buf: [2048]u8 = undefined;
|
||||
var connection_bytes = connection.toBytes(&connection_buf);
|
||||
headers.setPayloadLen(connection_bytes.len);
|
||||
|
||||
log.debug("Building full message", .{});
|
||||
var full_msg = blk: {
|
||||
var msg_buf: [2048]u8 = undefined;
|
||||
var msg_w: Writer = .fixed(&msg_buf);
|
||||
@@ -108,17 +113,21 @@ pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
|
||||
msg_w.writeAll(connection_bytes) catch unreachable;
|
||||
break :blk msg_w.buffered();
|
||||
};
|
||||
log.debug("Built full message. Sending message", .{});
|
||||
|
||||
try self.socket.send(full_msg);
|
||||
var res_buf: [4096]u8 = undefined;
|
||||
|
||||
// Ignore response from sentinel, just accept that we got one.
|
||||
log.debug("Awaiting handshake response", .{});
|
||||
_ = try self.socket.receive(&res_buf);
|
||||
try io.sleep(.fromMilliseconds(40), .real);
|
||||
|
||||
headers.udp.dst_port = udp_dest_port;
|
||||
headers.ip.id = rand.int(u16);
|
||||
headers.setPayloadLen(connection_bytes.len);
|
||||
|
||||
log.debug("Building final handshake message", .{});
|
||||
full_msg = blk: {
|
||||
var msg_buf: [2048]u8 = undefined;
|
||||
var msg_w: Writer = .fixed(&msg_buf);
|
||||
@@ -140,3 +149,4 @@ const EthIpUdp = @import("./EthIpUdp.zig").EthIpUdp;
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
const Writer = std.Io.Writer;
|
||||
const log = std.log;
|
||||
|
||||
@@ -14,12 +14,16 @@ pub fn init(socket: RawSocket, headers: EthIpUdp, connection: SaprusMessage) Con
|
||||
|
||||
pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 {
|
||||
_ = io;
|
||||
log.debug("Awaiting connection message", .{});
|
||||
const res = try self.socket.receive(buf);
|
||||
log.debug("Received {} byte connection message", .{res.len});
|
||||
const connection_res = blk: {
|
||||
const msg: SaprusMessage = try .parse(res[42..]);
|
||||
break :blk msg.connection;
|
||||
};
|
||||
|
||||
log.debug("Payload was {s}", .{connection_res.payload});
|
||||
|
||||
return connection_res.payload;
|
||||
}
|
||||
|
||||
@@ -29,14 +33,16 @@ pub fn send(self: *Connection, io: Io, buf: []const u8) !void {
|
||||
break :blk io_source.interface();
|
||||
};
|
||||
|
||||
log.debug("Sending connection message", .{});
|
||||
|
||||
self.connection.connection.payload = buf;
|
||||
const connection_bytes = blk: {
|
||||
var connection_bytes: [2048]u8 = undefined;
|
||||
break :blk self.connection.toBytes(&connection_bytes);
|
||||
};
|
||||
|
||||
self.headers.setPayloadLen(connection_bytes.len);
|
||||
self.headers.ip.id = rand.int(u16);
|
||||
self.headers.setPayloadLen(connection_bytes.len);
|
||||
|
||||
const full_msg = blk: {
|
||||
var msg_buf: [2048]u8 = undefined;
|
||||
@@ -47,12 +53,16 @@ pub fn send(self: *Connection, io: Io, buf: []const u8) !void {
|
||||
};
|
||||
|
||||
try self.socket.send(full_msg);
|
||||
|
||||
log.debug("Sent {} byte connection message", .{full_msg.len});
|
||||
}
|
||||
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
const Writer = std.Io.Writer;
|
||||
|
||||
const log = std.log;
|
||||
|
||||
const SaprusMessage = @import("./message.zig").Message;
|
||||
|
||||
const EthIpUdp = @import("./EthIpUdp.zig").EthIpUdp;
|
||||
|
||||
@@ -4,6 +4,8 @@ fd: i32,
|
||||
sockaddr_ll: std.posix.sockaddr.ll,
|
||||
mac: [6]u8,
|
||||
|
||||
pub const max_payload_len = 1000;
|
||||
|
||||
const Ifconf = extern struct {
|
||||
ifc_len: i32,
|
||||
ifc_ifcu: extern union {
|
||||
|
||||
63
src/main.zig
63
src/main.zig
@@ -38,7 +38,6 @@ pub fn main(init: std.process.Init) !void {
|
||||
} = .{};
|
||||
|
||||
{
|
||||
var payload_buf: [4096]u8 = undefined;
|
||||
var i: usize = 1;
|
||||
while (i < args.len) : (i += 1) {
|
||||
if (to_option.get(args[i])) |opt| {
|
||||
@@ -50,12 +49,9 @@ pub fn main(init: std.process.Init) !void {
|
||||
.relay => {
|
||||
i += 1;
|
||||
if (i < args.len) {
|
||||
var w: Writer = .fixed(&payload_buf);
|
||||
try w.printBase64(args[i]);
|
||||
flags.relay = w.buffered();
|
||||
flags.relay = args[i];
|
||||
} else {
|
||||
std.debug.print("-r/--relay requires a string\n", .{});
|
||||
return error.InvalidArguments;
|
||||
flags.relay = "";
|
||||
}
|
||||
},
|
||||
.dest => {
|
||||
@@ -70,7 +66,10 @@ pub fn main(init: std.process.Init) !void {
|
||||
.connect => {
|
||||
i += 1;
|
||||
if (i < args.len) {
|
||||
var w: Writer = .fixed(&payload_buf);
|
||||
var w: Writer = blk: {
|
||||
var buf: [2048]u8 = undefined;
|
||||
break :blk .fixed(&buf);
|
||||
};
|
||||
try w.printBase64(args[i]);
|
||||
flags.connect = w.buffered();
|
||||
} else {
|
||||
@@ -94,13 +93,45 @@ pub fn main(init: std.process.Init) !void {
|
||||
defer client.deinit();
|
||||
|
||||
if (flags.relay != null) {
|
||||
try client.sendRelay(init.io, flags.relay.?, parseDest(flags.dest));
|
||||
var chunk_writer_buf: [2048]u8 = undefined;
|
||||
var chunk_writer: Writer = .fixed(&chunk_writer_buf);
|
||||
if (flags.relay.?.len > 0) {
|
||||
var output_iter = std.mem.window(u8, flags.relay.?, SaprusClient.max_payload_len, SaprusClient.max_payload_len);
|
||||
while (output_iter.next()) |chunk| {
|
||||
chunk_writer.end = 0;
|
||||
try chunk_writer.print("{b64}", .{chunk});
|
||||
try client.sendRelay(init.io, chunk_writer.buffered(), parseDest(flags.dest));
|
||||
try init.io.sleep(.fromMilliseconds(40), .real);
|
||||
}
|
||||
} else {
|
||||
var stdin_file: std.Io.File = .stdin();
|
||||
var stdin_file_reader = stdin_file.reader(init.io, &.{});
|
||||
var stdin_reader = &stdin_file_reader.interface;
|
||||
var lim_buf: [SaprusClient.max_payload_len]u8 = undefined;
|
||||
var limited = stdin_reader.limited(.limited(10 * lim_buf.len), &lim_buf);
|
||||
var stdin = &limited.interface;
|
||||
|
||||
while (stdin.fillMore()) {
|
||||
chunk_writer.end = 0;
|
||||
try chunk_writer.print("{b64}", .{stdin.buffered()});
|
||||
try client.sendRelay(init.io, chunk_writer.buffered(), parseDest(flags.dest));
|
||||
try init.io.sleep(.fromMilliseconds(40), .real);
|
||||
try stdin.discardAll(stdin.end);
|
||||
} else |err| switch (err) {
|
||||
error.EndOfStream => {
|
||||
log.debug("end of stdin", .{});
|
||||
},
|
||||
else => |e| return e,
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (flags.connect != null) {
|
||||
reconnect: while (true) {
|
||||
log.debug("Starting connection", .{});
|
||||
var connection = try client.connect(init.io, flags.connect.?);
|
||||
log.debug("Connection started", .{});
|
||||
|
||||
while (true) {
|
||||
var res_buf: [2048]u8 = undefined;
|
||||
@@ -125,17 +156,24 @@ pub fn main(init: std.process.Init) !void {
|
||||
var child_stderr: std.ArrayList(u8) = .empty;
|
||||
defer child_stderr.deinit(init.gpa);
|
||||
|
||||
try child.collectOutput(init.gpa, &child_stdout, &child_stderr, 2048);
|
||||
try child.collectOutput(init.gpa, &child_stdout, &child_stderr, std.math.maxInt(usize));
|
||||
|
||||
const b64e = std.base64.standard.Encoder;
|
||||
// const b64e = std.base64.standard.Encoder;
|
||||
var cmd_output: Writer = blk: {
|
||||
var cmd_output_buf: [2048]u8 = undefined;
|
||||
const encoded_cmd_output = b64e.encode(&cmd_output_buf, child_stdout.items);
|
||||
break :blk .fixed(&cmd_output_buf);
|
||||
};
|
||||
|
||||
connection.send(init.io, encoded_cmd_output) catch continue;
|
||||
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| {
|
||||
cmd_output.end = 0;
|
||||
try cmd_output.print("{b64}", .{chunk});
|
||||
try connection.send(init.io, cmd_output.buffered());
|
||||
try init.io.sleep(.fromMilliseconds(40), .real);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unreachable;
|
||||
}
|
||||
@@ -156,6 +194,7 @@ fn parseDest(in: ?[]const u8) [4]u8 {
|
||||
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
const log = std.log;
|
||||
const ArrayList = std.ArrayList;
|
||||
const StaticStringMap = std.StaticStringMap;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user