支持会话无操作超时设置,默认15分钟没有操作则断开连接。

pull/105/head
Apex Liu 2018-04-30 01:54:59 +08:00
parent 420fd13830
commit 39c94b67be
23 changed files with 2032 additions and 1715 deletions

View File

@ -64,7 +64,9 @@ typedef struct TPP_INIT_ARGS
TPP_SESSION_END_FUNC func_session_end;
}TPP_INIT_ARGS;
typedef struct TPP_SET_CFG_ARGS {
ex_u32 noop_timeout; // as second.
}TPP_SET_CFG_ARGS;
#ifdef __cplusplus
extern "C"
@ -75,6 +77,7 @@ extern "C"
TPP_API ex_rv tpp_start(void);
TPP_API ex_rv tpp_stop(void);
TPP_API void tpp_timer(void);
TPP_API void tpp_set_cfg(TPP_SET_CFG_ARGS* cfg_args);
#ifdef __cplusplus
}
@ -84,5 +87,6 @@ typedef ex_rv (*TPP_INIT_FUNC)(TPP_INIT_ARGS* init_args);
typedef ex_rv (*TPP_START_FUNC)(void);
typedef ex_rv(*TPP_STOP_FUNC)(void);
typedef void(*TPP_TIMER_FUNC)(void);
typedef void(*TPP_SET_CFG_FUNC)(TPP_SET_CFG_ARGS* cfg_args);
#endif // __TP_PROTOCOL_INTERFACE_H__

View File

@ -184,6 +184,7 @@
<ClCompile Include="..\..\..\external\mbedtls\library\sha1.c" />
<ClCompile Include="..\..\..\external\mongoose\mongoose.c" />
<ClCompile Include="main.cpp" />
<ClCompile Include="tp_tpp_mgr.cpp" />
<ClCompile Include="ts_crypto.cpp" />
<ClCompile Include="ts_web_rpc.cpp" />
<ClCompile Include="ts_env.cpp" />
@ -210,6 +211,7 @@
<ClInclude Include="..\common\protocol_interface.h" />
<ClInclude Include="..\common\ts_const.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="tp_tpp_mgr.h" />
<ClInclude Include="ts_crypto.h" />
<ClInclude Include="ts_ver.h" />
<ClInclude Include="ts_web_rpc.h" />

View File

@ -106,6 +106,9 @@
<ClCompile Include="ts_web_rpc.cpp">
<Filter>main app</Filter>
</ClCompile>
<ClCompile Include="tp_tpp_mgr.cpp">
<Filter>main app</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ts_main.h">
@ -183,6 +186,9 @@
<ClInclude Include="..\..\..\common\teleport\teleport_const.h">
<Filter>common</Filter>
</ClInclude>
<ClInclude Include="tp_tpp_mgr.h">
<Filter>main app</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="tp_core.rc">

View File

