修复ssh远程连接后执行 rz 命令无法工作的问题。

pull/130/head
Apex Liu 2018-11-04 08:58:30 +08:00
parent b1524fd6a0
commit 46cd77420d
17 changed files with 1336 additions and 1388 deletions

19
.gitignore vendored
View File

@ -12,7 +12,6 @@
# for CMake
CMakeFiles
cmake_install.cmake
Makefile
cmake-build
cmake-build-debug
client/tp_assist_macos/build
@ -23,15 +22,15 @@ __pycache__
*.pyc
# for pyCharm
# for JetBrains IDEs.
**/.idea/workspace.xml
**/.idea/watcherTasks.xml
**/.idea/dictionaries
**/.idea/vcs.xml
**/.idea/misc.xml
**/.idea/modules.xml
**/.idea/dictionaries
**/.idea/watcherTasks.xml
**/.idea/inspectionProfiles
**/.idea/vcs.xml
**/.idea/codeStyles
**/.idea/inspectionProfiles
*.css.map
@ -45,15 +44,11 @@ __pycache__
/external/openssl
/external/python
/external/libssh
/external/libssh-win-static/include/libssh
/external/libssh-win-static/src
/external/libssh-win-static/lib
/external/mbedtls
/external/sqlite
/external/libuv
/client/tools/putty
/client/tools/winscp
/client/tp_assist_macos/DerivedData
# for dist folder
**/_tmp_
@ -72,6 +67,7 @@ __pycache__
# for generated files.
/config.ini
/build.bat
/build.sh
# for not finished code
/common/libex/test
@ -85,7 +81,7 @@ __pycache__
# for MacOS.
.DS_Store
# Xcode
# for Xcode
build/*
*.pbxuser
!default.pbxuser
@ -101,4 +97,3 @@ xcuserdata
profile
*.moved-aside
/server/share/tmp
/build.sh

51
.idea/encodings.xml Normal file
View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/common/libex/include/ex/ex_log.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/common/libex/include/ex/ex_path.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/common/libex/include/ex/ex_thread.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/common/libex/include/ex/ex_util.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/common/libex/src/ex_ini.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/common/libex/src/ex_log.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/common/libex/src/ex_util.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/common/pyshell/include/pys.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/common/pyshell/src/pys_api.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/common/pyshell/src/pys_core.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/common/teleport/teleport_const.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/common/base_env.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/common/base_record.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/common/base_record.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/common/protocol_interface.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/common/ts_const.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/common/ts_membuf.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/common/ts_membuf.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/common/ts_memstream.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/core/main.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/core/ts_env.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/core/ts_http_rpc.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/core/ts_http_rpc.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/core/ts_main.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/core/ts_session.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/core/ts_session.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/core/ts_web_rpc.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/core/ts_web_rpc.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/rdp/rdp_conn.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/rdp/rdp_conn.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/rdp/rdp_proxy.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/rdp/rdp_session.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/rdp/rdp_session.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/ssh/ssh_proxy.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/ssh/ssh_proxy.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/ssh/ssh_recorder.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/ssh/ssh_recorder.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/ssh/ssh_session.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/ssh/ssh_session.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/telnet/telnet_conn.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/telnet/telnet_conn.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/telnet/telnet_proxy.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/telnet/telnet_session.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_core/protocol/telnet/telnet_session.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_web/src/main.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/server/tp_web/src/ts_env.cpp" charset="GBK" />
</component>
</project>

2
.idea/teleport.iml Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

37
CMakeLists.txt Normal file
View File

@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.5)
project(teleport)
MESSAGE(STATUS "operation system is ${CMAKE_SYSTEM}")
MESSAGE(STATUS "current source directory is ${CMAKE_CURRENT_SOURCE_DIR}")
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)
# Determine the platform.
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
MESSAGE(STATUS "build on macOS...")
set(OS_MACOS 1)
set(OS_POSIX 1)
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(server/tp_web/src)
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 ()
MESSAGE(FATAL_ERROR "unsupported platform: ${CMAKE_SYSTEM_NAME}")
endif ()
add_subdirectory(server/tp_core/core)
add_subdirectory(server/tp_core/protocol/ssh)
add_subdirectory(server/tp_core/protocol/telnet)
#add_subdirectory(server/testssh/testssh)
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/server/tp_core/protocol/rdp")
add_subdirectory(server/tp_core/protocol/rdp)
endif ()

View File

@ -20,9 +20,8 @@ void *ExThreadBase::_thread_func(void *pParam)
_this->m_is_running = false;
_this->m_handle = 0;
_this->_on_stopped();
EXLOGV(" # thread [%s] exit.\n", _this->m_thread_name.c_str());
_this->_on_stopped();
return 0;
}
@ -49,7 +48,7 @@ bool ExThreadBase::start(void) {
{
return false;
}
m_listener_handle = h;
m_handle = h;
#else
pthread_t ptid = 0;
int ret = pthread_create(&ptid, NULL, _thread_func, (void *) this);
@ -76,7 +75,7 @@ bool ExThreadBase::stop(void) {
EXLOGV("[thread] wait thread [%s] exit.\n", m_thread_name.c_str());
#ifdef EX_OS_WIN32
if (WaitForSingleObject(m_listener_handle, INFINITE) != WAIT_OBJECT_0)
if (WaitForSingleObject(m_handle, INFINITE) != WAIT_OBJECT_0)
{
return false;
}
@ -91,7 +90,7 @@ bool ExThreadBase::stop(void) {
bool ExThreadBase::terminate(void) {
#ifdef EX_OS_WIN32
return (TerminateThread(m_listener_handle, 1) == TRUE);
return (TerminateThread(m_handle, 1) == TRUE);
#else
return (pthread_cancel(m_handle) == 0);
#endif

View File

@ -1 +0,0 @@
teleport

View File

@ -1,29 +0,0 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<Objective-C-extensions>
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" fileNamingConvention="NONE" />
<pair source="c" header="h" fileNamingConvention="NONE" />
</extensions>
</Objective-C-extensions>
</code_scheme>
</component>

View File

@ -1,51 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/../common/libex/include/ex/ex_log.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/../common/libex/include/ex/ex_path.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/../common/libex/include/ex/ex_thread.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/../common/libex/include/ex/ex_util.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/../common/libex/src/ex_ini.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/../common/libex/src/ex_log.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/../common/libex/src/ex_util.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/../common/pyshell/include/pys.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/../common/pyshell/src/pys_api.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/../common/pyshell/src/pys_core.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/../common/teleport/teleport_const.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/common/base_env.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/common/base_record.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/common/base_record.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/common/protocol_interface.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/common/ts_const.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/common/ts_membuf.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/common/ts_membuf.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/common/ts_memstream.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/core/main.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/core/ts_env.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/core/ts_http_rpc.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/core/ts_http_rpc.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/core/ts_main.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/core/ts_session.cpp" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/core/ts_session.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/tp_core/core/ts_web_rpc.cpp" charset="GBK" />
<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_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" />
<file url="file://$PROJECT_DIR$/tp_core/protocol/ssh/ssh_recorder.h" charset="GBK" />
<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>
</project>

View File

@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

View File

@ -1,38 +0,0 @@
cmake_minimum_required(VERSION 3.5)
project(teleport)
MESSAGE(STATUS "operation system is ${CMAKE_SYSTEM}")
MESSAGE(STATUS "current source directory is ${CMAKE_CURRENT_SOURCE_DIR}")
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)
# Determine the platform.
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
MESSAGE(STATUS "build on macOS...")
set(OS_MACOS 1)
set(OS_POSIX 1)
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 "${CMAKE_CURRENT_SOURCE_DIR}/../external/linux/release")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
MESSAGE(FATAL_ERROR "unsupported platform: Windows")
else()
MESSAGE(FATAL_ERROR "unsupported platform: ${CMAKE_SYSTEM_NAME}")
endif()
add_subdirectory(tp_core/core)
add_subdirectory(tp_core/protocol/ssh)
add_subdirectory(tp_core/protocol/telnet)
#add_subdirectory(testssh/testssh)
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tp_core/protocol/rdp")
add_subdirectory(tp_core/protocol/rdp)
endif()

View File

@ -68,17 +68,46 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data)
}
}
static struct mg_mgr g_mg_mgr;
static bool is_mg_mgr_initialized = false;
//static struct mg_mgr g_mg_mgr;
//static bool is_mg_mgr_initialized = false;
class MongooseManager {
public:
MongooseManager() {
mg_mgr_free(&m_mg_mgr);
m_initialized = false;
}
~MongooseManager() {
if (m_initialized) {
mg_mgr_free(&m_mg_mgr);
m_initialized = false;
}
}
struct mg_mgr* get_mg_mgr() {
if (!m_initialized) {
mg_mgr_init(&m_mg_mgr, NULL);
m_initialized = true;
}
return &m_mg_mgr;
}
private:
bool m_initialized;
struct mg_mgr m_mg_mgr;
};
static MongooseManager g_mg_mgr;
bool ts_http_get(const ex_astr& url, ex_astr& body)
{
if(!is_mg_mgr_initialized) {
mg_mgr_init(&g_mg_mgr, NULL);
is_mg_mgr_initialized = true;
}
// if(!is_mg_mgr_initialized) {
// mg_mgr_init(&g_mg_mgr, NULL);
// is_mg_mgr_initialized = true;
// }
mg_connection* nc = mg_connect_http(&g_mg_mgr, ev_handler, url.c_str(), NULL, NULL);
mg_connection* nc = mg_connect_http(g_mg_mgr.get_mg_mgr(), ev_handler, url.c_str(), NULL, NULL);
if (NULL == nc)
return false;
@ -92,7 +121,7 @@ bool ts_http_get(const ex_astr& url, ex_astr& body)
// int count = 0;
while (!hdata.exit_flag)
{
mg_mgr_poll(&g_mg_mgr, 100);
mg_mgr_poll(g_mg_mgr.get_mg_mgr(), 100);
// count++;
// if (count > 2)
// break;

View File

@ -4,205 +4,204 @@
SshProxy g_ssh_proxy;
SshProxy::SshProxy() :
ExThreadBase("ssh-proxy-thread"),
m_bind(NULL)
{
m_timer_counter = 0;
ExThreadBase("ssh-proxy-thread"),
m_bind(NULL) {
m_timer_counter = 0;
m_noop_timeout_sec = 900; // default to 15 minutes.
m_noop_timeout_sec = 900; // default to 15 minutes.
}
SshProxy::~SshProxy()
{
if (NULL != m_bind)
ssh_bind_free(m_bind);
SshProxy::~SshProxy() {
if (NULL != m_bind)
ssh_bind_free(m_bind);
ssh_finalize();
ssh_finalize();
}
bool SshProxy::init()
{
m_host_ip = g_ssh_env.bind_ip;
m_host_port = g_ssh_env.bind_port;
bool SshProxy::init() {
m_host_ip = g_ssh_env.bind_ip;
m_host_port = g_ssh_env.bind_port;
m_bind = ssh_bind_new();
if (NULL == m_bind)
{
EXLOGE("[ssh] can not create bind.\n");
return false;
}
m_bind = ssh_bind_new();
if (NULL == m_bind) {
EXLOGE("[ssh] can not create bind.\n");
return false;
}
if (SSH_OK != ssh_bind_options_set(m_bind, SSH_BIND_OPTIONS_BINDADDR, m_host_ip.c_str()))
{
EXLOGE("[ssh] can not set bind option: SSH_BIND_OPTIONS_BINDADDR.\n");
return false;
}
if (SSH_OK != ssh_bind_options_set(m_bind, SSH_BIND_OPTIONS_BINDPORT, &m_host_port))
{
EXLOGE("[ssh] can not set bind option: SSH_BIND_OPTIONS_BINDPORT.\n");
return false;
}
if (SSH_OK != ssh_bind_options_set(m_bind, SSH_BIND_OPTIONS_BINDADDR, m_host_ip.c_str())) {
EXLOGE("[ssh] can not set bind option: SSH_BIND_OPTIONS_BINDADDR.\n");
return false;
}
if (SSH_OK != ssh_bind_options_set(m_bind, SSH_BIND_OPTIONS_BINDPORT, &m_host_port)) {
EXLOGE("[ssh] can not set bind option: SSH_BIND_OPTIONS_BINDPORT.\n");
return false;
}
ex_wstr _key_file = g_ssh_env.etc_path;
ex_path_join(_key_file, false, L"tp_ssh_server.key", NULL);
ex_astr key_file;
ex_wstr2astr(_key_file, key_file);
ex_wstr _key_file = g_ssh_env.etc_path;
ex_path_join(_key_file, false, L"tp_ssh_server.key", NULL);
ex_astr key_file;
ex_wstr2astr(_key_file, key_file);
EXLOGV("[ssh] try to load ssh-server-key: %s\n", key_file.c_str());
if (SSH_OK != ssh_bind_options_set(m_bind, SSH_BIND_OPTIONS_RSAKEY, key_file.c_str()))
{
EXLOGE("[ssh] can not set bind option: SSH_BIND_OPTIONS_RSAKEY.\n");
return false;
}
EXLOGV("[ssh] try to load ssh-server-key: %s\n", key_file.c_str());
if (SSH_OK != ssh_bind_options_set(m_bind, SSH_BIND_OPTIONS_RSAKEY, key_file.c_str())) {
EXLOGE("[ssh] can not set bind option: SSH_BIND_OPTIONS_RSAKEY.\n");
return false;
}
if (ssh_bind_listen(m_bind) < 0)
{
EXLOGE("[ssh] listening to socket: %s\n", ssh_get_error(m_bind));
return false;
}
if (ssh_bind_listen(m_bind) < 0) {
EXLOGE("[ssh] listening to socket: %s\n", ssh_get_error(m_bind));
return false;
}
return true;
return true;
}
void SshProxy::timer() {
// timer() will be called per one second, and I will do my job per 5 seconds.
m_timer_counter++;
if(m_timer_counter < 5)
return;
// timer() will be called per one second, and I will do my job per 5 seconds.
m_timer_counter++;
if (m_timer_counter < 5)
return;
m_timer_counter = 0;
m_timer_counter = 0;
ExThreadSmartLock locker(m_lock);
ex_u32 t_now = (ex_u32)time(NULL);
ExThreadSmartLock locker(m_lock);
ex_u32 t_now = (ex_u32) time(NULL);
ts_ssh_sessions::iterator it;
for(it = m_sessions.begin(); 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);
}
ts_ssh_sessions::iterator it;
for (it = m_sessions.begin(); 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);
}
}
void SshProxy::set_cfg(ex_u32 noop_timeout) {
m_noop_timeout_sec = noop_timeout;
m_noop_timeout_sec = noop_timeout;
}
void SshProxy::kill_sessions(const ex_astrs& sessions) {
ExThreadSmartLock locker(m_lock);
ts_ssh_sessions::iterator it = m_sessions.begin();
for (; it != m_sessions.end(); ++it) {
for (size_t i = 0; i < sessions.size(); ++i) {
if (it->first->sid() == sessions[i]) {
EXLOGW("[ssh] try to kill %s\n", sessions[i].c_str());
it->first->check_noop_timeout(0, 0); // 立即结束
}
}
}
void SshProxy::kill_sessions(const ex_astrs &sessions) {
ExThreadSmartLock locker(m_lock);
ts_ssh_sessions::iterator it = m_sessions.begin();
for (; it != m_sessions.end(); ++it) {
for (size_t i = 0; i < sessions.size(); ++i) {
if (it->first->sid() == sessions[i]) {
EXLOGW("[ssh] try to kill %s\n", sessions[i].c_str());
it->first->check_noop_timeout(0, 0); // 立即结束
}
}
}
}
void SshProxy::_thread_loop()
{
EXLOGI("[ssh] TeleportServer-SSH ready on %s:%d\n", m_host_ip.c_str(), m_host_port);
void SshProxy::_thread_loop() {
EXLOGI("[ssh] TeleportServer-SSH ready on %s:%d\n", m_host_ip.c_str(), m_host_port);
for (;;)
{
// 注意ssh_new()出来的指针如果遇到停止标志本函数内部就释放了否则这个指针交给了SshSession类实例管理其析构时会释放。
ssh_session sess_to_client = ssh_new();
for (;;) {
// 注意ssh_new()出来的指针如果遇到停止标志本函数内部就释放了否则这个指针交给了SshSession类实例管理其析构时会释放。
ssh_session sess_to_client = ssh_new();
// int verbosity = 4;
// ssh_options_set(sess_to_client, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_set_blocking(sess_to_client, 1);
ssh_set_blocking(sess_to_client, 1);
struct sockaddr_storage sock_client;
char ip[32] = { 0 };
int len = sizeof(ip);
struct sockaddr_storage sock_client;
char ip[32] = {0};
int len = sizeof(ip);
if (ssh_bind_accept(m_bind, sess_to_client) != SSH_OK)
{
EXLOGE("[ssh] accepting a connection failed: %s.\n", ssh_get_error(m_bind));
continue;
}
EXLOGD("[ssh] ssh_bind_accept() returned...\n");
if (ssh_bind_accept(m_bind, sess_to_client) != SSH_OK) {
EXLOGE("[ssh] accepting a connection failed: %s.\n", ssh_get_error(m_bind));
continue;
}
EXLOGD("[ssh] ssh_bind_accept() returned...\n");
if (m_stop_flag)
{
ssh_free(sess_to_client);
break;
}
if (m_need_stop) {
ssh_free(sess_to_client);
break;
}
SshSession* sess = new SshSession(this, sess_to_client);
SshSession *sess = new SshSession(this, sess_to_client);
#ifdef EX_OS_WIN32
getpeername(ssh_get_fd(sess_to_client), (struct sockaddr*)&sock_client, &len);
getpeername(ssh_get_fd(sess_to_client), (struct sockaddr *) &sock_client, &len);
#else
getpeername(ssh_get_fd(sess_to_client), (struct sockaddr*)&sock_client, (unsigned int*)&len);
getpeername(ssh_get_fd(sess_to_client), (struct sockaddr*)&sock_client, (unsigned int*)&len);
#endif
sockaddr_in* addrin = (sockaddr_in*)&sock_client;
sockaddr_in *addrin = (sockaddr_in *) &sock_client;
if (0 == ex_ip4_name(addrin, ip, sizeof(ip)))
{
sess->client_ip(ip);
sess->client_port(addrin->sin_port);
}
if (0 == ex_ip4_name(addrin, ip, sizeof(ip))) {
sess->client_ip(ip);
sess->client_port(addrin->sin_port);
}
EXLOGV("[ssh] ------ NEW SSH CLIENT [%s:%d] ------\n", sess->client_ip(), sess->client_port());
EXLOGV("[ssh] ------ NEW SSH CLIENT [%s:%d] ------\n", sess->client_ip(), sess->client_port());
{
ExThreadSmartLock locker(m_lock);
m_sessions.insert(std::make_pair(sess, 0));
}
{
ExThreadSmartLock locker(m_lock);
m_sessions.insert(std::make_pair(sess, 0));
}
sess->start();
}
sess->start();
}
// 等待所有工作线程退出
m_thread_mgr.stop_all();
// 等待所有工作线程退出
//m_thread_mgr.stop_all();
EXLOGV("[ssh] main-loop end.\n");
{
ExThreadSmartLock locker(m_lock);
ts_ssh_sessions::iterator it = m_sessions.begin();
for (; it != m_sessions.end(); ++it) {
it->first->check_noop_timeout(0, 0); // 立即结束
}
}
for(;;)
{
{
ExThreadSmartLock locker(m_lock);
if (m_sessions.empty())
break;
ex_sleep_ms(100);
}
}
EXLOGV("[ssh] main-loop end.\n");
}
void SshProxy::_set_stop_flag()
{
m_stop_flag = true;
void SshProxy::_on_stop() {
ExThreadBase::_on_stop();
if (m_is_running)
{
// 用一个变通的方式来结束阻塞中的监听,就是连接一下它。
ex_astr host_ip = m_host_ip;
if (host_ip == "0.0.0.0")
host_ip = "127.0.0.1";
if (m_is_running) {
// 用一个变通的方式来结束阻塞中的监听,就是连接一下它。
ex_astr host_ip = m_host_ip;
if (host_ip == "0.0.0.0")
host_ip = "127.0.0.1";
ssh_session _session = ssh_new();
ssh_options_set(_session, SSH_OPTIONS_HOST, host_ip.c_str());
ssh_options_set(_session, SSH_OPTIONS_PORT, &m_host_port);
ssh_session _session = ssh_new();
ssh_options_set(_session, SSH_OPTIONS_HOST, host_ip.c_str());
ssh_options_set(_session, SSH_OPTIONS_PORT, &m_host_port);
int _timeout_us = 10;
ssh_options_set(_session, SSH_OPTIONS_TIMEOUT, &_timeout_us);
ssh_connect(_session);
ssh_free(_session);
}
int _timeout_us = 10;
ssh_options_set(_session, SSH_OPTIONS_TIMEOUT, &_timeout_us);
ssh_connect(_session);
ssh_free(_session);
}
m_thread_mgr.stop_all();
// m_thread_mgr.stop_all();
}
void SshProxy::session_finished(SshSession* sess)
{
// TODO: 向核心模块汇报此会话终止,以减少对应连接信息的引用计数
void SshProxy::session_finished(SshSession *sess) {
// TODO: 向核心模块汇报此会话终止,以减少对应连接信息的引用计数
ExThreadSmartLock locker(m_lock);
ts_ssh_sessions::iterator it = m_sessions.find(sess);
if (it != m_sessions.end())
{
m_sessions.erase(it);
EXLOGV("[ssh] client %s:%d session removed.\n", sess->client_ip(), sess->client_port());
}
else
{
EXLOGW("[ssh] when session %s:%d end, it not in charge.\n", sess->client_ip(), sess->client_port());
}
ExThreadSmartLock locker(m_lock);
ts_ssh_sessions::iterator it = m_sessions.find(sess);
if (it != m_sessions.end()) {
m_sessions.erase(it);
EXLOGV("[ssh] client %s:%d session removed.\n", sess->client_ip(), sess->client_port());
} else {
EXLOGW("[ssh] when session %s:%d end, it not in charge.\n", sess->client_ip(), sess->client_port());
}
delete sess;
delete sess;
}

View File

@ -22,7 +22,7 @@ public:
protected:
void _thread_loop();
void _set_stop_flag();
void _on_stop();
private:
ssh_bind m_bind;
@ -35,7 +35,7 @@ private:
ts_ssh_sessions m_sessions;
ExThreadManager m_thread_mgr;
// ExThreadManager m_thread_mgr;
//
ex_u32 m_noop_timeout_sec;

File diff suppressed because it is too large Load Diff

View File

@ -82,8 +82,9 @@ public:
const ex_astr& sid() { return m_sid; }
protected:
void _thread_loop(void);
void _set_stop_flag(void);
void _thread_loop();
void _on_stop();
void _on_stopped();
// record an error when session connecting or auth-ing.
void _session_error(int err_code);

View File

@ -11,9 +11,9 @@
#ifdef EX_OS_WIN32
# ifdef EX_DEBUG
# pragma comment(lib, "..\\..\\..\\..\\external\\libssh\\lib\\debug\\ssh.lib")
# pragma comment(lib, "..\\..\\..\\..\\external\\libssh\\build\\src\\Debug\\ssh.lib")
# else
# pragma comment(lib, "..\\..\\..\\..\\external\\libssh\\lib\\release\\ssh.lib")
# pragma comment(lib, "..\\..\\..\\..\\external\\libssh\\build\\src\\Release\\ssh.lib")
# endif
# pragma comment(lib, "libeay32.lib")
# pragma comment(lib, "ws2_32.lib")

View File

@ -2,8 +2,7 @@
#include "tpp_env.h"
#include <teleport_const.h>
#include <json/json.h>
#include <json/json.h>
TPP_API ex_rv tpp_init(TPP_INIT_ARGS* init_args)
{
@ -47,20 +46,20 @@ TPP_API void tpp_timer(void) {
static ex_rv _set_runtime_config(const char* param) {
Json::Value jp;
Json::Reader jreader;
Json::Reader jreader;
if (!jreader.parse(param, jp))
return TPE_JSON_FORMAT;
if (!jreader.parse(param, jp))
return TPE_JSON_FORMAT;
if (!jp.isObject())
return TPE_PARAM;
if (jp["noop_timeout"].isNull() || !jp["noop_timeout"].isUInt())
return TPE_PARAM;
ex_u32 noop_timeout = jp["noop_timeout"].asUInt();
if (noop_timeout == 0)
return TPE_PARAM;
if (jp["noop_timeout"].isNull() || !jp["noop_timeout"].isUInt())
return TPE_PARAM;
ex_u32 noop_timeout = jp["noop_timeout"].asUInt();
if (noop_timeout == 0)
return TPE_PARAM;
g_ssh_proxy.set_cfg(noop_timeout * 60);
@ -69,24 +68,24 @@ static ex_rv _set_runtime_config(const char* param) {
static ex_rv _kill_sessions(const char* param) {
Json::Value jp;
Json::Reader jreader;
Json::Reader jreader;
if (!jreader.parse(param, jp))
return TPE_JSON_FORMAT;
if (!jreader.parse(param, jp))
return TPE_JSON_FORMAT;
if (!jp.isArray())
return TPE_PARAM;
ex_astrs ss;
int cnt = jp.size();
for (int i = 0; i < cnt; ++i)
{
if (!jp[i].isString()) {
return TPE_PARAM;
}
ss.push_back(jp[i].asString());
}
int cnt = jp.size();
for (int i = 0; i < cnt; ++i)
{
if (!jp[i].isString()) {
return TPE_PARAM;
}
ss.push_back(jp[i].asString());
}
g_ssh_proxy.kill_sessions(ss);