url_config.cpp
2.32 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
#include "stdafx.h"
#include "url_config.h"
#include <sstream>
#include <fstream>
#include <exception>
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "AUDataManage.h"
#include "openssl_aes_cbc.h"
#include "string_converter.h"
static const std::string CIPHER_TEXT_FILE = "\\Config\\url_cfg";
static const std::string PLAIN_TEXT_FILE = CIPHER_TEXT_FILE + ".json";
static const std::string HTTP("http");
static const std::string HTTPS("https");
static const uint16_t INTERNET_DEFAULT_PORT = 0; // use the protocol-specific default
CString GetModulePath()
{
CString sPath;
TCHAR szPath[MAX_PATH] = { 0 };
GetModuleFileName(NULL, szPath, MAX_PATH);
CString sMudolePath = szPath;
CString sMudoleName = AfxGetAppName();
sPath = sMudolePath.Left(sMudolePath.GetLength() - sMudoleName.GetLength() - 4);
return sPath;
}
URLConfig::URLConfig()
{
std::stringstream ss;
try
{
#ifdef _DEBUG
// 在调试的时候,先生成加密文件
AES_CBC::EncryptFileToFile(GetModulePath().GetBuffer(0) + PLAIN_TEXT_FILE, GetModulePath().GetBuffer(0) + CIPHER_TEXT_FILE);
#endif
ss << AES_CBC::DecryptFromFile(GetModulePath().GetBuffer(0) + CIPHER_TEXT_FILE);
}
catch (std::exception& ex)
{
Log(CString(gbk_2_wstr(ex.what()).c_str()));
return;
}
namespace PT = boost::property_tree;
try //Parse the configuration file
{
PT::ptree ptree;
PT::read_json(ss, ptree);
m_host = ptree.get<std::string>("host_download");
m_port = ptree.get<uint16_t>("port");
m_download_url = ptree.get<std::string>("download_path");
m_download_file_url = ptree.get<std::string>("download_file_path");
}
catch (...) // Catch the exception in order for logging.
{
Log(CString(L"解析路径配置文件“./Config/url_cfg.json”时出错!请确认该配置文件存在,存放的json数据格式正确!"));
throw; // Don't swallow the exception!
}
}
URLConfig& URLConfig::Instance()
{
static URLConfig url_config;
return url_config;
}
std::string URLConfig::Host() const
{
return m_host;
}
uint16_t URLConfig::Port() const
{
return m_port;
}
std::string URLConfig::FullHost() const
{
return std::string("HTTP") + "://" + m_host + ":" + std::to_string(m_port);
}
std::string URLConfig::DownloadUrl() const
{
return m_download_url;
}
std::string URLConfig::DownloadFileUrl() const
{
return m_download_file_url;
}