diff --git a/client/Daemon.cpp b/client/Daemon.cpp index 79b8032d..e7a3575d 100644 --- a/client/Daemon.cpp +++ b/client/Daemon.cpp @@ -56,7 +56,19 @@ namespace i2p LogPrint("\n\n\n\ni2pd starting\n"); LogPrint("Version ", VERSION); LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string()); - i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs); + i2p::util::filesystem::ReadConfigFile( + i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs + ); + + if(i2p::util::config::HasArg("-install")) { + try { + i2p::util::filesystem::InstallFiles(); + LogPrint("Succesfully installed all files."); + } catch(const std::runtime_error& e) { + LogPrint(eLogError, "Failed to install: ", e.what()); + return false; + } + } isDaemon = i2p::util::config::GetArg("-daemon", 0); isLogging = i2p::util::config::GetArg("-log", 1); @@ -104,7 +116,6 @@ namespace i2p else StartLog (""); // write to stdout } - d.httpServer = new i2p::util::HTTPServer( i2p::util::config::GetArg("-httpaddress", "127.0.0.1"), i2p::util::config::GetArg("-httpport", 7070) diff --git a/client/HTTPServer.cpp b/client/HTTPServer.cpp index cff6256c..6f0bf309 100644 --- a/client/HTTPServer.cpp +++ b/client/HTTPServer.cpp @@ -107,7 +107,9 @@ void HTTPConnection::Send404Reply() "Error: 404 - webui not installed" "

It looks like your webui installation is broken.

" "

Run the following command to (re)install it:

" - "
./i2pd --install /path/to/webui
" + "
./i2pd --install=/path/to/webui
" + "

Or from a directory containing a folder named webui:

" + "
./i2pd --install
" "

The webui folder should come with the binaries.

" "" ); diff --git a/client/i2p.cpp b/client/i2p.cpp index fa277b38..8f683c08 100644 --- a/client/i2p.cpp +++ b/client/i2p.cpp @@ -3,13 +3,13 @@ #include "Daemon.h" #include "Reseed.h" -int main( int argc, char* argv[] ) +int main(int argc, char* argv[]) { - Daemon.init(argc, argv); - if (Daemon.start()) - { - while (Daemon.running) - { + if(!Daemon.init(argc, argv)) + return EXIT_FAILURE; + + if(Daemon.start()) { + while (Daemon.running) { //TODO Meeh: Find something better to do here. std::this_thread::sleep_for (std::chrono::seconds(1)); } diff --git a/core/util/util.cpp b/core/util/util.cpp index 650d32e6..b42d1962 100644 --- a/core/util/util.cpp +++ b/core/util/util.cpp @@ -125,6 +125,11 @@ namespace config { return nDefault; } + bool HasArg(const std::string& strArg) + { + return mapArgs.count(strArg); + } + } namespace filesystem @@ -258,6 +263,46 @@ namespace filesystem { return GetDataDir () / "certificates"; } + + void InstallFiles() + { + namespace bfs = boost::filesystem; + boost::system::error_code e; + const bfs::path source = bfs::canonical( + config::GetArg("-install", "webui"), e + ); + + const bfs::path destination = GetWebuiDataDir(); + + if(e || !bfs::is_directory(source)) + throw std::runtime_error("Given directory is invalid or does not exist"); + + // TODO: check that destination is not in source + + try { + CopyDir(source, destination); + } catch(...) { + throw std::runtime_error("Could not copy webui folder to i2pd folder."); + } + } + + void CopyDir(const boost::filesystem::path& src, const boost::filesystem::path& dest) + { + namespace bfs = boost::filesystem; + + bfs::create_directory(dest); + + for(bfs::directory_iterator file(src); file != bfs::directory_iterator(); ++file) { + const bfs::path current(file->path()); + if(bfs::is_directory(current)) + CopyDir(current, dest / current.filename()); + else + bfs::copy_file( + current, dest / current.filename(), + bfs::copy_option::overwrite_if_exists + ); + } + } } namespace http diff --git a/core/util/util.h b/core/util/util.h index 0fb000ed..b057d047 100644 --- a/core/util/util.h +++ b/core/util/util.h @@ -41,6 +41,11 @@ namespace util * @param nDefault the default value to be returned */ const char* GetCharArg(const std::string& strArg, const std::string& nDefault); + + /** + * @return true if the argument is set, false otherwise + */ + bool HasArg(const std::string& strArg); } namespace filesystem @@ -95,6 +100,18 @@ namespace util * @return the path of the certificates directory */ boost::filesystem::path GetCertificatesDir(); + + /** + * Installs the webui files. + * @throw std::runtime_error when installation fails + */ + void InstallFiles(); + + /** + * Copies all files and directories in src to dest. + * @warning overrides existing files + */ + void CopyDir(const boost::filesystem::path& src, const boost::filesystem::path& dest); } namespace http