Zig
Zig
Close an SSH Tunnel (CloseTunnel)
See more SSH Tunnel Examples
Demonstrates the Chilkat SshTunnel.CloseTunnel method, which disconnects active tunnel clients and stops the tunnel-manager thread. The only argument (waitForThreads) selects whether to wait for the worker threads to exit.
Background: This is the full shutdown — it stops the listener, disconnects clients, and ends the background threads, so it is what you call when the tunnel is no longer needed. Passing
true for waitForThreads makes the call block until the workers have actually exited, which matters before the program terminates or reuses the object; if a worker fails to stop, the method returns false and LastErrorText explains why.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Demonstrates the SshTunnel.CloseTunnel method, which disconnects active tunnel clients and stops
// the tunnel-manager thread. The only argument (waitForThreads) selects whether to wait for the
// worker threads to exit.
const tunnel = try chilkat.SshTunnel.init();
defer tunnel.deinit();
tunnel.setDestHostname("db.internal.example.com");
tunnel.setDestPort(5432);
const ssh_port = 22;
tunnel.connect("ssh.example.com", ssh_port) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
const password = "mySshPassword";
tunnel.authenticatePw("mySshLogin", password) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
const listen_port = 1080;
tunnel.beginAccepting(listen_port) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
// ... the tunnel runs ...
// Shut everything down: disconnect clients and stop the tunnel-manager thread. With
// waitForThreads true, the method waits for the worker threads to exit.
const wait_for_threads = true;
tunnel.closeTunnel(wait_for_threads) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
std.debug.print("Tunnel closed.\n", .{});
}