application_config.cpp
5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "stdafx.h"
#include "etradeclient/utility/application_config.h"
#include <sstream>
#include <exception>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
//#include "etradeclient/boost_patch/property_tree/json_parser.hpp" // WARNIING! Make sure to include our patched version.
#include "etradeclient/utility/logging.h"
#include "etradeclient/utility/openssl_aes_cbc.h"
#include "etradeclient/utility/string_converter.h"
static const std::string CIPHER_TEXT_FILE = "./Config/application_config";
static const std::string PLAIN_TEXT_FILE = CIPHER_TEXT_FILE + ".json";
ApplicationConfig::ApplicationConfig()
{
std::stringstream ss;
try
{
#ifdef _DEBUG
// 在调试的时候,先生成加密文件
AES_CBC::EncryptFileToFile(PLAIN_TEXT_FILE, CIPHER_TEXT_FILE);
#endif
ss << AES_CBC::DecryptFromFile(CIPHER_TEXT_FILE);
}
catch (std::exception& ex)
{
LOG_ERROR(gbk_2_wstr(ex.what()));
return;
}
namespace PT = boost::property_tree;
try //Parse the configuration file
{
PT::ptree ptree;
PT::read_json(ss, ptree);
m_msg_count_query_interval = ptree.get<uint32_t>("msg_count_query_interval");
m_pwd_machine_ip = ptree.get<std::string>("pwd_machine_ip");
m_pwd_machine_port = ptree.get<uint32_t>("pwd_machine_port");
m_pwd_machine_timeout = ptree.get<uint32_t>("pwd_machine_timeout");
m_pin_pad_pwd_len = ptree.get<uint8_t>("pin_pad_pwd_len");
m_pin_pad_timeout = ptree.get<uint8_t>("pin_pad_timeout");
m_client_cert_store = ptree.get<std::string>("client_cert_store");
m_client_cert_subject = ptree.get<std::string>("client_cert_subject");
m_client_cert_filename = ptree.get<std::string>("client_cert_filename");
m_client_cert_password = ptree.get<std::string>("client_cert_password");
m_is_encrypt = ptree.get<uint8_t>("is_encrypt"); //0为不加密,1为加密
m_encrypt_type = ptree.get<uint8_t>("encrypt_type"); //0为加密机加密,1为软加密
//为了安全起见,目前只支持加密机加密一种模式 add by daxiong 2017.10.19
if (m_is_encrypt == 1)
{
m_encrypt_type = 0;
}
//
}
catch (...) // Catch the exception in order for logging.
{
LOG_ERROR(L"解析路径配置文件" + str_2_wstr(CIPHER_TEXT_FILE) + L"出错!请确认该配置文件存在,存放的json数据格式正确!");
throw; // Don't swallow the exception!
}
}
void ApplicationConfig::ModifyCfg(uint32_t msg_query_interval,
const std::string& pwd_machine_ip,
uint32_t pwd_machine_port,
uint32_t pwd_machine_time_out,
uint8_t pin_pad_pwd_len,
uint8_t pin_pad_timeout)
{
std::stringstream ss;
namespace PT = boost::property_tree;
m_msg_count_query_interval = msg_query_interval;
m_pwd_machine_ip = pwd_machine_ip;
m_pwd_machine_port = pwd_machine_port;
m_pwd_machine_timeout = pwd_machine_time_out;
m_pin_pad_pwd_len = pin_pad_pwd_len;
m_pin_pad_timeout = pin_pad_timeout;
try
{
PT::ptree root;
root.put("msg_count_query_interval", m_msg_count_query_interval);
root.put("pwd_machine_ip", m_pwd_machine_ip);
root.put("pwd_machine_port", m_pwd_machine_port);
root.put("pwd_machine_timeout", m_pwd_machine_timeout);
root.put("pin_pad_pwd_len", m_pin_pad_pwd_len);
root.put("pin_pad_timeout", m_pin_pad_timeout);
root.put("client_cert_store", m_client_cert_store);
root.put("client_cert_subject", m_client_cert_subject);
root.put("client_cert_filename", m_client_cert_filename);
root.put("client_cert_password", m_client_cert_password);
root.put("is_encrypt", m_is_encrypt);
root.put("encrypt_type", m_encrypt_type);
PT::write_json(ss, root, true);
}
catch (...)
{
LOG_ERROR(L"组Json字符流时出错,请检查!");
throw; // Don't swallow the exception!
}
try
{
AES_CBC::EncryptToFile(ss.str(), CIPHER_TEXT_FILE);
#ifdef _DEBUG // Save to plain text file under debug mode.
std::ofstream ofs(PLAIN_TEXT_FILE, std::ios_base::out | std::ios_base::binary);
if (!ofs.is_open())
throw std::exception(("打开文件" + PLAIN_TEXT_FILE + "失败!").c_str());
ofs << ss.str();
ofs.close();
#endif
}
catch (std::exception& ex)
{
LOG_ERROR(gbk_2_wstr(ex.what()));
}
}
ApplicationConfig& ApplicationConfig::Instance()
{
static ApplicationConfig application_config;
return application_config;
}
uint32_t ApplicationConfig::MsgCountQueryInterval() const
{
return m_msg_count_query_interval;
}
std::string ApplicationConfig::PWDMachineIP() const
{
return m_pwd_machine_ip;
}
uint32_t ApplicationConfig::PWDMachinePort() const
{
return m_pwd_machine_port;
}
uint32_t ApplicationConfig::PWDMachineTimeout() const
{
return m_pwd_machine_timeout;
}
uint8_t ApplicationConfig::PINPadPWDLen() const
{
return m_pin_pad_pwd_len;
}
uint8_t ApplicationConfig::PINPadTimeout() const
{
return m_pin_pad_timeout;
}
std::string ApplicationConfig::ClientCertStore() const
{
return m_client_cert_store;
}
std::string ApplicationConfig::ClientCertSubject() const
{
return m_client_cert_subject;
}
std::string ApplicationConfig::ClientCertFileName() const
{
return m_client_cert_filename;
}
std::string ApplicationConfig::ClientCertPassword() const
{
return m_client_cert_password;
}
bool ApplicationConfig::IsEncrypt() const
{
return m_is_encrypt ? true : false;
}
uint8_t ApplicationConfig::EncryptType() const
{
return m_encrypt_type;
}