Zig
Zig
Stop an SSH Tunnel Listener (StopAccepting)
See more SSH Tunnel Examples
Demonstrates the Chilkat SshTunnel.StopAccepting method, which stops the local listener so no new client connections are accepted. The only argument (waitForThread) selects whether to wait for the listener thread to exit. Existing forwarded clients remain connected.
Background: The distinction from
CloseTunnel is the point: StopAccepting closes only the front door, leaving already-connected clients to finish their transfers undisturbed. That makes it the graceful way to drain a tunnel — stop taking new work, let existing work complete, then close — rather than cutting everyone off at once.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.StopAccepting method, which stops the local listener so that no new
// client connections are accepted. The only argument (waitForThread) selects whether to wait for
// the listener thread to exit. Existing forwarded clients remain connected.
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 for a while ...
// Stop accepting NEW connections, but leave already-connected clients running.
const wait_for_thread = true;
tunnel.stopAccepting(wait_for_thread) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
std.debug.print("The listener has stopped; existing clients continue.\n", .{});
const wait_for_thread_exit = true;
tunnel.closeTunnel(wait_for_thread_exit) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
}