@ -0,0 +1,120 @@
#include "tp_tpp_mgr.h"
#include "ts_main.h"
// #include "ts_session.h"
// #include "ts_http_rpc.h"
// #include "ts_web_rpc.h"
#include "ts_env.h"
// #include <mbedtls/platform.h>
// #include <mbedtls/debug.h>
TppManager g_tpp_mgr;
extern ExLogger g_ex_logger;
bool TppManager::load_tpp(const ex_wstr& libname)
{
ex_wstr filename;
#ifdef EX_OS_WIN32
filename = libname + L".dll";
#elif defined (EX_OS_LINUX)
filename = L"lib";
filename += libname;
filename += L".so";
#elif defined (EX_OS_MACOS)
filename = L"lib";
filename += libname;
filename += L".dylib";
#endif
ex_wstr libfile = g_env.m_exec_path;
ex_path_join(libfile, false, filename.c_str(), NULL);
EXLOGV(L"[core] load protocol lib: %ls\n", libfile.c_str());
TPP_LIB* lib = new TPP_LIB;
lib->dylib = ex_dlopen(libfile.c_str());
if (NULL == lib->dylib)
{
EXLOGE(L"[core] load dylib `%ls` failed.\n", libfile.c_str());
delete lib;
return false;
}
#ifdef EX_OS_WIN32
lib->init = (TPP_INIT_FUNC)GetProcAddress(lib->dylib, "tpp_init");
lib->start = (TPP_START_FUNC)GetProcAddress(lib->dylib, "tpp_start");
lib->stop = (TPP_STOP_FUNC)GetProcAddress(lib->dylib, "tpp_stop");
lib->timer = (TPP_TIMER_FUNC)GetProcAddress(lib->dylib, "tpp_timer");
lib->set_cfg = (TPP_SET_CFG_FUNC)GetProcAddress(lib->dylib, "tpp_set_cfg");
#else
lib->init = (TPP_INIT_FUNC)dlsym(lib->dylib, "tpp_init");
lib->start = (TPP_START_FUNC)dlsym(lib->dylib, "tpp_start");
lib->stop = (TPP_STOP_FUNC)dlsym(lib->dylib, "tpp_stop");
lib->timer = (TPP_TIMER_FUNC)dlsym(lib->dylib, "tpp_timer");
lib->set_cfg = (TPP_SET_CFG_FUNC)dlsym(lib->dylib, "tpp_set_cfg");
#endif
if (lib->init == NULL || lib->start == NULL || lib->stop == NULL || lib->timer == NULL || lib->set_cfg == NULL)
{
EXLOGE(L"[core] load dylib `%ls` failed, can not locate all functions.\n", libfile.c_str());
delete lib;
return false;
}
TPP_INIT_ARGS init_args;
init_args.logger = &g_ex_logger;
init_args.exec_path = g_env.m_exec_path;
init_args.etc_path = g_env.m_etc_path;
init_args.replay_path = g_env.m_replay_path;
init_args.cfg = &g_env.get_ini();
init_args.func_get_connect_info = tpp_get_connect_info;
init_args.func_free_connect_info = tpp_free_connect_info;
init_args.func_session_begin = tpp_session_begin;
init_args.func_session_update = tpp_session_update;
init_args.func_session_end = tpp_session_end;
if (EXRV_OK != lib->init(&init_args))
{
EXLOGE(L"[core] failed to init protocol `%ls`.\n", libname.c_str());
delete lib;
return false;
}
if (EXRV_OK != lib->start())
{
EXLOGE(L"[core] failed to start protocol `%ls`.\n", libname.c_str());
delete lib;
return false;
}
m_libs.push_back(lib);
return true;
}
void TppManager::stop_all(void) {
tpp_libs::iterator it = m_libs.begin();
for (; it != m_libs.end(); ++it)
{
(*it)->stop();
}
}
void TppManager::timer(void) {
tpp_libs::iterator it = m_libs.begin();
for (; it != m_libs.end(); ++it)
{
(*it)->timer();
}
}
void TppManager::set_config(int noop_timeout) {
TPP_SET_CFG_ARGS args;
args.noop_timeout = noop_timeout;
tpp_libs::iterator it = m_libs.begin();
for (; it != m_libs.end(); ++it)
{
(*it)->set_cfg(&args);
}
}

View File

@ -0,0 +1,61 @@
#ifndef __TP_TPP_MGR_H__
#define __TP_TPP_MGR_H__
#include "../common/protocol_interface.h"
#include <ex.h>
typedef struct TPP_LIB
{
TPP_LIB()
{
dylib = NULL;
init = NULL;
}
~TPP_LIB()
{
if (NULL != dylib)
ex_dlclose(dylib);
dylib = NULL;
}
EX_DYLIB_HANDLE dylib;
TPP_INIT_FUNC init;
TPP_START_FUNC start;
TPP_STOP_FUNC stop;
TPP_TIMER_FUNC timer;
TPP_SET_CFG_FUNC set_cfg;
}TPP_LIB;
typedef std::list<TPP_LIB*> tpp_libs;
class TppManager
{
public:
TppManager()
{
}
~TppManager()
{
tpp_libs::iterator it = m_libs.begin();
for (; it != m_libs.end(); ++it)
{
delete (*it);
}
m_libs.clear();
}
bool load_tpp(const ex_wstr& libfile);
void stop_all(void);
void timer(void); // ´óÔ¼1Ãëµ÷ÓÃÒ»´Î
int count(void) { return m_libs.size(); }
void set_config(int noop_timeout);
private:
tpp_libs m_libs;
};
extern TppManager g_tpp_mgr;
#endif // __TP_TPP_MGR_H__

View File

