Browse Source

Merge pull request #21 from meeh420/master

Adding support for config file
pull/23/head
orignal 11 years ago
parent
commit
be42dbded3
  1. 2
      Makefile
  2. 12
      i2p.cpp
  3. 144
      util.cpp
  4. 19
      util.h

2
Makefile

@ -6,7 +6,7 @@ OBJECTS = obj/i2p.o obj/base64.o obj/NTCPSession.o obj/RouterInfo.o obj/Transpor
obj/TunnelGateway.o obj/TransitTunnel.o obj/I2NPProtocol.o obj/Log.o obj/Garlic.o \ obj/TunnelGateway.o obj/TransitTunnel.o obj/I2NPProtocol.o obj/Log.o obj/Garlic.o \
obj/HTTPServer.o obj/Streaming.o obj/Identity.o obj/SSU.o obj/util.o obj/Reseed.o obj/HTTPServer.o obj/Streaming.o obj/Identity.o obj/SSU.o obj/util.o obj/Reseed.o
INCFLAGS = INCFLAGS =
LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem -lboost_regex -lpthread LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
LIBS = LIBS =
all: obj i2p all: obj i2p

12
i2p.cpp

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <cryptopp/integer.h> #include <cryptopp/integer.h>
#include <boost/filesystem.hpp>
#include "Log.h" #include "Log.h"
#include "base64.h" #include "base64.h"
#include "Transports.h" #include "Transports.h"
@ -14,7 +15,7 @@
int main( int argc, char* argv[] ) int main( int argc, char* argv[] )
{ {
i2p::util::OptionParser(argc,argv); i2p::util::config::OptionParser(argc,argv);
#ifdef _WIN32 #ifdef _WIN32
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
SetConsoleCP(1251); SetConsoleCP(1251);
@ -22,10 +23,15 @@ int main( int argc, char* argv[] )
setlocale(LC_ALL, "Russian"); setlocale(LC_ALL, "Russian");
#endif #endif
LogPrint("\n\n\n\ni2pd starting\n");
LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string());
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
//TODO: This is an ugly workaround. fix it. //TODO: This is an ugly workaround. fix it.
//TODO: Autodetect public IP. //TODO: Autodetect public IP.
i2p::context.OverrideNTCPAddress(i2p::util::GetCharArg("--host", "127.0.0.1"), i2p::util::GetIntArg("--port", 17070)); i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"),
int httpport = i2p::util::GetIntArg("--httpport", 7070); i2p::util::config::GetArg("-port", 17070));
int httpport = i2p::util::config::GetArg("-httpport", 7070);
i2p::util::HTTPServer httpServer (httpport); i2p::util::HTTPServer httpServer (httpport);

144
util.cpp

@ -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,11 +17,16 @@ namespace i2p
{ {
namespace util namespace util
{ {
std::map<std::string, std::string> mapArgs;
void OptionParser(int argc, const char* const argv[]) namespace config
{ {
std::map<std::string, std::string> mapArgs;
std::map<std::string, std::vector<std::string> > mapMultiArgs;
void OptionParser(int argc, const char* const argv[])
{
mapArgs.clear(); mapArgs.clear();
mapMultiArgs.clear();
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
std::string strKey (argv[i]); std::string strKey (argv[i]);
@ -26,25 +37,140 @@ void OptionParser(int argc, const char* const argv[])
strValue = strKey.substr(has_data+1); strValue = strKey.substr(has_data+1);
strKey = strKey.substr(0, has_data); 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] != '-') if (strKey[0] != '-')
break; break;
mapArgs[strKey] = strValue; mapArgs[strKey] = strValue;
mapMultiArgs[strKey].push_back(strValue);
} }
}
int GetIntArg(const std::string& strArg, int nDefault) 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)) if (mapArgs.count(strArg))
return atoi(mapArgs[strArg].c_str()); return atoi(mapArgs[strArg].c_str());
return nDefault; 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

19
util.h

@ -4,14 +4,31 @@
#include <map> #include <map>
#include <string> #include <string>
#define PAIRTYPE(t1, t2) std::pair<t1, t2>
namespace i2p namespace i2p
{ {
namespace util namespace util
{ {
namespace config
{
extern std::map<std::string, std::string> mapArgs; extern std::map<std::string, std::string> mapArgs;
extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
void OptionParser(int argc, const char* const argv[]); void OptionParser(int argc, const char* const argv[]);
int GetIntArg(const std::string& strArg, int nDefault); int GetArg(const std::string& strArg, int nDefault);
std::string GetArg(const std::string& strArg, const std::string& strDefault);
const char* GetCharArg(const std::string& strArg, const std::string& nDefault); const char* GetCharArg(const std::string& strArg, const std::string& nDefault);
}
namespace filesystem
{
const boost::filesystem::path &GetDataDir();
boost::filesystem::path GetDefaultDataDir();
boost::filesystem::path GetConfigFile();
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet,
std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
}
namespace http namespace http
{ {
std::string httpRequest(const std::string& address); std::string httpRequest(const std::string& address);

Loading…
Cancel
Save