Cleanup of socket creation on terminal pane

This commit is contained in:
oupson 2023-03-01 11:21:42 +01:00
parent d7b4940608
commit 2167084fa6
Signed by: oupson
GPG Key ID: 3BD88615552EFCB7
1 changed files with 12 additions and 17 deletions

View File

@ -26,7 +26,7 @@ namespace Footerm {
private SSH2.Session<bool>? session; private SSH2.Session<bool>? session;
private SSH2.Channel? channel; private SSH2.Channel? channel;
private Socket socket; private SocketConnection socket;
private int slave_pty; private int slave_pty;
private Footerm.Model.Server server; private Footerm.Model.Server server;
@ -60,13 +60,11 @@ namespace Footerm {
private async void connect_to_server() throws GLib.IOError, GLib.Error { private async void connect_to_server() throws GLib.IOError, GLib.Error {
var addrs = new NetworkAddress(this.server.hostname, this.server.port); var addrs = new NetworkAddress(this.server.hostname, this.server.port);
var addr = addrs.enumerate().next(); var addr = addrs.enumerate().next();
this.socket = new Socket(addr.get_family(), SocketType.STREAM, SocketProtocol.TCP); var client = new SocketClient();
socket.connect(addr, null); this.socket = client.connect(addr);
var sock = socket.get_fd(); // TODO
this.session = SSH2.Session.create<bool> (); this.session = SSH2.Session.create<bool> ();
if (session.handshake(sock) != SSH2.Error.NONE) { if (session.handshake(this.socket.get_socket().get_fd()) != SSH2.Error.NONE) {
stderr.printf("Failure establishing SSH session\n"); stderr.printf("Failure establishing SSH session\n");
return; return;
} }
@ -78,7 +76,6 @@ namespace Footerm {
} }
stdout.printf("\n"); stdout.printf("\n");
// TODO QUERY PASSWORD FROM libsecret
var secrets = Footerm.Services.Secrets.get_instance(); var secrets = Footerm.Services.Secrets.get_instance();
var password = yield secrets.get_password(this.server); var password = yield secrets.get_password(this.server);
@ -86,7 +83,7 @@ namespace Footerm {
stdout.printf("\tAuthentication by password failed!\n"); stdout.printf("\tAuthentication by password failed!\n");
session.disconnect("Normal Shutdown, Thank you for playing"); session.disconnect("Normal Shutdown, Thank you for playing");
session = null; session = null;
Posix.close(sock); this.socket.close();
return; return;
} else { } else {
stdout.printf("\tAuthentication by password succeeded.\n"); stdout.printf("\tAuthentication by password succeeded.\n");
@ -100,7 +97,7 @@ namespace Footerm {
stderr.printf("Failed requesting pty\n"); stderr.printf("Failed requesting pty\n");
session.disconnect("Normal Shutdown, Thank you for playing"); session.disconnect("Normal Shutdown, Thank you for playing");
session = null; session = null;
Posix.close(sock); this.socket.close();
} }
channel.set_env("TERM", "xterm-256color"); channel.set_env("TERM", "xterm-256color");
@ -109,7 +106,7 @@ namespace Footerm {
stderr.printf("Unable to request shell on allocated pty\n"); stderr.printf("Unable to request shell on allocated pty\n");
session.disconnect("Normal Shutdown, Thank you for playing"); session.disconnect("Normal Shutdown, Thank you for playing");
session = null; session = null;
Posix.close(sock); this.socket.close();
} }
var master_pty = Posix.posix_openpt(Posix.O_RDWR); var master_pty = Posix.posix_openpt(Posix.O_RDWR);
@ -147,21 +144,19 @@ namespace Footerm {
session.blocking = false; session.blocking = false;
var sock_channel = new GLib.IOChannel.unix_new(sock); var inner_socket = socket.get_socket();
sock_channel.set_encoding(null); var source = inner_socket.create_source(GLib.IOCondition.IN, null);
sock_channel.set_buffered(false); source.set_callback(this.on_ssh_event);
sock_channel.set_close_on_unref(false); source.attach(null);
var slave_channel = new GLib.IOChannel.unix_new(slave_pty); var slave_channel = new GLib.IOChannel.unix_new(slave_pty);
slave_channel.set_encoding(null); slave_channel.set_encoding(null);
slave_channel.set_buffered(false); slave_channel.set_buffered(false);
sock_channel.add_watch(GLib.IOCondition.IN, this.on_ssh_event);
slave_channel.add_watch(GLib.IOCondition.IN, this.on_slave_event); slave_channel.add_watch(GLib.IOCondition.IN, this.on_slave_event);
} }
} }
private bool on_ssh_event(GLib.IOChannel source, GLib.IOCondition condition) { private bool on_ssh_event(Socket source, GLib.IOCondition condition) {
if (condition == IOCondition.HUP) { if (condition == IOCondition.HUP) {
print("The connection has been broken.\n"); print("The connection has been broken.\n");
return false; return false;