diff options
author | 2023-10-03 22:09:45 +0600 | |
---|---|---|
committer | 2023-10-03 22:09:45 +0600 | |
commit | ac24c495d06522303652232a6ede276c2d630f9e (patch) | |
tree | 0383831c5cdc85896fc8cf414b6ecab3bd85f452 /src | |
parent | 047a1cf0fc63d140afc83a15d69004744e68bf3f (diff) | |
download | jadupc-remote-support-console-ac24c495d06522303652232a6ede276c2d630f9e.tar.gz jadupc-remote-support-console-ac24c495d06522303652232a6ede276c2d630f9e.zip |
Backport to debian
Signed-off-by: Mubashshir <ahm@jadupc.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/meson.build | 16 | ||||
-rw-r--r-- | src/tmate/session.vala | 111 | ||||
-rw-r--r-- | src/window.vala | 20 |
3 files changed, 99 insertions, 48 deletions
diff --git a/src/meson.build b/src/meson.build index 930b210..9cac7f5 100644 --- a/src/meson.build +++ b/src/meson.build @@ -8,17 +8,15 @@ srcs += files( subdir('tmate') +vte_dep = dependency('vte-2.91') + +if vte_dep.version().version_compare('<0.63, >=0.62') + vapi_overrides += 'vte-0.62' +endif + deps = [ dependency('gtk+-3.0'), - dependency('vte-2.91'), - #meson.get_compiler('vala').find_library('posix'), + vte_dep, config_h, config_dep ] - -executable(application_name, srcs + resources, - include_directories: config_h_dir, - vala_args: '--target-glib=2.50', dependencies: deps, - install: true, - c_args: ['-include', 'config.h'], -) diff --git a/src/tmate/session.vala b/src/tmate/session.vala index 5571654..356aa66 100644 --- a/src/tmate/session.vala +++ b/src/tmate/session.vala @@ -2,6 +2,8 @@ namespace Tmate { public interface SessionInterface : Object { + public abstract signal void address (SessionType session, string address); + public abstract string? @get(SessionType session); public abstract bool start(string? config = null); public abstract bool stop(); @@ -16,6 +18,7 @@ namespace Tmate private string? _addr_http_ro = null; private IOChannel? stdout = null; private Pid? pid = null; + private uint error_count = 0; // Regex private Regex r_connect = /^Connecting to .+\.\.\.$/; @@ -26,9 +29,14 @@ namespace Tmate private Regex r_ssh_ro = /^ssh \w+ read \w+[:] .+ (.+)+$/; private Regex r_joined = /^.+ joined \(([^()]+)\) -- (\d+) clients? .+$/; private Regex r_left = /^.+ left \(([^()]+)\) -- (\d+) clients? .+$/; + private Regex r_closed = /^Session closed$/; + private Regex r_note = /^(Note: |Reconnecting)/; + private Regex r_restarted = /^Session shell restarted$/; + private Regex r_netfail= /^[^ ]+ lookup failure\..+ \((.+)\)$/; public SessionType flags { get; private set;} public string? socket { get; private set;} + public bool terminate = true; construct { reset(); } @@ -36,32 +44,47 @@ namespace Tmate { info (_("Session: unix://%s"), socket); } + + public virtual signal void network_error(string message) + { + if(error_count ++ > 5) stop(); + } + public virtual signal void stopped () { info (_("Session terminated.")); + reset(); } - public override signal void address (SessionType session, string address) + + private void address_set(SessionType session, string address) { info (_("%s address: %s"), session.to_string(), address); - switch(session) { - case SSH : - _addr_ssh = address; - break; - case HTTP: - _addr_http = address; - break; - case HTTP_READONLY: - _addr_http_ro = address; - break; - case SSH_READONLY : - _addr_ssh_ro = address; - break; - case DISCONNECTED : + + if (session in SessionType.SSH|SessionType.SSH_READONLY|SessionType.HTTP|SessionType.HTTP_READONLY) { + switch(session) { + case SSH: + _addr_ssh = address; + break; + case HTTP: + _addr_http = address; + break; + case HTTP_READONLY: + _addr_http_ro = address; + break; + case SSH_READONLY : + _addr_ssh_ro = address; + break; + default: + warn_if_reached(); + break; + } + + flags |= session; + this.address(session, address); + } else if (session == DISCONNECTED) { _addr_http = _addr_ssh = _addr_http_ro = _addr_ssh_ro = null; - break; - default: - warn_if_reached(); - break; + flags = DISCONNECTED; + this.address(DISCONNECTED, ""); } } public virtual signal void joined (string mate, int mates) @@ -83,6 +106,7 @@ namespace Tmate pid = null; socket = null; flags = DISCONNECTED; + error_count = 0; } private bool send(string[] args) @@ -129,10 +153,29 @@ namespace Tmate return null; } + public bool stop() + { + return send({"kill-server"}); + } + + private void restart() + { + if(terminate) { + stop(); + return; + } + + started(socket); + if((bool)_addr_ssh) address(SSH, _addr_ssh); + if((bool)_addr_ssh_ro) address(SSH_READONLY, _addr_ssh_ro); + if((bool)_addr_http) address(HTTP, _addr_http); + if((bool)_addr_http_ro) address(HTTP_READONLY, _addr_http_ro); + } + public bool start(string? config = null) { if(! (pid == null)) { - started.emit(socket); + started(socket); return true; } @@ -164,15 +207,22 @@ namespace Tmate if(r_socket.match(line, 0, out info)) socket = info.fetch(1); else if (r_connect.match(line) && socket != null) - started.emit(socket); + started(socket); else if(r_http.match(line, 0, out info)) - address.emit(HTTP, info.fetch(1)); + address_set(HTTP, info.fetch(1)); else if(r_http_ro.match(line, 0, out info)) - address.emit(HTTP_READONLY, info.fetch(1)); + address_set(HTTP_READONLY, info.fetch(1)); else if(r_ssh.match(line, 0, out info)) - address.emit(SSH, info.fetch(1)); + address_set(SSH, info.fetch(1)); else if(r_ssh_ro.match(line, 0, out info)) - address.emit(SSH_READONLY, info.fetch(1)); + address_set(SSH_READONLY, info.fetch(1)); + else if(r_restarted.match(line)) + restart(); + else if(r_closed.match(line)) + stopped(); + else if(r_netfail.match(line, 0, out info)) + network_error(info.fetch(1)); + else if(r_note.match(line)) unlikely(false); // Ignore line else debug("Unprocessed line: \"%s\"", line); } catch (IOChannelError e) { @@ -187,21 +237,16 @@ namespace Tmate ChildWatch.add(pid, (pid, status) => { Process.close_pid(pid); - reset(); - stopped.emit(); + if(flags != DISCONNECTED) + stopped(); }); return true; } catch (SpawnError e) { warning("Error: %s", e.message); - reset(); + stopped(); return false; } } - - public bool stop() - { - return false; - } } } diff --git a/src/window.vala b/src/window.vala index 5cdfc65..640e287 100644 --- a/src/window.vala +++ b/src/window.vala @@ -47,6 +47,11 @@ namespace JadupcSupport infobar.response.connect(() => infobar.set_revealed(false)); + tmate.network_error.connect((message) => { + show_in_infobar(ERROR, message); + }); + + tmate.stopped.connect(reset_window); tmate.address.connect((stype, addr) => { if(Tmate.SessionType.SSH in stype) on_session_connect(addr); @@ -63,6 +68,7 @@ namespace JadupcSupport configFactory.delete(config); tmate.disconnect(session_handler); }); + tmate.start(config); }); @@ -87,7 +93,11 @@ namespace JadupcSupport private void reset_window() { session.label = _("Disconnected..."); - Timeout.add_once(view.transition_duration, () => terminal.reset(true, true)); + Timeout.add(view.transition_duration, () => { + terminal.reset(true, true); + return false; + }); + view.visible_child_name = "session"; } @@ -100,14 +110,12 @@ namespace JadupcSupport private void on_session_connect(string url) { + terminal.spawn_async(Vte.PtyFlags.DEFAULT, "~", {"tmate", "-S", tmate.socket, "attach"}, {@"PATH=$(Config.SECURE_PATH)"}, + SpawnFlags.SEARCH_PATH_FROM_ENVP, null, -1, null, spawn_async_cb); + session.label = @"<a href=\"ssh://$url\">ssh://$url</a>"; view.visible_child_name = "terminal"; terminal.has_focus = true; - debug(url); - debug(tmate.socket); - - terminal.spawn_async(Vte.PtyFlags.DEFAULT, "~", {"tmate", "-S", tmate.socket, "attach"}, {@"PATH=$(Config.SECURE_PATH)"}, - SpawnFlags.SEARCH_PATH_FROM_ENVP, null, -1, null, spawn_async_cb); } } } |