pull/130/head
Apex Liu 2018-11-07 03:34:30 +08:00
parent a358b79095
commit c78ead3457
6 changed files with 773 additions and 904 deletions

View File

@ -18,7 +18,7 @@
extern HINSTANCE g_hInstance;
HWND g_hDlgMain = NULL;
HWND g_hDlgMain = nullptr;
static DWORD g_dwTaskbarRecreateMessage = 0;
static BOOL g_IsTrayIconShowed = FALSE;
@ -90,19 +90,13 @@ INT_PTR CALLBACK eomDlgMainProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARA
case IDM_OPEN_WEB:
{
ShellExecute(NULL, _T("open"), TS_WEB_URL, NULL, NULL, SW_SHOW);
return TRUE;
}break;
case IDM_OPEN_BBS:
{
ShellExecute(NULL, _T("open"), TS_BBS_URL, NULL, NULL, SW_SHOW);
ShellExecute(nullptr, _T("open"), TS_WEB_URL, nullptr, nullptr, SW_SHOW);
return TRUE;
}break;
case IDM_OPEN_CONFIG:
{
ShellExecute(NULL, _T("open"), _T("http://127.0.0.1:50022/config"), NULL, NULL, SW_SHOW);
ShellExecute(nullptr, _T("open"), _T("http://127.0.0.1:50022/config"), nullptr, nullptr, SW_SHOW);
return TRUE;
}break;
@ -129,7 +123,7 @@ INT_PTR CALLBACK eomDlgMainProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARA
HMENU hPopup = GetSubMenu(hMenu, 0);
SetMenuDefaultItem(hPopup, IDM_ABOUT, FALSE);
TrackPopupMenu(hPopup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwndDlg, NULL);
TrackPopupMenu(hPopup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwndDlg, nullptr);
DestroyMenu(hMenu);
}
@ -216,7 +210,7 @@ void center_window(HWND hwndDlg)
rc.top = (cyScreen - (rc.bottom - rc.top)) / 3;
}
SetWindowPos(hwndDlg, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE);
SetWindowPos(hwndDlg, nullptr, rc.left, rc.top, 0, 0, SWP_NOSIZE);
return;
}

Binary file not shown.

Binary file not shown.

View File

@ -1,8 +1,7 @@
#ifndef __TS_CONST_H__
#define __TS_CONST_H__
#define TS_WEB_URL L"http://teleport.eomsoft.net/"
#define TS_BBS_URL L"http://bbs.eomsoft.net/"
#define TS_WEB_URL L"https://tp4a.com/"
#define TS_TRAY_MSG L"Teleport助手正常工作中"
#define TS_HTTP_RPC_PORT 50022

View File

