mirror of https://github.com/tp4a/teleport
245 lines
4.9 KiB
C++
245 lines
4.9 KiB
C++
#include "stdafx.h"
|
||
//#include "ts_ini.h"
|
||
#include "ts_cfg.h"
|
||
#include "ts_env.h"
|
||
|
||
TsCfgSSH g_cfgSSH;
|
||
TsCfgScp g_cfgScp;
|
||
TsCfgTelnet g_cfgTelnet;
|
||
|
||
void split_by_char(ex_wstr s, char ch, std::vector<ex_wstr>& ret)
|
||
{
|
||
int pos;
|
||
|
||
while (s.length() != 0)
|
||
{
|
||
pos = s.find_first_of(ch, 0);
|
||
if (-1 == pos)
|
||
{
|
||
ret.push_back(s);
|
||
s = s.erase(0, s.length());
|
||
}
|
||
else if (0 == pos)
|
||
{
|
||
s = s.erase(0, pos + 1);
|
||
}
|
||
else
|
||
{
|
||
ex_wstr temp;
|
||
temp.append(s, 0, pos);
|
||
ret.push_back(temp);
|
||
s = s.erase(0, pos + 1);
|
||
}
|
||
}
|
||
}
|
||
|
||
//=====================================================================
|
||
// Base Configuration Class
|
||
//=====================================================================
|
||
|
||
TsClientCfgBase::TsClientCfgBase()
|
||
{
|
||
m_clientsetmap.clear();
|
||
m_client_list.clear();
|
||
}
|
||
|
||
TsClientCfgBase::~TsClientCfgBase()
|
||
{}
|
||
|
||
bool TsClientCfgBase::_init(void)
|
||
{
|
||
client_set temp;
|
||
|
||
ExIniSection* cfg = NULL;
|
||
cfg = m_ini.GetSection(_T("common"));
|
||
if (NULL == cfg)
|
||
{
|
||
EXLOGE("[ERROR] Invalid configuration, [common] section not found.\n");
|
||
return false;
|
||
}
|
||
|
||
ex_wstr _wstr;
|
||
if (!cfg->GetStr(_T("current_client"), _wstr)) {
|
||
return false;
|
||
}
|
||
|
||
m_current_client = _wstr;
|
||
|
||
if (!cfg->GetStr(_T("client_list"), _wstr)) {
|
||
return false;
|
||
}
|
||
|
||
std::vector<ex_wstr> c_list;
|
||
split_by_char(_wstr, ',', c_list);
|
||
|
||
std::vector<ex_wstr>::iterator it;
|
||
for (it = c_list.begin(); it != c_list.end(); it++)
|
||
{
|
||
ex_wstr sec_name = it->c_str();
|
||
cfg = m_ini.GetSection(sec_name);
|
||
if (NULL == cfg)
|
||
{
|
||
EXLOGE("[ERROR] Invalid configuration, [common] section not found.\n");
|
||
return false;
|
||
}
|
||
|
||
if (!cfg->GetStr(_T("name"), _wstr)) {
|
||
continue;
|
||
}
|
||
temp.name = _wstr;
|
||
|
||
if (!cfg->GetStr(_T("path"), _wstr)) {
|
||
continue;
|
||
}
|
||
temp.path = _wstr;
|
||
|
||
if (!cfg->GetStr(_T("alias_name"), _wstr)) {
|
||
continue;
|
||
}
|
||
temp.alias_name = _wstr;
|
||
|
||
if (!cfg->GetStr(_T("command_line"), _wstr)) {
|
||
continue;
|
||
}
|
||
temp.commandline = _wstr;
|
||
|
||
if (!cfg->GetStr(_T("desc"), _wstr)) {
|
||
continue;
|
||
}
|
||
temp.desc = _wstr;
|
||
|
||
temp.is_default = false;
|
||
|
||
m_clientsetmap[temp.name] = temp;
|
||
m_client_list.push_back(temp.name);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
void TsClientCfgBase::set(ex_wstr sec_name, ex_wstr key, ex_wstr value)
|
||
{
|
||
if (sec_name != _T("common"))
|
||
{
|
||
clientsetmap::iterator it = m_clientsetmap.find(sec_name);
|
||
if (it == m_clientsetmap.end())
|
||
return;
|
||
}
|
||
|
||
ExIniSection* cfg = NULL;
|
||
cfg = m_ini.GetSection(sec_name);
|
||
if (NULL == cfg)
|
||
{
|
||
EXLOGE("[ERROR] Invalid configuration, [common] section not found.\n");
|
||
return;
|
||
}
|
||
cfg->SetValue(key, value);
|
||
|
||
return;
|
||
}
|
||
|
||
void TsClientCfgBase::save()
|
||
{
|
||
m_ini.Save(EX_CODEPAGE_UTF8);
|
||
}
|
||
|
||
//=====================================================
|
||
|
||
TsCfgSSH::TsCfgSSH()
|
||
{}
|
||
|
||
TsCfgSSH::~TsCfgSSH()
|
||
{}
|
||
|
||
bool TsCfgSSH::init(void)
|
||
{
|
||
m_current_client = _T("putty");
|
||
|
||
client_set temp;
|
||
temp.name = _T("putty");
|
||
temp.alias_name = _T("PuTTY (<28><><EFBFBD><EFBFBD>)");
|
||
temp.path = g_env.m_tools_path;
|
||
temp.path += _T("\\putty\\putty.exe");
|
||
temp.commandline = _T("-ssh -pw **** -P {host_port} -l {user_name} {host_ip}");
|
||
temp.desc = _T("PuTTYΪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>Simon Tathamά<6D><CEAC><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>MIT licence<63><65>Ȩ<EFBFBD><C8A8>");
|
||
temp.is_default = true;
|
||
|
||
m_clientsetmap[temp.name] = temp;
|
||
m_client_list.push_back(temp.name);
|
||
|
||
if (!m_ini.LoadFromFile(g_env.m_ssh_client_conf_file))
|
||
{
|
||
EXLOGE("can not load ssh config file.\n");
|
||
return false;
|
||
}
|
||
|
||
return _init();
|
||
}
|
||
|
||
//=====================================================
|
||
|
||
TsCfgScp::TsCfgScp()
|
||
{}
|
||
|
||
TsCfgScp::~TsCfgScp()
|
||
{}
|
||
|
||
bool TsCfgScp::init(void)
|
||
{
|
||
m_current_client = _T("winscp");
|
||
client_set temp;
|
||
|
||
|
||
temp.name = _T("winscp");
|
||
temp.alias_name = _T("WinSCP (<28><><EFBFBD><EFBFBD>)");
|
||
temp.path = g_env.m_tools_path;
|
||
temp.path += _T("\\winscp\\winscp.exe");
|
||
temp.commandline = _T("/sessionname=\"TP#{real_ip}\" {user_name}:****@{host_ip}:{host_port}");
|
||
temp.desc = _T("WinSCP<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Windows<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>SSH<EFBFBD>Ŀ<EFBFBD>Դͼ<EFBFBD>λ<EFBFBD>SFTP<EFBFBD>ͻ<EFBFBD><EFBFBD>ˡ<EFBFBD>ͬʱ֧<EFBFBD><EFBFBD>SCPЭ<EFBFBD>顣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ܾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>̼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>䰲ȫ<EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>");
|
||
temp.is_default = true;
|
||
m_clientsetmap[temp.name] = temp;
|
||
m_client_list.push_back(temp.name);
|
||
|
||
if (!m_ini.LoadFromFile(g_env.m_scp_client_conf_file))
|
||
{
|
||
EXLOGE("can not load scp config file.\n");
|
||
return false;
|
||
}
|
||
|
||
return _init();
|
||
}
|
||
|
||
//=====================================================
|
||
|
||
TsCfgTelnet::TsCfgTelnet()
|
||
{}
|
||
|
||
TsCfgTelnet::~TsCfgTelnet()
|
||
{}
|
||
|
||
bool TsCfgTelnet::init(void)
|
||
{
|
||
m_current_client = _T("putty");
|
||
client_set temp;
|
||
|
||
temp.name = _T("putty");
|
||
temp.alias_name = _T("PuTTY (<28><><EFBFBD><EFBFBD>)");
|
||
temp.path = g_env.m_tools_path;
|
||
temp.path += _T("\\putty\\putty.exe");
|
||
//temp.commandline = _T("-telnet -P {host_port} -l {user_name} {host_ip}");
|
||
temp.commandline = _T("telnet://{user_name}@{host_ip}:{host_port}");
|
||
temp.desc = _T("PuTTYΪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>Simon Tathamά<6D><CEAC><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>MIT licence<63><65>Ȩ<EFBFBD><C8A8>");
|
||
|
||
temp.is_default = true;
|
||
m_clientsetmap[temp.name] = temp;
|
||
m_client_list.push_back(temp.name);
|
||
|
||
if (!m_ini.LoadFromFile(g_env.m_telnet_client_conf_file))
|
||
{
|
||
EXLOGE("can not load telnet config file.\n");
|
||
return false;
|
||
}
|
||
|
||
return _init();
|
||
}
|