mirror of
https://github.com/tmate-io/tmate-ssh-server.git
synced 2020-11-18 19:53:51 -08:00
Refactoring
This commit is contained in:
parent
825ad72720
commit
dd1078d183
@ -179,13 +179,13 @@ dist_tmate_slave_SOURCES = \
|
|||||||
signal.c \
|
signal.c \
|
||||||
status.c \
|
status.c \
|
||||||
tmate-debug.c \
|
tmate-debug.c \
|
||||||
tmate-client-decoder.c \
|
tmate-daemon-decoder.c \
|
||||||
tmate-client-encoder.c \
|
tmate-daemon-encoder.c \
|
||||||
tmate-msgpack.c \
|
tmate-msgpack.c \
|
||||||
tmate-master.c \
|
tmate-master.c \
|
||||||
tmate-slave.c \
|
tmate-slave.c \
|
||||||
tmate-ssh-client-pty.c \
|
tmate-ssh-client-pty.c \
|
||||||
tmate-ssh-client.c \
|
tmate-ssh-daemon.c \
|
||||||
tmate-ssh-server.c \
|
tmate-ssh-server.c \
|
||||||
tmux-bare.c \
|
tmux-bare.c \
|
||||||
tty-acs.c \
|
tty-acs.c \
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "tmate.h"
|
#include "tmate.h"
|
||||||
#include "tmate-protocol.h"
|
#include "tmate-protocol.h"
|
||||||
|
|
||||||
#define pack(what, ...) _pack(&tmate_session->client_encoder, what, __VA_ARGS__)
|
#define pack(what, ...) _pack(&tmate_session->daemon_encoder, what, __VA_ARGS__)
|
||||||
|
|
||||||
static void __tmate_notify(const char *msg)
|
static void __tmate_notify(const char *msg)
|
||||||
{
|
{
|
@ -161,7 +161,7 @@ static void set_session_token(struct tmate_session *session,
|
|||||||
memset(cmdline, 0, cmdline_end - cmdline);
|
memset(cmdline, 0, cmdline_end - cmdline);
|
||||||
sprintf(cmdline, "tmate-slave [%s] %s %s",
|
sprintf(cmdline, "tmate-slave [%s] %s %s",
|
||||||
session->session_token,
|
session->session_token,
|
||||||
session->ssh_client.role == TMATE_ROLE_SERVER ? "(server)" : "(client)",
|
session->ssh_client.role == TMATE_ROLE_DAEMON ? "(daemon)" : "(pty client)",
|
||||||
session->ssh_client.ip_address);
|
session->ssh_client.ip_address);
|
||||||
}
|
}
|
||||||
static void create_session_ro_symlink(struct tmate_session *session)
|
static void create_session_ro_symlink(struct tmate_session *session)
|
||||||
@ -297,7 +297,7 @@ static void setup_ncurse(int fd, const char *name)
|
|||||||
tmate_fatal("Cannot setup terminal");
|
tmate_fatal("Cannot setup terminal");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tmate_spawn_slave_server(struct tmate_session *session)
|
static void tmate_spawn_slave_daemon(struct tmate_session *session)
|
||||||
{
|
{
|
||||||
struct tmate_ssh_client *client = &session->ssh_client;
|
struct tmate_ssh_client *client = &session->ssh_client;
|
||||||
|
|
||||||
@ -342,7 +342,7 @@ static void tmate_spawn_slave_server(struct tmate_session *session)
|
|||||||
/* never reached */
|
/* never reached */
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tmate_spawn_slave_client(struct tmate_session *session)
|
static void tmate_spawn_slave_pty_client(struct tmate_session *session)
|
||||||
{
|
{
|
||||||
struct tmate_ssh_client *client = &session->ssh_client;
|
struct tmate_ssh_client *client = &session->ssh_client;
|
||||||
char *argv_rw[] = {(char *)"attach", NULL};
|
char *argv_rw[] = {(char *)"attach", NULL};
|
||||||
@ -400,7 +400,7 @@ static void tmate_spawn_slave_client(struct tmate_session *session)
|
|||||||
|
|
||||||
setup_ncurse(slave_pty, "screen-256color");
|
setup_ncurse(slave_pty, "screen-256color");
|
||||||
|
|
||||||
tmate_ssh_client_pty_init(session);
|
tmate_client_pty_init(session);
|
||||||
|
|
||||||
/* the unused session->master_fd will get closed automatically */
|
/* the unused session->master_fd will get closed automatically */
|
||||||
|
|
||||||
@ -418,9 +418,8 @@ static void tmate_spawn_slave_client(struct tmate_session *session)
|
|||||||
|
|
||||||
void tmate_spawn_slave(struct tmate_session *session)
|
void tmate_spawn_slave(struct tmate_session *session)
|
||||||
{
|
{
|
||||||
|
switch (session->ssh_client.role) {
|
||||||
if (session->ssh_client.role == TMATE_ROLE_SERVER)
|
case TMATE_ROLE_DAEMON: tmate_spawn_slave_daemon(session); break;
|
||||||
tmate_spawn_slave_server(session);
|
case TMATE_ROLE_PTY_CLIENT: tmate_spawn_slave_pty_client(session); break;
|
||||||
else
|
}
|
||||||
tmate_spawn_slave_client(session);
|
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ void tmate_flush_pty(struct tmate_session *session)
|
|||||||
close(session->pty);
|
close(session->pty);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tmate_ssh_client_pty_init(struct tmate_session *session)
|
void tmate_client_pty_init(struct tmate_session *session)
|
||||||
{
|
{
|
||||||
struct tmate_ssh_client *client = &session->ssh_client;
|
struct tmate_ssh_client *client = &session->ssh_client;
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ static void on_master_read(struct bufferevent *bev, void *_session)
|
|||||||
master_in = bufferevent_get_input(session->bev_master);
|
master_in = bufferevent_get_input(session->bev_master);
|
||||||
|
|
||||||
while (evbuffer_get_length(master_in)) {
|
while (evbuffer_get_length(master_in)) {
|
||||||
tmate_decoder_get_buffer(&session->client_decoder, &buf, &len);
|
tmate_decoder_get_buffer(&session->daemon_decoder, &buf, &len);
|
||||||
|
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
tmate_fatal("No more room in client decoder. Message too big?");
|
tmate_fatal("No more room in client decoder. Message too big?");
|
||||||
@ -93,7 +93,7 @@ static int on_ssh_channel_read(ssh_session _session, ssh_channel channel,
|
|||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
while (total_len) {
|
while (total_len) {
|
||||||
tmate_decoder_get_buffer(&session->client_decoder, &buf, &len);
|
tmate_decoder_get_buffer(&session->daemon_decoder, &buf, &len);
|
||||||
|
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
tmate_fatal("No more room in client decoder. Message too big?");
|
tmate_fatal("No more room in client decoder. Message too big?");
|
||||||
@ -103,7 +103,7 @@ static int on_ssh_channel_read(ssh_session _session, ssh_channel channel,
|
|||||||
|
|
||||||
memcpy(buf, data, len);
|
memcpy(buf, data, len);
|
||||||
|
|
||||||
tmate_decoder_commit(&session->client_decoder, len);
|
tmate_decoder_commit(&session->daemon_decoder, len);
|
||||||
|
|
||||||
total_len -= len;
|
total_len -= len;
|
||||||
written += len;
|
written += len;
|
||||||
@ -169,8 +169,8 @@ void tmate_daemon_init(struct tmate_session *session)
|
|||||||
client->channel_cb.channel_data_function = on_ssh_channel_read,
|
client->channel_cb.channel_data_function = on_ssh_channel_read,
|
||||||
ssh_set_channel_callbacks(client->channel, &client->channel_cb);
|
ssh_set_channel_callbacks(client->channel, &client->channel_cb);
|
||||||
|
|
||||||
tmate_encoder_init(&session->client_encoder, on_daemon_encoder_write, session);
|
tmate_encoder_init(&session->daemon_encoder, on_daemon_encoder_write, session);
|
||||||
tmate_decoder_init(&session->client_decoder, on_daemon_decoder_read, session);
|
tmate_decoder_init(&session->daemon_decoder, on_daemon_decoder_read, session);
|
||||||
|
|
||||||
if (tmate_has_master())
|
if (tmate_has_master())
|
||||||
init_master(session);
|
init_master(session);
|
@ -45,7 +45,7 @@ static int shell_request(ssh_session session, ssh_channel channel,
|
|||||||
{
|
{
|
||||||
struct tmate_ssh_client *client = userdata;
|
struct tmate_ssh_client *client = userdata;
|
||||||
|
|
||||||
client->role = TMATE_ROLE_CLIENT;
|
client->role = TMATE_ROLE_PTY_CLIENT;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -56,7 +56,7 @@ static int subsystem_request(ssh_session session, ssh_channel channel,
|
|||||||
struct tmate_ssh_client *client = userdata;
|
struct tmate_ssh_client *client = userdata;
|
||||||
|
|
||||||
if (!strcmp(subsystem, "tmate"))
|
if (!strcmp(subsystem, "tmate"))
|
||||||
client->role = TMATE_ROLE_SERVER;
|
client->role = TMATE_ROLE_DAEMON;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
16
tmate.h
16
tmate.h
@ -103,12 +103,10 @@ extern void unpack_array(struct tmate_unpacker *uk, struct tmate_unpacker *neste
|
|||||||
(tmp_uk)->argc > 0 && (init_unpacker(nested_uk, (tmp_uk)->argv[0]), 1); \
|
(tmp_uk)->argc > 0 && (init_unpacker(nested_uk, (tmp_uk)->argv[0]), 1); \
|
||||||
(tmp_uk)->argv++, (tmp_uk)->argc--)
|
(tmp_uk)->argv++, (tmp_uk)->argc--)
|
||||||
|
|
||||||
/* tmate-client-encoder.c */
|
/* tmate-daemon-encoder.c */
|
||||||
|
|
||||||
#define TMATE_LATEST_VERSION "1.8.10"
|
#define TMATE_LATEST_VERSION "1.8.10"
|
||||||
|
|
||||||
extern void tmate_client_encoder_init(struct tmate_encoder *encoder);
|
|
||||||
|
|
||||||
extern void printflike1 tmate_notify(const char *fmt, ...);
|
extern void printflike1 tmate_notify(const char *fmt, ...);
|
||||||
extern void printflike2 tmate_notify_later(int timeout, const char *fmt, ...);
|
extern void printflike2 tmate_notify_later(int timeout, const char *fmt, ...);
|
||||||
extern void tmate_notify_client_join(struct client *c);
|
extern void tmate_notify_client_join(struct client *c);
|
||||||
@ -122,7 +120,7 @@ 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_send_env(const char *name, const char *value);
|
||||||
extern void tmate_send_client_ready(void);
|
extern void tmate_send_client_ready(void);
|
||||||
|
|
||||||
/* tmate-client-decoder.c */
|
/* tmate-daemon-decoder.c */
|
||||||
|
|
||||||
#define TMATE_HLIMIT 2000
|
#define TMATE_HLIMIT 2000
|
||||||
#define TMATE_PANE_ACTIVE 1
|
#define TMATE_PANE_ACTIVE 1
|
||||||
@ -140,7 +138,7 @@ extern void tmate_daemon_init(struct tmate_session *session);
|
|||||||
|
|
||||||
/* tmate-ssh-client-pty.c */
|
/* tmate-ssh-client-pty.c */
|
||||||
|
|
||||||
extern void tmate_ssh_client_pty_init(struct tmate_session *session);
|
extern void tmate_client_pty_init(struct tmate_session *session);
|
||||||
extern void tmate_flush_pty(struct tmate_session *session);
|
extern void tmate_flush_pty(struct tmate_session *session);
|
||||||
|
|
||||||
/* tmate-ssh-server.c */
|
/* tmate-ssh-server.c */
|
||||||
@ -148,8 +146,8 @@ extern void tmate_flush_pty(struct tmate_session *session);
|
|||||||
#define TMATE_SSH_BANNER "tmate"
|
#define TMATE_SSH_BANNER "tmate"
|
||||||
#define TMATE_SSH_KEEPALIVE 60
|
#define TMATE_SSH_KEEPALIVE 60
|
||||||
|
|
||||||
#define TMATE_ROLE_SERVER 1
|
#define TMATE_ROLE_DAEMON 1
|
||||||
#define TMATE_ROLE_CLIENT 2
|
#define TMATE_ROLE_PTY_CLIENT 2
|
||||||
|
|
||||||
struct tmate_ssh_client {
|
struct tmate_ssh_client {
|
||||||
char ip_address[64];
|
char ip_address[64];
|
||||||
@ -213,8 +211,8 @@ struct tmate_session {
|
|||||||
const char *session_token;
|
const char *session_token;
|
||||||
const char *session_token_ro;
|
const char *session_token_ro;
|
||||||
|
|
||||||
struct tmate_encoder client_encoder;
|
struct tmate_encoder daemon_encoder;
|
||||||
struct tmate_decoder client_decoder;
|
struct tmate_decoder daemon_decoder;
|
||||||
int client_protocol_version;
|
int client_protocol_version;
|
||||||
struct event ev_notify_timer;
|
struct event ev_notify_timer;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user