Compare commits

..

2 commits

Author SHA1 Message Date
50ac6f1310
get password from file 2023-10-14 22:16:04 -05:00
b93db148f6
clean up config deinit 2023-10-14 21:26:06 -05:00
2 changed files with 17 additions and 18 deletions

1
.gitignore vendored
View file

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

View file

@ -47,14 +47,6 @@ const Config = struct {
path: ?[]const u8 = null, path: ?[]const u8 = null,
port: ?u16 = 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 { pub fn ConfigWrapper(comptime T: type) type {
@ -94,18 +86,33 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator(); const allocator = gpa.allocator();
const config = try readConfig(allocator, "config.json"); const config = try readConfig(allocator, "config.json");
defer config.deinit();
const b64 = std.base64.standard.Encoder; 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; var auth_buf: [256]u8 = undefined;
const auth = try std.fmt.bufPrint( const auth = try std.fmt.bufPrint(
&auth_buf, &auth_buf,
"{s}:{s}", "{s}:{s}",
.{ .{
config.value.loki.username, config.value.loki.username,
config.value.loki.password.?, password,
}, },
); );
var auth_encoded_buf: [b64.calcSize(auth_buf.len)]u8 = undefined; var auth_encoded_buf: [b64.calcSize(auth_buf.len)]u8 = undefined;
var auth_encoded = b64.encode(&auth_encoded_buf, auth); var auth_encoded = b64.encode(&auth_encoded_buf, auth);
var auth_header_buf: [256]u8 = undefined; var auth_header_buf: [256]u8 = undefined;
@ -113,18 +120,9 @@ pub fn main() !void {
const uri = try std.Uri.parse(config.value.loki.url); 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 }; var client = std.http.Client{ .allocator = allocator };
defer client.deinit(); 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_buf: [16]u8 = undefined;
var port: []u8 = undefined; var port: []u8 = undefined;
if (config.value.iperf3.port) |p| { if (config.value.iperf3.port) |p| {