hostapps/src/connect.zig
2024-05-14 15:23:31 -05:00

153 lines
5.1 KiB
Zig

const std = @import("std");
const builtin = @import("builtin");
const Config = @import("config.zig").Config;
const options = @import("options.zig");
const ansi = @import("anzi").ANSI(.{});
const kex_algorithms = [_][]const u8{
"diffie-hellman-group14-sha1",
"diffie-hellman-group1-sha1",
"diffie-hellman-group-exchange-sha1",
"diffie-hellman-group-exchange-sha256",
"ecdh-sha2-nistp256",
};
const ciphers = [_][]const u8{
"aes256-cbc",
"aes192-cbc",
"3des-cbc",
"aes128-cbc",
"aes256-ctr",
"aes192-ctr",
"aes128-ctr",
};
const macs = [_][]const u8{
"hmac-md5",
"hmac-sha1",
"hmac-sha2-256-etm@openssh.com",
"hmac-sha2-512-etm@openssh.com",
"hmac-sha2-256",
"hmac-sha2-512",
};
const ssh_options = [_]struct { key: []const u8, value: []const u8 }{
.{ .key = "ControlMaster", .value = "no" },
.{ .key = "ControlPath", .value = "none" },
.{ .key = "ForwardX11", .value = "no" },
.{ .key = "ForwardX11Trusted", .value = "no" },
.{ .key = "HostKeyAlgorithms", .value = "+ssh-rsa" },
.{ .key = "PubkeyAcceptedKeyTypes", .value = "+ssh-rsa" },
};
pub fn connect() !void {
var arena = std.heap.ArenaAllocator.init(options.options.alloc);
const alloc = arena.allocator();
defer arena.deinit();
std.log.info("config: {}", .{options.options});
const config = try Config.read(options.options.alloc, options.options.config);
var path: []const u8 = undefined;
var args = std.ArrayList([]const u8).init(alloc);
// std.log.info("name: {s}", .{config.name});
// std.log.info("class {s}", .{config.class});
// std.log.info("type: {}", .{config.type});
// std.log.info("comment: {s}", .{config.comment});
// std.log.info("address: {s}", .{config.address});
// std.log.info("port: {d}", .{config.port});
// std.log.info("username: {s}", .{config.username});
// std.log.info("manufacturer: {s}", .{config.manufacturer});
// std.log.info("model: {s}", .{config.model});
// std.log.info("part number: {s}", .{config.part_number});
switch (config.type) {
.ssh => {
path = options.options.ssh_path;
try args.append("ssh");
try args.append("-y");
for (options.options.identities) |identity| {
try args.append("-i");
try args.append(identity);
}
if (options.options.proxy_jump) |p| {
try args.append("-o");
try args.append(try std.fmt.allocPrint(alloc, "ProxyJump={s}", .{p}));
}
for (ssh_options) |option| {
try args.append("-o");
try args.append(try std.fmt.allocPrint(alloc, "{s}={s}", .{ option.key, option.value }));
}
try args.append("-o");
try args.append(try std.fmt.allocPrint(alloc, "Ciphers={s}", .{try std.mem.join(alloc, ",", &ciphers)}));
try args.append("-o");
try args.append(try std.fmt.allocPrint(alloc, "KexAlgorithms={s}", .{try std.mem.join(alloc, ",", &kex_algorithms)}));
try args.append("-o");
try args.append(try std.fmt.allocPrint(alloc, "MACs={s}", .{try std.mem.join(alloc, ",", &macs)}));
try args.append("-l");
try args.append(config.username);
if (config.port != 22) {
try args.append("-p");
try args.append(try std.fmt.allocPrint(alloc, "{d}", .{config.port}));
}
try args.append(config.address);
},
.telnet => {
if (options.options.proxy_jump) |p| {
path = options.options.ssh_path;
try args.append("ssh");
try args.append("-t");
try args.append(try std.fmt.allocPrint(alloc, "ssh://{s}", .{p}));
try args.append("telnet");
} else {
path = options.options.telnet_path;
try args.append("telnet");
}
try args.append(config.address);
if (config.port != 23) {
try args.append(try std.fmt.allocPrint(alloc, "{d}", .{config.port}));
}
},
}
const pathZ = try alloc.dupeZ(u8, path);
const argsZ = try alloc.allocSentinel(?[*:0]const u8, args.items.len, null);
for (args.items, 0..) |arg, i| argsZ[i] = (try alloc.dupeZ(u8, arg)).ptr;
// for (argsZ, 0..) |arg, i| {
// if (arg) |a| std.log.info("{d} {s}", .{ i, a });
// }
if (builtin.output_mode == .Exe) {
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print(
"{}",
.{
ansi.IconNameAndWindowTitle{
.icon_name = config.name,
.window_title = try std.fmt.allocPrint(alloc, "{s} \u{2013} {s}", .{ config.name, config.address }),
},
},
);
try bw.flush();
const envp = @as([*:null]const ?[*:0]const u8, @ptrCast(std.os.environ.ptr));
return std.posix.execveZ(pathZ, argsZ, envp);
}
}