Compare commits
2 commits
02f224d412
...
50ac6f1310
Author | SHA1 | Date | |
---|---|---|---|
50ac6f1310 | |||
b93db148f6 |
2 changed files with 17 additions and 18 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,3 +2,4 @@
|
|||
/zig-cache
|
||||
/zig-out
|
||||
/config.json
|
||||
/password
|
||||
|
|
34
src/main.zig
34
src/main.zig
|
@ -47,14 +47,6 @@ 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 {
|
||||
|
@ -94,18 +86,33 @@ 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,
|
||||
config.value.loki.password.?,
|
||||
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;
|
||||
|
@ -113,18 +120,9 @@ 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| {
|
||||
|
|
Loading…
Reference in a new issue