修正升级了jsoncpp库之后tp_core无法编译的问题。

pull/236/head
Apex Liu 2019-11-17 04:43:33 +08:00
parent 483aa810ee
commit f99af6a2df
2 changed files with 929 additions and 873 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,320 +1,359 @@
#include "ts_web_rpc.h" #include "ts_web_rpc.h"
#include "ts_env.h" #include "ts_env.h"
#include "ts_crypto.h" #include "ts_crypto.h"
#include "ts_http_client.h" #include "ts_http_client.h"
#include "../common/ts_const.h" #include "../common/ts_const.h"
#include <ex/ex_str.h> #include <ex/ex_str.h>
#include <teleport_const.h> #include <teleport_const.h>
bool ts_web_rpc_register_core() bool ts_web_rpc_register_core()
{ {
Json::FastWriter json_writer; //Json::FastWriter json_writer;
Json::Value jreq; Json::Value jreq;
jreq["method"] = "register_core"; jreq["method"] = "register_core";
jreq["param"]["rpc"] = g_env.core_server_rpc; jreq["param"]["rpc"] = g_env.core_server_rpc;
ex_astr json_param; ex_astr json_param;
json_param = json_writer.write(jreq); //json_param = json_writer.write(jreq);
Json::StreamWriterBuilder jwb;
ex_astr param; std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ts_url_encode(json_param.c_str(), param); ex_aoss os;
jwriter->write(jreq, &os);
ex_astr url = g_env.web_server_rpc; json_param = os.str();
url += "?";
url += param; ex_astr param;
ts_url_encode(json_param.c_str(), param);
ex_astr body;
return ts_http_get(url, body); ex_astr url = g_env.web_server_rpc;
} url += "?";
url += param;
int ts_web_rpc_get_conn_info(int conn_id, TS_CONNECT_INFO& info)
{ ex_astr body;
Json::FastWriter json_writer; return ts_http_get(url, body);
Json::Value jreq; }
jreq["method"] = "get_conn_info";
jreq["param"]["conn_id"] = conn_id; int ts_web_rpc_get_conn_info(int conn_id, TS_CONNECT_INFO& info)
{
ex_astr json_param; //Json::FastWriter json_writer;
json_param = json_writer.write(jreq); Json::Value jreq;
jreq["method"] = "get_conn_info";
ex_astr param; jreq["param"]["conn_id"] = conn_id;
ts_url_encode(json_param.c_str(), param);
ex_astr json_param;
ex_astr url = g_env.web_server_rpc; //json_param = json_writer.write(jreq);
url += "?"; Json::StreamWriterBuilder jwb;
url += param; std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
ex_astr body; jwriter->write(jreq, &os);
if (!ts_http_get(url, body)) json_param = os.str();
{
EXLOGE("[core] get conn info from web-server failed: can not connect to web-server.\n"); ex_astr param;
return TPE_NETWORK; ts_url_encode(json_param.c_str(), param);
}
if (body.length() == 0) { ex_astr url = g_env.web_server_rpc;
EXLOGE("[core] get conn info from web-server failed: got nothing.\n"); url += "?";
return TPE_NETWORK; url += param;
}
ex_astr body;
Json::Reader jreader; if (!ts_http_get(url, body))
Json::Value jret; {
EXLOGE("[core] get conn info from web-server failed: can not connect to web-server.\n");
if (!jreader.parse(body.c_str(), jret)) return TPE_NETWORK;
return TPE_PARAM; }
if (!jret.isObject()) if (body.length() == 0) {
return TPE_PARAM; EXLOGE("[core] get conn info from web-server failed: got nothing.\n");
if (!jret["data"].isObject()) return TPE_NETWORK;
return TPE_PARAM; }
Json::Value& _jret = jret["data"]; //Json::Reader jreader;
Json::Value jret;
if(!_jret["user_id"].isInt())
EXLOGE("connection info: need `user_id`.\n"); //if (!jreader.parse(body.c_str(), jret))
if(!_jret["host_id"].isInt()) Json::CharReaderBuilder jcrb;
EXLOGE("connection info: need `host_id`.\n"); std::unique_ptr<Json::CharReader> const jreader(jcrb.newCharReader());
if(!_jret["acc_id"].isInt()) const char *str_json_begin = body.c_str();
EXLOGE("connection info: need `acc_id`.\n"); ex_astr err;
if(!_jret["conn_port"].isInt())
EXLOGE("connection info: need `conn_port`.\n"); //if (!jreader.parse(func_args.c_str(), jsRoot)) {
if(!_jret["protocol_type"].isInt()) if (!jreader->parse(str_json_begin, str_json_begin + body.length(), &jret, &err))
EXLOGE("connection info: need `protocol_type`.\n"); return TPE_PARAM;
if(!_jret["protocol_sub_type"].isInt()) if (!jret.isObject())
EXLOGE("connection info: need `protocol_sub_type`.\n"); return TPE_PARAM;
if(!_jret["auth_type"].isInt()) if (!jret["data"].isObject())
EXLOGE("connection info: need `auth_type`.\n"); return TPE_PARAM;
if (!_jret["protocol_flag"].isUInt())
EXLOGE("connection info: need `protocol_flag`.\n"); Json::Value& _jret = jret["data"];
if (!_jret["record_flag"].isUInt())
EXLOGE("connection info: need `record_flag`.\n"); if(!_jret["user_id"].isInt())
if (!_jret["_enc"].isInt()) EXLOGE("connection info: need `user_id`.\n");
EXLOGE("connection info: need `_enc`.\n"); if(!_jret["host_id"].isInt())
if(!_jret["user_username"].isString()) EXLOGE("connection info: need `host_id`.\n");
EXLOGE("connection info: need `user_username`.\n"); if(!_jret["acc_id"].isInt())
if(!_jret["host_ip"].isString()) EXLOGE("connection info: need `acc_id`.\n");
EXLOGE("connection info: need `host_ip`.\n"); if(!_jret["conn_port"].isInt())
if(!_jret["conn_ip"].isString()) EXLOGE("connection info: need `conn_port`.\n");
EXLOGE("connection info: need `conn_ip`.\n"); if(!_jret["protocol_type"].isInt())
if(!_jret["client_ip"].isString()) EXLOGE("connection info: need `protocol_type`.\n");
EXLOGE("connection info: need `client_ip`.\n"); if(!_jret["protocol_sub_type"].isInt())
if(!_jret["acc_username"].isString()) EXLOGE("connection info: need `protocol_sub_type`.\n");
EXLOGE("connection info: need `acc_username`.\n"); if(!_jret["auth_type"].isInt())
if(!_jret["acc_secret"].isString()) EXLOGE("connection info: need `auth_type`.\n");
EXLOGE("connection info: need `acc_secret`.\n"); if (!_jret["protocol_flag"].isUInt())
if(!_jret["username_prompt"].isString()) EXLOGE("connection info: need `protocol_flag`.\n");
EXLOGE("connection info: need `username_prompt`.\n"); if (!_jret["record_flag"].isUInt())
if(!_jret["password_prompt"].isString()) EXLOGE("connection info: need `record_flag`.\n");
EXLOGE("connection info: need `password_prompt`.\n"); if (!_jret["_enc"].isInt())
EXLOGE("connection info: need `_enc`.\n");
if ( if(!_jret["user_username"].isString())
!_jret["user_id"].isInt() EXLOGE("connection info: need `user_username`.\n");
|| !_jret["host_id"].isInt() if(!_jret["host_ip"].isString())
|| !_jret["acc_id"].isInt() EXLOGE("connection info: need `host_ip`.\n");
|| !_jret["conn_port"].isInt() if(!_jret["conn_ip"].isString())
|| !_jret["protocol_type"].isInt() EXLOGE("connection info: need `conn_ip`.\n");
|| !_jret["protocol_sub_type"].isInt() if(!_jret["client_ip"].isString())
|| !_jret["auth_type"].isInt() EXLOGE("connection info: need `client_ip`.\n");
|| !_jret["protocol_flag"].isUInt() if(!_jret["acc_username"].isString())
|| !_jret["record_flag"].isUInt() EXLOGE("connection info: need `acc_username`.\n");
|| !_jret["_enc"].isInt() if(!_jret["acc_secret"].isString())
EXLOGE("connection info: need `acc_secret`.\n");
|| !_jret["user_username"].isString() if(!_jret["username_prompt"].isString())
|| !_jret["host_ip"].isString() EXLOGE("connection info: need `username_prompt`.\n");
|| !_jret["conn_ip"].isString() if(!_jret["password_prompt"].isString())
|| !_jret["client_ip"].isString() EXLOGE("connection info: need `password_prompt`.\n");
|| !_jret["acc_username"].isString()
|| !_jret["acc_secret"].isString() if (
|| !_jret["username_prompt"].isString() !_jret["user_id"].isInt()
|| !_jret["password_prompt"].isString() || !_jret["host_id"].isInt()
) || !_jret["acc_id"].isInt()
{ || !_jret["conn_port"].isInt()
EXLOGE("got connection info from web-server, but not all info valid.\n"); || !_jret["protocol_type"].isInt()
return TPE_PARAM; || !_jret["protocol_sub_type"].isInt()
} || !_jret["auth_type"].isInt()
|| !_jret["protocol_flag"].isUInt()
int user_id; || !_jret["record_flag"].isUInt()
int host_id; || !_jret["_enc"].isInt()
int acc_id;
ex_astr user_username;// 申请本次连接的用户名 || !_jret["user_username"].isString()
ex_astr host_ip;// 真正的远程主机IP如果是直接连接模式则与remote_host_ip相同 || !_jret["host_ip"].isString()
ex_astr conn_ip;// 要连接的远程主机的IP如果是端口映射模式则为路由主机的IP || !_jret["conn_ip"].isString()
int conn_port;// 要连接的远程主机的端口(如果是端口映射模式,则为路由主机的端口) || !_jret["client_ip"].isString()
ex_astr client_ip; || !_jret["acc_username"].isString()
ex_astr acc_username; // 远程主机的账号 || !_jret["acc_secret"].isString()
ex_astr acc_secret;// 远程主机账号的密码(或者私钥) || !_jret["username_prompt"].isString()
ex_astr username_prompt; || !_jret["password_prompt"].isString()
ex_astr password_prompt; )
int protocol_type = 0; {
int protocol_sub_type = 0; EXLOGE("got connection info from web-server, but not all info valid.\n");
int auth_type = 0; return TPE_PARAM;
int protocol_flag = 0; }
int record_flag = 0;
bool _enc; int user_id;
int host_id;
user_id = _jret["user_id"].asInt(); int acc_id;
host_id = _jret["host_id"].asInt(); ex_astr user_username;// 申请本次连接的用户名
acc_id = _jret["acc_id"].asInt(); ex_astr host_ip;// 真正的远程主机IP如果是直接连接模式则与remote_host_ip相同
user_username = _jret["user_username"].asString(); ex_astr conn_ip;// 要连接的远程主机的IP如果是端口映射模式则为路由主机的IP
host_ip = _jret["host_ip"].asString(); int conn_port;// 要连接的远程主机的端口(如果是端口映射模式,则为路由主机的端口)
conn_ip = _jret["conn_ip"].asString(); ex_astr client_ip;
conn_port = _jret["conn_port"].asInt(); ex_astr acc_username; // 远程主机的账号
client_ip = _jret["client_ip"].asString(); ex_astr acc_secret;// 远程主机账号的密码(或者私钥)
acc_username = _jret["acc_username"].asString(); ex_astr username_prompt;
acc_secret = _jret["acc_secret"].asString(); ex_astr password_prompt;
username_prompt = _jret["username_prompt"].asString(); int protocol_type = 0;
password_prompt = _jret["password_prompt"].asString(); int protocol_sub_type = 0;
protocol_type = _jret["protocol_type"].asInt(); int auth_type = 0;
protocol_sub_type = _jret["protocol_sub_type"].asInt(); int protocol_flag = 0;
protocol_flag = _jret["protocol_flag"].asUInt(); int record_flag = 0;
record_flag = _jret["record_flag"].asUInt(); bool _enc;
auth_type = _jret["auth_type"].asInt();
_enc = _jret["_enc"].asBool(); user_id = _jret["user_id"].asInt();
host_id = _jret["host_id"].asInt();
acc_id = _jret["acc_id"].asInt();
// 进一步判断参数是否合法 user_username = _jret["user_username"].asString();
// 注意account_id可以为-1表示这是一次测试连接。 host_ip = _jret["host_ip"].asString();
if (user_id <= 0 || host_id <= 0 conn_ip = _jret["conn_ip"].asString();
|| user_username.length() == 0 conn_port = _jret["conn_port"].asInt();
|| host_ip.length() == 0 || conn_ip.length() == 0 || client_ip.length() == 0 client_ip = _jret["client_ip"].asString();
|| conn_port <= 0 || conn_port >= 65535 acc_username = _jret["acc_username"].asString();
|| acc_username.length() == 0 || acc_secret.length() == 0 acc_secret = _jret["acc_secret"].asString();
|| !(protocol_type == TP_PROTOCOL_TYPE_RDP || protocol_type == TP_PROTOCOL_TYPE_SSH || protocol_type == TP_PROTOCOL_TYPE_TELNET) username_prompt = _jret["username_prompt"].asString();
|| !(auth_type == TP_AUTH_TYPE_NONE || auth_type == TP_AUTH_TYPE_PASSWORD || auth_type == TP_AUTH_TYPE_PRIVATE_KEY) password_prompt = _jret["password_prompt"].asString();
) protocol_type = _jret["protocol_type"].asInt();
{ protocol_sub_type = _jret["protocol_sub_type"].asInt();
return TPE_PARAM; protocol_flag = _jret["protocol_flag"].asUInt();
} record_flag = _jret["record_flag"].asUInt();
auth_type = _jret["auth_type"].asInt();
if (_enc) { _enc = _jret["_enc"].asBool();
ex_astr _auth;
if (!ts_db_field_decrypt(acc_secret, _auth))
return TPE_FAILED; // 进一步判断参数是否合法
// 注意account_id可以为-1表示这是一次测试连接。
acc_secret = _auth; if (user_id <= 0 || host_id <= 0
} || user_username.length() == 0
|| host_ip.length() == 0 || conn_ip.length() == 0 || client_ip.length() == 0
info.user_id = user_id; || conn_port <= 0 || conn_port >= 65535
info.host_id = host_id; || acc_username.length() == 0 || acc_secret.length() == 0
info.acc_id = acc_id; || !(protocol_type == TP_PROTOCOL_TYPE_RDP || protocol_type == TP_PROTOCOL_TYPE_SSH || protocol_type == TP_PROTOCOL_TYPE_TELNET)
info.user_username = user_username; || !(auth_type == TP_AUTH_TYPE_NONE || auth_type == TP_AUTH_TYPE_PASSWORD || auth_type == TP_AUTH_TYPE_PRIVATE_KEY)
info.host_ip = host_ip; )
info.conn_ip = conn_ip; {
info.conn_port = conn_port; return TPE_PARAM;
info.client_ip = client_ip; }
info.acc_username = acc_username;
info.acc_secret = acc_secret; if (_enc) {
info.username_prompt = username_prompt; ex_astr _auth;
info.password_prompt = password_prompt; if (!ts_db_field_decrypt(acc_secret, _auth))
info.protocol_type = protocol_type; return TPE_FAILED;
info.protocol_sub_type = protocol_sub_type;
info.auth_type = auth_type; acc_secret = _auth;
info.protocol_flag = protocol_flag; }
info.record_flag = record_flag;
info.user_id = user_id;
return TPE_OK; info.host_id = host_id;
} info.acc_id = acc_id;
info.user_username = user_username;
bool ts_web_rpc_session_begin(TS_CONNECT_INFO& info, int& record_id) info.host_ip = host_ip;
{ info.conn_ip = conn_ip;
Json::FastWriter json_writer; info.conn_port = conn_port;
Json::Value jreq; info.client_ip = client_ip;
info.acc_username = acc_username;
jreq["method"] = "session_begin"; info.acc_secret = acc_secret;
jreq["param"]["sid"] = info.sid.c_str(); info.username_prompt = username_prompt;
jreq["param"]["user_id"] = info.user_id; info.password_prompt = password_prompt;
jreq["param"]["host_id"] = info.host_id; info.protocol_type = protocol_type;
jreq["param"]["acc_id"] = info.acc_id; info.protocol_sub_type = protocol_sub_type;
jreq["param"]["user_username"] = info.user_username.c_str(); info.auth_type = auth_type;
jreq["param"]["acc_username"] = info.acc_username.c_str(); info.protocol_flag = protocol_flag;
jreq["param"]["host_ip"] = info.host_ip.c_str(); info.record_flag = record_flag;
jreq["param"]["conn_ip"] = info.conn_ip.c_str();
jreq["param"]["client_ip"] = info.client_ip.c_str(); return TPE_OK;
//jreq["param"]["sys_type"] = info.sys_type; }
jreq["param"]["conn_port"] = info.conn_port;
jreq["param"]["auth_type"] = info.auth_type; bool ts_web_rpc_session_begin(TS_CONNECT_INFO& info, int& record_id)
jreq["param"]["protocol_type"] = info.protocol_type; {
jreq["param"]["protocol_sub_type"] = info.protocol_sub_type; //Json::FastWriter json_writer;
Json::Value jreq;
ex_astr json_param;
json_param = json_writer.write(jreq); jreq["method"] = "session_begin";
jreq["param"]["sid"] = info.sid.c_str();
ex_astr param; jreq["param"]["user_id"] = info.user_id;
ts_url_encode(json_param.c_str(), param); jreq["param"]["host_id"] = info.host_id;
jreq["param"]["acc_id"] = info.acc_id;
ex_astr url = g_env.web_server_rpc; jreq["param"]["user_username"] = info.user_username.c_str();
url += "?"; jreq["param"]["acc_username"] = info.acc_username.c_str();
url += param; jreq["param"]["host_ip"] = info.host_ip.c_str();
jreq["param"]["conn_ip"] = info.conn_ip.c_str();
ex_astr body; jreq["param"]["client_ip"] = info.client_ip.c_str();
if (!ts_http_get(url, body)) //jreq["param"]["sys_type"] = info.sys_type;
{ jreq["param"]["conn_port"] = info.conn_port;
// EXLOGV("request `rpc::session_begin` from web return: "); jreq["param"]["auth_type"] = info.auth_type;
// EXLOGV(body.c_str()); jreq["param"]["protocol_type"] = info.protocol_type;
// EXLOGV("\n"); jreq["param"]["protocol_sub_type"] = info.protocol_sub_type;
return false;
} ex_astr json_param;
//json_param = json_writer.write(jreq);
Json::Reader jreader; Json::StreamWriterBuilder jwb;
Json::Value jret; std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
if (!jreader.parse(body.c_str(), jret)) jwriter->write(jreq, &os);
return false; json_param = os.str();
if (!jret.isObject())
return false; ex_astr param;
if (!jret["data"].isObject()) ts_url_encode(json_param.c_str(), param);
return false;
if (!jret["data"]["rid"].isUInt()) ex_astr url = g_env.web_server_rpc;
return false; url += "?";
url += param;
record_id = jret["data"]["rid"].asUInt();
ex_astr body;
return true; if (!ts_http_get(url, body))
} {
// EXLOGV("request `rpc::session_begin` from web return: ");
bool ts_web_rpc_session_update(int record_id, int protocol_sub_type, int state) { // EXLOGV(body.c_str());
Json::FastWriter json_writer; // EXLOGV("\n");
Json::Value jreq; return false;
jreq["method"] = "session_update"; }
jreq["param"]["rid"] = record_id;
jreq["param"]["protocol_sub_type"] = protocol_sub_type; //Json::Reader jreader;
jreq["param"]["code"] = state; Json::Value jret;
ex_astr json_param; //if (!jreader.parse(body.c_str(), jret))
json_param = json_writer.write(jreq); Json::CharReaderBuilder jcrb;
std::unique_ptr<Json::CharReader> const jreader(jcrb.newCharReader());
ex_astr param; const char *str_json_begin = body.c_str();
ts_url_encode(json_param.c_str(), param); ex_astr err;
ex_astr url = g_env.web_server_rpc; //if (!jreader.parse(func_args.c_str(), jsRoot)) {
url += "?"; if (!jreader->parse(str_json_begin, str_json_begin + body.length(), &jret, &err))
url += param; return false;
if (!jret.isObject())
ex_astr body; return false;
return ts_http_get(url, body); if (!jret["data"].isObject())
} return false;
if (!jret["data"]["rid"].isUInt())
return false;
//session 结束
bool ts_web_rpc_session_end(const char* sid, int record_id, int ret_code) record_id = jret["data"]["rid"].asUInt();
{
// TODO: 对指定的sid相关的会话的引用计数减一但减到0时销毁 return true;
}
Json::FastWriter json_writer;
Json::Value jreq; bool ts_web_rpc_session_update(int record_id, int protocol_sub_type, int state) {
jreq["method"] = "session_end"; //Json::FastWriter json_writer;
jreq["param"]["rid"] = record_id; Json::Value jreq;
jreq["param"]["code"] = ret_code; jreq["method"] = "session_update";
jreq["param"]["rid"] = record_id;
ex_astr json_param; jreq["param"]["protocol_sub_type"] = protocol_sub_type;
json_param = json_writer.write(jreq); jreq["param"]["code"] = state;
ex_astr param; ex_astr json_param;
ts_url_encode(json_param.c_str(), param); //json_param = json_writer.write(jreq);
Json::StreamWriterBuilder jwb;
ex_astr url = g_env.web_server_rpc; std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
url += "?"; ex_aoss os;
url += param; jwriter->write(jreq, &os);
json_param = os.str();
ex_astr body;
return ts_http_get(url, body); ex_astr param;
} ts_url_encode(json_param.c_str(), param);
ex_astr url = g_env.web_server_rpc;
url += "?";
url += param;
ex_astr body;
return ts_http_get(url, body);
}
//session 结束
bool ts_web_rpc_session_end(const char* sid, int record_id, int ret_code)
{
// TODO: 对指定的sid相关的会话的引用计数减一但减到0时销毁
//Json::FastWriter json_writer;
Json::Value jreq;
jreq["method"] = "session_end";
jreq["param"]["rid"] = record_id;
jreq["param"]["code"] = ret_code;
ex_astr json_param;
//json_param = json_writer.write(jreq);
Json::StreamWriterBuilder jwb;
std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
jwriter->write(jreq, &os);
json_param = os.str();
ex_astr param;
ts_url_encode(json_param.c_str(), param);
ex_astr url = g_env.web_server_rpc;
url += "?";
url += param;
ex_astr body;
return ts_http_get(url, body);
}