|
|
@ -3,7 +3,13 @@ |
|
|
|
#include <cctype> |
|
|
|
#include <cctype> |
|
|
|
#include <functional> |
|
|
|
#include <functional> |
|
|
|
#include <fstream> |
|
|
|
#include <fstream> |
|
|
|
|
|
|
|
#include <set> |
|
|
|
#include <boost/asio.hpp> |
|
|
|
#include <boost/asio.hpp> |
|
|
|
|
|
|
|
#include <boost/filesystem.hpp> |
|
|
|
|
|
|
|
#include <boost/filesystem/fstream.hpp> |
|
|
|
|
|
|
|
#include <boost/foreach.hpp> |
|
|
|
|
|
|
|
#include <boost/program_options/detail/config_file.hpp> |
|
|
|
|
|
|
|
#include <boost/program_options/parsers.hpp> |
|
|
|
#include "util.h" |
|
|
|
#include "util.h" |
|
|
|
#include "Log.h" |
|
|
|
#include "Log.h" |
|
|
|
|
|
|
|
|
|
|
@ -11,40 +17,160 @@ namespace i2p |
|
|
|
{ |
|
|
|
{ |
|
|
|
namespace util |
|
|
|
namespace util |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::map<std::string, std::string> mapArgs; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OptionParser(int argc, const char* const argv[]) |
|
|
|
namespace config |
|
|
|
{ |
|
|
|
{ |
|
|
|
mapArgs.clear(); |
|
|
|
std::map<std::string, std::string> mapArgs; |
|
|
|
for (int i = 1; i < argc; i++) |
|
|
|
std::map<std::string, std::vector<std::string> > mapMultiArgs; |
|
|
|
{ |
|
|
|
|
|
|
|
std::string strKey (argv[i]); |
|
|
|
|
|
|
|
std::string strValue; |
|
|
|
|
|
|
|
size_t has_data = strKey.find('='); |
|
|
|
|
|
|
|
if (has_data != std::string::npos) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
strValue = strKey.substr(has_data+1); |
|
|
|
|
|
|
|
strKey = strKey.substr(0, has_data); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (strKey[0] != '-') |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mapArgs[strKey] = strValue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int GetIntArg(const std::string& strArg, int nDefault) |
|
|
|
void OptionParser(int argc, const char* const argv[]) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (mapArgs.count(strArg)) |
|
|
|
mapArgs.clear(); |
|
|
|
return atoi(mapArgs[strArg].c_str()); |
|
|
|
mapMultiArgs.clear(); |
|
|
|
return nDefault; |
|
|
|
for (int i = 1; i < argc; i++) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
std::string strKey (argv[i]); |
|
|
|
|
|
|
|
std::string strValue; |
|
|
|
|
|
|
|
size_t has_data = strKey.find('='); |
|
|
|
|
|
|
|
if (has_data != std::string::npos) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
strValue = strKey.substr(has_data+1); |
|
|
|
|
|
|
|
strKey = strKey.substr(0, has_data); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32 |
|
|
|
|
|
|
|
boost::to_lower(strKey); |
|
|
|
|
|
|
|
if (boost::algorithm::starts_with(strKey, "/")) |
|
|
|
|
|
|
|
strKey = "-" + strKey.substr(1); |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
if (strKey[0] != '-') |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mapArgs[strKey] = strValue; |
|
|
|
|
|
|
|
mapMultiArgs[strKey].push_back(strValue); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_FOREACH(PAIRTYPE(const std::string,std::string)& entry, mapArgs) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
std::string name = entry.first; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// interpret --foo as -foo (as long as both are not set)
|
|
|
|
|
|
|
|
if (name.find("--") == 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
std::string singleDash(name.begin()+1, name.end()); |
|
|
|
|
|
|
|
if (mapArgs.count(singleDash) == 0) |
|
|
|
|
|
|
|
mapArgs[singleDash] = entry.second; |
|
|
|
|
|
|
|
name = singleDash; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char* GetCharArg(const std::string& strArg, const std::string& nDefault) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (mapArgs.count(strArg)) |
|
|
|
|
|
|
|
return mapArgs[strArg].c_str(); |
|
|
|
|
|
|
|
return nDefault.c_str(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string GetArg(const std::string& strArg, const std::string& strDefault) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (mapArgs.count(strArg)) |
|
|
|
|
|
|
|
return mapArgs[strArg]; |
|
|
|
|
|
|
|
return strDefault; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int GetArg(const std::string& strArg, int nDefault) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (mapArgs.count(strArg)) |
|
|
|
|
|
|
|
return atoi(mapArgs[strArg].c_str()); |
|
|
|
|
|
|
|
return nDefault; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const char* GetCharArg(const std::string& strArg, const std::string& nDefault) |
|
|
|
namespace filesystem |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (mapArgs.count(strArg)) |
|
|
|
const boost::filesystem::path &GetDataDir() |
|
|
|
return mapArgs[strArg].c_str(); |
|
|
|
{ |
|
|
|
return nDefault.c_str(); |
|
|
|
|
|
|
|
|
|
|
|
static boost::filesystem::path path; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (i2p::util::config::mapArgs.count("-datadir")) { |
|
|
|
|
|
|
|
path = boost::filesystem::system_complete(i2p::util::config::mapArgs["-datadir"]); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
path = GetDefaultDataDir(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!boost::filesystem::exists( path )) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Create data directory
|
|
|
|
|
|
|
|
if (!boost::filesystem::create_directory( path )) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint("Failed to create data directory!"); |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!boost::filesystem::is_directory(path)) { |
|
|
|
|
|
|
|
path = GetDefaultDataDir(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return path; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boost::filesystem::path GetConfigFile() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
boost::filesystem::path pathConfigFile(i2p::util::config::GetArg("-conf", "i2p.conf")); |
|
|
|
|
|
|
|
if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir() / pathConfigFile; |
|
|
|
|
|
|
|
return pathConfigFile; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, |
|
|
|
|
|
|
|
std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
boost::filesystem::ifstream streamConfig(GetConfigFile()); |
|
|
|
|
|
|
|
if (!streamConfig.good()) |
|
|
|
|
|
|
|
return; // No i2pd.conf file is OK
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::set<std::string> setOptions; |
|
|
|
|
|
|
|
setOptions.insert("*"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Don't overwrite existing settings so command line settings override i2pd.conf
|
|
|
|
|
|
|
|
std::string strKey = std::string("-") + it->string_key; |
|
|
|
|
|
|
|
if (mapSettingsRet.count(strKey) == 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
mapSettingsRet[strKey] = it->value[0]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mapMultiSettingsRet[strKey].push_back(it->value[0]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boost::filesystem::path GetDefaultDataDir() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Windows < Vista: C:\Documents and Settings\Username\Application Data\i2pd
|
|
|
|
|
|
|
|
// Windows >= Vista: C:\Users\Username\AppData\Roaming\i2pd
|
|
|
|
|
|
|
|
// Mac: ~/Library/Application Support/i2pd
|
|
|
|
|
|
|
|
// Unix: ~/.i2pd
|
|
|
|
|
|
|
|
#ifdef WIN32 |
|
|
|
|
|
|
|
// Windows
|
|
|
|
|
|
|
|
return GetSpecialFolderPath(CSIDL_APPDATA) / "i2pd"; |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
boost::filesystem::path pathRet; |
|
|
|
|
|
|
|
char* pszHome = getenv("HOME"); |
|
|
|
|
|
|
|
if (pszHome == NULL || strlen(pszHome) == 0) |
|
|
|
|
|
|
|
pathRet = boost::filesystem::path("/"); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
pathRet = boost::filesystem::path(pszHome); |
|
|
|
|
|
|
|
#ifdef MAC_OSX |
|
|
|
|
|
|
|
// Mac
|
|
|
|
|
|
|
|
pathRet /= "Library/Application Support"; |
|
|
|
|
|
|
|
boost::filesystem::create_directory(pathRet); |
|
|
|
|
|
|
|
return pathRet / "i2pd"; |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
// Unix
|
|
|
|
|
|
|
|
return pathRet / ".i2pd"; |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
namespace http |
|
|
|
namespace http |
|
|
@ -102,26 +228,26 @@ namespace http |
|
|
|
|
|
|
|
|
|
|
|
void url::parse(const std::string& url_s) |
|
|
|
void url::parse(const std::string& url_s) |
|
|
|
{ |
|
|
|
{ |
|
|
|
const std::string prot_end("://"); |
|
|
|
const std::string prot_end("://"); |
|
|
|
std::string::const_iterator prot_i = search(url_s.begin(), url_s.end(), |
|
|
|
std::string::const_iterator prot_i = search(url_s.begin(), url_s.end(), |
|
|
|
prot_end.begin(), prot_end.end()); |
|
|
|
prot_end.begin(), prot_end.end()); |
|
|
|
protocol_.reserve(distance(url_s.begin(), prot_i)); |
|
|
|
protocol_.reserve(distance(url_s.begin(), prot_i)); |
|
|
|
transform(url_s.begin(), prot_i, |
|
|
|
transform(url_s.begin(), prot_i, |
|
|
|
back_inserter(protocol_), |
|
|
|
back_inserter(protocol_), |
|
|
|
std::ptr_fun<int,int>(tolower)); // protocol is icase
|
|
|
|
std::ptr_fun<int,int>(tolower)); // protocol is icase
|
|
|
|
if( prot_i == url_s.end() ) |
|
|
|
if( prot_i == url_s.end() ) |
|
|
|
return; |
|
|
|
return; |
|
|
|
advance(prot_i, prot_end.length()); |
|
|
|
advance(prot_i, prot_end.length()); |
|
|
|
std::string::const_iterator path_i = find(prot_i, url_s.end(), '/'); |
|
|
|
std::string::const_iterator path_i = find(prot_i, url_s.end(), '/'); |
|
|
|
host_.reserve(distance(prot_i, path_i)); |
|
|
|
host_.reserve(distance(prot_i, path_i)); |
|
|
|
transform(prot_i, path_i, |
|
|
|
transform(prot_i, path_i, |
|
|
|
back_inserter(host_), |
|
|
|
back_inserter(host_), |
|
|
|
std::ptr_fun<int,int>(tolower)); // host is icase
|
|
|
|
std::ptr_fun<int,int>(tolower)); // host is icase
|
|
|
|
std::string::const_iterator query_i = find(path_i, url_s.end(), '?'); |
|
|
|
std::string::const_iterator query_i = find(path_i, url_s.end(), '?'); |
|
|
|
path_.assign(path_i, query_i); |
|
|
|
path_.assign(path_i, query_i); |
|
|
|
if( query_i != url_s.end() ) |
|
|
|
if( query_i != url_s.end() ) |
|
|
|
++query_i; |
|
|
|
++query_i; |
|
|
|
query_.assign(query_i, url_s.end()); |
|
|
|
query_.assign(query_i, url_s.end()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|