From db787b22ec5c4521865a83094affccd0e8c86f7b Mon Sep 17 00:00:00 2001 From: Nicolas Viennot Date: Fri, 22 Jan 2016 23:14:35 -0500 Subject: [PATCH] Let the proxy do the connection messages --- tmate-daemon-decoder.c | 32 +++++++++++++++++--------------- tmate-daemon-encoder.c | 2 +- tmate-protocol.h | 7 ++++--- tmate-proxy.c | 17 ++++++++++++++--- tmate-ssh-daemon.c | 1 - tmate.h | 5 ++--- 6 files changed, 38 insertions(+), 26 deletions(-) diff --git a/tmate-daemon-decoder.c b/tmate-daemon-decoder.c index f3a8d9b1..b8b20d28 100644 --- a/tmate-daemon-decoder.c +++ b/tmate-daemon-decoder.c @@ -10,7 +10,6 @@ static void tmate_header(struct tmate_session *session, struct tmate_unpacker *uk) { char port_arg[16] = {0}; - char *client_version = xstrdup("< 1.8.6"); char tmp[512]; session->client_protocol_version = unpack_int(uk); @@ -19,30 +18,33 @@ static void tmate_header(struct tmate_session *session, } if (session->client_protocol_version >= 3) { - free(client_version); - client_version = unpack_string(uk); + session->client_version = unpack_string(uk); + } else { + session->client_version = xstrdup("1.8.5"); } tmate_notice("Daemon header: client version: %s, protocol version: %d", - client_version, session->client_protocol_version); + session->client_version, session->client_protocol_version); -#if 0 - if (strcmp(client_version, TMATE_LATEST_VERSION)) - tmate_notify_later(10, "A new version is available, please upgrade :)"); -#endif - - free(client_version); + if (tmate_has_proxy()) { + /* If we have a proxy, it takes care of all the following notificatons */ + tmate_send_proxy_header(session); + return; + } if (tmate_settings->ssh_port != 22) sprintf(port_arg, " -p%d", tmate_settings->ssh_port); - sprintf(tmp, "ssh%s ro-%s@%s", port_arg, session->session_token_ro, tmate_settings->tmate_host); - tmate_notify("Remote session read only: %s (clear your screen if you share this)", tmp); - tmate_send_env("tmate_ssh_ro", tmp); + sprintf(tmp, "ssh%s %s@%s", port_arg, session->session_token_ro, tmate_settings->tmate_host); + + tmate_notify("Note: clear your terminal before sharing readonly access"); + tmate_notify("ssh session read only: %s", tmp); sprintf(tmp, "ssh%s %s@%s", port_arg, session->session_token, tmate_settings->tmate_host); - tmate_notify("Remote session: %s", tmp); - tmate_send_env("tmate_ssh", tmp); + tmate_notify("ssh session: %s", tmp); + + tmate_set_env("tmate_ssh_ro", tmp); + tmate_set_env("tmate_ssh", tmp); tmate_send_client_ready(); } diff --git a/tmate-daemon-encoder.c b/tmate-daemon-encoder.c index 646808d5..f73fe024 100644 --- a/tmate-daemon-encoder.c +++ b/tmate-daemon-encoder.c @@ -61,7 +61,7 @@ void tmate_send_client_ready(void) pack(int, TMATE_IN_READY); } -void tmate_send_env(const char *name, const char *value) +void tmate_set_env(const char *name, const char *value) { if (tmate_session->client_protocol_version < 4) return; diff --git a/tmate-protocol.h b/tmate-protocol.h index 0897dc4f..4022fbf8 100644 --- a/tmate-protocol.h +++ b/tmate-protocol.h @@ -5,7 +5,7 @@ #define TMATE_MAX_MESSAGE_SIZE (17*1024) enum tmate_control_out_msg_types { - TMATE_CTL_AUTH, + TMATE_CTL_HEADER, TMATE_CTL_DEAMON_OUT_MSG, TMATE_CTL_SNAPSHOT, TMATE_CTL_CLIENT_JOIN, @@ -14,8 +14,9 @@ enum tmate_control_out_msg_types { }; /* -[TMATE_CTL_AUTH, int: ctl_proto_version, string: ip_address, string: pubkey, - string: session_token, string: session_token_ro] +[TMATE_CTL_HEADER, int: ctl_proto_version, string: ip_address, string: pubkey, + string: session_token, string: session_token_ro, string: ssh_cmd_fmt] + string: client_version, int: client_protocol_version] [TMATE_CTL_DEAMON_OUT_MSG, object: msg] [TMATE_CTL_SNAPSHOT, [[int: pane_id, [int: cur_x, int: cur_y], int: mode, [[string: line_utf8, [int: char_attr, ...]], ...], ...], ...]] diff --git a/tmate-proxy.c b/tmate-proxy.c index 7af94a78..57d18344 100644 --- a/tmate-proxy.c +++ b/tmate-proxy.c @@ -6,7 +6,7 @@ #include "tmate.h" #include "tmate-protocol.h" -#define CONTROL_PROTOCOL_VERSION 1 +#define CONTROL_PROTOCOL_VERSION 2 #define pack(what, ...) _pack(&tmate_session->proxy_encoder, what, __VA_ARGS__) @@ -242,16 +242,27 @@ void tmate_send_proxy_daemon_msg(__unused struct tmate_session *session, void tmate_send_proxy_header(struct tmate_session *session) { + char port_arg[16] = {0}; + char ssh_cmd_fmt[512]; + if (!tmate_has_proxy()) return; - pack(array, 6); - pack(int, TMATE_CTL_AUTH); + pack(array, 9); + pack(int, TMATE_CTL_HEADER); pack(int, CONTROL_PROTOCOL_VERSION); pack(string, session->ssh_client.ip_address); pack(string, session->ssh_client.pubkey); pack(string, session->session_token); pack(string, session->session_token_ro); + + if (tmate_settings->ssh_port != 22) + sprintf(port_arg, " -p%d", tmate_settings->ssh_port); + sprintf(ssh_cmd_fmt, "ssh%s %%s@%s", port_arg, tmate_settings->tmate_host); + pack(string, ssh_cmd_fmt); + + pack(string, session->client_version); + pack(int, session->client_protocol_version); } static void on_proxy_decoder_read(void *userdata, struct tmate_unpacker *uk) diff --git a/tmate-ssh-daemon.c b/tmate-ssh-daemon.c index 55110335..029371d6 100644 --- a/tmate-ssh-daemon.c +++ b/tmate-ssh-daemon.c @@ -80,5 +80,4 @@ void tmate_daemon_init(struct tmate_session *session) tmate_decoder_init(&session->daemon_decoder, on_daemon_decoder_read, session); tmate_init_proxy(session, NULL); - tmate_send_proxy_header(session); } diff --git a/tmate.h b/tmate.h index f425f614..cf1885ba 100644 --- a/tmate.h +++ b/tmate.h @@ -86,8 +86,6 @@ extern void unpack_array(struct tmate_unpacker *uk, struct tmate_unpacker *neste /* tmate-daemon-encoder.c */ -#define TMATE_LATEST_VERSION "1.8.10" - extern void printflike(1, 2) tmate_notify(const char *fmt, ...); extern void printflike(2, 3) tmate_notify_later(int timeout, const char *fmt, ...); @@ -97,7 +95,7 @@ extern void tmate_client_pane_key(int pane_id, key_code key); extern void tmate_client_cmd(int client_id, const char *cmd); extern void tmate_client_set_active_pane(int client_id, int win_idx, int pane_id); extern int tmate_should_exec_cmd_locally(const struct cmd_entry *cmd); -extern void tmate_send_env(const char *name, const char *value); +extern void tmate_set_env(const char *name, const char *value); extern void tmate_send_client_ready(void); extern void tmate_send_mc_obj(msgpack_object *obj); @@ -210,6 +208,7 @@ struct tmate_session { struct tmate_encoder daemon_encoder; struct tmate_decoder daemon_decoder; + const char *client_version; int client_protocol_version; struct event ev_notify_timer;