@ -4,6 +4,9 @@
#include "ts_session.h"
#include "ts_crypto.h"
#include "ts_web_rpc.h"
#include "tp_tpp_mgr.h"
extern TppManager g_tpp_mgr;
#include <teleport_const.h>
@ -249,24 +252,22 @@ void TsHttpRpc::_create_json_ret(ex_astr& buf, int errcode, const char* message)
void TsHttpRpc::_process_request(const ex_astr& func_cmd, const Json::Value& json_param, ex_astr& buf)
{
if (func_cmd == "request_session")
{
if (func_cmd == "request_session") {
_rpc_func_request_session(json_param, buf);
}
else if (func_cmd == "get_config")
{
else if (func_cmd == "get_config") {
_rpc_func_get_config(json_param, buf);
}
else if (func_cmd == "enc")
{
else if (func_cmd == "set_config") {
_rpc_func_set_config(json_param, buf);
}
else if (func_cmd == "enc") {
_rpc_func_enc(json_param, buf);
}
else if (func_cmd == "exit")
{
else if (func_cmd == "exit") {
_rpc_func_exit(json_param, buf);
}
else
{
else {
EXLOGE("[core] rpc got unknown command: %s\n", func_cmd.c_str());
_create_json_ret(buf, TPE_UNKNOWN_CMD);
}
@ -422,6 +423,46 @@ void TsHttpRpc::_rpc_func_enc(const Json::Value& json_param, ex_astr& buf)
_create_json_ret(buf, TPE_OK, jr_data);
}
void TsHttpRpc::_rpc_func_set_config(const Json::Value& json_param, ex_astr& buf)
{
// https://github.com/eomsoft/teleport/wiki/TELEPORT-CORE-JSON-RPC#set_config
/*
{
"noop-timeout": 900 # 900s = 15m
}
*/
if (json_param.isArray())
{
_create_json_ret(buf, TPE_PARAM);
return;
}
if (json_param["noop_timeout"].isNull() || !json_param["noop_timeout"].isUInt())
{
_create_json_ret(buf, TPE_PARAM);
return;
}
int noop_timeout = json_param["noop_timeout"].asUInt();
if (noop_timeout == 0)
{
_create_json_ret(buf, TPE_PARAM);
return;
}
//static TppManager g_tpp_mgr;
EXLOGV("[core] no-op timeout set to %d minutes.\n", noop_timeout);
g_tpp_mgr.set_config(noop_timeout);
// Json::Value jr_data;
// jr_data["c"] = cipher_text;
// _create_json_ret(buf, TPE_OK, jr_data);
_create_json_ret(buf, TPE_OK);
}
/*
void TsHttpRpc::_rpc_func_enc(const Json::Value& json_param, ex_astr& buf)
{

View File

@ -1,76 +1,54 @@
#ifndef __TS_HTTP_RPC_H__
#define __TS_HTTP_RPC_H__
#include "mongoose.h"
#include <ex.h>
#include <json/json.h>
/*
//=================================================================
使
127.0.0.1:52080http
GET
http://127.0.0.1:52080/method/json_param
json_param使url_encodejson
POST
http://127.0.0.1:52080/method
postjson_param
URI
method
json_param
json
{"code":0,"data":varb}
code0datadata
*/
class TsHttpRpc : public ExThreadBase
{
public:
TsHttpRpc();
~TsHttpRpc();
bool init(void);
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);
void _process_request(const ex_astr& func_cmd, const Json::Value& json_param, ex_astr& buf);
//void _create_json_ret(ex_astr& buf, Json::Value& jr_root);
void _create_json_ret(ex_astr& buf, int errcode, const Json::Value& jr_data);
void _create_json_ret(ex_astr& buf, int errcode);
void _create_json_ret(ex_astr& buf, int errcode, const char* message);
// 获取core服务的配置信息主要是支持的各个协议是否启用以及其端口号等
void _rpc_func_get_config(const Json::Value& json_param, ex_astr& buf);
// 请求一个会话ID
void _rpc_func_request_session(const Json::Value& json_param, ex_astr& buf);
// 加密一个字符串返回的是密文的BASE64编码
void _rpc_func_enc(const Json::Value& json_param, ex_astr& buf);
// 要求整个核心服务退出
void _rpc_func_exit(const Json::Value& json_param, ex_astr& buf);
static void _mg_event_handler(struct mg_connection *nc, int ev, void *ev_data);
private:
ex_astr m_host_ip;
int m_host_port;
struct mg_mgr m_mg_mgr;
};
#endif // __TS_HTTP_RPC_H__
#ifndef __TS_HTTP_RPC_H__
#define __TS_HTTP_RPC_H__
#include "mongoose.h"
#include <ex.h>
#include <json/json.h>
// JSON-RPC documentation at:
// https://github.com/eomsoft/teleport/wiki/TELEPORT-CORE-JSON-RPC
class TsHttpRpc : public ExThreadBase
{
public:
TsHttpRpc();
~TsHttpRpc();
bool init(void);
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);
void _process_request(const ex_astr& func_cmd, const Json::Value& json_param, ex_astr& buf);
//void _create_json_ret(ex_astr& buf, Json::Value& jr_root);
void _create_json_ret(ex_astr& buf, int errcode, const Json::Value& jr_data);
void _create_json_ret(ex_astr& buf, int errcode);
void _create_json_ret(ex_astr& buf, int errcode, const char* message);
// 获取core服务的配置信息主要是支持的各个协议是否启用以及其端口号等
void _rpc_func_get_config(const Json::Value& json_param, ex_astr& buf);
// set run-time configuration, like no-op-timeout.
void _rpc_func_set_config(const Json::Value& json_param, ex_astr& buf);
// 请求一个会话ID
void _rpc_func_request_session(const Json::Value& json_param, ex_astr& buf);
// 加密一个字符串返回的是密文的BASE64编码
void _rpc_func_enc(const Json::Value& json_param, ex_astr& buf);
// 要求整个核心服务退出
void _rpc_func_exit(const Json::Value& json_param, ex_astr& buf);
static void _mg_event_handler(struct mg_connection *nc, int ev, void *ev_data);
private:
ex_astr m_host_ip;
int m_host_port;
struct mg_mgr m_mg_mgr;
};
#endif // __TS_HTTP_RPC_H__

View File

@ -3,6 +3,7 @@
#include "ts_http_rpc.h"
#include "ts_web_rpc.h"
#include "ts_env.h"
#include "tp_tpp_mgr.h"
#include <mbedtls/platform.h>
#include <mbedtls/debug.h>
@ -97,154 +98,153 @@ bool tpp_session_update(int db_id, int protocol_sub_type, int state) {
return ts_web_rpc_session_update(db_id, protocol_sub_type, state);
}
bool tpp_session_end(const char* sid, int db_id, int ret)
{
bool tpp_session_end(const char* sid, int db_id, int ret) {
return ts_web_rpc_session_end(sid, db_id, ret);
}
typedef struct TPP_LIB
{
TPP_LIB()
{
dylib = NULL;
init = NULL;
}
~TPP_LIB()
{
if (NULL != dylib)
ex_dlclose(dylib);
dylib = NULL;
}
EX_DYLIB_HANDLE dylib;
TPP_INIT_FUNC init;
TPP_START_FUNC start;
TPP_STOP_FUNC stop;
TPP_TIMER_FUNC timer;
}TPP_LIB;
typedef std::list<TPP_LIB*> tpp_libs;
class TppManager
{
public:
TppManager()
{
}
~TppManager()
{
tpp_libs::iterator it = m_libs.begin();
for (; it != m_libs.end(); ++it)
{
delete (*it);
}
m_libs.clear();
}
bool load_tpp(const ex_wstr& libfile);
void stop_all(void);
void timer(void); // ´óÔ¼1Ãëµ÷ÓÃÒ»´Î
int count(void) { return m_libs.size(); }
private:
tpp_libs m_libs;
};
static TppManager g_tpp_mgr;
extern ExLogger g_ex_logger;
bool TppManager::load_tpp(const ex_wstr& libname)
{
ex_wstr filename;
#ifdef EX_OS_WIN32
filename = libname + L".dll";
#elif defined (EX_OS_LINUX)
filename = L"lib";
filename += libname;
filename += L".so";
#elif defined (EX_OS_MACOS)
filename = L"lib";
filename += libname;
filename += L".dylib";
#endif
ex_wstr libfile = g_env.m_exec_path;
ex_path_join(libfile, false, filename.c_str(), NULL);
EXLOGV(L"[core] load protocol lib: %ls\n", libfile.c_str());
TPP_LIB* lib = new TPP_LIB;
lib->dylib = ex_dlopen(libfile.c_str());
if (NULL == lib->dylib)
{
EXLOGE(L"[core] load dylib `%ls` failed.\n", libfile.c_str());
delete lib;
return false;
}
#ifdef EX_OS_WIN32
lib->init = (TPP_INIT_FUNC)GetProcAddress(lib->dylib, "tpp_init");
lib->start = (TPP_START_FUNC)GetProcAddress(lib->dylib, "tpp_start");
lib->stop = (TPP_STOP_FUNC)GetProcAddress(lib->dylib, "tpp_stop");
lib->timer = (TPP_TIMER_FUNC)GetProcAddress(lib->dylib, "tpp_timer");
#else
lib->init = (TPP_INIT_FUNC)dlsym(lib->dylib, "tpp_init");
lib->start = (TPP_START_FUNC)dlsym(lib->dylib, "tpp_start");
lib->stop = (TPP_STOP_FUNC)dlsym(lib->dylib, "tpp_stop");
lib->timer = (TPP_TIMER_FUNC)dlsym(lib->dylib, "tpp_timer");
#endif
if (lib->init == NULL || lib->start == NULL || lib->stop == NULL || lib->timer == NULL)
{
EXLOGE(L"[core] load dylib `%ls` failed, can not locate all functions.\n", libfile.c_str());
delete lib;
return false;
}
TPP_INIT_ARGS init_args;
init_args.logger = &g_ex_logger;
init_args.exec_path = g_env.m_exec_path;
init_args.etc_path = g_env.m_etc_path;
init_args.replay_path = g_env.m_replay_path;
init_args.cfg = &g_env.get_ini();
init_args.func_get_connect_info = tpp_get_connect_info;
init_args.func_free_connect_info = tpp_free_connect_info;
init_args.func_session_begin = tpp_session_begin;
init_args.func_session_update = tpp_session_update;
init_args.func_session_end = tpp_session_end;
if (EXRV_OK != lib->init(&init_args))
{
EXLOGE(L"[core] failed to init protocol `%ls`.\n", libname.c_str());
delete lib;
return false;
}
if (EXRV_OK != lib->start())
{
EXLOGE(L"[core] failed to start protocol `%ls`.\n", libname.c_str());
delete lib;
return false;
}
m_libs.push_back(lib);
return true;
}
void TppManager::stop_all(void) {
tpp_libs::iterator it = m_libs.begin();
for (; it != m_libs.end(); ++it)
{
(*it)->stop();
}
}
void TppManager::timer(void) {
tpp_libs::iterator it = m_libs.begin();
for (; it != m_libs.end(); ++it)
{
(*it)->timer();
}
}
// typedef struct TPP_LIB
// {
// TPP_LIB()
// {
// dylib = NULL;
// init = NULL;
// }
// ~TPP_LIB()
// {
// if (NULL != dylib)
// ex_dlclose(dylib);
// dylib = NULL;
// }
//
// EX_DYLIB_HANDLE dylib;
// TPP_INIT_FUNC init;
// TPP_START_FUNC start;
// TPP_STOP_FUNC stop;
// TPP_TIMER_FUNC timer;
// }TPP_LIB;
//
// typedef std::list<TPP_LIB*> tpp_libs;
//
// class TppManager
// {
// public:
// TppManager()
// {
// }
// ~TppManager()
// {
// tpp_libs::iterator it = m_libs.begin();
// for (; it != m_libs.end(); ++it)
// {
// delete (*it);
// }
// m_libs.clear();
// }
//
// bool load_tpp(const ex_wstr& libfile);
// void stop_all(void);
// void timer(void); // ´óÔ¼1Ãëµ÷ÓÃÒ»´Î
// int count(void) { return m_libs.size(); }
//
// private:
// tpp_libs m_libs;
// };
//
// static TppManager g_tpp_mgr;
// extern ExLogger g_ex_logger;
//
// bool TppManager::load_tpp(const ex_wstr& libname)
// {
// ex_wstr filename;
// #ifdef EX_OS_WIN32
// filename = libname + L".dll";
// #elif defined (EX_OS_LINUX)
// filename = L"lib";
// filename += libname;
// filename += L".so";
// #elif defined (EX_OS_MACOS)
// filename = L"lib";
// filename += libname;
// filename += L".dylib";
// #endif
//
// ex_wstr libfile = g_env.m_exec_path;
// ex_path_join(libfile, false, filename.c_str(), NULL);
// EXLOGV(L"[core] load protocol lib: %ls\n", libfile.c_str());
//
// TPP_LIB* lib = new TPP_LIB;
//
// lib->dylib = ex_dlopen(libfile.c_str());
// if (NULL == lib->dylib)
// {
// EXLOGE(L"[core] load dylib `%ls` failed.\n", libfile.c_str());
// delete lib;
// return false;
// }
//
// #ifdef EX_OS_WIN32
// lib->init = (TPP_INIT_FUNC)GetProcAddress(lib->dylib, "tpp_init");
// lib->start = (TPP_START_FUNC)GetProcAddress(lib->dylib, "tpp_start");
// lib->stop = (TPP_STOP_FUNC)GetProcAddress(lib->dylib, "tpp_stop");
// lib->timer = (TPP_TIMER_FUNC)GetProcAddress(lib->dylib, "tpp_timer");
// #else
// lib->init = (TPP_INIT_FUNC)dlsym(lib->dylib, "tpp_init");
// lib->start = (TPP_START_FUNC)dlsym(lib->dylib, "tpp_start");
// lib->stop = (TPP_STOP_FUNC)dlsym(lib->dylib, "tpp_stop");
// lib->timer = (TPP_TIMER_FUNC)dlsym(lib->dylib, "tpp_timer");
// #endif
//
// if (lib->init == NULL || lib->start == NULL || lib->stop == NULL || lib->timer == NULL)
// {
// EXLOGE(L"[core] load dylib `%ls` failed, can not locate all functions.\n", libfile.c_str());
// delete lib;
// return false;
// }
//
// TPP_INIT_ARGS init_args;
// init_args.logger = &g_ex_logger;
// init_args.exec_path = g_env.m_exec_path;
// init_args.etc_path = g_env.m_etc_path;
// init_args.replay_path = g_env.m_replay_path;
// init_args.cfg = &g_env.get_ini();
// init_args.func_get_connect_info = tpp_get_connect_info;
// init_args.func_free_connect_info = tpp_free_connect_info;
// init_args.func_session_begin = tpp_session_begin;
// init_args.func_session_update = tpp_session_update;
// init_args.func_session_end = tpp_session_end;
//
// if (EXRV_OK != lib->init(&init_args))
// {
// EXLOGE(L"[core] failed to init protocol `%ls`.\n", libname.c_str());
// delete lib;
// return false;
// }
// if (EXRV_OK != lib->start())
// {
// EXLOGE(L"[core] failed to start protocol `%ls`.\n", libname.c_str());
// delete lib;
// return false;
// }
//
// m_libs.push_back(lib);
// return true;
// }
//
// void TppManager::stop_all(void) {
// tpp_libs::iterator it = m_libs.begin();
// for (; it != m_libs.end(); ++it)
// {
// (*it)->stop();
// }
// }
//
// void TppManager::timer(void) {
// tpp_libs::iterator it = m_libs.begin();
// for (; it != m_libs.end(); ++it)
// {
// (*it)->timer();
// }
// }
int ts_main(void)
{

View File

@ -1,6 +1,15 @@
#ifndef __TS_MAIN_H__
#define __TS_MAIN_H__
int ts_main(void);
#endif // __TS_MAIN_H__
#ifndef __TS_MAIN_H__
#define __TS_MAIN_H__
#include "../common/protocol_interface.h"
int ts_main(void);
TPP_CONNECT_INFO* tpp_get_connect_info(const char* sid);
void tpp_free_connect_info(TPP_CONNECT_INFO* info);
bool tpp_session_begin(const TPP_CONNECT_INFO* info, int* db_id);
bool tpp_session_update(int db_id, int protocol_sub_type, int state);
bool tpp_session_end(const char* sid, int db_id, int ret);
#endif // __TS_MAIN_H__

View File

@ -8,6 +8,8 @@ SshProxy::SshProxy() :
m_bind(NULL)
{
m_timer_counter = 0;
m_noop_timeout_sec = 900; // default to 15 minutes.
}
SshProxy::~SshProxy()
@ -71,13 +73,20 @@ void SshProxy::timer() {
m_timer_counter = 0;
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);
}
}
void SshProxy::set_cfg(TPP_SET_CFG_ARGS* args) {
m_noop_timeout_sec = args->noop_timeout;
}
void SshProxy::_thread_loop()
{
EXLOGI("[ssh] TeleportServer-SSH ready on %s:%d\n", m_host_ip.c_str(), m_host_port);

View File

@ -15,6 +15,7 @@ public:
bool init();
void timer();
void set_cfg(TPP_SET_CFG_ARGS* args);
void session_finished(SshSession* sess);
@ -34,6 +35,9 @@ private:
ts_ssh_sessions m_sessions;
ExThreadManager m_thread_mgr;
//
ex_u32 m_noop_timeout_sec;
};
extern SshProxy g_ssh_proxy;

File diff suppressed because it is too large Load Diff

View File

@ -37,6 +37,7 @@ private:
ssh_channel srv_channel;
TppSshRec rec;
ex_u32 last_access_timestamp;
int state;
int db_id;
@ -45,6 +46,7 @@ private:
int win_width; // window width, in char count.
bool is_first_server_data;
bool need_close;
// for ssh command record cache.
bool server_ready;
@ -74,6 +76,8 @@ public:
// save record cache into file. be called per 5 seconds.
void save_record();
//
void check_noop_timeout(ex_u32 t_now, ex_u32 timeout);
protected:
void _thread_loop(void);

View File

@ -38,3 +38,7 @@ TPP_API void tpp_timer(void) {
// be called per one second.
g_ssh_proxy.timer();
}
TPP_API void tpp_set_cfg(TPP_SET_CFG_ARGS* cfg_args) {
g_ssh_proxy.set_cfg(cfg_args);
}

View File

@ -8,6 +8,7 @@ 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()
@ -44,13 +45,20 @@ void TelnetProxy::timer() {
m_timer_counter = 0;
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)
{
it->first->save_record();
if(0 != m_noop_timeout_sec)
it->first->check_noop_timeout(t_now, m_noop_timeout_sec);
}
}
void TelnetProxy::set_cfg(TPP_SET_CFG_ARGS* args) {
m_noop_timeout_sec = args->noop_timeout;
}
void TelnetProxy::_thread_loop(void)
{
struct sockaddr_in addr;

View File

@ -16,6 +16,8 @@ public:
bool init();
void timer();
void set_cfg(TPP_SET_CFG_ARGS* args);
uv_loop_t* get_loop() { return &m_loop; }
void clean_session();
@ -37,6 +39,8 @@ private:
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;

View File

@ -22,6 +22,7 @@ TelnetSession::TelnetSession(TelnetProxy *proxy) :
m_is_relay = false;
m_is_closed = false;
m_first_client_pkg = true;
m_last_access_timestamp = (ex_u32)time(NULL);
m_win_width = 0;
m_win_height = 0;
@ -54,6 +55,13 @@ void TelnetSession::save_record() {
m_rec.save_record();
}
void TelnetSession::check_noop_timeout(ex_u32 t_now, ex_u32 timeout) {
if (t_now - m_last_access_timestamp > timeout) {
EXLOGW("[telnet] need close session by timeout.\n");
_do_close(TP_SESS_STAT_END);
}
}
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)
@ -465,41 +473,41 @@ sess_state TelnetSession::_do_server_connected() {
int w = 50;
if (m_win_width != 0) {
#ifdef EX_OS_WIN32
int w = min(m_win_width, 128);
#else
int w = std::min(m_win_width, 128);
#endif
#ifdef EX_OS_WIN32
int w = min(m_win_width, 128);
#else
int w = std::min(m_win_width, 128);
#endif
m_startup_win_size_recorded = true;
m_rec.record_win_size_startup(m_win_width, m_win_height);
}
char buf[512] = { 0 };
const char *auth_mode = NULL;
if (m_conn_info->auth_type == TP_AUTH_TYPE_PASSWORD)
auth_mode = "password";
else if (m_conn_info->auth_type == TP_AUTH_TYPE_NONE)
auth_mode = "nothing";
else
auth_mode = "unknown";
ex_astr line(w, '=');
snprintf(buf, sizeof(buf),
"\r\n"\
"%s\r\n"\
"Teleport TELNET Bastion Server...\r\n"\
" - teleport to %s:%d\r\n"\
" - authroized by %s\r\n"\
"%s\r\n"\
"\r\n\r\n",
line.c_str(),
m_conn_ip.c_str(),
m_conn_port, auth_mode,
line.c_str()
);
char buf[512] = { 0 };
const char *auth_mode = NULL;
if (m_conn_info->auth_type == TP_AUTH_TYPE_PASSWORD)
auth_mode = "password";
else if (m_conn_info->auth_type == TP_AUTH_TYPE_NONE)
auth_mode = "nothing";
else
auth_mode = "unknown";
ex_astr line(w, '=');
snprintf(buf, sizeof(buf),
"\r\n"\
"%s\r\n"\
"Teleport TELNET Bastion Server...\r\n"\
" - teleport to %s:%d\r\n"\
" - authroized by %s\r\n"\
"%s\r\n"\
"\r\n\r\n",
line.c_str(),
m_conn_ip.c_str(),
m_conn_port, auth_mode,
line.c_str()
);
m_conn_client->send((ex_u8*)buf, strlen(buf));
if (m_is_putty_mode)
@ -520,6 +528,8 @@ sess_state TelnetSession::_do_server_connected() {
}
sess_state TelnetSession::_do_relay(TelnetConn *conn) {
m_last_access_timestamp = (ex_u32)time(NULL);
TelnetSession* _this = conn->session();
bool is_processed = false;

View File

@ -49,6 +49,8 @@ public:
void record(ex_u8 type, const ex_u8* data, size_t size) {
m_rec.record(type, data, size);
}
//
void check_noop_timeout(ex_u32 t_now, ex_u32 timeout);
void client_addr(const char* addr) { m_client_addr = addr; }
const char* client_addr() const { return m_client_addr.c_str(); }
@ -85,6 +87,7 @@ private:
bool m_first_client_pkg;
bool m_is_relay; // 是否进入relay模式了只有进入relay模式才会有录像存在
bool m_is_closed;
ex_u32 m_last_access_timestamp;
TppTelnetRec m_rec;
int m_win_width;

View File

@ -30,3 +30,8 @@ TPP_API void tpp_timer(void) {
// be called per one second.
g_telnet_proxy.timer();
}
TPP_API void tpp_set_cfg(TPP_SET_CFG_ARGS* cfg_args) {
g_telnet_proxy.set_cfg(cfg_args);
}

View File

@ -88,8 +88,8 @@
</tr>
<tr>
<td class="key">认证方式</td>
<td class="value">
注意:可以为每个用户指定特定的认证方式。
<td class="value">设置系统启用的登录认证方式
<span class="desc">还可以为每个用户指定特定的登录认证方式。</span>
</td>
</tr>
## <tr>
@ -167,7 +167,7 @@
<tr>
<td colspan="2" class="title">
## <hr class="hr-sm"/>
全局RDP选项
全局RDP选项(注:尚未实现)
</td>
</tr>
@ -196,7 +196,7 @@
<tr>
<td colspan="2" class="title">
<hr class="hr-sm"/>
全局SSH选项
全局SSH选项(注:尚未实现)
</td>
</tr>

View File

@ -56,10 +56,13 @@ class WebApp:
rep = urllib.request.urlopen(req, timeout=3)
body = rep.read().decode()
x = json.loads(body)
log.d('connect core server and get config info succeeded.\n')
cfg.update_core(x['data'])
if 'code' not in x or x['code'] != 0:
log.e('connect core-server for get config info failed.\n')
else:
cfg.update_core(x['data'])
log.d('get config info of core-server succeeded.\n')
except:
log.w('can not connect to core server to get config, maybe it not start yet, ignore.\n')
log.w('can not connect to core-server to get config, maybe it not start yet, ignore.\n')
def run(self):
log.i('\n')
@ -93,6 +96,23 @@ class WebApp:
cfg.app_mode = APP_MODE_NORMAL
_db.load_system_config()
try:
# 将运行时配置发送给核心服务
req = {'method': 'set_config', 'param': {'noop_timeout': tp_cfg().sys.session.noop_timeout}}
req_data = json.dumps(req)
data = urllib.parse.quote(req_data).encode('utf-8')
req = urllib.request.Request(url=cfg.common.core_server_rpc, data=data)
rep = urllib.request.urlopen(req, timeout=3)
body = rep.read().decode()
x = json.loads(body)
if 'code' not in x or x['code'] != 0:
print(x)
log.e('connect core-server for set runtime-config failed.\n')
else:
log.d('set runtime-config for core-server succeeded.\n')
except:
log.w('can not connect to core-server to set runtime-config, maybe it not start yet, ignore.\n')
if not tp_session().init():
log.e('can not initialize session manager.\n')
return 0

View File

@ -103,7 +103,7 @@ class RpcHandler(TPBaseJsonHandler):
code = param['code']
except:
return self.write_json(TPE_PARAM)
if 'rid' not in param or 'code' not in param :
if 'rid' not in param or 'code' not in param:
return self.write_json(TPE_PARAM)
if not record.session_update(rid, protocol_sub_type, code):
@ -140,6 +140,13 @@ class RpcHandler(TPBaseJsonHandler):
log.d('update base server config info.\n')
tp_cfg().update_core(ret_data)
# 将运行时配置发送给核心服务
req = {'method': 'set_config', 'param': {'noop_timeout': tp_cfg().sys.session.noop_timeout}}
_yr = core_service_async_post_http(req)
code, ret_data = yield _yr
if code != TPE_OK:
return self.write_json(code, 'set runtime-config to core-service failed.')
return self.write_json(TPE_OK)
def _exit(self):

View File

@ -216,6 +216,7 @@ class DoGetLogsHandler(TPBaseJsonHandler):
class DoSaveCfgHandler(TPBaseJsonHandler):
@tornado.gen.coroutine
def post(self):
ret = self.check_privilege(TP_PRIVILEGE_SYS_CONFIG)
if ret != TPE_OK:
@ -287,11 +288,20 @@ class DoSaveCfgHandler(TPBaseJsonHandler):
_flag_ssh = _cfg['flag_ssh']
err = system_model.save_config(self, '更新连接控制设置', 'session', _cfg)
if err == TPE_OK:
try:
req = {'method': 'set_config', 'param': {'noop_timeout': _noop_timeout}}
_yr = core_service_async_post_http(req)
code, ret_data = yield _yr
if code != TPE_OK:
log.e('can not set runtime-config to core-server.\n')
return self.write_json(code)
except:
pass
tp_cfg().sys.session.noop_timeout = _noop_timeout
tp_cfg().sys.session.flag_record = _flag_record
tp_cfg().sys.session.flag_rdp = _flag_rdp
tp_cfg().sys.session.flag_ssh = _flag_ssh
tp_session().update_default_expire()
else:
return self.write_json(err)