try to fix thread problem when use libuv.

pull/130/head
Apex Liu 2018-11-03 14:11:02 +08:00
parent 25d562ccf4
commit b1524fd6a0
16 changed files with 1355 additions and 1422 deletions

View File

@ -31,10 +31,12 @@ public:
bool terminate(void);
protected:
// 线程循环
// main loop of this thread.
virtual void _thread_loop(void) = 0;
// 设置停止标志,让线程能够正常结束
virtual void _set_stop_flag(void) = 0;
// called by another thread when thread ready to stop.
virtual void _on_stop(void) {};
// called inside thread when thread fully stopped.
virtual void _on_stopped(void) {};
#ifdef EX_OS_WIN32
static unsigned int WINAPI _thread_func(LPVOID lpParam);
@ -46,7 +48,7 @@ protected:
ex_astr m_thread_name;
EX_THREAD_HANDLE m_handle;
bool m_is_running;
bool m_stop_flag;
bool m_need_stop;
};

View File

@ -9,36 +9,38 @@
#ifdef EX_OS_WIN32
unsigned int WINAPI ExThreadBase::_thread_func(LPVOID pParam)
#else
void *ExThreadBase::_thread_func(void *pParam)
#endif
{
ExThreadBase* p = (ExThreadBase*)pParam;
ex_astr thread_name = p->m_thread_name;
p->m_is_running = true;
p->_thread_loop();
p->m_is_running = false;
// if(!p->m_stop_by_request)
// p->m_thread_manager->_remove_thread(p);
ExThreadBase *_this = (ExThreadBase *) pParam;
EXLOGV(" # thread [%s] end.\n", thread_name.c_str());
_this->m_is_running = true;
_this->_thread_loop();
_this->m_is_running = false;
_this->m_handle = 0;
_this->_on_stopped();
EXLOGV(" # thread [%s] exit.\n", _this->m_thread_name.c_str());
return 0;
}
ExThreadBase::ExThreadBase(const char *thread_name) :
m_handle(0),
m_is_running(false),
m_stop_flag(false)
{
m_need_stop(false) {
m_thread_name = thread_name;
}
ExThreadBase::~ExThreadBase()
{
ExThreadBase::~ExThreadBase() {
if(m_is_running) {
EXLOGE(" # thread [%s] not stop before destroy.\n", m_thread_name.c_str());
}
}
bool ExThreadBase::start(void)
{
bool ExThreadBase::start(void) {
m_need_stop = false;
EXLOGV(" . thread [%s] starting.\n", m_thread_name.c_str());
#ifdef WIN32
HANDLE h = (HANDLE)_beginthreadex(NULL, 0, _thread_func, (void*)this, 0, NULL);
@ -47,12 +49,11 @@ bool ExThreadBase::start(void)
{
return false;
}
m_handle = h;
m_listener_handle = h;
#else
pthread_t ptid = 0;
int ret = pthread_create(&ptid, NULL, _thread_func, (void *) this);
if (ret != 0)
{
if (ret != 0) {
return false;
}
m_handle = ptid;
@ -62,24 +63,25 @@ bool ExThreadBase::start(void)
return true;
}
bool ExThreadBase::stop(void)
{
EXLOGV("[thread] try to stop thread [%s].\n", m_thread_name.c_str());
_set_stop_flag();
EXLOGV("[thread] wait thread [%s] end.\n", m_thread_name.c_str());
if(m_handle == 0)
bool ExThreadBase::stop(void) {
if (m_handle == 0) {
EXLOGW("[thread] thread [%s] already stopped.\n", m_thread_name.c_str());
return true;
}
EXLOGV("[thread] try to stop thread [%s].\n", m_thread_name.c_str());
m_need_stop = true;
_on_stop();
EXLOGV("[thread] wait thread [%s] exit.\n", m_thread_name.c_str());
#ifdef EX_OS_WIN32
if (WaitForSingleObject(m_handle, INFINITE) != WAIT_OBJECT_0)
if (WaitForSingleObject(m_listener_handle, INFINITE) != WAIT_OBJECT_0)
{
return false;
}
#else
if (pthread_join(m_handle, NULL) != 0)
{
if (pthread_join(m_handle, NULL) != 0) {
return false;
}
#endif
@ -87,12 +89,11 @@ bool ExThreadBase::stop(void)
return true;
}
bool ExThreadBase::terminate(void)
{
bool ExThreadBase::terminate(void) {
#ifdef EX_OS_WIN32
return TerminateThread(m_handle, 1) ? true : false;
return (TerminateThread(m_listener_handle, 1) == TRUE);
#else
return pthread_cancel(m_handle) == 0 ? true : false;
return (pthread_cancel(m_handle) == 0);
#endif
}
@ -100,39 +101,31 @@ bool ExThreadBase::terminate(void)
//
//=========================================================
ExThreadManager::ExThreadManager()
{}
ExThreadManager::ExThreadManager() {}
ExThreadManager::~ExThreadManager()
{
if (m_threads.size() > 0)
{
ExThreadManager::~ExThreadManager() {
if (!m_threads.empty()) {
EXLOGE("when destroy thread manager, there are %d thread not exit.\n", m_threads.size());
stop_all();
}
}
void ExThreadManager::stop_all(void)
{
void ExThreadManager::stop_all(void) {
ExThreadSmartLock locker(m_lock);
ex_threads::iterator it = m_threads.begin();
for (; it != m_threads.end(); ++it)
{
for (; it != m_threads.end(); ++it) {
(*it)->stop();
}
m_threads.clear();
}
void ExThreadManager::add(ExThreadBase* tb)
{
void ExThreadManager::add(ExThreadBase *tb) {
ExThreadSmartLock locker(m_lock);
ex_threads::iterator it = m_threads.begin();
for (; it != m_threads.end(); ++it)
{
if ((*it) == tb)
{
for (; it != m_threads.end(); ++it) {
if ((*it) == tb) {
EXLOGE("when add thread to manager, it already exist.\n");
return;
}
@ -141,29 +134,24 @@ void ExThreadManager::add(ExThreadBase* tb)
m_threads.push_back(tb);
}
void ExThreadManager::remove(ExThreadBase* tb)
{
void ExThreadManager::remove(ExThreadBase *tb) {
ExThreadSmartLock locker(m_lock);
ex_threads::iterator it = m_threads.begin();
for (; it != m_threads.end(); ++it)
{
if ((*it) == tb)
{
//delete (*it);
for (; it != m_threads.end(); ++it) {
if ((*it) == tb) {
m_threads.erase(it);
return;
}
}
EXLOGE("when remove thread from manager, it not exist.\n");
EXLOGE("thread not hold by thread-manager while remove it.\n");
}
//=========================================================
//
//=========================================================
ExThreadLock::ExThreadLock()
{
ExThreadLock::ExThreadLock() {
#ifdef EX_OS_WIN32
InitializeCriticalSection(&m_locker);
#else
@ -175,8 +163,7 @@ ExThreadLock::ExThreadLock()
#endif
}
ExThreadLock::~ExThreadLock()
{
ExThreadLock::~ExThreadLock() {
#ifdef EX_OS_WIN32
DeleteCriticalSection(&m_locker);
#else
@ -184,8 +171,7 @@ ExThreadLock::~ExThreadLock()
#endif
}
void ExThreadLock::lock(void)
{
void ExThreadLock::lock(void) {
#ifdef EX_OS_WIN32
EnterCriticalSection(&m_locker);
#else
@ -193,8 +179,7 @@ void ExThreadLock::lock(void)
#endif
}
void ExThreadLock::unlock(void)
{
void ExThreadLock::unlock(void) {
#ifdef EX_OS_WIN32
LeaveCriticalSection(&m_locker);
#else
@ -206,8 +191,7 @@ void ExThreadLock::unlock(void)
//
//=========================================================
int ex_atomic_add(volatile int* pt, int t)
{
int ex_atomic_add(volatile int *pt, int t) {
#ifdef EX_OS_WIN32
return (int)InterlockedExchangeAdd((long*)pt, (long)t);
#else
@ -215,8 +199,7 @@ int ex_atomic_add(volatile int* pt, int t)
#endif
}
int ex_atomic_inc(volatile int* pt)
{
int ex_atomic_inc(volatile int *pt) {
#ifdef EX_OS_WIN32
return (int)InterlockedIncrement((long*)pt);
#else
@ -224,8 +207,7 @@ int ex_atomic_inc(volatile int* pt)
#endif
}
int ex_atomic_dec(volatile int* pt)
{
int ex_atomic_dec(volatile int *pt) {
#ifdef EX_OS_WIN32
return (int)InterlockedDecrement((long*)pt);
#else
@ -234,8 +216,7 @@ int ex_atomic_dec(volatile int* pt)
}
ex_u64 ex_get_thread_id(void)
{
ex_u64 ex_get_thread_id(void) {
#ifdef EX_OS_WIN32
return GetCurrentThreadId();
#else

View File

@ -31,11 +31,9 @@
<file url="file://$PROJECT_DIR$/tp_core/core/ts_web_rpc.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/rdp/rdp_conn.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/rdp/rdp_conn.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/rdp/rdp_keys.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/rdp/rdp_package.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/rdp/rdp_package.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/rdp/rdp_proxy.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/rdp/rdp_session.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/rdp/rdp_session.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/ssh/ssh_proxy.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/ssh/ssh_proxy.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/ssh/ssh_recorder.cpp" charset="GBK" />
@ -43,7 +41,10 @@
<file url="file://$PROJECT_DIR$/tp_core/protocol/ssh/ssh_session.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/ssh/ssh_session.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/telnet/telnet_conn.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/telnet/telnet_conn.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/telnet/telnet_proxy.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/telnet/telnet_session.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/telnet/telnet_session.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_web/src/main.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_web/src/ts_env.cpp" charset="GBK" />
</component>

View File

@ -4,7 +4,8 @@ project(teleport)
MESSAGE(STATUS "operation system is ${CMAKE_SYSTEM}")
MESSAGE(STATUS "current source directory is ${CMAKE_CURRENT_SOURCE_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${teleport_SOURCE_DIR}/../out/server/x64/bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../out/server/x64/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../out/server/x64/bin")
set(CMAKE_CONFIGURATION_TYPES Debug Release)
@ -13,13 +14,13 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
MESSAGE(STATUS "build on macOS...")
set(OS_MACOS 1)
set(OS_POSIX 1)
set(TP_EXTERNAL_RELEASE_DIR "${teleport_SOURCE_DIR}/../external/macos/release")
set(TP_EXTERNAL_RELEASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../external/macos/release")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(OS_LINUX 1)
set(OS_POSIX 1)
MESSAGE(STATUS "build on Linux...")
add_subdirectory(tp_web/src)
set(TP_EXTERNAL_RELEASE_DIR "${teleport_SOURCE_DIR}/../external/linux/release")
set(TP_EXTERNAL_RELEASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../external/linux/release")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
MESSAGE(FATAL_ERROR "unsupported platform: Windows")
else()

View File

@ -359,9 +359,8 @@ void _sig_handler(int signum, siginfo_t* info, void* ptr)
{
if (signum == SIGINT || signum == SIGTERM)
{
EXLOGW("[core] received signal SIGINT, exit now.\n");
EXLOGW("\n[core] received signal SIGINT, exit now.\n");
g_exit_flag = true;
// exit(1);
}
}

View File

@ -62,7 +62,7 @@ void TsHttpRpc::_thread_loop(void)
{
EXLOGI("[core] TeleportServer-RPC ready on %s:%d\n", m_host_ip.c_str(), m_host_port);
while (!m_stop_flag)
while (!m_need_stop)
{
mg_mgr_poll(&m_mg_mgr, 500);
}
@ -70,10 +70,6 @@ void TsHttpRpc::_thread_loop(void)
EXLOGV("[core] rpc main loop end.\n");
}
void TsHttpRpc::_set_stop_flag(void)
{
m_stop_flag = true;
}
bool TsHttpRpc::init(void)
{

View File

@ -20,7 +20,6 @@ public:
protected:
void _thread_loop(void);
void _set_stop_flag(void);
private:
ex_rv _parse_request(struct http_message* req, ex_astr& func_cmd, Json::Value& json_param);

View File

@ -10,8 +10,7 @@
bool g_exit_flag = false;
TPP_CONNECT_INFO* tpp_get_connect_info(const char* sid)
{
TPP_CONNECT_INFO *tpp_get_connect_info(const char *sid) {
TS_CONNECT_INFO sinfo;
bool ret = g_session_mgr.get_connect_info(sid, sinfo);
if (!ret)
@ -51,8 +50,7 @@ TPP_CONNECT_INFO* tpp_get_connect_info(const char* sid)
return info;
}
void tpp_free_connect_info(TPP_CONNECT_INFO* info)
{
void tpp_free_connect_info(TPP_CONNECT_INFO *info) {
if (NULL == info)
return;
@ -70,8 +68,7 @@ void tpp_free_connect_info(TPP_CONNECT_INFO* info)
free(info);
}
bool tpp_session_begin(const TPP_CONNECT_INFO* info, int* db_id)
{
bool tpp_session_begin(const TPP_CONNECT_INFO *info, int *db_id) {
if (NULL == info || NULL == db_id)
return false;
@ -246,8 +243,7 @@ bool tpp_session_end(const char* sid, int db_id, int ret) {
// }
// }
int ts_main(void)
{
int ts_main(void) {
ExIniFile &ini = g_env.get_ini();
EXLOGI(L"\n");
@ -262,39 +258,33 @@ int ts_main(void)
bool all_ok = true;
do {
if (!g_session_mgr.start())
{
if (!g_session_mgr.start()) {
EXLOGE(L"[core] failed to start session-id manager.\n");
all_ok = false;
break;
}
if (!rpc.init() || !rpc.start())
{
if (!rpc.init() || !rpc.start()) {
EXLOGE(L"[core] rpc init/start failed.\n");
all_ok = false;
break;
}
ex_ini_sections::iterator it = secs.begin();
for (; it != secs.end(); ++it)
{
if (it->first.length() > 9 && 0 == wcsncmp(it->first.c_str(), L"protocol-", 9))
{
for (; it != secs.end(); ++it) {
if (it->first.length() > 9 && 0 == wcsncmp(it->first.c_str(), L"protocol-", 9)) {
ex_wstr libname;
if (!it->second->GetStr(L"lib", libname))
continue;
bool enabled = false;
it->second->GetBool(L"enabled", enabled, false);
if (!enabled)
{
if (!enabled) {
EXLOGV(L"[core] `%ls` not enabled.\n", libname.c_str());
continue;
}
if (!g_tpp_mgr.load_tpp(libname))
{
if (!g_tpp_mgr.load_tpp(libname)) {
all_ok = false;
break;
}
@ -303,13 +293,11 @@ int ts_main(void)
} while (0);
if (0 == g_tpp_mgr.count())
{
if (0 == g_tpp_mgr.count()) {
all_ok = false;
}
if (!all_ok)
{
if (!all_ok) {
g_exit_flag = true;
}
@ -317,13 +305,13 @@ int ts_main(void)
ts_web_rpc_register_core();
EXLOGV("[core] ---- initialized, ready for service ----\n");
while (!g_exit_flag)
{
while (!g_exit_flag) {
ex_sleep_ms(1000);
g_tpp_mgr.timer();
}
}
EXLOGW("[core] try to stop all thread and exit.\n");
g_tpp_mgr.stop_all();
rpc.stop();
g_session_mgr.stop();

View File

@ -7,64 +7,47 @@
TsSessionManager g_session_mgr;
TsSessionManager::TsSessionManager() :
ExThreadBase("sid-mgr-thread")
{
ExThreadBase("sid-mgr-thread") {
}
TsSessionManager::~TsSessionManager()
{
TsSessionManager::~TsSessionManager() {
ts_connections::iterator it_conn = m_connections.begin();
for (; it_conn != m_connections.end(); ++it_conn)
{
for (; it_conn != m_connections.end(); ++it_conn) {
delete it_conn->second;
}
m_connections.clear();
}
void TsSessionManager::_thread_loop(void)
{
for (;;)
{
void TsSessionManager::_thread_loop(void) {
for (;;) {
ex_sleep_ms(1000);
if (m_stop_flag)
if (m_need_stop)
return;
_remove_expired_connect_info();
}
}
void TsSessionManager::_set_stop_flag(void)
{
m_stop_flag = true;
}
void TsSessionManager::_remove_expired_connect_info(void)
{
void TsSessionManager::_remove_expired_connect_info(void) {
// 超过15秒未进行连接的connect-info会被移除
ExThreadSmartLock locker(m_lock);
ex_u64 _now = ex_get_tick_count();
ts_connections::iterator it = m_connections.begin();
for (; it != m_connections.end(); )
{
for (; it != m_connections.end();) {
//EXLOGD("[core] check expired connect info: [%s] %d, %d %d %d\n", it->first.c_str(), it->second->ref_count, int(_now), int(it->second->ticket_start), int(_now - it->second->ticket_start));
if (it->second->ref_count == 0 && _now - 15000 > it->second->ticket_start)
{
if (it->second->ref_count == 0 && _now - 15000 > it->second->ticket_start) {
EXLOGD("[core] remove connection info, because timeout: %s\n", it->first.c_str());
delete it->second;
m_connections.erase(it++);
EXLOGD("[core] there are %d connection info exists.\n", m_connections.size());
}
else
{
} else {
++it;
}
}
}
bool TsSessionManager::get_connect_info(const ex_astr& sid, TS_CONNECT_INFO& info)
{
bool TsSessionManager::get_connect_info(const ex_astr &sid, TS_CONNECT_INFO &info) {
ExThreadSmartLock locker(m_lock);
ts_connections::iterator it = m_connections.find(sid);
@ -113,8 +96,7 @@ bool TsSessionManager::free_connect_info(const ex_astr& sid) {
m_connections.erase(it);
EXLOGD("[core] there are %d connection info exists.\n", m_connections.size());
}
}
else {
} else {
if (it->second->ref_count == 1)
it->second->ref_count = 0;
it->second->ticket_start = ex_get_tick_count() + 45000; // 我们将时间向后移动45秒这样如果没有发生RDP的第二次连接这个连接信息就会在一分钟后被清除。
@ -124,17 +106,16 @@ bool TsSessionManager::free_connect_info(const ex_astr& sid) {
return true;
}
bool TsSessionManager::request_session(ex_astr& sid, TS_CONNECT_INFO* info)
{
bool TsSessionManager::request_session(ex_astr &sid, TS_CONNECT_INFO *info) {
ExThreadSmartLock locker(m_lock);
EXLOGD("[core] request session: account: [%s], protocol: [%d], auth-mode: [%d]\n", info->acc_username.c_str(), info->protocol_type, info->auth_type);
EXLOGD("[core] request session: account: [%s], protocol: [%d], auth-mode: [%d]\n", info->acc_username.c_str(),
info->protocol_type, info->auth_type);
ex_astr _sid;
int retried = 0;
ts_connections::iterator it;
for (;;)
{
for (;;) {
_gen_session_id(_sid, info, 6);
it = m_connections.find(_sid);
if (it == m_connections.end())
@ -151,8 +132,7 @@ bool TsSessionManager::request_session(ex_astr& sid, TS_CONNECT_INFO* info)
m_connections.insert(std::make_pair(_sid, info));
sid = _sid;
if (info->protocol_type == TP_PROTOCOL_TYPE_RDP)
{
if (info->protocol_type == TP_PROTOCOL_TYPE_RDP) {
info->ref_count = 1; // 因为RDP连接之前可能会有很长时间用于确认是否连接、是否信任证书所以很容易超时我们认为将引用计数+1防止因超时被清除。
char szTmp[8] = {0};
snprintf(szTmp, 8, "%02X", (unsigned char) (info->acc_username.length() + info->acc_secret.length()));
@ -162,8 +142,7 @@ bool TsSessionManager::request_session(ex_astr& sid, TS_CONNECT_INFO* info)
return true;
}
void TsSessionManager::_gen_session_id(ex_astr& sid, const TS_CONNECT_INFO* info, int len)
{
void TsSessionManager::_gen_session_id(ex_astr &sid, const TS_CONNECT_INFO *info, int len) {
mbedtls_sha1_context sha;
ex_u8 sha_digist[20] = {0};
@ -184,8 +163,7 @@ void TsSessionManager::_gen_session_id(ex_astr& sid, const TS_CONNECT_INFO* info
int _len = len / 2 + 1;
int i = 0;
int offset = 0;
for (i = 0; i < _len; ++i)
{
for (i = 0; i < _len; ++i) {
snprintf(szTmp + offset, 64 - offset, "%02X", sha_digist[i]);
offset += 2;
}

View File

@ -57,10 +57,7 @@ public:
bool free_connect_info(const ex_astr& sid);
protected:
// 线程循环
void _thread_loop(void);
// 设置停止标志,让线程能够正常结束
void _set_stop_flag(void);
private:
void _gen_session_id(ex_astr& sid, const TS_CONNECT_INFO* info, int len);

View File

@ -4,8 +4,7 @@
#include "../../common/ts_const.h"
#include <teleport_const.h>
ex_astr _uv_str_error(int retcode)
{
ex_astr _uv_str_error(int retcode) {
ex_astr err;
err = uv_err_name(retcode);
err += ":";
@ -17,8 +16,7 @@ TelnetConn::TelnetConn(TelnetSession *sess, bool is_server_side) : m_session(ses
if (is_server_side) {
m_name = "cli<->tp";
m_state = TELNET_CONN_STATE_CONNECTED;
}
else {
} else {
m_name = "tp<->srv";
m_state = TELNET_CONN_STATE_FREE;
}
@ -27,6 +25,8 @@ TelnetConn::TelnetConn(TelnetSession *sess, bool is_server_side) : m_session(ses
uv_tcp_init(sess->get_loop(), &m_handle);
m_handle.data = this;
uv_async_init(sess->get_loop(), &m_stop_handle, _on_stop_cb);
m_stop_handle.data = this;
}
TelnetConn::~TelnetConn() {
@ -44,6 +44,12 @@ bool TelnetConn::start_recv() {
}
void TelnetConn::close() {
if (m_state == TELNET_CONN_STATE_FREE || m_state == TELNET_CONN_STATE_CLOSING)
return;
uv_async_send(&m_stop_handle);
}
void TelnetConn::_do_close() {
if (m_state == TELNET_CONN_STATE_FREE || m_state == TELNET_CONN_STATE_CLOSING)
return;
@ -62,6 +68,18 @@ void TelnetConn::close() {
uv_close(handle(), _uv_on_closed);
}
//static
void TelnetConn::_on_stop_cb(uv_async_t *handle) {
TelnetConn *_this = (TelnetConn *) handle->data;
uv_close((uv_handle_t *) &_this->m_stop_handle, _this->_on_stop_handler_closed);
}
//static
void TelnetConn::_on_stop_handler_closed(uv_handle_t *handle) {
TelnetConn *_this = (TelnetConn *) handle->data;
_this->_do_close();
}
void TelnetConn::_uv_on_closed(uv_handle_t *handle) {
TelnetConn *_this = (TelnetConn *) handle->data;
_this->m_state = TELNET_CONN_STATE_FREE;
@ -69,7 +87,7 @@ void TelnetConn::_uv_on_closed(uv_handle_t *handle) {
}
void TelnetConn::_on_alloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
TelnetConn *_this = (TelnetConn *) handle->data;
// TelnetConn *_this = (TelnetConn *) handle->data;
buf->base = (char *) calloc(1, suggested_size);
buf->len = suggested_size;
}
@ -80,8 +98,7 @@ void TelnetConn::_on_recv(uv_stream_t *handle, ssize_t nread, const uv_buf_t *bu
if (nread == 0) {
free(buf->base);
return;
}
else if (nread < 0) {
} else if (nread < 0) {
free(buf->base);
if (nread == UV_EOF)
@ -101,8 +118,7 @@ void TelnetConn::_on_recv(uv_stream_t *handle, ssize_t nread, const uv_buf_t *bu
_this->m_session->close(TP_SESS_STAT_END);
return;
}
else {
} else {
// #ifdef LOG_DATA
// if(!_this->m_session->is_relay())
// EXLOG_BIN((ex_u8*)buf->base, nread, "[telnet] [%s] RECV %d.", _this->m_name, nread);
@ -170,13 +186,14 @@ void TelnetConn::connect(const char *server_ip, ex_u16 server_port) {
if (m_state == TELNET_CONN_STATE_CONNECTED) {
// 当前已经连接上了服务器了,断开重连
EXLOGV("[telnet] [%s] [%s] try to disconnect. %s:%d\n", m_name, m_session->client_addr(), server_ip, server_port);
EXLOGV("[telnet] [%s] [%s] try to disconnect from real TELNET server %s:%d and reconnect.\n", m_name,
m_session->client_addr(), server_ip, server_port);
m_state = TELNET_CONN_STATE_CLOSING;
uv_close((uv_handle_t *) &m_handle, _uv_on_reconnect);
return;
}
else {
EXLOGV("[telnet] [%s] [%s] try to connect to real TELNET server at %s:%d\n", m_name, m_session->client_addr(), server_ip, server_port);
} else {
EXLOGV("[telnet] [%s] [%s] try to connect to real TELNET server %s:%d\n", m_name, m_session->client_addr(),
server_ip, server_port);
}
struct sockaddr_in addr;
@ -197,7 +214,6 @@ void TelnetConn::connect(const char *server_ip, ex_u16 server_port) {
m_timer_running = true;
//m_is_connecting = true;
m_state = TELNET_CONN_STATE_CONNECTING;
int err = 0;
if ((err = uv_tcp_connect(conn_req, &m_handle, (const struct sockaddr *) &addr, _uv_on_connected)) != 0) {
@ -206,25 +222,24 @@ void TelnetConn::connect(const char *server_ip, ex_u16 server_port) {
m_timer_running = false;
uv_timer_stop(&m_timer_connect_timeout);
uv_close((uv_handle_t*)&m_timer_connect_timeout, _uv_on_timer_connect_timeout_closed);
uv_close((uv_handle_t *) &m_timer_connect_timeout, NULL);
m_state = TELNET_CONN_STATE_FREE;
m_session->close(TP_SESS_STAT_ERR_CONNECT);
}
}
void TelnetConn::_uv_on_connect_timeout(uv_timer_t *timer)
{
void TelnetConn::_uv_on_connect_timeout(uv_timer_t *timer) {
TelnetConn *_this = (TelnetConn *) timer->data;
if (_this->m_timer_running) {
_this->m_timer_running = false;
uv_timer_stop(&_this->m_timer_connect_timeout);
uv_close((uv_handle_t*)&_this->m_timer_connect_timeout, _this->_uv_on_timer_connect_timeout_closed);
uv_close((uv_handle_t *) &_this->m_timer_connect_timeout, NULL);
}
// 如果在连接成功之前就超时了,则关闭连接
EXLOGE("[telnet] [%s] timeout when connect to real TELNET server, cancel connection.\n", _this->m_name);
EXLOGE("[telnet] [%s] timeout when connect to real TELNET server.\n", _this->m_name);
_this->m_state = TELNET_CONN_STATE_CLOSING;
uv_close(_this->handle(), _uv_on_closed);
}
@ -246,7 +261,7 @@ void TelnetConn::_uv_on_connected(uv_connect_t *req, int status) {
if (_this->m_timer_running) {
_this->m_timer_running = false;
uv_timer_stop(&_this->m_timer_connect_timeout);
uv_close((uv_handle_t*)&_this->m_timer_connect_timeout, _this->_uv_on_timer_connect_timeout_closed);
uv_close((uv_handle_t *) &_this->m_timer_connect_timeout, NULL);
}
if (status != 0) {
@ -268,5 +283,5 @@ void TelnetConn::_uv_on_connected(uv_connect_t *req, int status) {
}
//static
void TelnetConn::_uv_on_timer_connect_timeout_closed(uv_handle_t *handle) {
}
//void TelnetConn::_uv_on_timer_connect_timeout_closed(uv_handle_t *handle) {
//}

View File

@ -19,6 +19,7 @@ class TelnetSession;
class TelnetConn {
public:
TelnetConn(TelnetSession *sess, bool is_server_side);
~TelnetConn();
TelnetSession *session() { return m_session; }
@ -27,32 +28,50 @@ public:
const char *name() const { return m_name; }
bool is_server_side() const { return m_is_server; }
ex_u8 state() const { return m_state; }
uv_handle_t *handle() { return (uv_handle_t *) &m_handle; }
uv_tcp_t *tcp_handle() { return &m_handle; }
uv_stream_t *stream_handle() { return (uv_stream_t *) &m_handle; }
MemBuffer &data() { return m_buf_data; }
bool send(MemBuffer &mbuf);
bool send(const ex_u8 *data, size_t size);
// connect to real server, for proxy-client-side only.
void connect(const char *server_ip, ex_u16 server_port = 3389);
// try to close this connection. return current TELNET_CONN_STATE_XXXX.
void close();
bool start_recv();
private:
static void _on_alloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
static void _on_recv(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf);
static void _on_send_done(uv_write_t *req, int status);
static void _uv_on_connect_timeout(uv_timer_t *timer);
static void _uv_on_connected(uv_connect_t *req, int status);
static void _uv_on_reconnect(uv_handle_t *handle);
static void _uv_on_closed(uv_handle_t *handle);
static void _uv_on_timer_connect_timeout_closed(uv_handle_t *handle);
// static void _uv_on_timer_connect_timeout_closed(uv_handle_t *handle);
static void _on_stop_cb(uv_async_t *handle);
static void _on_stop_handler_closed(uv_handle_t *handle);
void _do_close();
bool _raw_send(const ex_u8 *data, size_t size);
@ -66,6 +85,7 @@ private:
uv_tcp_t m_handle;
uv_timer_t m_timer_connect_timeout;
bool m_timer_running; // does m_timer_connect_timeout initialized and started.
uv_async_t m_stop_handle; // event for stop whole listener handler.
ex_u8 m_state; // TELNET_CONN_STATE_XXXX

View File

@ -4,21 +4,18 @@
TelnetProxy g_telnet_proxy;
TelnetProxy::TelnetProxy() : ExThreadBase("telnet-proxy-thread")
{
TelnetProxy::TelnetProxy() : ExThreadBase("telnet-proxy-thread") {
memset(&m_loop, 0, sizeof(uv_loop_t));
m_timer_counter = 0;
m_noop_timeout_sec = 900;
}
TelnetProxy::~TelnetProxy()
{
if (m_sessions.size() > 0)
TelnetProxy::~TelnetProxy() {
if (!m_sessions.empty())
EXLOGE("[telnet] not all session stopped.\n");
}
bool TelnetProxy::init()
{
bool TelnetProxy::init() {
if (0 != uv_loop_init(&m_loop))
return false;
@ -26,12 +23,16 @@ bool TelnetProxy::init()
return false;
m_clean_session_handle.data = this;
if (0 != uv_async_init(&m_loop, &m_stop_handle, _on_stop_cb))
return false;
m_stop_handle.data = this;
m_host_ip = g_telnet_env.bind_ip;
m_host_port = g_telnet_env.bind_port;
if (0 != uv_tcp_init(&m_loop, &m_handle))
if (0 != uv_tcp_init(&m_loop, &m_listener_handle))
return false;
m_handle.data = this;
m_listener_handle.data = this;
return true;
}
@ -47,8 +48,7 @@ void TelnetProxy::timer() {
ExThreadSmartLock locker(m_lock);
ex_u32 t_now = (ex_u32) time(NULL);
ts_telnet_sessions::iterator it = m_sessions.begin();
for (; it != m_sessions.end(); ++it)
{
for (; it != m_sessions.end(); ++it) {
it->first->save_record();
if (0 != m_noop_timeout_sec)
it->first->check_noop_timeout(t_now, m_noop_timeout_sec);
@ -72,21 +72,20 @@ void TelnetProxy::kill_sessions(const ex_astrs& sessions) {
}
}
void TelnetProxy::_thread_loop(void)
{
void TelnetProxy::_thread_loop(void) {
struct sockaddr_in addr;
if (0 != uv_ip4_addr(m_host_ip.c_str(), m_host_port, &addr)) {
EXLOGE("[telnet] invalid ip/port for TELNET listener.\n");
return;
}
if (0 != uv_tcp_bind(&m_handle, (const struct sockaddr*) &addr, 0)) {
if (0 != uv_tcp_bind(&m_listener_handle, (const struct sockaddr *) &addr, 0)) {
EXLOGE("[telnet] can not bind %s:%d.\n", m_host_ip.c_str(), m_host_port);
return;
}
// 开始监听,有客户端连接到来时,会回调 _on_client_connect()
if (0 != uv_listen((uv_stream_t*)&m_handle, 8, _on_client_connect)) {
if (0 != uv_listen((uv_stream_t *) &m_listener_handle, 8, _on_client_connect)) {
EXLOGE("[telnet] can not listen on %s:%d.\n", m_host_ip.c_str(), m_host_port);
return;
}
@ -98,83 +97,100 @@ void TelnetProxy::_thread_loop(void)
EXLOGE("[telnet] main-loop end. %s\n", uv_strerror(err));
}
// https://github.com/libuv/libuv/issues/709
//
// uv_close is not thread safe (see the docs here. That means you cannot call it from outside of the loop thread.
// http://docs.libuv.org/en/v1.x/design.html#the-i-o-loop
//
// The usual way to approach this when the loop is running in another thread is to use a uv_async_t handle,
// because uv_async_send is thread safe. You could have one such handle and call uv_stop in its callback.
// Then, after uv_run returns you can use uv_walk to close all handles and run the loop one last time so
// that close callbacks are called.
// 注意,如果在 uv_loop_close() 内部崩溃可能某个uv的handle未关闭。
uv_loop_close(&m_loop);
EXLOGV("[telnet] main-loop end.\n");
}
void TelnetProxy::_set_stop_flag(void) {
m_stop_flag = true;
// static
void TelnetProxy::_on_stop(void) {
ExThreadBase::_on_stop();
if (m_is_running) {
uv_close((uv_handle_t*)&m_handle, _on_listener_closed);
uv_async_send(&m_stop_handle);
}
}
// static
void TelnetProxy::_on_listener_closed(uv_handle_t* handle)
{
void TelnetProxy::_on_listener_closed(uv_handle_t *handle) {
TelnetProxy *_this = (TelnetProxy *) handle->data;
EXLOGV("[telnet] listener close.\n");
_this->_close_all_sessions();
uv_close((uv_handle_t *) &_this->m_stop_handle, _on_stop_handle_closed);
// _this->_close_all_sessions();
}
void TelnetProxy::clean_session() {
uv_async_send(&m_clean_session_handle);
}
void TelnetProxy::_close_all_sessions(void)
{
void TelnetProxy::_close_all_sessions(void) {
ExThreadSmartLock locker(m_lock);
if (m_sessions.size() == 0) {
if (m_sessions.empty()) {
_close_clean_session_handle();
return;
}
ts_telnet_sessions::iterator it = m_sessions.begin();
for (; it != m_sessions.end(); ++it)
{
for (; it != m_sessions.end(); ++it) {
it->first->close(TP_SESS_STAT_ERR_RESET);
}
}
// static
void TelnetProxy::_on_clean_session_cb(uv_async_t* handle)
{
void TelnetProxy::_on_clean_session_cb(uv_async_t *handle) {
TelnetProxy *_this = (TelnetProxy *) handle->data;
// check closed session
ExThreadSmartLock locker(_this->m_lock);
ts_telnet_sessions::iterator it = _this->m_sessions.begin();
for (; it != _this->m_sessions.end(); )
{
for (; it != _this->m_sessions.end();) {
if (it->first->is_closed()) {
delete it->first;
_this->m_sessions.erase(it++);
EXLOGD("[telnet] - removed one session.\n");
}
else {
} else {
it++;
}
}
if (_this->m_stop_flag && _this->m_sessions.size() == 0) {
if (_this->m_need_stop && _this->m_sessions.empty()) {
_this->_close_clean_session_handle();
}
}
////static
//void TelnetProxy::_on_clean_session_handle_closed(uv_handle_t *handle) {
//}
//static
void TelnetProxy::_on_clean_session_handle_closed(uv_handle_t *handle) {
void TelnetProxy::_on_stop_handle_closed(uv_handle_t *handle) {
TelnetProxy *_this = (TelnetProxy *) handle->data;
_this->_close_all_sessions();
}
//static
void TelnetProxy::_on_stop_cb(uv_async_t *handle) {
TelnetProxy *_this = (TelnetProxy *) handle->data;
uv_close((uv_handle_t *) &_this->m_listener_handle, _on_listener_closed);
}
// static
void TelnetProxy::_on_client_connect(uv_stream_t* server, int status)
{
void TelnetProxy::_on_client_connect(uv_stream_t *server, int status) {
if (0 != status)
return;
@ -182,19 +198,16 @@ void TelnetProxy::_on_client_connect(uv_stream_t* server, int status)
_this->_on_accept(server);
}
bool TelnetProxy::_on_accept(uv_stream_t* server)
{
bool TelnetProxy::_on_accept(uv_stream_t *server) {
TelnetSession *sess = new TelnetSession(this);
if (0 != uv_accept(server, sess->client()->stream_handle()))
{
if (0 != uv_accept(server, sess->client()->stream_handle())) {
EXLOGE("[telnet] socket accept failed.\n");
delete sess;
return false;
}
if (m_stop_flag)
{
if (m_need_stop) {
delete sess;
return false;
}
@ -202,12 +215,10 @@ bool TelnetProxy::_on_accept(uv_stream_t* server)
// 获取客户端IP地址和端口号
struct sockaddr sock_client;
int namelen = sizeof(sock_client);
if (0 == uv_tcp_getpeername(sess->client()->tcp_handle(), &sock_client, &namelen))
{
if (0 == uv_tcp_getpeername(sess->client()->tcp_handle(), &sock_client, &namelen)) {
sockaddr_in *addrin = (sockaddr_in *) &sock_client;
char ip[17] = {0};
if (0 == uv_ip4_name(addrin, ip, sizeof(ip)))
{
if (0 == uv_ip4_name(addrin, ip, sizeof(ip))) {
char client_addr[64] = {0};
snprintf(client_addr, 64, "%s:%d", ip, addrin->sin_port);
sess->client_addr(client_addr);
@ -227,5 +238,5 @@ bool TelnetProxy::_on_accept(uv_stream_t* server)
}
void TelnetProxy::_close_clean_session_handle() {
uv_close((uv_handle_t*)&m_clean_session_handle, _on_clean_session_handle_closed);
uv_close((uv_handle_t *) &m_clean_session_handle, NULL);
}

View File

@ -8,15 +8,18 @@
typedef std::map<TelnetSession *, unsigned char> ts_telnet_sessions;
class TelnetProxy : public ExThreadBase
{
class TelnetProxy : public ExThreadBase {
public:
TelnetProxy();
~TelnetProxy();
bool init();
void timer();
void set_cfg(ex_u32 noop_timeout);
void kill_sessions(const ex_astrs &sessions);
uv_loop_t *get_loop() { return &m_loop; }
@ -25,27 +28,36 @@ public:
protected:
void _thread_loop();
void _set_stop_flag();
void _on_stop();
void _close_all_sessions();
void _close_clean_session_handle();
private:
static void _on_client_connect(uv_stream_t *server, int status);
static void _on_listener_closed(uv_handle_t *handle);
static void _on_clean_session_cb(uv_async_t *handle);
static void _on_clean_session_handle_closed(uv_handle_t *handle);
// static void _on_clean_session_handle_closed(uv_handle_t *handle);
static void _on_stop_cb(uv_async_t *handle);
static void _on_stop_handle_closed(uv_handle_t *handle);
bool _on_accept(uv_stream_t *server);
private:
bool m_stop_flag;
int m_timer_counter;
//
ex_u32 m_noop_timeout_sec;
uv_loop_t m_loop;
uv_tcp_t m_handle;
uv_tcp_t m_listener_handle;
uv_async_t m_clean_session_handle;
uv_async_t m_stop_handle; // event for stop whole listener handler.
ExThreadLock m_lock;

View File

@ -14,8 +14,7 @@
TelnetSession::TelnetSession(TelnetProxy *proxy) :
m_proxy(proxy),
m_conn_info(NULL)
{
m_conn_info(NULL) {
m_state = TP_SESS_STAT_RUNNING;
m_startup_win_size_recorded = false;
m_db_id = 0;
@ -66,8 +65,7 @@ void TelnetSession::check_noop_timeout(ex_u32 t_now, ex_u32 timeout) {
void TelnetSession::_session_error(int err_code) {
int db_id = 0;
if (!g_telnet_env.session_begin(m_conn_info, &db_id) || db_id == 0)
{
if (!g_telnet_env.session_begin(m_conn_info, &db_id) || db_id == 0) {
EXLOGE("[telnet] can not write session error to database.\n");
return;
}
@ -91,10 +89,8 @@ bool TelnetSession::_on_session_begin() {
return true;
}
bool TelnetSession::_on_session_end()
{
if (m_db_id > 0)
{
bool TelnetSession::_on_session_end() {
if (m_db_id > 0) {
// 如果会话过程中没有发生错误,则将其状态改为结束,否则记录下错误值
if (m_state == TP_SESS_STAT_RUNNING || m_state == TP_SESS_STAT_STARTED)
m_state = TP_SESS_STAT_END;
@ -183,14 +179,14 @@ sess_state TelnetSession::_do_close(int state) {
m_state = TP_SESS_STAT_ERR_INTERNAL;
else
m_state = state;
}
else {
} else {
if (!m_is_relay)
_session_error(state);
m_state = state;
}
EXLOGV("[telnet] close session.\n");
EXLOGD("[telnet] _do_close(), conn_client::state=%d, conn_server:state=%d\n", m_conn_client->state(), m_conn_server->state());
EXLOGD("[telnet] _do_close(), conn_client::state=%d, conn_server:state=%d\n", m_conn_client->state(),
m_conn_server->state());
m_conn_client->close();
m_conn_server->close();
@ -206,7 +202,8 @@ sess_state TelnetSession::_do_check_closing() {
}
void TelnetSession::on_conn_close() {
EXLOGD("[telnet] on_conn_close(), conn_client::state=%d, conn_server:state=%d\n", m_conn_client->state(), m_conn_server->state());
EXLOGD("[telnet] on_conn_close(), conn_client::state=%d, conn_server:state=%d\n", m_conn_client->state(),
m_conn_server->state());
if (m_conn_client->state() == TELNET_CONN_STATE_FREE && m_conn_server->state() == TELNET_CONN_STATE_FREE) {
m_status = s_all_conn_closed;
do_next(m_conn_client);
@ -245,8 +242,7 @@ sess_state TelnetSession::_do_negotiation_with_client(TelnetConn* conn) {
m_sid = (char *) mbuf.data();
return _do_connect_server();
}
else {
} else {
m_is_putty_mode = true;
}
}
@ -267,96 +263,71 @@ sess_state TelnetSession::_do_negotiation_with_client(TelnetConn* conn) {
MemStream s(conn->data());
ex_u8 ch = 0;
ex_u8 ch_cmd = 0;
for (; s.left() > 0;)
{
for (; s.left() > 0;) {
ch = s.get_u8();
if (ch == TELNET_IAC)
{
if (ch == TELNET_IAC) {
if (s.left() < 2)
return _do_close(TP_SESS_STAT_ERR_BAD_PKG);
if (mbuf_sub.size() > 0)
{
if (mbuf_sub.size() > 0) {
// 已经得到一个sub negotiation在处理新的数据前先处理掉旧的
EXLOG_BIN(mbuf_sub.data(), mbuf_sub.size(), "-=-=-=-=-=");
ms_sub.reset();
}
ch_cmd = s.get_u8();
if (ch_cmd == TELNET_SB)
{
if (ch_cmd == TELNET_SB) {
// SUB NEGOTIATION变长数据以 FF F0 结束
bool have_SE = false;
ex_u8 ch_sub = 0;
for (; s.left() > 0;)
{
for (; s.left() > 0;) {
ch_sub = s.get_u8();
if (ch_sub == TELNET_IAC)
{
if (s.left() > 0)
{
if (s.get_u8() == TELNET_SE)
{
if (ch_sub == TELNET_IAC) {
if (s.left() > 0) {
if (s.get_u8() == TELNET_SE) {
have_SE = true;
break;
}
else
} else
return _do_close(TP_SESS_STAT_ERR_BAD_PKG);
}
}
else
{
} else {
ms_sub.put_u8(ch_sub);
}
}
if (!have_SE)
return _do_close(TP_SESS_STAT_ERR_BAD_PKG);
}
else if (ch_cmd == TELNET_DONT)
{
} else if (ch_cmd == TELNET_DONT) {
ms_resp.put_u8(TELNET_IAC);
ms_resp.put_u8(TELNET_WONT);
ms_resp.put_u8(s.get_u8());
}
else if (ch_cmd == TELNET_DO)
{
} else if (ch_cmd == TELNET_DO) {
ms_resp.put_u8(TELNET_IAC);
ms_resp.put_u8(TELNET_WILL);
ms_resp.put_u8(s.get_u8());
}
else if (ch_cmd == TELNET_WONT)
{
} else if (ch_cmd == TELNET_WONT) {
ms_resp.put_u8(TELNET_IAC);
ms_resp.put_u8(TELNET_DONT);
ms_resp.put_u8(s.get_u8());
}
else if (ch_cmd == TELNET_WILL)
{
} else if (ch_cmd == TELNET_WILL) {
ms_resp.put_u8(TELNET_IAC);
ms_resp.put_u8(TELNET_DO);
ms_resp.put_u8(s.get_u8());
}
else
{
} else {
s.skip(1);
}
}
else
{
} else {
ms_msg.put_u8(ch);
}
}
conn->data().empty();
if (mbuf_resp.size() > 0)
{
if (mbuf_resp.size() > 0) {
conn->send(mbuf_resp.data(), mbuf_resp.size());
}
if (mbuf_sub.size() == 5)
{
if (mbuf_sub.size() == 5) {
// 客户端窗口大小
if (0x1f == mbuf_sub.data()[0]) {
ms_sub.rewind();
@ -383,14 +354,12 @@ sess_state TelnetSession::_do_negotiation_with_client(TelnetConn* conn) {
return s_negotiation_with_client;
}
if (mbuf_sub.size() > 8)
{
if (mbuf_sub.size() > 8) {
// 可能含有putty的登录用户名信息就是SID啦
if (0 == memcmp(mbuf_sub.data(), "\x27\x00\x00\x55\x53\x45\x52\x01", 8)) // '...USER.
{
mbuf_sub.pop(8);
for (; mbuf_sub.size() > 0;)
{
for (; mbuf_sub.size() > 0;) {
if (mbuf_sub.data()[mbuf_sub.size() - 1] == 0x0a || mbuf_sub.data()[mbuf_sub.size() - 1] == 0x0d)
mbuf_sub.size(mbuf_sub.size() - 1);
else
@ -402,8 +371,7 @@ sess_state TelnetSession::_do_negotiation_with_client(TelnetConn* conn) {
}
}
if (m_sid.length() > 0)
{
if (m_sid.length() > 0) {
return _do_connect_server();
}
@ -419,8 +387,7 @@ sess_state TelnetSession::_do_connect_server() {
if (NULL == m_conn_info) {
EXLOGE("[telnet] no such session: %s\n", m_sid.c_str());
return _do_close(TP_SESS_STAT_ERR_SESSION);
}
else {
} else {
m_conn_ip = m_conn_info->conn_ip;
m_conn_port = m_conn_info->conn_port;
m_acc_name = m_conn_info->acc_username;
@ -434,7 +401,8 @@ sess_state TelnetSession::_do_connect_server() {
}
if (m_conn_info->auth_type != TP_AUTH_TYPE_NONE) {
if (m_acc_name.length() == 0 || m_username_prompt.length() == 0 || m_acc_secret.length() == 0 || m_password_prompt.length() == 0) {
if (m_acc_name.length() == 0 || m_username_prompt.length() == 0 || m_acc_secret.length() == 0 ||
m_password_prompt.length() == 0) {
EXLOGE("[telnet] invalid connection param.\n");
return _do_close(TP_SESS_STAT_ERR_SESSION);
}
@ -512,8 +480,7 @@ sess_state TelnetSession::_do_server_connected() {
m_conn_client->send((ex_u8 *) buf, strlen(buf));
if (m_is_putty_mode)
{
if (m_is_putty_mode) {
if (m_conn_info->auth_type != TP_AUTH_TYPE_NONE) {
ex_astr login_info = "login: ";
login_info += m_conn_info->acc_username;
@ -535,22 +502,18 @@ sess_state TelnetSession::_do_relay(TelnetConn *conn) {
TelnetSession *_this = conn->session();
bool is_processed = false;
if (conn->is_server_side())
{
if (conn->is_server_side()) {
// EXLOG_BIN(m_conn_client->data().data(), m_conn_client->data().size(), "<-- client:");
// 收到了客户端发来的数据
if (_this->m_is_putty_mode && !_this->m_username_sent)
{
if (_this->_putty_replace_username(m_conn_client, m_conn_server))
{
if (_this->m_is_putty_mode && !_this->m_username_sent) {
if (_this->_putty_replace_username(m_conn_client, m_conn_server)) {
//_this->m_username_sent = true;
is_processed = true;
}
}
if (is_processed)
{
if (is_processed) {
m_conn_client->data().empty();
return s_relay;
}
@ -565,27 +528,23 @@ sess_state TelnetSession::_do_relay(TelnetConn *conn) {
m_conn_server->send(m_conn_client->data().data(), m_conn_client->data().size());
m_conn_client->data().empty();
}
else
{
} else {
// EXLOG_BIN(m_conn_server->data().data(), m_conn_server->data().size(), "--> server:");
// 收到了服务端返回的数据
if (m_conn_server->data().data()[0] != TELNET_IAC)
m_rec.record(TS_RECORD_TYPE_TELNET_DATA, m_conn_server->data().data(), m_conn_server->data().size());
if (!_this->m_username_sent && _this->m_acc_name.length() > 0)
{
if (_this->_parse_find_and_send(m_conn_server, m_conn_client, _this->m_username_prompt.c_str(), _this->m_acc_name.c_str()))
{
if (!_this->m_username_sent && _this->m_acc_name.length() > 0) {
if (_this->_parse_find_and_send(m_conn_server, m_conn_client, _this->m_username_prompt.c_str(),
_this->m_acc_name.c_str())) {
// _this->m_username_sent = true;
is_processed = true;
}
}
if (!_this->m_password_sent && _this->m_password_prompt.length() > 0)
{
if (_this->_parse_find_and_send(m_conn_server, m_conn_client, _this->m_password_prompt.c_str(), _this->m_acc_secret.c_str()))
{
if (!_this->m_password_sent && _this->m_password_prompt.length() > 0) {
if (_this->_parse_find_and_send(m_conn_server, m_conn_client, _this->m_password_prompt.c_str(),
_this->m_acc_secret.c_str())) {
_this->m_username_sent = true;
_this->m_password_sent = true;
_this->m_username_sent = true;
@ -593,8 +552,7 @@ sess_state TelnetSession::_do_relay(TelnetConn *conn) {
}
}
if (is_processed)
{
if (is_processed) {
m_conn_server->data().empty();
return s_relay;
}
@ -606,8 +564,8 @@ sess_state TelnetSession::_do_relay(TelnetConn *conn) {
return s_relay;
}
bool TelnetSession::_parse_find_and_send(TelnetConn* conn_recv, TelnetConn* conn_remote, const char* find, const char* send)
{
bool TelnetSession::_parse_find_and_send(TelnetConn *conn_recv, TelnetConn *conn_remote, const char *find,
const char *send) {
// EXLOGV("find prompt and send: [%s] => [%s]\n", find, send);
// EXLOG_BIN(conn_recv->data().data(), conn_recv->data().size(), "find prompt in data:");
@ -618,10 +576,8 @@ bool TelnetSession::_parse_find_and_send(TelnetConn* conn_recv, TelnetConn* conn
}
int find_range = conn_recv->data().size() - find_len;
for (int i = 0; i <= find_range; ++i)
{
if (0 == memcmp(conn_recv->data().data() + i, find, find_len))
{
for (int i = 0; i <= find_range; ++i) {
if (0 == memcmp(conn_recv->data().data() + i, find, find_len)) {
conn_remote->send(conn_recv->data().data(), conn_recv->data().size());
conn_recv->data().empty();
@ -712,8 +668,7 @@ bool TelnetSession::_parse_find_and_send(TelnetConn* conn_recv, TelnetConn* conn
return false;
}
bool TelnetSession::_putty_replace_username(TelnetConn* conn_recv, TelnetConn* conn_remote)
{
bool TelnetSession::_putty_replace_username(TelnetConn *conn_recv, TelnetConn *conn_remote) {
bool replaced = false;
MemBuffer mbuf_msg;
@ -723,17 +678,14 @@ bool TelnetSession::_putty_replace_username(TelnetConn* conn_recv, TelnetConn* c
MemStream s(conn_recv->data());
ex_u8 ch = 0;
ex_u8 ch_cmd = 0;
for (; s.left() > 0;)
{
for (; s.left() > 0;) {
ch = s.get_u8();
if (ch == TELNET_IAC)
{
if (ch == TELNET_IAC) {
if (s.left() < 2)
return false;
ch_cmd = s.get_u8();
if (ch_cmd == TELNET_SB)
{
if (ch_cmd == TELNET_SB) {
size_t _begin = s.offset();
size_t _end = 0;
@ -741,20 +693,15 @@ bool TelnetSession::_putty_replace_username(TelnetConn* conn_recv, TelnetConn* c
// SUB NEGOTIATION变长数据以 FF F0 结束
bool have_SE = false;
ex_u8 ch_sub = 0;
for (; s.left() > 0;)
{
for (; s.left() > 0;) {
_end = s.offset();
ch_sub = s.get_u8();
if (ch_sub == TELNET_IAC)
{
if (s.left() > 0)
{
if (s.get_u8() == TELNET_SE)
{
if (ch_sub == TELNET_IAC) {
if (s.left() > 0) {
if (s.get_u8() == TELNET_SE) {
have_SE = true;
break;
}
else
} else
return false;
}
}
@ -764,8 +711,7 @@ bool TelnetSession::_putty_replace_username(TelnetConn* conn_recv, TelnetConn* c
return false;
size_t len = _end - _begin;
if (len <= 8 || 0 != memcmp("\x27\x00\x00\x55\x53\x45\x52\x01", conn_recv->data().data() + _begin, 8))
{
if (len <= 8 || 0 != memcmp("\x27\x00\x00\x55\x53\x45\x52\x01", conn_recv->data().data() + _begin, 8)) {
ms_msg.put_u8(TELNET_IAC);
ms_msg.put_u8(TELNET_SB);
ms_msg.put_bin(conn_recv->data().data() + _begin, len);
@ -786,22 +732,17 @@ bool TelnetSession::_putty_replace_username(TelnetConn* conn_recv, TelnetConn* c
ms_msg.put_u8(TELNET_SE);
replaced = true;
}
else
{
} else {
ms_msg.put_u8(ch);
ms_msg.put_u8(ch_cmd);
ms_msg.put_u8(s.get_u8());
}
}
else
{
} else {
ms_msg.put_u8(ch);
}
}
if (replaced)
{
if (replaced) {
conn_remote->send(mbuf_msg.data(), mbuf_msg.size());
return true;
}
@ -817,34 +758,26 @@ bool TelnetSession::_parse_win_size(TelnetConn* conn) {
bool is_sub = false;
MemStream s(conn->data());
for (; s.left() > 0;)
{
if (s.get_u8() == TELNET_IAC)
{
for (; s.left() > 0;) {
if (s.get_u8() == TELNET_IAC) {
if (s.left() < 2)
return false;
if (s.get_u8() == TELNET_SB)
{
if (s.get_u8() == TELNET_SB) {
size_t _begin = s.offset();
size_t _end = 0;
// SUB NEGOTIATION变长数据以 TELNET_IAC+TELNET_SE (FF F0) 结束
bool have_SE = false;
ex_u8 ch_sub = 0;
for (; s.left() > 0;)
{
for (; s.left() > 0;) {
_end = s.offset();
if (s.get_u8() == TELNET_IAC)
{
if (s.left() > 0)
{
if (s.get_u8() == TELNET_SE)
{
if (s.get_u8() == TELNET_IAC) {
if (s.left() > 0) {
if (s.get_u8() == TELNET_SE) {
have_SE = true;
break;
}
else
} else
return false;
}
}