Compare commits

..

No commits in common. "50ac6f1310f32a6b5e06d9616f9f24af95f56d13" and "02f224d41237fe87f2f8029e6e3e40312a043fcf" have entirely different histories.

2 changed files with 18 additions and 17 deletions

1
.gitignore vendored
View file

@ -2,4 +2,3 @@
/zig-cache
/zig-out
/config.json
/password

View file

@ -47,6 +47,14 @@ const Config = struct {
path: ?[]const u8 = null,
port: ?u16 = null,
},
pub fn deinit(self: @This(), allocator: std.mem.Allocator) void {
allocator.free(self.loki.url);
allocator.free(self.loki.username);
if (self.loki.password) |password| allocator.free(password);
if (self.loki.password_file) |password_file| allocator.free(password_file);
if (self.iperf3.path) |path| allocator.free(path);
}
};
pub fn ConfigWrapper(comptime T: type) type {
@ -86,33 +94,18 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const config = try readConfig(allocator, "config.json");
defer config.deinit();
const b64 = std.base64.standard.Encoder;
var password_buffer: [128]u8 = undefined;
var password: []const u8 = undefined;
if (config.value.loki.password_file) |password_file_path| {
password = try std.fs.cwd().readFile(password_file_path, &password_buffer);
password = std.mem.trimRight(u8, password, "\r\n");
} else if (config.value.loki.password) |password_data| {
@memcpy(&password_buffer, password_data);
password = password_buffer[0..password_data.len];
} else {
iperf3_log.err("unable to determine password!", .{});
return;
}
var auth_buf: [256]u8 = undefined;
const auth = try std.fmt.bufPrint(
&auth_buf,
"{s}:{s}",
.{
config.value.loki.username,
password,
config.value.loki.password.?,
},
);
var auth_encoded_buf: [b64.calcSize(auth_buf.len)]u8 = undefined;
var auth_encoded = b64.encode(&auth_encoded_buf, auth);
var auth_header_buf: [256]u8 = undefined;
@ -120,9 +113,18 @@ pub fn main() !void {
const uri = try std.Uri.parse(config.value.loki.url);
// var headers = std.http.Headers{ .allocator = allocator };
// try headers.append("Authorization", auth_header);
// try headers.append("Content-Type", "application/json");
var client = std.http.Client{ .allocator = allocator };
defer client.deinit();
// const stderr_file = std.io.getStdErr().writer();
// var stderr_bw = std.io.bufferedWriter(stderr_file);
// const stderr = stderr_bw.writer();
// _ = stderr;
var port_buf: [16]u8 = undefined;
var port: []u8 = undefined;
if (config.value.iperf3.port) |p| {