改进:TP核心服务每60秒向已连接的客户端和远程主机发送keep-alive消息,防止连接中断。但仍保留无操作超时就断开的限制。

dev
Apex Liu 2020-06-19 02:22:26 +08:00
parent 7b46c3bc63
commit e7635e7756
4 changed files with 195 additions and 171 deletions

View File

@ -6,7 +6,8 @@ SshProxy g_ssh_proxy;
SshProxy::SshProxy() : SshProxy::SshProxy() :
ExThreadBase("ssh-proxy-thread"), ExThreadBase("ssh-proxy-thread"),
m_bind(NULL) { m_bind(NULL) {
m_timer_counter = 0; m_timer_counter_check_noop = 0;
m_timer_counter_send_keep_alive= 0;
m_noop_timeout_sec = 900; // default to 15 minutes. m_noop_timeout_sec = 900; // default to 15 minutes.
m_listener_running = false; m_listener_running = false;
} }
@ -59,21 +60,33 @@ bool SshProxy::init() {
} }
void SshProxy::timer() { void SshProxy::timer() {
// timer() will be called per one second, and I will do my job per 5 seconds. // timer() will be called per one second
m_timer_counter++; m_timer_counter_check_noop++;
if (m_timer_counter < 5) m_timer_counter_send_keep_alive++;
return;
m_timer_counter = 0; // check no-op per 5 seconds.
if (m_timer_counter_check_noop >= 5) {
m_timer_counter_check_noop = 0;
ExThreadSmartLock locker(m_lock); ExThreadSmartLock locker(m_lock);
ex_u32 t_now = (ex_u32) time(NULL); ex_u32 t_now = (ex_u32)time(NULL);
ts_ssh_sessions::iterator it; ts_ssh_sessions::iterator it;
for (it = m_sessions.begin(); it != m_sessions.end(); ++it) { for (it = m_sessions.begin(); it != m_sessions.end(); ++it) {
it->first->save_record(); it->first->save_record();
if (0 != m_noop_timeout_sec) if (0 != m_noop_timeout_sec)
it->first->check_noop_timeout(t_now, m_noop_timeout_sec); it->first->check_noop_timeout(t_now, m_noop_timeout_sec);
}
}
// send keep-alive every 60 seconds
if (m_timer_counter_send_keep_alive >= 60) {
m_timer_counter_send_keep_alive = 0;
ExThreadSmartLock locker(m_lock);
ts_ssh_sessions::iterator it;
for (it = m_sessions.begin(); it != m_sessions.end(); ++it) {
it->first->send_keep_alive();
}
} }
} }

View File

@ -26,7 +26,8 @@ protected:
private: private:
ssh_bind m_bind; ssh_bind m_bind;
int m_timer_counter; int m_timer_counter_check_noop;
int m_timer_counter_send_keep_alive;
ExThreadLock m_lock; ExThreadLock m_lock;
bool m_listener_running; bool m_listener_running;

View File

@ -399,7 +399,7 @@ void SshSession::save_record() {
} }
void SshSession::check_noop_timeout(ex_u32 t_now, ex_u32 timeout) { void SshSession::check_noop_timeout(ex_u32 t_now, ex_u32 timeout) {
ExThreadSmartLock locker(m_lock); // ExThreadSmartLock locker(m_lock);
tp_channels::iterator it = m_channels.begin(); tp_channels::iterator it = m_channels.begin();
for (; it != m_channels.end(); ++it) { for (; it != m_channels.end(); ++it) {
if ((*it)->need_close) if ((*it)->need_close)
@ -415,6 +415,15 @@ void SshSession::check_noop_timeout(ex_u32 t_now, ex_u32 timeout) {
} }
} }
void SshSession::send_keep_alive() {
EXLOGD("[ssh] send keep-alive.\n");
if(m_srv_session)
ssh_send_keepalive(m_srv_session);
if (m_cli_session)
ssh_send_keepalive(m_cli_session);
}
int SshSession::_on_auth_password_request(ssh_session session, const char *user, const char *password, void *userdata) { int SshSession::_on_auth_password_request(ssh_session session, const char *user, const char *password, void *userdata) {
// here, `user` is the session-id we need. // here, `user` is the session-id we need.
SshSession *_this = (SshSession *) userdata; SshSession *_this = (SshSession *) userdata;

View File

@ -1,4 +1,4 @@
#ifndef __SSH_SESSION_H__ #ifndef __SSH_SESSION_H__
#define __SSH_SESSION_H__ #define __SSH_SESSION_H__
#include "ssh_recorder.h" #include "ssh_recorder.h"
@ -78,6 +78,7 @@ public:
void save_record(); void save_record();
// //
void check_noop_timeout(ex_u32 t_now, ex_u32 timeout); void check_noop_timeout(ex_u32 t_now, ex_u32 timeout);
void send_keep_alive();
const ex_astr& sid() { return m_sid; } const ex_astr& sid() { return m_sid; }
@ -140,13 +141,13 @@ private:
int m_ssh_ver; int m_ssh_ver;
// 一个ssh_session中可以打开多个ssh_channel // 一个ssh_session中可以打开多个ssh_channel
tp_channels m_channels; tp_channels m_channels;
bool m_have_error; bool m_have_error;
bool m_recving_from_srv; // 是否正在从服务器接收数据? bool m_recving_from_srv; // 是否正在从服务器接收数据?
bool m_recving_from_cli; // 是否正在从客户端接收数据? bool m_recving_from_cli; // 是否正在从客户端接收数据?
struct ssh_server_callbacks_struct m_srv_cb; struct ssh_server_callbacks_struct m_srv_cb;
struct ssh_channel_callbacks_struct m_cli_channel_cb; struct ssh_channel_callbacks_struct m_cli_channel_cb;