@ -119,10 +119,8 @@ password 51:b:%s\n\
TsHttpRpc g_http_interface;
void http_rpc_main_loop(void)
{
if (!g_http_interface.init(TS_HTTP_RPC_HOST, TS_HTTP_RPC_PORT))
{
void http_rpc_main_loop(void) {
if (!g_http_interface.init(TS_HTTP_RPC_HOST, TS_HTTP_RPC_PORT)) {
EXLOGE("[ERROR] can not start HTTP-RPC listener, maybe port %d is already in use.\n", TS_HTTP_RPC_PORT);
return;
}
@ -135,39 +133,29 @@ void http_rpc_main_loop(void)
EXLOGW("[rpc] main loop end.\n");
}
void http_rpc_stop(void)
{
void http_rpc_stop(void) {
g_http_interface.stop();
}
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
int ts_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded)
{
int ts_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded) {
int i, j, a, b;
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++)
{
if (src[i] == '%')
{
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
if (src[i] == '%') {
if (i < src_len - 2 && isxdigit(*(const unsigned char *)(src + i + 1)) &&
isxdigit(*(const unsigned char *)(src + i + 2))) {
a = tolower(*(const unsigned char *)(src + i + 1));
b = tolower(*(const unsigned char *)(src + i + 2));
dst[j] = (char)((HEXTOI(a) << 4) | HEXTOI(b));
i += 2;
}
else
{
} else {
return -1;
}
}
else if (is_form_url_encoded && src[i] == '+')
{
} else if (is_form_url_encoded && src[i] == '+') {
dst[j] = ' ';
}
else
{
} else {
dst[j] = src[i];
}
}
@ -177,8 +165,7 @@ int ts_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_f
return i >= src_len ? j : -1;
}
bool calc_psw51b(const char* password, std::string& ret)
{
bool calc_psw51b(const char* password, std::string& ret) {
DATA_BLOB DataIn;
DATA_BLOB DataOut;
@ -189,12 +176,11 @@ bool calc_psw51b(const char* password, std::string& ret)
DataIn.pbData = (BYTE*)w_pswd.c_str();
if (!CryptProtectData(&DataIn, L"psw", NULL, NULL, NULL, 0, &DataOut))
if (!CryptProtectData(&DataIn, L"psw", nullptr, nullptr, nullptr, 0, &DataOut))
return false;
char szRet[5] = {0};
for (int i = 0; i < DataOut.cbData; ++i)
{
char szRet[5] = { 0 };
for (DWORD i = 0; i < DataOut.cbData; ++i) {
sprintf_s(szRet, 5, "%02X", DataOut.pbData[i]);
ret += szRet;
}
@ -203,36 +189,30 @@ bool calc_psw51b(const char* password, std::string& ret)
return true;
}
TsHttpRpc::TsHttpRpc()
{
TsHttpRpc::TsHttpRpc() {
m_stop = false;
mg_mgr_init(&m_mg_mgr, NULL);
mg_mgr_init(&m_mg_mgr, nullptr);
}
TsHttpRpc::~TsHttpRpc()
{
TsHttpRpc::~TsHttpRpc() {
mg_mgr_free(&m_mg_mgr);
}
bool TsHttpRpc::init(const char* ip, int port)
{
bool TsHttpRpc::init(const char* ip, int port) {
char file_name[MAX_PATH] = { 0 };
if (!GetModuleFileNameA(NULL, file_name, MAX_PATH))
if (!GetModuleFileNameA(nullptr, file_name, MAX_PATH))
return false;
int len = strlen(file_name);
if (file_name[len] == '\\')
{
file_name[len] = '\0';
}
char* match = strrchr(file_name, '\\');
if (match != NULL)
{
*match = '\0';
}
struct mg_connection* nc = NULL;
char* match = strrchr(file_name, '\\');
if (match)
*match = '\0';
struct mg_connection* nc = nullptr;
char addr[128] = { 0 };
if (0 == strcmp(ip, "127.0.0.1") || 0 == strcmp(ip, "localhost"))
@ -241,8 +221,7 @@ bool TsHttpRpc::init(const char* ip, int port)
ex_strformat(addr, 128, "tcp://%s:%d", ip, port);
nc = mg_bind(&m_mg_mgr, addr, _mg_event_handler);
if (nc == NULL)
{
if (!nc) {
EXLOGE("[rpc] TsHttpRpc::init %s:%d\n", ip, port);
return false;
}
@ -266,32 +245,26 @@ bool TsHttpRpc::init(const char* ip, int port)
return true;
}
void TsHttpRpc::run(void)
{
while(!m_stop)
{
void TsHttpRpc::run(void) {
while (!m_stop) {
mg_mgr_poll(&m_mg_mgr, 500);
}
}
void TsHttpRpc::stop(void)
{
void TsHttpRpc::stop(void) {
m_stop = true;
}
void TsHttpRpc::_mg_event_handler(struct mg_connection *nc, int ev, void *ev_data)
{
void TsHttpRpc::_mg_event_handler(struct mg_connection *nc, int ev, void *ev_data) {
struct http_message *hm = (struct http_message*)ev_data;
TsHttpRpc* _this = (TsHttpRpc*)nc->user_data;
if (NULL == _this)
{
if (!_this) {
EXLOGE("[ERROR] invalid http request.\n");
return;
}
switch (ev)
{
switch (ev) {
case MG_EV_HTTP_REQUEST:
{
ex_astr uri;
@ -302,7 +275,7 @@ void TsHttpRpc::_mg_event_handler(struct mg_connection *nc, int ev, void *ev_dat
uri = &_uri[0];
#ifdef EX_DEBUG
char* dbg_method = NULL;
char* dbg_method = nullptr;
if (hm->method.len == 3 && 0 == memcmp(hm->method.p, "GET", hm->method.len))
dbg_method = "GET";
else if (hm->method.len == 4 && 0 == memcmp(hm->method.p, "POST", hm->method.len))
@ -315,8 +288,7 @@ void TsHttpRpc::_mg_event_handler(struct mg_connection *nc, int ev, void *ev_dat
ex_astr ret_buf;
bool b_is_index = false;
if (uri == "/")
{
if (uri == "/") {
ex_wstr page = L"<html lang=\"zh_CN\"><head><meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title>Teleport助手</title>\n<style type=\"text/css\">\n.box{padding:20px;margin:40px;border:1px solid #78b17c;background-color:#e4ffe5;}\n</style>\n</head><body><div class=\"box\">Teleport助手工作正常</div></body></html>";
ex_wstr2astr(page, ret_buf, EX_CODEPAGE_UTF8);
@ -325,29 +297,24 @@ void TsHttpRpc::_mg_event_handler(struct mg_connection *nc, int ev, void *ev_dat
return;
}
if (uri == "/config")
{
if (uri == "/config") {
uri = "/index.html";
b_is_index = true;
}
ex_astr temp;
int offset = uri.find("/", 1);
if (offset > 0)
{
temp = uri.substr(1, offset-1);
if (offset > 0) {
temp = uri.substr(1, offset - 1);
if(temp == "api") {
if (temp == "api") {
ex_astr method;
ex_astr json_param;
int rv = _this->_parse_request(hm, method, json_param);
if (0 != rv)
{
if (0 != rv) {
EXLOGE("[ERROR] http-rpc got invalid request.\n");
_this->_create_json_ret(ret_buf, rv);
}
else
{
} else {
_this->_process_js_request(method, json_param, ret_buf);
}
@ -360,8 +327,7 @@ void TsHttpRpc::_mg_event_handler(struct mg_connection *nc, int ev, void *ev_dat
ex_astr file_suffix;
offset = uri.rfind(".");
if (offset > 0)
{
if (offset > 0) {
file_suffix = uri.substr(offset, uri.length());
}
@ -370,10 +336,9 @@ void TsHttpRpc::_mg_event_handler(struct mg_connection *nc, int ev, void *ev_dat
FILE* file = ex_fopen(index_path.c_str(), "rb");
if (file)
{
if (file) {
unsigned long file_size = 0;
char* buf = 0;
char* buf = nullptr;
size_t ret = 0;
fseek(file, 0, SEEK_END);
@ -388,12 +353,10 @@ void TsHttpRpc::_mg_event_handler(struct mg_connection *nc, int ev, void *ev_dat
mg_printf(nc, "HTTP/1.0 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-Length: %ld\r\nContent-Type: %s\r\n\r\n", file_size, content_type.c_str());
mg_send(nc, buf, (int)file_size);
delete []buf;
delete[]buf;
nc->flags |= MG_F_SEND_AND_CLOSE;
return;
}
else if (b_is_index)
{
} else if (b_is_index) {
ex_wstr page = L"<html lang=\"zh_CN\"><html><head><title>404 Not Found</title></head><body bgcolor=\"white\"><center><h1>404 Not Found</h1></center><hr><center><p>Teleport Assistor configuration page not found.</p></center></body></html>";
ex_wstr2astr(page, ret_buf, EX_CODEPAGE_UTF8);
@ -409,9 +372,8 @@ void TsHttpRpc::_mg_event_handler(struct mg_connection *nc, int ev, void *ev_dat
}
}
int TsHttpRpc::_parse_request(struct http_message* req, ex_astr& func_cmd, ex_astr& func_args)
{
if (NULL == req)
int TsHttpRpc::_parse_request(struct http_message* req, ex_astr& func_cmd, ex_astr& func_args) {
if (!req)
return TPE_FAILED;
bool is_get = true;
@ -427,12 +389,9 @@ int TsHttpRpc::_parse_request(struct http_message* req, ex_astr& func_cmd, ex_as
size_t pos_start = 1; // 跳过第一个字节,一定是 '/'
size_t i = 0;
for (i = pos_start; i < req->uri.len; ++i)
{
if (req->uri.p[i] == '/')
{
if (i - pos_start > 0)
{
for (i = pos_start; i < req->uri.len; ++i) {
if (req->uri.p[i] == '/') {
if (i - pos_start > 0) {
ex_astr tmp_uri;
tmp_uri.assign(req->uri.p + pos_start, i - pos_start);
strs.push_back(tmp_uri);
@ -440,51 +399,37 @@ int TsHttpRpc::_parse_request(struct http_message* req, ex_astr& func_cmd, ex_as
pos_start = i + 1; // 跳过当前找到的分隔符
}
}
if (pos_start < req->uri.len)
{
if (pos_start < req->uri.len) {
ex_astr tmp_uri;
tmp_uri.assign(req->uri.p + pos_start, req->uri.len - pos_start);
strs.push_back(tmp_uri);
}
if (0 == strs.size() || strs[0] != "api")
if (strs.empty() || strs[0] != "api")
return TPE_PARAM;
if (is_get)
{
if (2 == strs.size())
{
if (is_get) {
if (2 == strs.size()) {
func_cmd = strs[1];
}
else if (3 == strs.size())
{
} else if (3 == strs.size()) {
func_cmd = strs[1];
func_args = strs[2];
}
else
{
} else {
return TPE_PARAM;
}
}
else
{
if (2 == strs.size())
{
} else {
if (2 == strs.size()) {
func_cmd = strs[1];
}
else
{
} else {
return TPE_PARAM;
}
if (req->body.len > 0)
{
if (req->body.len > 0) {
func_args.assign(req->body.p, req->body.len);
}
}
if (func_args.length() > 0)
{
if (func_args.length() > 0) {
// 将参数进行 url-decode 解码
int len = func_args.length() * 2;
ex_chars sztmp;
@ -501,41 +446,26 @@ int TsHttpRpc::_parse_request(struct http_message* req, ex_astr& func_cmd, ex_as
return TPE_OK;
}
void TsHttpRpc::_process_js_request(const ex_astr& func_cmd, const ex_astr& func_args, ex_astr& buf)
{
if (func_cmd == "get_version")
{
void TsHttpRpc::_process_js_request(const ex_astr& func_cmd, const ex_astr& func_args, ex_astr& buf) {
if (func_cmd == "get_version") {
_rpc_func_get_version(func_args, buf);
}
else if (func_cmd == "run")
{
} else if (func_cmd == "run") {
_rpc_func_run_client(func_args, buf);
}
else if (func_cmd == "rdp_play")
{
} else if (func_cmd == "rdp_play") {
_rpc_func_rdp_play(func_args, buf);
}
else if (func_cmd == "get_config")
{
} else if (func_cmd == "get_config") {
_rpc_func_get_config(func_args, buf);
}
else if (func_cmd == "set_config")
{
} else if (func_cmd == "set_config") {
_rpc_func_set_config(func_args, buf);
}
else if (func_cmd == "file_action")
{
} else if (func_cmd == "file_action") {
_rpc_func_file_action(func_args, buf);
}
else
{
} else {
EXLOGE("[rpc] got unknown command: %s\n", func_cmd.c_str());
_create_json_ret(buf, TPE_UNKNOWN_CMD);
}
}
void TsHttpRpc::_create_json_ret(ex_astr& buf, int errcode)
{
void TsHttpRpc::_create_json_ret(ex_astr& buf, int errcode) {
// 返回: {"code":123}
Json::FastWriter jr_writer;
@ -545,14 +475,12 @@ void TsHttpRpc::_create_json_ret(ex_astr& buf, int errcode)
buf = jr_writer.write(jr_root);
}
void TsHttpRpc::_create_json_ret(ex_astr& buf, Json::Value& jr_root)
{
void TsHttpRpc::_create_json_ret(ex_astr& buf, Json::Value& jr_root) {
Json::FastWriter jr_writer;
buf = jr_writer.write(jr_root);
}
void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
{
void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf) {
// 入参:{"ip":"192.168.5.11","port":22,"uname":"root","uauth":"abcdefg","authmode":1,"protocol":2}
// authmode: 1=password, 2=private-key
// protocol: 1=rdp, 2=ssh
@ -562,13 +490,11 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
Json::Reader jreader;
Json::Value jsRoot;
if (!jreader.parse(func_args.c_str(), jsRoot))
{
if (!jreader.parse(func_args.c_str(), jsRoot)) {
_create_json_ret(buf, TPE_JSON_FORMAT);
return;
}
if (!jsRoot.isObject())
{
if (!jsRoot.isObject()) {
_create_json_ret(buf, TPE_PARAM);
return;
}
@ -578,8 +504,7 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
|| !jsRoot["teleport_port"].isNumeric() || !jsRoot["remote_host_ip"].isString()
|| !jsRoot["session_id"].isString() || !jsRoot["protocol_type"].isNumeric() || !jsRoot["protocol_sub_type"].isNumeric()
|| !jsRoot["protocol_flag"].isNumeric()
)
{
) {
_create_json_ret(buf, TPE_PARAM);
return;
}
@ -609,15 +534,14 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
ex_wstr tmp_rdp_file; // for .rdp file
if (pro_type == TP_PROTOCOL_TYPE_RDP)
{
if (pro_type == TP_PROTOCOL_TYPE_RDP) {
//==============================================
// RDP
//==============================================
bool flag_clipboard = (protocol_flag & TP_FLAG_RDP_CLIPBOARD);
bool flag_disk = (protocol_flag & TP_FLAG_RDP_DISK);
bool flag_console = (protocol_flag & TP_FLAG_RDP_CONSOLE);
bool flag_clipboard = ((protocol_flag & TP_FLAG_RDP_CLIPBOARD) == TP_FLAG_RDP_CLIPBOARD);
bool flag_disk = ((protocol_flag & TP_FLAG_RDP_DISK) == TP_FLAG_RDP_DISK);
bool flag_console = ((protocol_flag & TP_FLAG_RDP_CONSOLE) == TP_FLAG_RDP_CONSOLE);
int rdp_w = 800;
int rdp_h = 640;
@ -626,8 +550,7 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
if (!jsRoot["rdp_width"].isNull()) {
if (jsRoot["rdp_width"].isNumeric()) {
rdp_w = jsRoot["rdp_width"].asUInt();
}
else {
} else {
_create_json_ret(buf, TPE_PARAM);
return;
}
@ -636,8 +559,7 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
if (!jsRoot["rdp_height"].isNull()) {
if (jsRoot["rdp_height"].isNumeric()) {
rdp_h = jsRoot["rdp_height"].asUInt();
}
else {
} else {
_create_json_ret(buf, TPE_PARAM);
return;
}
@ -646,8 +568,7 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
if (!jsRoot["rdp_console"].isNull()) {
if (jsRoot["rdp_console"].isBool()) {
rdp_console = jsRoot["rdp_console"].asBool();
}
else {
} else {
_create_json_ret(buf, TPE_PARAM);
return;
}
@ -660,12 +581,11 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
int split_pos = sid.length() - 2;
ex_astr real_sid = sid.substr(0, split_pos);
ex_astr str_pwd_len = sid.substr(split_pos, sid.length());
int n_pwd_len = strtol(str_pwd_len.c_str(), NULL, 16);
int n_pwd_len = strtol(str_pwd_len.c_str(), nullptr, 16);
n_pwd_len -= real_sid.length();
n_pwd_len -= 2;
char szPwd[256] = { 0 };
for (int i = 0; i < n_pwd_len; i++)
{
for (int i = 0; i < n_pwd_len; i++) {
szPwd[i] = '*';
}
@ -692,8 +612,7 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
width = iWidth;
higth = iHeight;
display = 2;
}
else {
} else {
width = rdp_w;
higth = rdp_h;
display = 1;
@ -701,22 +620,19 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
cx = (iWidth - width) / 2;
cy = (iHeight - higth) / 2;
if (cx < 0)
{
if (cx < 0) {
cx = 0;
}
if (cy < 0)
{
if (cy < 0) {
cy = 0;
}
// int console_mode = 0;
// if (rdp_console)
// console_mode = 1;
// int console_mode = 0;
// if (rdp_console)
// console_mode = 1;
std::string psw51b;
if (!calc_psw51b(szPwd, psw51b))
{
if (!calc_psw51b(szPwd, psw51b)) {
EXLOGE("calc password failed.\n");
_create_json_ret(buf, TPE_FAILED);
return;
@ -739,8 +655,7 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
char sz_file_name[MAX_PATH] = { 0 };
char temp_path[MAX_PATH] = { 0 };
DWORD ret = GetTempPathA(MAX_PATH, temp_path);
if (ret <= 0)
{
if (ret <= 0) {
EXLOGE("fopen failed (%d).\n", GetLastError());
_create_json_ret(buf, TPE_FAILED);
return;
@ -752,8 +667,7 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
sprintf_s(sz_file_name, ("%s%s.rdp"), temp_path, temp_host_ip.c_str());
FILE* f = NULL;
if (fopen_s(&f, sz_file_name, "wt") != 0)
{
if (fopen_s(&f, sz_file_name, "wt") != 0) {
EXLOGE("fopen failed (%d).\n", GetLastError());
_create_json_ret(buf, TPE_OPENFILE);
return;
@ -765,8 +679,7 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
// 变量替换
ex_replace_all(w_exe_path, _T("{tmp_rdp_file}"), tmp_rdp_file);
}
else if (g_cfg.rdp_name == L"freerdp") {
} else if (g_cfg.rdp_name == L"freerdp") {
w_exe_path += L"{size} {console} {clipboard} {drives} ";
w_exe_path += g_cfg.rdp_cmdline;
@ -775,23 +688,22 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
if (rdp_w == 0 || rdp_h == 0) {
//全屏
w_screen = _T("/f");
}
else {
char sz_size[64] = {0};
} else {
char sz_size[64] = { 0 };
ex_strformat(sz_size, 63, "/size:%dx%d", rdp_w, rdp_h);
ex_astr2wstr(sz_size, w_screen);
}
// wchar_t* w_console = NULL;
//
// if (flag_console && rdp_console)
// {
// w_console = L"/admin";
// }
// else
// {
// w_console = L"";
// }
// wchar_t* w_console = NULL;
//
// if (flag_console && rdp_console)
// {
// w_console = L"/admin";
// }
// else
// {
// w_console = L"";
// }
ex_wstr w_password;
ex_astr2wstr(szPwd, w_password);
@ -813,42 +725,34 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
//ex_replace_all(w_exe_path, _T("{clipboard}"), L"+clipboard");
if(flag_clipboard)
if (flag_clipboard)
ex_replace_all(w_exe_path, _T("{clipboard}"), L"/clipboard");
else
ex_replace_all(w_exe_path, _T("{clipboard}"), L"-clipboard");
if(flag_disk)
if (flag_disk)
ex_replace_all(w_exe_path, _T("{drives}"), L"/drives");
else
ex_replace_all(w_exe_path, _T("{drives}"), L"-drives");
}
else {
} else {
_create_json_ret(buf, TPE_FAILED);
return;
}
}
else if (pro_type == TP_PROTOCOL_TYPE_SSH)
{
} else if (pro_type == TP_PROTOCOL_TYPE_SSH) {
//==============================================
// SSH
//==============================================
if (pro_sub == TP_PROTOCOL_TYPE_SSH_SHELL)
{
if (pro_sub == TP_PROTOCOL_TYPE_SSH_SHELL) {
w_exe_path = _T("\"");
w_exe_path += g_cfg.ssh_app + _T("\" ");
w_exe_path += g_cfg.ssh_cmdline;
}
else
{
} else {
w_exe_path = _T("\"");
w_exe_path += g_cfg.scp_app + _T("\" ");
w_exe_path += g_cfg.scp_cmdline;
}
}
else if (pro_type == TP_PROTOCOL_TYPE_TELNET)
{
} else if (pro_type == TP_PROTOCOL_TYPE_TELNET) {
//==============================================
// TELNET
//==============================================
@ -876,8 +780,7 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
ex_wstr2astr(w_exe_path, utf8_path, EX_CODEPAGE_UTF8);
root_ret["path"] = utf8_path;
if (!CreateProcess(NULL, (wchar_t *)w_exe_path.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
if (!CreateProcess(NULL, (wchar_t *)w_exe_path.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
EXLOGE(_T("CreateProcess() failed. Error=0x%08X.\n %s\n"), GetLastError(), w_exe_path.c_str());
root_ret["code"] = TPE_START_CLIENT;
_create_json_ret(buf, root_ret);
@ -888,13 +791,11 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
_create_json_ret(buf, root_ret);
}
void TsHttpRpc::_rpc_func_rdp_play(const ex_astr& func_args, ex_astr& buf)
{
void TsHttpRpc::_rpc_func_rdp_play(const ex_astr& func_args, ex_astr& buf) {
Json::Reader jreader;
Json::Value jsRoot;
if (!jreader.parse(func_args.c_str(), jsRoot))
{
if (!jreader.parse(func_args.c_str(), jsRoot)) {
_create_json_ret(buf, TPE_JSON_FORMAT);
return;
}
@ -907,8 +808,7 @@ void TsHttpRpc::_rpc_func_rdp_play(const ex_astr& func_args, ex_astr& buf)
|| !jsRoot["acc"].isString()
|| !jsRoot["host"].isString()
|| !jsRoot["start"].isString()
)
{
) {
_create_json_ret(buf, TPE_PARAM);
return;
}
@ -924,7 +824,7 @@ void TsHttpRpc::_rpc_func_rdp_play(const ex_astr& func_args, ex_astr& buf)
char cmd_args[1024] = { 0 };
ex_strformat(cmd_args, 1023, "%d \"%s\" \"%09d-%s-%s-%s-%s\"", rid, a_sid.c_str(), rid, a_user.c_str(), a_acc.c_str(), a_host.c_str(), a_start.c_str());
// TODO: 理论上不应该由助手来提前做域名转为IP这样的操作而是应该讲域名发送给播放器,由播放器自己去处理
// TODO: 理论上不应该由助手来提前做域名转为IP这样的操作而是应该将域名发送给播放器,由播放器自己去处理
// 但是在改造FreeRDP制作的播放器时为了从服务器上下载文件使用了Mongoose库如果传入的是域名会出现问题貌似是异步查询DNS的问题
// 所以暂时先由助手进行域名IP转换。
{
@ -956,8 +856,7 @@ void TsHttpRpc::_rpc_func_rdp_play(const ex_astr& func_args, ex_astr& buf)
int i = 0;
char* _ip = NULL;
if (tp_host->h_addrtype == AF_INET)
{
if (tp_host->h_addrtype == AF_INET) {
struct in_addr addr;
while (tp_host->h_addr_list[i] != 0) {
addr.s_addr = *(u_long *)tp_host->h_addr_list[i++];
@ -1005,8 +904,7 @@ void TsHttpRpc::_rpc_func_rdp_play(const ex_astr& func_args, ex_astr& buf)
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcess(NULL, (wchar_t *)w_exe_path.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
if (!CreateProcess(NULL, (wchar_t *)w_exe_path.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
EXLOGE(_T("CreateProcess() failed. Error=0x%08X.\n %s\n"), GetLastError(), w_exe_path.c_str());
root_ret["code"] = TPE_START_CLIENT;
_create_json_ret(buf, root_ret);
@ -1018,25 +916,22 @@ void TsHttpRpc::_rpc_func_rdp_play(const ex_astr& func_args, ex_astr& buf)
return;
}
void TsHttpRpc::_rpc_func_get_config(const ex_astr& func_args, ex_astr& buf)
{
void TsHttpRpc::_rpc_func_get_config(const ex_astr& func_args, ex_astr& buf) {
Json::Value jr_root;
jr_root["code"] = 0;
jr_root["data"] = g_cfg.get_root();
_create_json_ret(buf, jr_root);
}
void TsHttpRpc::_rpc_func_set_config(const ex_astr& func_args, ex_astr& buf)
{
void TsHttpRpc::_rpc_func_set_config(const ex_astr& func_args, ex_astr& buf) {
Json::Reader jreader;
Json::Value jsRoot;
if (!jreader.parse(func_args.c_str(), jsRoot))
{
if (!jreader.parse(func_args.c_str(), jsRoot)) {
_create_json_ret(buf, TPE_JSON_FORMAT);
return;
}
if(!g_cfg.save(func_args))
if (!g_cfg.save(func_args))
_create_json_ret(buf, TPE_FAILED);
else
_create_json_ret(buf, TPE_OK);
@ -1047,14 +942,12 @@ void TsHttpRpc::_rpc_func_file_action(const ex_astr& func_args, ex_astr& buf) {
Json::Reader jreader;
Json::Value jsRoot;
if (!jreader.parse(func_args.c_str(), jsRoot))
{
if (!jreader.parse(func_args.c_str(), jsRoot)) {
_create_json_ret(buf, TPE_JSON_FORMAT);
return;
}
// 判断参数是否正确
if (!jsRoot["action"].isNumeric())
{
if (!jsRoot["action"].isNumeric()) {
_create_json_ret(buf, TPE_PARAM);
return;
}
@ -1067,8 +960,7 @@ void TsHttpRpc::_rpc_func_file_action(const ex_astr& func_args, ex_astr& buf) {
BOOL ret = FALSE;
wchar_t wszReturnPath[MAX_PATH] = _T("");
if (action == 1 || action == 2)
{
if (action == 1 || action == 2) {
OPENFILENAME ofn;
ex_wstr wsDefaultName;
ex_wstr wsDefaultPath;
@ -1085,19 +977,14 @@ void TsHttpRpc::_rpc_func_file_action(const ex_astr& func_args, ex_astr& buf) {
ofn.lpstrInitialDir = wsDefaultPath.c_str();
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST;
if (action == 1)
{
if (action == 1) {
ofn.Flags |= OFN_FILEMUSTEXIST;
ret = GetOpenFileName(&ofn);
}
else
{
} else {
ofn.Flags |= OFN_OVERWRITEPROMPT;
ret = GetSaveFileName(&ofn);
}
}
else if (action == 3)
{
} else if (action == 3) {
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
bi.hwndOwner = NULL;
@ -1108,31 +995,24 @@ void TsHttpRpc::_rpc_func_file_action(const ex_astr& func_args, ex_astr& buf) {
bi.lpfn = NULL;
bi.iImage = 0; //初始化入口参数bi结束
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
if (pIDList)
{
if (pIDList) {
ret = true;
SHGetPathFromIDList(pIDList, wszReturnPath);
}
else
{
} else {
ret = false;
}
}
else if (action == 4)
{
} else if (action == 4) {
ex_wstr wsDefaultName;
ex_wstr wsDefaultPath;
if (wsDefaultPath.length() == 0)
{
if (wsDefaultPath.length() == 0) {
_create_json_ret(buf, TPE_PARAM);
return;
}
ex_wstr::size_type pos = 0;
while (ex_wstr::npos != (pos = wsDefaultPath.find(L"/", pos)))
{
while (ex_wstr::npos != (pos = wsDefaultPath.find(L"/", pos))) {
wsDefaultPath.replace(pos, 1, L"\\");
pos += 1;
}
@ -1146,10 +1026,8 @@ void TsHttpRpc::_rpc_func_file_action(const ex_astr& func_args, ex_astr& buf) {
ret = false;
}
if (ret)
{
if (action == 1 || action == 2 || action == 3)
{
if (ret) {
if (action == 1 || action == 2 || action == 3) {
ex_astr utf8_path;
ex_wstr2astr(wszReturnPath, utf8_path, EX_CODEPAGE_UTF8);
Json::Value root;
@ -1158,22 +1036,17 @@ void TsHttpRpc::_rpc_func_file_action(const ex_astr& func_args, ex_astr& buf) {
_create_json_ret(buf, root);
return;
}
else
{
} else {
_create_json_ret(buf, TPE_OK);
return;
}
}
else
{
} else {
_create_json_ret(buf, TPE_DATA);
return;
}
}
void TsHttpRpc::_rpc_func_get_version(const ex_astr& func_args, ex_astr& buf)
{
void TsHttpRpc::_rpc_func_get_version(const ex_astr& func_args, ex_astr& buf) {
Json::Value root_ret;
ex_wstr w_version = TP_ASSIST_VER;
ex_astr version;

View File

@ -45,6 +45,9 @@ void http_rpc_stop(void);
typedef std::map<ex_astr, ex_astr> content_type_map;
// for https server, see
// http://www.xiaovdiy.cn/?post=284
class TsHttpRpc
{
public: