2024-01-12 22:47:10 -06:00
|
|
|
const std = @import("std");
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
const build_options = @import("build_options");
|
2024-12-13 10:39:38 -06:00
|
|
|
const Options = @import("options.zig");
|
|
|
|
const Config = @import("config.zig");
|
|
|
|
const connect = @import("connect.zig").connect;
|
2024-01-12 22:47:10 -06:00
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
2024-12-13 10:39:38 -06:00
|
|
|
const gpa_alloc = gpa.allocator();
|
|
|
|
var arena = std.heap.ArenaAllocator.init(gpa_alloc);
|
|
|
|
const arena_alloc = arena.allocator();
|
|
|
|
defer arena.deinit();
|
2024-01-12 22:47:10 -06:00
|
|
|
|
2024-12-13 10:39:38 -06:00
|
|
|
var config: ?*Config = null;
|
|
|
|
var options: Options = .{};
|
|
|
|
var identities = std.ArrayList([]const u8).init(arena_alloc);
|
|
|
|
errdefer identities.deinit();
|
2024-01-12 22:47:10 -06:00
|
|
|
|
2024-12-13 10:39:38 -06:00
|
|
|
{
|
|
|
|
const OptionNames = enum {
|
|
|
|
config,
|
|
|
|
identity,
|
|
|
|
agent,
|
|
|
|
@"proxy-jump",
|
|
|
|
@"ssh-command",
|
|
|
|
@"telnet-command",
|
|
|
|
};
|
2024-05-14 15:23:31 -05:00
|
|
|
|
2024-12-13 10:39:38 -06:00
|
|
|
const args = try std.process.argsAlloc(arena_alloc);
|
|
|
|
defer std.process.argsFree(arena_alloc, args);
|
|
|
|
|
|
|
|
var index: usize = 1;
|
|
|
|
while (index < args.len) {
|
|
|
|
if (std.mem.startsWith(u8, args[index], "--")) {
|
|
|
|
const key, const value = kv: {
|
|
|
|
if (std.mem.indexOfScalarPos(u8, args[index], 2, '=')) |end| {
|
|
|
|
break :kv .{ args[index][2..end], args[index][end + 1 .. args.len] };
|
|
|
|
} else {
|
|
|
|
const key = args[index][2..];
|
|
|
|
index += 1;
|
|
|
|
const value = args[index];
|
|
|
|
break :kv .{ key, value };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (std.meta.stringToEnum(OptionNames, key)) |option| switch (option) {
|
|
|
|
.config => {
|
|
|
|
config = try Config.read(arena_alloc, value);
|
|
|
|
},
|
|
|
|
.agent => {
|
|
|
|
options.agent = try arena_alloc.dupe(u8, value);
|
|
|
|
},
|
|
|
|
.identity => {
|
|
|
|
try identities.append(try arena_alloc.dupe(u8, value));
|
2024-05-14 15:23:31 -05:00
|
|
|
},
|
2024-12-13 10:39:38 -06:00
|
|
|
.@"proxy-jump" => {
|
|
|
|
options.proxy_jump = try arena_alloc.dupe(u8, value);
|
2024-05-14 15:23:31 -05:00
|
|
|
},
|
2024-12-13 10:39:38 -06:00
|
|
|
.@"ssh-command" => {
|
|
|
|
options.ssh_command = try arena_alloc.dupe(u8, value);
|
|
|
|
},
|
|
|
|
.@"telnet-command" => {
|
|
|
|
options.telnet_command = try arena_alloc.dupe(u8, value);
|
|
|
|
},
|
|
|
|
} else {
|
|
|
|
std.log.err("unknown option: {s}", .{key});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (identities.items.len > 0) {
|
|
|
|
options.identities = identities.items;
|
|
|
|
}
|
|
|
|
}
|
2024-05-14 15:23:31 -05:00
|
|
|
|
2024-12-13 10:39:38 -06:00
|
|
|
try connect(arena_alloc, &options, config orelse {
|
|
|
|
std.log.err("no configuration!", .{});
|
|
|
|
return;
|
|
|
|
});
|
2024-01-12 22:47:10 -06:00
|
|
|
}
|