url_config.cpp 2.21 KB
#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_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;
}
std::string URLConfig::FullHost() const
{
	return std::string("HTTP") + "://" + m_host + ":" + "80";
}

std::string URLConfig::DownloadUrl() const
{
	return m_download_url;
}

std::string URLConfig::DownloadFileUrl() const
{
	return m_download_file_url;
}