diff --git a/.gitignore b/.gitignore index 6525ba45..9b4bcdf2 100644 --- a/.gitignore +++ b/.gitignore @@ -13,8 +13,10 @@ build/CMakeFiles/* build/tests build/client build/core +build/benchmark build/i2pd build/i2pd-tests +build/i2pd-benchmark *.cmake *.a *.o diff --git a/CMakeLists.txt b/CMakeLists.txt index e03b0f9b..0a014a58 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,9 @@ option(WITH_BINARY "Build binary" ON) option(WITH_STATIC "Static build" OFF) option(WITH_UPNP "Include support for UPnP client" OFF) option(WITH_TESTS "Build unit tests" OFF) +option(WITH_BENCHMARK "Build benchmarking code" OFF) +option(WITH_OPTIMIZE "Optimization flags" OFF) +option(I2PD_DATA_PATH "The path to the i2pd data folder") set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/build/cmake_modules") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) @@ -80,6 +83,9 @@ if(WITH_AESNI) add_definitions( "-maes -DAESNI") endif() +if(WITH_OPTIMIZE AND (NOT MSVC)) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") +endif() # Libraries # TODO: once CMake 3.1+ becomes mainstream, see e.g. @@ -93,17 +99,8 @@ endif() if(WITH_STATIC) set(Boost_USE_STATIC_LIBS ON) - set(Boost_USE_STATIC_RUNTIME ON) - if(WIN32) - # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace - # Note that you might need to rebuild Crypto++ - foreach(flag_var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE - CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) - if(${flag_var} MATCHES "/MD") - string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") - endif(${flag_var} MATCHES "/MD") - endforeach(flag_var) - else() + set(Boost_USE_STATIC_RUNTIME OFF) + if(NOT WIN32) set(CMAKE_FIND_LIBRARY_SUFFIXES .a) endif() @@ -120,10 +117,10 @@ elseif(NOT WIN32) # No need in -fPIC overhead for binary if not interested in library # HINT: revert c266cff CMakeLists.txt: compilation speed up set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") +else() # Not a static build + add_definitions(-DBOOST_ALL_DYN_LINK) endif() -add_definitions(-DBOOST_ALL_DYN_LINK) - find_package( Boost COMPONENTS system filesystem regex program_options date_time thread chrono REQUIRED @@ -148,6 +145,18 @@ include_directories( "core/" ) +if(I2PD_DATA_PATH) + set(I2PD_DATA_DIR ${I2PD_DATA_PATH}) + # Using custom path, make sure the code knows about this + add_definitions(-DI2PD_CUSTOM_DATA_PATH="${I2PD_DATA_PATH}") +elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows") + set(I2PD_DATA_DIR "$ENV{APPDATA}\\i2pd") +elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set(I2PD_DATA_DIR "$ENV{HOME}/Library/Application Support/i2pd") +else() + set(I2PD_DATA_DIR "$ENV{HOME}/.i2pd") +endif() + # Show summary message(STATUS "---------------------------------------") message(STATUS "Build type : ${CMAKE_BUILD_TYPE}") @@ -155,6 +164,7 @@ message(STATUS "Compiler vendor : ${CMAKE_CXX_COMPILER_ID}") message(STATUS "Compiler version : ${CMAKE_CXX_COMPILER_VERSION}") message(STATUS "Compiler path : ${CMAKE_CXX_COMPILER}") message(STATUS "Install prefix: : ${CMAKE_INSTALL_PREFIX}") +message(STATUS "I2PD data directory: ${I2PD_DATA_DIR}") message(STATUS "Options:") message(STATUS " AESNI : ${WITH_AESNI}") message(STATUS " HARDENING : ${WITH_HARDENING}") @@ -163,6 +173,8 @@ message(STATUS " BINARY : ${WITH_BINARY}") message(STATUS " STATIC BUILD : ${WITH_STATIC}") message(STATUS " UPnP : ${WITH_UPNP}") message(STATUS " TESTS : ${WITH_TESTS}") +message(STATUS " BENCHMARKING : ${WITH_BENCHMARK}") +message(STATUS " OPTIMIZATION : ${WITH_OPTIMIZE}") message(STATUS "---------------------------------------") # Handle paths nicely @@ -171,6 +183,13 @@ include(GNUInstallDirs) set(CORE_NAME "${PROJECT_NAME}-core") set(CLIENT_NAME "${PROJECT_NAME}-client") set(TESTS_NAME "${PROJECT_NAME}-tests") +set(BENCHMARK_NAME "${PROJECT_NAME}-benchmark") add_subdirectory(core) add_subdirectory(client) add_subdirectory(tests) +add_subdirectory(benchmark) + +if(WITH_BINARY) + file(MAKE_DIRECTORY "${I2PD_DATA_DIR}/webui") + file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/webui" DESTINATION "${I2PD_DATA_DIR}") +endif() diff --git a/README.md b/README.md index a9805e98..4ccd289f 100644 --- a/README.md +++ b/README.md @@ -15,13 +15,7 @@ http://download.i2p.io/purplei2p/i2pd/releases/ Build Statuses --------------- -- Linux x64 - Maintaince -- Linux ARM - Maintaince -- Mac OS X - Maintaince +- Linux x64 - Maintenance +- Linux ARM - Maintenance +- Mac OS X - Maintenance - Microsoft VC13 - To be added - - -License -------- - -This project is licensed under the BSD 3-clause license, which can be found in the doc directory. diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt new file mode 100644 index 00000000..25d4b12c --- /dev/null +++ b/benchmark/CMakeLists.txt @@ -0,0 +1,14 @@ +set(BENCHMARK_SRC + "main.cpp" +) + +if(WITH_BENCHMARK) + add_executable(${BENCHMARK_NAME} ${BENCHMARK_SRC}) + target_link_libraries( + ${BENCHMARK_NAME} ${CORE_NAME} ${DL_LIB} ${Boost_LIBRARIES} ${CRYPTO++_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + ) + install(TARGETS + ${BENCHMARK_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ) +endif() diff --git a/benchmark/main.cpp b/benchmark/main.cpp new file mode 100644 index 00000000..387c505d --- /dev/null +++ b/benchmark/main.cpp @@ -0,0 +1,83 @@ +#include "crypto/Signature.h" +#include +#include +#include +#include + +typedef std::function KeyGenerator; + +template +void benchmark(std::size_t count, std::size_t public_key_size, std::size_t private_key_size, + std::size_t signature_size, KeyGenerator generator) +{ + typedef std::chrono::time_point TimePoint; + CryptoPP::AutoSeededRandomPool rng; + + uint8_t private_key[private_key_size] = {}; + uint8_t public_key[public_key_size] = {}; + + generator(rng, private_key, public_key); + + Verifier verifier(public_key); + Signer signer(private_key); + + uint8_t message[512] = {}; + uint8_t output[signature_size] = {}; + + std::chrono::nanoseconds sign_duration(0); + std::chrono::nanoseconds verify_duration(0); + + for(std::size_t i = 0; i < count; ++i) { + rng.GenerateBlock(message, 512); + TimePoint begin1 = std::chrono::high_resolution_clock::now(); + signer.Sign(rng, message, 512, output); + TimePoint end1 = std::chrono::high_resolution_clock::now(); + + sign_duration += std::chrono::duration_cast(end1 - begin1); + + TimePoint begin2 = std::chrono::high_resolution_clock::now(); + verifier.Verify(message, 512, output); + TimePoint end2 = std::chrono::high_resolution_clock::now(); + + verify_duration += std::chrono::duration_cast(end2 - begin2); + } + std::cout << "Conducted " << count << " experiments." << std::endl; + std::cout << "Total sign time: " << std::chrono::duration_cast(sign_duration).count() << std::endl; + std::cout << "Total verify time: " << std::chrono::duration_cast(verify_duration).count() << std::endl; +} + + +int main() +{ + using namespace i2p::crypto; + std::cout << "--------DSA---------" << std::endl; + benchmark( + 1000, DSA_PUBLIC_KEY_LENGTH, + DSA_PRIVATE_KEY_LENGTH, DSA_SIGNATURE_LENGTH, + &CreateDSARandomKeys + ); + std::cout << "-----ECDSAP256------" << std::endl; + benchmark( + 1000, ECDSAP256_KEY_LENGTH, + ECDSAP256_KEY_LENGTH, 64, + &CreateECDSAP256RandomKeys + ); + std::cout << "-----ECDSAP384------" << std::endl; + benchmark( + 1000, ECDSAP384_KEY_LENGTH, + ECDSAP384_KEY_LENGTH, 64, + &CreateECDSAP384RandomKeys + ); + std::cout << "-----ECDSAP521------" << std::endl; + benchmark( + 1000, ECDSAP521_KEY_LENGTH, + ECDSAP521_KEY_LENGTH, 64, + &CreateECDSAP521RandomKeys + ); + std::cout << "-----EDDSA25519-----" << std::endl; + benchmark( + 1000, EDDSA25519_PUBLIC_KEY_LENGTH, + EDDSA25519_PRIVATE_KEY_LENGTH, 64, + &CreateEDDSARandomKeys + ); +} diff --git a/client/ClientContext.cpp b/client/ClientContext.cpp index 2cc46ae6..442b5929 100644 --- a/client/ClientContext.cpp +++ b/client/ClientContext.cpp @@ -114,10 +114,12 @@ namespace client // I2P Control int i2pcontrolPort = i2p::util::config::GetArg("-i2pcontrolport", 0); if(i2pcontrolPort) { - m_I2PControlService = new I2PControlService( + m_I2PControlService = new i2pcontrol::I2PControlService( i2p::util::config::GetArg("-i2pcontroladdress", "127.0.0.1"), i2pcontrolPort, - i2p::util::config::GetArg("-i2pcontrolpassword", I2P_CONTROL_DEFAULT_PASSWORD) + i2p::util::config::GetArg( + "-i2pcontrolpassword", i2pcontrol::constants::DEFAULT_PASSWORD + ) ); m_I2PControlService->Start(); LogPrint("I2PControl started"); diff --git a/client/ClientContext.h b/client/ClientContext.h index 33d1960f..d552fa8a 100644 --- a/client/ClientContext.h +++ b/client/ClientContext.h @@ -72,7 +72,7 @@ namespace client std::map > m_ServerTunnels; // destination->tunnel SAMBridge * m_SamBridge; BOBCommandChannel * m_BOBCommandChannel; - I2PControlService * m_I2PControlService; + i2pcontrol::I2PControlService * m_I2PControlService; public: // for HTTP 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/DaemonWin32.cpp b/client/DaemonWin32.cpp index 3f5d5b00..e260cbfa 100644 --- a/client/DaemonWin32.cpp +++ b/client/DaemonWin32.cpp @@ -4,7 +4,7 @@ #ifdef _WIN32 -#include "./Win32/Win32Service.h" +#include "Win32Service.h" namespace i2p { diff --git a/client/HTTPServer.cpp b/client/HTTPServer.cpp index 617be9b5..994f9b13 100644 --- a/client/HTTPServer.cpp +++ b/client/HTTPServer.cpp @@ -1,994 +1,264 @@ #include -#include -#include #include -#include "util/base64.h" +#include #include "util/Log.h" -#include "tunnel/Tunnel.h" -#include "tunnel/TransitTunnel.h" -#include "transport/Transports.h" -#include "NetworkDatabase.h" +#include "util/util.h" #include "util/I2PEndian.h" -#include "Streaming.h" -#include "Destination.h" -#include "RouterContext.h" -#include "ClientContext.h" #include "HTTPServer.h" -// For image and info -#include "version.h" +namespace i2p { +namespace util { -namespace i2p +HTTPConnection::HTTPConnection(boost::asio::ip::tcp::socket* socket, + std::shared_ptr session) + : m_Socket(socket), m_BufferLen(0), m_Request(), m_Reply(), m_Session(session) { -namespace util -{ - - const std::string HTTPConnection::itoopieImage = - "\"ICToopie"; - - const std::string HTTPConnection::itoopieFavicon = - "data:image/vnd.microsoft.icon;base64," - "AAABAAEAQEAAAAEAIAAoQgAAFgAAACgAAABAAAAAgAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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ACAt/QC+//8At/v/ALP1/gAAAP95eXns/////2FhYeoBAQH/AAAA/gEBAf" - "9eXl7lDQ0N9gAgLP0Av///ALf7/wCNwf4AAAD/NDQ03AAAAP8AAAD+AAAA/6Ojo+b/////eXl55AAAA" - "P8AtPf+ALf7/wC3+/8AoNv9AAAA/gAAAP4AAACHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/AAAA/gAAAP0Ax///ALf7/wCIu/0AAAD/ysrK6/" - "/////8/Pz6Hx8f6wAAAP8kJCTm/f39/WVlZe4ABAX8AMT//wC3/P8AV3X9ERER7/////9MTEzrAAAA6" - "ElJSeb///////////X19e8AAAD/AHuo/QC3+/8At/v/AIW2/gAAAP4AAAD/AAAAZAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0wAAAP4AAAD+AK" - "fl/gC3+/8AjcH+AAAA/83Nze/////////////////////w//////////9gYGDsAA8V/QDC//8Auv//A" - "EJa/UFBQeb////////////////////////////////////vAAAA/wBvlfwAt/v/ALf7/wBdff0AAAD+" - "AAAA/wAAAC0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAI4AAAD+AAAA/gBnjP0At/v/ALz//gAAAP+CgoLx/////////////////////////////" - "///CAkJ8QBCXP4Auf7/ALj8/wBScf0DAwPw////////////////////////////////tra26AAAAP8A" - "hrf+ALf7/wDA//8AHCf9AAAA/gAAAP8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA3AAAA/wAAAP4AEBb9AMT//wC6//8AQVn+AAAA9+Pj4" - "/L/////////////////////cnJy1gAAAP8AlMr9ALf7/wC3+/8AhLT+AAAA/5mZmeL/////////////" - "/////////////R0dHesAAAD9AL///gC3+/8ApuX+AAAA/gAAAP4AAAC1AAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPMAAAD+AAAA/" - "gCGtv0Atvr/ALn+/wARF/0AAAD3cHBw7bGxseqioqLuOjo67QAAAP8ARFz9ALz//wC3+/8At/v/AML/" - "/wAbJf0AAAD/jo6O5v////b//////f397zw8PPEAAAD/AGCC/QC4/P8Auv//AEJb/QAAAP4AAAD/AAA" - "AUQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAABpAAAA/wAAAP4AERf9AMP//wC3+/8Auv/+AEVf/AAAAP8AAAD/AAAA/wAAAP0AWnn9" - "AMH//wC3+/8At/v/ALf7/wC3+/8AtPf+ABsm/QAAAP8AAADwDg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AAAAAAKAAAAIgAAADRAAAA/wAAA" - "P8AAAD/AAAA/wAAAP8AAAD/AAAA5QAAAJoAAABWAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAHAAAAIAAAACQAAAAUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA///gAf///////4AAAAD/////wAAAAP////" - "/AAAAB/////+AAAAH/////4AAAA//////gAAAD/////8AAAAH/////wAAAAP////+AAAAA/////4AAA" - "AD/////gAAAAP////+AAAAA/////4AAAAD/////gAAAAP////+AAAAA/////4AAAAB/////gAAAAD//" - "//+AAAAAP////wAAAAA////+AAAAAD////wAAAAAP///8AAAAAA////gAAAAAB///8AAAAAAH///gAA" - "AAAAf//+AAAAAAA///4AAAAAAD///4AAAAAAP///wAAAAAAf///wAAAAAD////gAAAAAP////gAAAAB" - "/////AAAAAP////+AAAAD/////wAAAAf////+AAAAB/////4AAAAD/////AAAAAP////8AAAAA/////" - "wAAAAB////+AAAAAH////4AAAAAf////gAAAAA////+AAAAAD////4AAAAAP////gAAAAA////+AAAA" - "AD////4AAAAAf////gAAAAB////+AAAAAH////8AAAAAf////wAAAAD/////gAAAAP////+AAAAB///" - "//8AAAAH/////wAAAA//////gAAAH//////AAAA//////+AAAH//////+AAA///////+AAP///////+" - "AH//////////////8="; - - const char HTTP_COMMAND_TUNNELS[] = "tunnels"; - const char HTTP_COMMAND_TRANSIT_TUNNELS[] = "transit_tunnels"; - const char HTTP_COMMAND_TRANSPORTS[] = "transports"; - const char HTTP_COMMAND_START_ACCEPTING_TUNNELS[] = "start_accepting_tunnels"; - const char HTTP_COMMAND_STOP_ACCEPTING_TUNNELS[] = "stop_accepting_tunnels"; - const char HTTP_COMMAND_LOCAL_DESTINATIONS[] = "local_destinations"; - const char HTTP_COMMAND_LOCAL_DESTINATION[] = "local_destination"; - const char HTTP_PARAM_BASE32_ADDRESS[] = "b32"; - const char HTTP_COMMAND_SAM_SESSIONS[] = "sam_sessions"; - const char HTTP_COMMAND_SAM_SESSION[] = "sam_session"; - const char HTTP_PARAM_SAM_SESSION_ID[] = "id"; - namespace misc_strings - { - - const char name_value_separator[] = { ':', ' ' }; - const char crlf[] = { '\r', '\n' }; - - } // namespace misc_strings - - std::vector HTTPConnection::reply::to_buffers(int status) - { - std::vector buffers; - if (headers.size () > 0) - { - buffers.push_back(boost::asio::buffer("HTTP/1.1 ", 9)); - buffers.push_back(boost::asio::buffer(boost::lexical_cast(status), 3)); - buffers.push_back(boost::asio::buffer(" ", 1)); - std::string status_string; - switch (status) - { - case 105: status_string = "Name Not Resolved"; break; - case 200: status_string = "OK"; break; - case 400: status_string = "Bad Request"; break; - case 404: status_string = "Not Found"; break; - case 408: status_string = "Request Timeout"; break; - case 500: status_string = "Internal Server Error"; break; - case 502: status_string = "Bad Gateway"; break; - case 503: status_string = "Not Implemented"; break; - case 504: status_string = "Gateway Timeout"; break; - } - buffers.push_back(boost::asio::buffer(status_string, status_string.size())); - buffers.push_back(boost::asio::buffer(misc_strings::crlf)); - for (std::size_t i = 0; i < headers.size(); ++i) - { - header& h = headers[i]; - buffers.push_back(boost::asio::buffer(h.name)); - buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator)); - buffers.push_back(boost::asio::buffer(h.value)); - buffers.push_back(boost::asio::buffer(misc_strings::crlf)); - } - buffers.push_back(boost::asio::buffer(misc_strings::crlf)); - } - buffers.push_back(boost::asio::buffer(content)); - return buffers; - } +} - void HTTPConnection::Terminate () - { - m_Socket->close (); - } +void HTTPConnection::Terminate() +{ + m_Socket->close(); +} - void HTTPConnection::Receive () - { - m_Socket->async_read_some (boost::asio::buffer (m_Buffer, HTTP_CONNECTION_BUFFER_SIZE), - std::bind(&HTTPConnection::HandleReceive, shared_from_this (), - std::placeholders::_1, std::placeholders::_2)); - } +void HTTPConnection::Receive() +{ + m_Socket->async_read_some( + boost::asio::buffer(m_Buffer, HTTP_CONNECTION_BUFFER_SIZE), std::bind( + &HTTPConnection::HandleReceive, shared_from_this(), + std::placeholders::_1, std::placeholders::_2 + ) + ); +} - void HTTPConnection::HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred) - { - if(!ecode) { - m_Buffer[bytes_transferred] = 0; - m_BufferLen = bytes_transferred; +void HTTPConnection::HandleReceive(const boost::system::error_code& e, std::size_t nb_bytes) +{ + if(!e) { + m_Buffer[nb_bytes] = 0; + m_BufferLen = nb_bytes; + const std::string data = std::string(m_Buffer, m_Buffer + m_BufferLen); + if(!m_Request.hasData()) // New request + m_Request = i2p::util::http::Request(data); + else + m_Request.update(data); + + if(m_Request.isComplete()) { RunRequest(); + m_Request.clear(); + } else { + Receive(); } - else if (ecode != boost::asio::error::operation_aborted) - Terminate (); - } - - void HTTPConnection::RunRequest () - { - auto address = ExtractAddress (); - if (address.length () > 1 && address[1] != '?') // not just '/' or '/?' - return; // TODO: error handling + } else if(e != boost::asio::error::operation_aborted) + Terminate(); +} - HandleRequest (address); +void HTTPConnection::RunRequest() +{ + try { + if(m_Request.getMethod() == "GET") + return HandleRequest(); + if(m_Request.getHeader("Content-Type").find("application/json") != std::string::npos) + return HandleI2PControlRequest(); + } catch(...) { + // Ignore the error for now, probably Content-Type doesn't exist + // Could also be invalid json data } + // Unsupported method + m_Reply = i2p::util::http::Response(502, ""); + SendReply(); +} - std::string HTTPConnection::ExtractAddress () +void HTTPConnection::ExtractParams(const std::string& str, std::map& params) +{ + if(str[0] != '&') return; + size_t pos = 1, end; + do { - char * get = strstr (m_Buffer, "GET"); - if (get) - { - char * http = strstr (get, "HTTP"); - if (http) - return std::string (get + 4, http - get - 5); - } - return ""; - } + end = str.find('&', pos); + std::string param = str.substr(pos, end - pos); + LogPrint(param); + size_t e = param.find('='); + if(e != std::string::npos) + params[param.substr(0, e)] = param.substr(e+1); + pos = end + 1; + } + while(end != std::string::npos); +} - void HTTPConnection::ExtractParams (const std::string& str, std::map& params) - { - if (str[0] != '&') return; - size_t pos = 1, end; - do - { - end = str.find ('&', pos); - std::string param = str.substr (pos, end - pos); - LogPrint (param); - size_t e = param.find ('='); - if (e != std::string::npos) - params[param.substr(0, e)] = param.substr(e+1); - pos = end + 1; - } - while (end != std::string::npos); - } - - void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode) - { - if (ecode != boost::asio::error::operation_aborted) - { - boost::system::error_code ignored_ec; - m_Socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec); - Terminate (); - } +void HTTPConnection::HandleWriteReply(const boost::system::error_code& e) +{ + if(e != boost::asio::error::operation_aborted) { + boost::system::error_code ignored_ec; + m_Socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec); + Terminate(); } +} - void HTTPConnection::HandleRequest (const std::string& address) - { - std::stringstream s; - // Html5 head start - s << "\n"; // TODO: Add support for locale. - s << ""; // TODO: Find something to parse html/template system. This is horrible. - s << "Purple I2P " << VERSION " Webconsole"; - // Head end - if (address.length () > 1) - HandleCommand (address.substr (2), s); - else - FillContent (s); - s << ""; - SendReply (s.str ()); +void HTTPConnection::Send404Reply() +{ + try { + const std::string error_page = "404.html"; + m_Reply = i2p::util::http::Response(404, GetFileContents(error_page, true)); + m_Reply.setHeader("Content-Type", i2p::util::http::getMimeType(error_page)); + } catch(const std::runtime_error&) { + // Failed to load 404.html, assume the webui is incorrectly installed + m_Reply = i2p::util::http::Response(404, + "" + "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
" + "

Or from a directory containing a folder named webui:

" + "
./i2pd --install
" + "

The webui folder should come with the binaries.

" + "" + ); } + SendReply(); +} - void HTTPConnection::FillContent (std::stringstream& s) - { - s << "

Welcome to the Webconsole!


"; - s << "Uptime: " << boost::posix_time::to_simple_string ( - boost::posix_time::time_duration (boost::posix_time::seconds ( - i2p::context.GetUptime ()))) << "
"; - s << "Status: "; - switch (i2p::context.GetStatus ()) - { - case eRouterStatusOK: s << "OK"; break; - case eRouterStatusTesting: s << "Testing"; break; - case eRouterStatusFirewalled: s << "Firewalled"; break; - default: s << "Unknown"; - } - s << "
"; - s << "Tunnel creation success rate: " << i2p::tunnel::tunnels.GetTunnelCreationSuccessRate () << "%
"; - s << "Received: " << i2p::transport::transports.GetTotalReceivedBytes ()/1000 << "K"; - s << " (" << i2p::transport::transports.GetInBandwidth () <<" Bps)
"; - s << "Sent: " << i2p::transport::transports.GetTotalSentBytes ()/1000 << "K"; - s << " (" << i2p::transport::transports.GetOutBandwidth () <<" Bps)
"; - s << "Data path: " << i2p::util::filesystem::GetDataDir().string() << "

"; - s << "Our external address:" << "
" ; - for (auto& address : i2p::context.GetRouterInfo().GetAddresses()) - { - switch (address.transportStyle) - { - case i2p::data::RouterInfo::eTransportNTCP: - if (address.host.is_v6 ()) - s << "NTCP6  "; - else - s << "NTCP  "; - break; - case i2p::data::RouterInfo::eTransportSSU: - if (address.host.is_v6 ()) - s << "SSU6     "; - else - s << "SSU     "; - break; - default: - s << "Unknown  "; - } - s << address.host.to_string() << ":" << address.port << "
"; - } - s << "
Routers: " << i2p::data::netdb.GetNumRouters () << " "; - s << "Floodfills: " << i2p::data::netdb.GetNumFloodfills () << " "; - s << "LeaseSets: " << i2p::data::netdb.GetNumLeaseSets () << "
"; - - s << "
Local destinations"; - s << "
Tunnels"; - s << "
Transit tunnels"; - s << "
Transports"; - if (i2p::client::context.GetSAMBridge ()) - s << "
SAM sessions"; - s << "
"; - - if (i2p::context.AcceptsTunnels ()) - s << "
Stop accepting tunnels
"; - else - s << "
Start accepting tunnels
"; - } +std::string HTTPConnection::GetFileContents(const std::string& filename, bool preprocess) const +{ + boost::system::error_code e; - void HTTPConnection::HandleCommand (const std::string& command, std::stringstream& s) - { - size_t paramsPos = command.find('&'); - std::string cmd = command.substr (0, paramsPos); - if (cmd == HTTP_COMMAND_TRANSPORTS) - ShowTransports (s); - else if (cmd == HTTP_COMMAND_TUNNELS) - ShowTunnels (s); - else if (cmd == HTTP_COMMAND_TRANSIT_TUNNELS) - ShowTransitTunnels (s); - else if (cmd == HTTP_COMMAND_START_ACCEPTING_TUNNELS) - StartAcceptingTunnels (s); - else if (cmd == HTTP_COMMAND_STOP_ACCEPTING_TUNNELS) - StopAcceptingTunnels (s); - else if (cmd == HTTP_COMMAND_LOCAL_DESTINATIONS) - ShowLocalDestinations (s); - else if (cmd == HTTP_COMMAND_LOCAL_DESTINATION) - { - std::map params; - ExtractParams (command.substr (paramsPos), params); - auto b32 = params[HTTP_PARAM_BASE32_ADDRESS]; - ShowLocalDestination (b32, s); - } - else if (cmd == HTTP_COMMAND_SAM_SESSIONS) - ShowSAMSessions (s); - else if (cmd == HTTP_COMMAND_SAM_SESSION) - { - std::map params; - ExtractParams (command.substr (paramsPos), params); - auto id = params[HTTP_PARAM_SAM_SESSION_ID]; - ShowSAMSession (id, s); - } - } + // Use canonical to avoid .. or . in path + const boost::filesystem::path address = boost::filesystem::canonical( + i2p::util::filesystem::GetWebuiDataDir() / filename, e + ); - void HTTPConnection::ShowTransports (std::stringstream& s) - { - auto ntcpServer = i2p::transport::transports.GetNTCPServer (); - if (ntcpServer) - { - s << "NTCP
"; - for (auto it: ntcpServer->GetNTCPSessions ()) - { - if (it.second && it.second->IsEstablished ()) - { - // incoming connection doesn't have remote RI - auto outgoing = it.second->GetRemoteRouter (); - if (outgoing) s << "-->"; - s << it.second->GetRemoteIdentity ().GetIdentHash ().ToBase64 ().substr (0, 4) << ": " - << it.second->GetSocket ().remote_endpoint().address ().to_string (); - if (!outgoing) s << "-->"; - s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]"; - s << "
"; - } - s << std::endl; - } - } - auto ssuServer = i2p::transport::transports.GetSSUServer (); - if (ssuServer) - { - s << "
SSU
"; - for (auto it: ssuServer->GetSessions ()) - { - // incoming connections don't have remote router - auto outgoing = it.second->GetRemoteRouter (); - auto endpoint = it.second->GetRemoteEndpoint (); - if (outgoing) s << "-->"; - s << endpoint.address ().to_string () << ":" << endpoint.port (); - if (!outgoing) s << "-->"; - s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]"; - if (it.second->GetRelayTag ()) - s << " [itag:" << it.second->GetRelayTag () << "]"; - s << "
"; - s << std::endl; - } - } - } + const std::string address_str = address.string(); - void HTTPConnection::ShowTunnels (std::stringstream& s) - { - s << "Queue size:" << i2p::tunnel::tunnels.GetQueueSize () << "
"; - - for (auto it: i2p::tunnel::tunnels.GetOutboundTunnels ()) - { - it->GetTunnelConfig ()->Print (s); - auto state = it->GetState (); - if (state == i2p::tunnel::eTunnelStateFailed) - s << " " << "Failed"; - else if (state == i2p::tunnel::eTunnelStateExpiring) - s << " " << "Exp"; - s << " " << (int)it->GetNumSentBytes () << "
"; - s << std::endl; - } + std::ifstream ifs(address_str, std::ios_base::in | std::ios_base::binary); + if(e || !ifs || !isAllowed(address_str)) + throw std::runtime_error("Cannot load " + address_str + "."); + + std::string str; + ifs.seekg(0, ifs.end); + str.resize(ifs.tellg()); + ifs.seekg(0, ifs.beg); + ifs.read(&str[0], str.size()); + ifs.close(); + + if(preprocess) + return i2p::util::http::preprocessContent(str, address.parent_path().string()); + else + return str; +} - for (auto it: i2p::tunnel::tunnels.GetInboundTunnels ()) - { - it.second->GetTunnelConfig ()->Print (s); - auto state = it.second->GetState (); - if (state == i2p::tunnel::eTunnelStateFailed) - s << " " << "Failed"; - else if (state == i2p::tunnel::eTunnelStateExpiring) - s << " " << "Exp"; - s << " " << (int)it.second->GetNumReceivedBytes () << "
"; - s << std::endl; - } - } +void HTTPConnection::HandleRequest() +{ - void HTTPConnection::ShowTransitTunnels (std::stringstream& s) - { - for (auto it: i2p::tunnel::tunnels.GetTransitTunnels ()) - { - if (dynamic_cast(it.second)) - s << it.second->GetTunnelID () << "-->"; - else if (dynamic_cast(it.second)) - s << "-->" << it.second->GetTunnelID (); - else - s << "-->" << it.second->GetTunnelID () << "-->"; - s << " " << it.second->GetNumTransmittedBytes () << "
"; - } + std::string uri = m_Request.getUri(); + if(uri == "/") + uri = "index.html"; + + try { + m_Reply = i2p::util::http::Response(200, GetFileContents(uri, true)); + m_Reply.setHeader("Content-Type", i2p::util::http::getMimeType(uri) + "; charset=UTF-8"); + SendReply(); + } catch(const std::runtime_error&) { + // Cannot open the file for some reason, send 404 + Send404Reply(); } +} - void HTTPConnection::ShowLocalDestinations (std::stringstream& s) - { - for (auto& it: i2p::client::context.GetDestinations ()) - { - auto ident = it.second->GetIdentHash ();; - s << ""; - s << i2p::client::context.GetAddressBook ().ToAddress(ident) << "
" << std::endl; - } - } - - void HTTPConnection::ShowLocalDestination (const std::string& b32, std::stringstream& s) - { - i2p::data::IdentHash ident; - ident.FromBase32 (b32); - auto dest = i2p::client::context.FindLocalDestination (ident); - if (dest) - { - s << "Base64:
" << dest->GetIdentity ().ToBase64 () << "

"; - s << "LeaseSets: " << dest->GetNumRemoteLeaseSets () << "
"; - auto pool = dest->GetTunnelPool (); - if (pool) - { - s << "Tunnels:
"; - for (auto it: pool->GetOutboundTunnels ()) - { - it->GetTunnelConfig ()->Print (s); - auto state = it->GetState (); - if (state == i2p::tunnel::eTunnelStateFailed) - s << " " << "Failed"; - else if (state == i2p::tunnel::eTunnelStateExpiring) - s << " " << "Exp"; - s << "
" << std::endl; - } - for (auto it: pool->GetInboundTunnels ()) - { - it->GetTunnelConfig ()->Print (s); - auto state = it->GetState (); - if (state == i2p::tunnel::eTunnelStateFailed) - s << " " << "Failed"; - else if (state == i2p::tunnel::eTunnelStateExpiring) - s << " " << "Exp"; - s << "
" << std::endl; - } - } - s << "
Streams:
"; - for (auto it: dest->GetStreamingDestination ()->GetStreams ()) - { - s << it.first << "->" << i2p::client::context.GetAddressBook ().ToAddress(it.second->GetRemoteIdentity ()) << " "; - s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]"; - s << " [out:" << it.second->GetSendQueueSize () << "][in:" << it.second->GetReceiveQueueSize () << "]"; - s << "[buf:" << it.second->GetSendBufferSize () << "]"; - s << "[RTT:" << it.second->GetRTT () << "]"; - s << "[Window:" << it.second->GetWindowSize () << "]"; - s << "[Status:" << (int)it.second->GetStatus () << "]"; - s << "
"<< std::endl; - } - } - } - - void HTTPConnection::ShowSAMSessions (std::stringstream& s) - { - auto sam = i2p::client::context.GetSAMBridge (); - if (sam) - { - for (auto& it: sam->GetSessions ()) - { - s << ""; - s << it.first << "
" << std::endl; - } - } - } +void HTTPConnection::HandleI2PControlRequest() +{ + std::stringstream ss(m_Request.getContent()); + const client::i2pcontrol::I2PControlSession::Response rsp = m_Session->handleRequest(ss); + m_Reply = i2p::util::http::Response(200, rsp.toJsonString()); + m_Reply.setHeader("Content-Type", "application/json"); + SendReply(); +} - void HTTPConnection::ShowSAMSession (const std::string& id, std::stringstream& s) - { - auto sam = i2p::client::context.GetSAMBridge (); - if (sam) - { - auto session = sam->FindSession (id); - if (session) - { - auto& ident = session->localDestination->GetIdentHash(); - s << ""; - s << i2p::client::context.GetAddressBook ().ToAddress(ident) << "
" << std::endl; - s << "Streams:
"; - for (auto it: session->sockets) - { - switch (it->GetSocketType ()) - { - case i2p::client::eSAMSocketTypeSession: - s << "session"; - break; - case i2p::client::eSAMSocketTypeStream: - s << "stream"; - break; - case i2p::client::eSAMSocketTypeAcceptor: - s << "acceptor"; - break; - default: - s << "unknown"; - } - s << " [" << it->GetSocket ().remote_endpoint() << "]"; - s << "
" << std::endl; - } - } - } - } - - void HTTPConnection::StartAcceptingTunnels (std::stringstream& s) - { - i2p::context.SetAcceptsTunnels (true); - s << "Accepting tunnels started" << std::endl; - } +bool HTTPConnection::isAllowed(const std::string& address) const +{ + const std::size_t pos_dot = address.find_last_of('.'); + const std::size_t pos_slash = address.find_last_of('/'); + if(pos_dot == std::string::npos || pos_dot == address.size() - 1) + return false; + if(pos_slash != std::string::npos && pos_dot < pos_slash) + return false; + return true; +} - void HTTPConnection::StopAcceptingTunnels (std::stringstream& s) - { - i2p::context.SetAcceptsTunnels (false); - s << "Accepting tunnels stopped" << std::endl; +void HTTPConnection::SendReply() +{ + // we need the date header to be compliant with HTTP 1.1 + std::time_t time_now = std::time(nullptr); + char time_buff[128]; + if(std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now)) ) { + m_Reply.setHeader("Date", std::string(time_buff)); + m_Reply.setContentLength(); } + boost::asio::async_write( + *m_Socket, boost::asio::buffer(m_Reply.toString()), + std::bind(&HTTPConnection::HandleWriteReply, shared_from_this(), std::placeholders::_1) + ); +} - void HTTPConnection::SendReply (const std::string& content, int status) - { - m_Reply.content = content; - m_Reply.headers.resize(3); - // we need the date header to be compliant with HTTP 1.1 - std::time_t time_now = std::time(nullptr); - char time_buff[128]; - if ( std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now)) ) { - m_Reply.headers[0].name = "Date"; - m_Reply.headers[0].value = std::string(time_buff); - m_Reply.headers[1].name = "Content-Length"; - m_Reply.headers[1].value = boost::lexical_cast(m_Reply.content.size()); - m_Reply.headers[2].name = "Content-Type"; - m_Reply.headers[2].value = "text/html"; - } - boost::asio::async_write (*m_Socket, m_Reply.to_buffers(status), - std::bind (&HTTPConnection::HandleWriteReply, shared_from_this (), std::placeholders::_1)); - } +HTTPServer::HTTPServer(const std::string& address, int port): + m_Thread(nullptr), m_Work(m_Service), + m_Acceptor(m_Service, boost::asio::ip::tcp::endpoint( + boost::asio::ip::address::from_string(address), port) + ), + m_NewSocket(nullptr), + m_Session(std::make_shared(m_Service)) +{ - HTTPServer::HTTPServer (const std::string& address, int port): - m_Thread (nullptr), m_Work (m_Service), - m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port)), - m_NewSocket (nullptr) - { +} - } +HTTPServer::~HTTPServer() +{ + Stop(); +} - HTTPServer::~HTTPServer () - { - Stop (); - } +void HTTPServer::Start() +{ + m_Thread = new std::thread(std::bind(&HTTPServer::Run, this)); + m_Acceptor.listen(); + m_Session->start(); + Accept(); +} - void HTTPServer::Start () +void HTTPServer::Stop() +{ + m_Session->stop(); + m_Acceptor.close(); + m_Service.stop(); + if(m_Thread) { - m_Thread = new std::thread (std::bind (&HTTPServer::Run, this)); - m_Acceptor.listen (); - Accept (); + m_Thread->join(); + delete m_Thread; + m_Thread = nullptr; } +} - void HTTPServer::Stop () - { - m_Acceptor.close(); - m_Service.stop (); - if (m_Thread) - { - m_Thread->join (); - delete m_Thread; - m_Thread = nullptr; - } - } +void HTTPServer::Run() +{ + m_Service.run(); +} - void HTTPServer::Run () - { - m_Service.run (); - } +void HTTPServer::Accept() +{ + m_NewSocket = new boost::asio::ip::tcp::socket(m_Service); + m_Acceptor.async_accept(*m_NewSocket, boost::bind(&HTTPServer::HandleAccept, this, + boost::asio::placeholders::error)); +} - void HTTPServer::Accept () - { - m_NewSocket = new boost::asio::ip::tcp::socket (m_Service); - m_Acceptor.async_accept (*m_NewSocket, boost::bind (&HTTPServer::HandleAccept, this, - boost::asio::placeholders::error)); +void HTTPServer::HandleAccept(const boost::system::error_code& ecode) +{ + if(!ecode) { + CreateConnection(m_NewSocket); + Accept(); } +} - void HTTPServer::HandleAccept(const boost::system::error_code& ecode) - { - if (!ecode) - { - CreateConnection(m_NewSocket); - Accept (); - } - } +void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket* m_NewSocket) +{ + auto conn = std::make_shared(m_NewSocket, m_Session); + conn->Receive(); +} - void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket) - { - auto conn = std::make_shared (m_NewSocket); - conn->Receive (); - } } } - diff --git a/client/HTTPServer.h b/client/HTTPServer.h index dcb74b3e..a4209e2d 100644 --- a/client/HTTPServer.h +++ b/client/HTTPServer.h @@ -6,119 +6,80 @@ #include #include #include +#include "i2pcontrol/I2PControl.h" +#include "util/HTTP.h" -namespace i2p -{ -namespace util -{ - const size_t HTTP_CONNECTION_BUFFER_SIZE = 8192; - const int HTTP_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds - class HTTPConnection: public std::enable_shared_from_this - { - protected: - - struct header - { - std::string name; - std::string value; - }; - - struct request - { - std::string method; - std::string uri; - std::string host; - int port; - int http_version_major; - int http_version_minor; - std::vector
headers; - }; - - struct reply - { - std::vector
headers; - std::string content; - - std::vector to_buffers (int status); - }; - - public: - - HTTPConnection (boost::asio::ip::tcp::socket * socket): - m_Socket (socket), m_Timer (socket->get_io_service ()), - m_BufferLen (0) {}; - ~HTTPConnection() { delete m_Socket; } - void Receive (); - - private: - - void Terminate (); - void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); - void HandleWriteReply(const boost::system::error_code& ecode); - void SendReply (const std::string& content, int status = 200); - - void HandleRequest (const std::string& address); - void HandleCommand (const std::string& command, std::stringstream& s); - void ShowTransports (std::stringstream& s); - void ShowTunnels (std::stringstream& s); - void ShowTransitTunnels (std::stringstream& s); - void ShowLocalDestinations (std::stringstream& s); - void ShowLocalDestination (const std::string& b32, std::stringstream& s); - void ShowSAMSessions (std::stringstream& s); - void ShowSAMSession (const std::string& id, std::stringstream& s); - void StartAcceptingTunnels (std::stringstream& s); - void StopAcceptingTunnels (std::stringstream& s); - void FillContent (std::stringstream& s); - std::string ExtractAddress (); - void ExtractParams (const std::string& str, std::map& params); - - - protected: - - boost::asio::ip::tcp::socket * m_Socket; - boost::asio::deadline_timer m_Timer; - char m_Buffer[HTTP_CONNECTION_BUFFER_SIZE + 1], m_StreamBuffer[HTTP_CONNECTION_BUFFER_SIZE + 1]; - size_t m_BufferLen; - request m_Request; - reply m_Reply; - - protected: - - virtual void RunRequest (); - - public: +namespace i2p { +namespace util { - static const std::string itoopieImage; - static const std::string itoopieFavicon; - }; +const size_t HTTP_CONNECTION_BUFFER_SIZE = 8192; +const int HTTP_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds - class HTTPServer - { - public: +class HTTPConnection: public std::enable_shared_from_this { +public: - HTTPServer (const std::string& address, int port); - virtual ~HTTPServer (); + HTTPConnection(boost::asio::ip::tcp::socket* socket, + std::shared_ptr session); - void Start (); - void Stop (); + ~HTTPConnection() { delete m_Socket; } + void Receive(); + +private: - private: + void Terminate(); + void HandleReceive(const boost::system::error_code& ecode, std::size_t bytes_transferred); + void RunRequest(); + void HandleWriteReply(const boost::system::error_code& ecode); + void SendReply(); - void Run (); - void Accept (); - void HandleAccept(const boost::system::error_code& ecode); - - private: + void Send404Reply(); - std::thread * m_Thread; - boost::asio::io_service m_Service; - boost::asio::io_service::work m_Work; - boost::asio::ip::tcp::acceptor m_Acceptor; - boost::asio::ip::tcp::socket * m_NewSocket; + /* + * @throw std::runtime_error when the file is not accessible + */ + std::string GetFileContents(const std::string& filename, bool preprocess) const; - protected: - virtual void CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket); - }; + void HandleRequest(); + void HandleI2PControlRequest(); + void ExtractParams(const std::string& str, std::map& params); + + bool isAllowed(const std::string& address) const; +private: + boost::asio::ip::tcp::socket* m_Socket; + char m_Buffer[HTTP_CONNECTION_BUFFER_SIZE + 1]; + size_t m_BufferLen; + util::http::Request m_Request; + util::http::Response m_Reply; + std::shared_ptr m_Session; +}; + +class HTTPServer { +public: + + HTTPServer(const std::string& address, int port); + virtual ~HTTPServer(); + + void Start(); + void Stop(); + +private: + + void Run(); + void Accept(); + void HandleAccept(const boost::system::error_code& ecode); + +private: + + std::thread * m_Thread; + boost::asio::io_service m_Service; + boost::asio::io_service::work m_Work; + boost::asio::ip::tcp::acceptor m_Acceptor; + boost::asio::ip::tcp::socket * m_NewSocket; + std::shared_ptr m_Session; + +protected: + void CreateConnection(boost::asio::ip::tcp::socket* m_NewSocket); +}; } } diff --git a/client/Win32Service.cpp b/client/Win32Service.cpp index c9189fac..90e909af 100644 --- a/client/Win32Service.cpp +++ b/client/Win32Service.cpp @@ -8,7 +8,7 @@ #include #include "Daemon.h" -#include "Log.h" +#include "util/Log.h" I2PService *I2PService::s_service = NULL; 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/client/i2pcontrol/I2PControl.cpp b/client/i2pcontrol/I2PControl.cpp index d6b7a066..f9c9a195 100644 --- a/client/i2pcontrol/I2PControl.cpp +++ b/client/i2pcontrol/I2PControl.cpp @@ -11,6 +11,7 @@ #include #include +#include "util/util.h" #include "util/Log.h" #include "util/Timestamp.h" #include "transport/Transports.h" @@ -21,6 +22,65 @@ namespace i2p { namespace client { +namespace i2pcontrol { + +JsonObject::JsonObject(const std::string& value) + : children(), value("\"" + value + "\"") +{ + +} + +JsonObject::JsonObject(int value) + : children(), value(std::to_string(value)) +{ + +} + +JsonObject::JsonObject(double v) + : children(), value() +{ + std::ostringstream oss; + oss << std::fixed << std::setprecision(2) << v; + value = oss.str(); +} + +JsonObject& JsonObject::operator[](const std::string& key) +{ + return children[key]; +} + +std::string JsonObject::toString() const +{ + if(children.empty()) + return value; + + std::ostringstream oss; + oss << '{'; + for(auto it = children.begin(); it != children.end(); ++it) { + if(it != children.begin()) + oss << ','; + oss << '"' << it->first << "\":" << it->second.toString(); + } + oss << '}'; + return oss.str(); +} + +JsonObject tunnelToJsonObject(i2p::tunnel::Tunnel* tunnel) +{ + JsonObject obj; + + std::stringstream ss; + tunnel->GetTunnelConfig()->Print(ss); // TODO: use a JsonObject + obj["layout"] = JsonObject(ss.str()); + + const auto state = tunnel->GetState(); + if(state == i2p::tunnel::eTunnelStateFailed) + obj["state"] = JsonObject("failed"); + else if(state == i2p::tunnel::eTunnelStateExpiring) + obj["state"] = JsonObject("expiring"); + + return obj; +} I2PControlSession::Response::Response(const std::string& version) : id(), version(version), error(ErrorCode::None), parameters() @@ -90,6 +150,11 @@ void I2PControlSession::Response::setParam(const std::string& param, double valu parameters[param] = oss.str(); } +void I2PControlSession::Response::setParam(const std::string& param, const JsonObject& value) +{ + parameters[param] = value.toString(); +} + void I2PControlSession::Response::setError(ErrorCode code) { error = code; @@ -104,28 +169,35 @@ I2PControlSession::I2PControlSession(boost::asio::io_service& ios, const std::st : password(pass), tokens(), tokensMutex(), service(ios), shutdownTimer(ios), expireTokensTimer(ios) { + using namespace i2p::client::i2pcontrol::constants; // Method handlers - methodHandlers[I2P_CONTROL_METHOD_AUTHENTICATE] = &I2PControlSession::handleAuthenticate; - methodHandlers[I2P_CONTROL_METHOD_ECHO] = &I2PControlSession::handleEcho; - methodHandlers[I2P_CONTROL_METHOD_I2PCONTROL] = &I2PControlSession::handleI2PControl; - methodHandlers[I2P_CONTROL_METHOD_ROUTER_INFO] = &I2PControlSession::handleRouterInfo; - methodHandlers[I2P_CONTROL_METHOD_ROUTER_MANAGER] = &I2PControlSession::handleRouterManager; - methodHandlers[I2P_CONTROL_METHOD_NETWORK_SETTING] = &I2PControlSession::handleNetworkSetting; + methodHandlers[METHOD_AUTHENTICATE] = &I2PControlSession::handleAuthenticate; + methodHandlers[METHOD_ECHO] = &I2PControlSession::handleEcho; + methodHandlers[METHOD_I2PCONTROL] = &I2PControlSession::handleI2PControl; + methodHandlers[METHOD_ROUTER_INFO] = &I2PControlSession::handleRouterInfo; + methodHandlers[METHOD_ROUTER_MANAGER] = &I2PControlSession::handleRouterManager; + methodHandlers[METHOD_NETWORK_SETTING] = &I2PControlSession::handleNetworkSetting; // RouterInfo handlers - routerInfoHandlers[I2P_CONTROL_ROUTER_INFO_UPTIME] = &I2PControlSession::handleUptime; - routerInfoHandlers[I2P_CONTROL_ROUTER_INFO_VERSION] = &I2PControlSession::handleVersion; - routerInfoHandlers[I2P_CONTROL_ROUTER_INFO_STATUS] = &I2PControlSession::handleStatus; - routerInfoHandlers[I2P_CONTROL_ROUTER_INFO_NETDB_KNOWNPEERS]= &I2PControlSession::handleNetDbKnownPeers; - routerInfoHandlers[I2P_CONTROL_ROUTER_INFO_NETDB_ACTIVEPEERS] = &I2PControlSession::handleNetDbActivePeers; - routerInfoHandlers[I2P_CONTROL_ROUTER_INFO_NET_STATUS] = &I2PControlSession::handleNetStatus; - routerInfoHandlers[I2P_CONTROL_ROUTER_INFO_TUNNELS_PARTICIPATING] = &I2PControlSession::handleTunnelsParticipating; - routerInfoHandlers[I2P_CONTROL_ROUTER_INFO_BW_IB_1S] = &I2PControlSession::handleInBandwidth1S; - routerInfoHandlers[I2P_CONTROL_ROUTER_INFO_BW_OB_1S] = &I2PControlSession::handleOutBandwidth1S; + routerInfoHandlers[ROUTER_INFO_UPTIME] = &I2PControlSession::handleUptime; + routerInfoHandlers[ROUTER_INFO_VERSION] = &I2PControlSession::handleVersion; + routerInfoHandlers[ROUTER_INFO_STATUS] = &I2PControlSession::handleStatus; + routerInfoHandlers[ROUTER_INFO_DATAPATH] = &I2PControlSession::handleDatapath; + routerInfoHandlers[ROUTER_INFO_NETDB_KNOWNPEERS]= &I2PControlSession::handleNetDbKnownPeers; + routerInfoHandlers[ROUTER_INFO_NETDB_ACTIVEPEERS] = &I2PControlSession::handleNetDbActivePeers; + routerInfoHandlers[ROUTER_INFO_NETDB_LEASESETS] = &I2PControlSession::handleNetDbLeaseSets; + routerInfoHandlers[ROUTER_INFO_NETDB_FLOODFILLS] = &I2PControlSession::handleNetDbFloodfills; + routerInfoHandlers[ROUTER_INFO_NET_STATUS] = &I2PControlSession::handleNetStatus; + routerInfoHandlers[ROUTER_INFO_TUNNELS_PARTICIPATING] = &I2PControlSession::handleTunnelsParticipating; + routerInfoHandlers[ROUTER_INFO_TUNNELS_CREATION_SUCCESS] = &I2PControlSession::handleTunnelsCreationSuccess; + routerInfoHandlers[ROUTER_INFO_TUNNELS_IN_LIST] = &I2PControlSession::handleTunnelsInList; + routerInfoHandlers[ROUTER_INFO_TUNNELS_OUT_LIST] = &I2PControlSession::handleTunnelsOutList; + routerInfoHandlers[ROUTER_INFO_BW_IB_1S] = &I2PControlSession::handleInBandwidth1S; + routerInfoHandlers[ROUTER_INFO_BW_OB_1S] = &I2PControlSession::handleOutBandwidth1S; // RouterManager handlers - routerManagerHandlers[I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN] = &I2PControlSession::handleShutdown; - routerManagerHandlers[I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN_GRACEFUL] = &I2PControlSession::handleShutdownGraceful; - routerManagerHandlers[I2P_CONTROL_ROUTER_MANAGER_RESEED] = &I2PControlSession::handleReseed; + routerManagerHandlers[ROUTER_MANAGER_SHUTDOWN] = &I2PControlSession::handleShutdown; + routerManagerHandlers[ROUTER_MANAGER_SHUTDOWN_GRACEFUL] = &I2PControlSession::handleShutdownGraceful; + routerManagerHandlers[ROUTER_MANAGER_RESEED] = &I2PControlSession::handleReseed; } void I2PControlSession::start() @@ -147,9 +219,9 @@ I2PControlSession::Response I2PControlSession::handleRequest(std::stringstream& Response response; try { - response.setId(pt.get(I2P_CONTROL_PROPERTY_ID)); + response.setId(pt.get(constants::PROPERTY_ID)); - std::string method = pt.get(I2P_CONTROL_PROPERTY_METHOD); + std::string method = pt.get(constants::PROPERTY_METHOD); auto it = methodHandlers.find(method); if(it == methodHandlers.end()) { // Not found LogPrint(eLogWarning, "Unknown I2PControl method ", method); @@ -157,8 +229,8 @@ I2PControlSession::Response I2PControlSession::handleRequest(std::stringstream& return response; } - PropertyTree params = pt.get_child(I2P_CONTROL_PROPERTY_PARAMS); - if(method != I2P_CONTROL_METHOD_AUTHENTICATE && !authenticate(params, response)) { + PropertyTree params = pt.get_child(constants::PROPERTY_PARAMS); + if(method != constants::METHOD_AUTHENTICATE && !authenticate(params, response)) { LogPrint(eLogWarning, "I2PControl invalid token presented"); return response; } @@ -177,14 +249,14 @@ I2PControlSession::Response I2PControlSession::handleRequest(std::stringstream& bool I2PControlSession::authenticate(const PropertyTree& pt, Response& response) { try { - std::string token = pt.get(I2P_CONTROL_PARAM_TOKEN); + std::string token = pt.get(constants::PARAM_TOKEN); std::lock_guard lock(tokensMutex); auto it = tokens.find(token); if(it == tokens.end()) { response.setError(ErrorCode::NonexistentToken); return false; - } else if(util::GetSecondsSinceEpoch() - it->second > I2P_CONTROL_TOKEN_LIFETIME) { + } else if(util::GetSecondsSinceEpoch() - it->second > constants::TOKEN_LIFETIME) { response.setError(ErrorCode::ExpiredToken); return false; } @@ -199,12 +271,12 @@ bool I2PControlSession::authenticate(const PropertyTree& pt, Response& response) std::string I2PControlSession::generateToken() const { - byte random_data[I2P_CONTROL_TOKEN_SIZE] = {}; + byte random_data[constants::TOKEN_SIZE] = {}; CryptoPP::AutoSeededRandomPool rng; - rng.GenerateBlock(random_data, I2P_CONTROL_TOKEN_SIZE); + rng.GenerateBlock(random_data, constants::TOKEN_SIZE); std::string token; CryptoPP::StringSource ss( - random_data, I2P_CONTROL_TOKEN_SIZE, true, + random_data, constants::TOKEN_SIZE, true, new CryptoPP::HexEncoder(new CryptoPP::StringSink(token)) ); return token; @@ -212,8 +284,8 @@ std::string I2PControlSession::generateToken() const void I2PControlSession::handleAuthenticate(const PropertyTree& pt, Response& response) { - const int api = pt.get(I2P_CONTROL_PARAM_API); - const std::string given_pass = pt.get(I2P_CONTROL_PARAM_PASSWORD); + const int api = pt.get(constants::PARAM_API); + const std::string given_pass = pt.get(constants::PARAM_PASSWORD); LogPrint(eLogDebug, "I2PControl Authenticate API = ", api, " Password = ", given_pass); if(given_pass != password) { LogPrint( @@ -224,8 +296,8 @@ void I2PControlSession::handleAuthenticate(const PropertyTree& pt, Response& res return; } const std::string token = generateToken(); - response.setParam(I2P_CONTROL_PARAM_API, api); - response.setParam(I2P_CONTROL_PARAM_TOKEN, token); + response.setParam(constants::PARAM_API, api); + response.setParam(constants::PARAM_TOKEN, token); std::lock_guard lock(tokensMutex); tokens.insert(std::make_pair(token, util::GetSecondsSinceEpoch())); @@ -233,9 +305,9 @@ void I2PControlSession::handleAuthenticate(const PropertyTree& pt, Response& res void I2PControlSession::handleEcho(const PropertyTree& pt, Response& response) { - const std::string echo = pt.get(I2P_CONTROL_PARAM_ECHO); + const std::string echo = pt.get(constants::PARAM_ECHO); LogPrint(eLogDebug, "I2PControl Echo Echo = ", echo); - response.setParam(I2P_CONTROL_PARAM_RESULT, echo); + response.setParam(constants::PARAM_RESULT, echo); } void I2PControlSession::handleI2PControl(const PropertyTree&, Response&) @@ -249,7 +321,7 @@ void I2PControlSession::handleRouterInfo(const PropertyTree& pt, Response& respo { LogPrint(eLogDebug, "I2PControl RouterInfo"); for(const auto& pair : pt) { - if(pair.first == I2P_CONTROL_PARAM_TOKEN) + if(pair.first == constants::PARAM_TOKEN) continue; LogPrint(eLogDebug, pair.first); auto it = routerInfoHandlers.find(pair.first); @@ -266,7 +338,7 @@ void I2PControlSession::handleRouterManager(const PropertyTree& pt, Response& re { LogPrint(eLogDebug, "I2PControl RouterManager"); for(const auto& pair : pt) { - if(pair.first == I2P_CONTROL_PARAM_TOKEN) + if(pair.first == constants::PARAM_TOKEN) continue; LogPrint(eLogDebug, pair.first); auto it = routerManagerHandlers.find(pair.first); @@ -286,53 +358,112 @@ void I2PControlSession::handleNetworkSetting(const PropertyTree&, Response&) void I2PControlSession::handleUptime(Response& response) { - response.setParam(I2P_CONTROL_ROUTER_INFO_UPTIME, (int)i2p::context.GetUptime()*1000); + response.setParam(constants::ROUTER_INFO_UPTIME, (int)i2p::context.GetUptime()*1000); } void I2PControlSession::handleVersion(Response& response) { - response.setParam(I2P_CONTROL_ROUTER_INFO_VERSION, VERSION); + response.setParam(constants::ROUTER_INFO_VERSION, VERSION); } void I2PControlSession::handleStatus(Response& response) { - response.setParam(I2P_CONTROL_ROUTER_INFO_STATUS, "???"); // TODO: + response.setParam(constants::ROUTER_INFO_STATUS, "???"); // TODO: +} + +void I2PControlSession::handleDatapath(Response& response) +{ + response.setParam( + constants::ROUTER_INFO_DATAPATH, + i2p::util::filesystem::GetDefaultDataDir().string() + ); } void I2PControlSession::handleNetDbKnownPeers(Response& response) { response.setParam( - I2P_CONTROL_ROUTER_INFO_NETDB_KNOWNPEERS, i2p::data::netdb.GetNumRouters() + constants::ROUTER_INFO_NETDB_KNOWNPEERS, i2p::data::netdb.GetNumRouters() ); } void I2PControlSession::handleNetDbActivePeers(Response& response) { response.setParam( - I2P_CONTROL_ROUTER_INFO_NETDB_ACTIVEPEERS, + constants::ROUTER_INFO_NETDB_ACTIVEPEERS, (int)i2p::transport::transports.GetPeers().size() ); } +void I2PControlSession::handleNetDbFloodfills(Response& response) +{ + response.setParam( + constants::ROUTER_INFO_NETDB_FLOODFILLS, + (int)i2p::data::netdb.GetNumFloodfills() + ); +} + +void I2PControlSession::handleNetDbLeaseSets(Response& response) +{ + response.setParam( + constants::ROUTER_INFO_NETDB_LEASESETS, + (int)i2p::data::netdb.GetNumLeaseSets() + ); +} + void I2PControlSession::handleNetStatus(Response& response) { response.setParam( - I2P_CONTROL_ROUTER_INFO_NET_STATUS, (int)i2p::context.GetStatus() + constants::ROUTER_INFO_NET_STATUS, (int)i2p::context.GetStatus() ); } void I2PControlSession::handleTunnelsParticipating(Response& response) { response.setParam( - I2P_CONTROL_ROUTER_INFO_TUNNELS_PARTICIPATING, + constants::ROUTER_INFO_TUNNELS_PARTICIPATING, (int)i2p::tunnel::tunnels.GetTransitTunnels().size() ); } +void I2PControlSession::handleTunnelsCreationSuccess(Response& response) +{ + response.setParam( + constants::ROUTER_INFO_TUNNELS_CREATION_SUCCESS, + i2p::tunnel::tunnels.GetTunnelCreationSuccessRate() + ); +} + +void I2PControlSession::handleTunnelsInList(Response& response) +{ + JsonObject list; + + for(auto pair : i2p::tunnel::tunnels.GetInboundTunnels()) { + const std::string id = std::to_string(pair.first); + list[id] = tunnelToJsonObject(pair.second.get()); + list[id]["bytes"] = JsonObject( + static_cast(pair.second->GetNumReceivedBytes()) + ); + } + response.setParam(constants::ROUTER_INFO_TUNNELS_IN_LIST, list); +} + +void I2PControlSession::handleTunnelsOutList(Response& response) +{ + JsonObject list; + for(auto tunnel : i2p::tunnel::tunnels.GetOutboundTunnels()) { + const std::string id = std::to_string(tunnel->GetTunnelID()); + list[id] = tunnelToJsonObject(tunnel.get()); + list[id]["bytes"] = JsonObject( + static_cast(tunnel->GetNumSentBytes()) + ); + } + response.setParam(constants::ROUTER_INFO_TUNNELS_OUT_LIST, list); +} + void I2PControlSession::handleInBandwidth1S(Response& response) { response.setParam( - I2P_CONTROL_ROUTER_INFO_BW_IB_1S, + constants::ROUTER_INFO_BW_IB_1S, (double)i2p::transport::transports.GetInBandwidth() ); } @@ -340,7 +471,7 @@ void I2PControlSession::handleInBandwidth1S(Response& response) void I2PControlSession::handleOutBandwidth1S(Response& response) { response.setParam( - I2P_CONTROL_ROUTER_INFO_BW_OB_1S, + constants::ROUTER_INFO_BW_OB_1S, (double)i2p::transport::transports.GetOutBandwidth() ); } @@ -348,7 +479,7 @@ void I2PControlSession::handleOutBandwidth1S(Response& response) void I2PControlSession::handleShutdown(Response& response) { LogPrint(eLogInfo, "Shutdown requested"); - response.setParam(I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN, ""); + response.setParam(constants::ROUTER_MANAGER_SHUTDOWN, ""); // 1 second to make sure response has been sent shutdownTimer.expires_from_now(boost::posix_time::seconds(1)); shutdownTimer.async_wait([](const boost::system::error_code&) { @@ -361,7 +492,7 @@ void I2PControlSession::handleShutdownGraceful(Response& response) i2p::context.SetAcceptsTunnels(false); int timeout = i2p::tunnel::tunnels.GetTransitTunnelsExpirationTimeout(); LogPrint(eLogInfo, "Graceful shutdown requested. Will shutdown after ", timeout, " seconds"); - response.setParam(I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN_GRACEFUL, ""); + response.setParam(constants::ROUTER_MANAGER_SHUTDOWN_GRACEFUL, ""); shutdownTimer.expires_from_now(boost::posix_time::seconds(timeout + 1)); shutdownTimer.async_wait([](const boost::system::error_code&) { Daemon.running = 0; @@ -371,7 +502,7 @@ void I2PControlSession::handleShutdownGraceful(Response& response) void I2PControlSession::handleReseed(Response& response) { LogPrint(eLogInfo, "Reseed requested"); - response.setParam(I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN, ""); + response.setParam(constants::ROUTER_MANAGER_SHUTDOWN, ""); i2p::data::netdb.Reseed(); } @@ -385,7 +516,7 @@ void I2PControlSession::expireTokens(const boost::system::error_code& error) const uint64_t now = util::GetSecondsSinceEpoch(); std::lock_guard lock(tokensMutex); for(auto it = tokens.begin(); it != tokens.end(); ) { - if(now - it->second > I2P_CONTROL_TOKEN_LIFETIME) + if(now - it->second > constants::TOKEN_LIFETIME) it = tokens.erase(it); else ++it; @@ -394,7 +525,7 @@ void I2PControlSession::expireTokens(const boost::system::error_code& error) void I2PControlSession::startExpireTokensJob() { - expireTokensTimer.expires_from_now(boost::posix_time::seconds(I2P_CONTROL_TOKEN_LIFETIME)); + expireTokensTimer.expires_from_now(boost::posix_time::seconds(constants::TOKEN_LIFETIME)); expireTokensTimer.async_wait(std::bind( &I2PControlSession::expireTokens, shared_from_this(), std::placeholders::_1 )); @@ -402,3 +533,4 @@ void I2PControlSession::startExpireTokensJob() } } +} diff --git a/client/i2pcontrol/I2PControl.h b/client/i2pcontrol/I2PControl.h index eb9b3eab..bdd05e56 100644 --- a/client/i2pcontrol/I2PControl.h +++ b/client/i2pcontrol/I2PControl.h @@ -9,52 +9,94 @@ #include namespace i2p { + +// Forward declaration +namespace tunnel { class Tunnel; } + namespace client { +namespace i2pcontrol { -const char I2P_CONTROL_DEFAULT_PASSWORD[] = "itoopie"; -const uint64_t I2P_CONTROL_TOKEN_LIFETIME = 600; // Token lifetime in seconds -const std::size_t I2P_CONTROL_TOKEN_SIZE = 8; // Token size in bytes +namespace constants { -const char I2P_CONTROL_PROPERTY_ID[] = "id"; -const char I2P_CONTROL_PROPERTY_METHOD[] = "method"; -const char I2P_CONTROL_PROPERTY_PARAMS[] = "params"; -const char I2P_CONTROL_PROPERTY_RESULT[] = "result"; +const char DEFAULT_PASSWORD[] = "itoopie"; +const uint64_t TOKEN_LIFETIME = 600; // Token lifetime in seconds +const std::size_t TOKEN_SIZE = 8; // Token size in bytes + +const char PROPERTY_ID[] = "id"; +const char PROPERTY_METHOD[] = "method"; +const char PROPERTY_PARAMS[] = "params"; +const char PROPERTY_RESULT[] = "result"; // methods -const char I2P_CONTROL_METHOD_AUTHENTICATE[] = "Authenticate"; -const char I2P_CONTROL_METHOD_ECHO[] = "Echo"; -const char I2P_CONTROL_METHOD_I2PCONTROL[] = "I2PControl"; -const char I2P_CONTROL_METHOD_ROUTER_INFO[] = "RouterInfo"; -const char I2P_CONTROL_METHOD_ROUTER_MANAGER[] = "RouterManager"; -const char I2P_CONTROL_METHOD_NETWORK_SETTING[] = "NetworkSetting"; +const char METHOD_AUTHENTICATE[] = "Authenticate"; +const char METHOD_ECHO[] = "Echo"; +const char METHOD_I2PCONTROL[] = "I2PControl"; +const char METHOD_ROUTER_INFO[] = "RouterInfo"; +const char METHOD_ROUTER_MANAGER[] = "RouterManager"; +const char METHOD_NETWORK_SETTING[] = "NetworkSetting"; // params -const char I2P_CONTROL_PARAM_API[] = "API"; -const char I2P_CONTROL_PARAM_PASSWORD[] = "Password"; -const char I2P_CONTROL_PARAM_TOKEN[] = "Token"; -const char I2P_CONTROL_PARAM_ECHO[] = "Echo"; -const char I2P_CONTROL_PARAM_RESULT[] = "Result"; +const char PARAM_API[] = "API"; +const char PARAM_PASSWORD[] = "Password"; +const char PARAM_TOKEN[] = "Token"; +const char PARAM_ECHO[] = "Echo"; +const char PARAM_RESULT[] = "Result"; // I2PControl -const char I2P_CONTROL_I2PCONTROL_ADDRESS[] = "i2pcontrol.address"; -const char I2P_CONTROL_I2PCONTROL_PASSWORD[] = "i2pcontrol.password"; -const char I2P_CONTROL_I2PCONTROL_PORT[] = "i2pcontrol.port"; +const char I2PCONTROL_ADDRESS[] = "i2pcontrol.address"; +const char I2PCONTROL_PASSWORD[] = "i2pcontrol.password"; +const char I2PCONTROL_PORT[] = "i2pcontrol.port"; // RouterInfo requests -const char I2P_CONTROL_ROUTER_INFO_UPTIME[] = "i2p.router.uptime"; -const char I2P_CONTROL_ROUTER_INFO_VERSION[] = "i2p.router.version"; -const char I2P_CONTROL_ROUTER_INFO_STATUS[] = "i2p.router.status"; -const char I2P_CONTROL_ROUTER_INFO_NETDB_KNOWNPEERS[] = "i2p.router.netdb.knownpeers"; -const char I2P_CONTROL_ROUTER_INFO_NETDB_ACTIVEPEERS[] = "i2p.router.netdb.activepeers"; -const char I2P_CONTROL_ROUTER_INFO_NET_STATUS[] = "i2p.router.net.status"; -const char I2P_CONTROL_ROUTER_INFO_TUNNELS_PARTICIPATING[] = "i2p.router.net.tunnels.participating"; -const char I2P_CONTROL_ROUTER_INFO_BW_IB_1S[] = "i2p.router.net.bw.inbound.1s"; -const char I2P_CONTROL_ROUTER_INFO_BW_OB_1S[] = "i2p.router.net.bw.outbound.1s"; +const char ROUTER_INFO_UPTIME[] = "i2p.router.uptime"; +const char ROUTER_INFO_VERSION[] = "i2p.router.version"; +const char ROUTER_INFO_STATUS[] = "i2p.router.status"; +const char ROUTER_INFO_DATAPATH[] = "i2p.router.datapath"; +const char ROUTER_INFO_NETDB_KNOWNPEERS[] = "i2p.router.netdb.knownpeers"; +const char ROUTER_INFO_NETDB_ACTIVEPEERS[] = "i2p.router.netdb.activepeers"; +const char ROUTER_INFO_NETDB_FLOODFILLS[] = "i2p.router.netdb.floodfills"; +const char ROUTER_INFO_NETDB_LEASESETS[] = "i2p.router.netdb.leasesets"; +const char ROUTER_INFO_NET_STATUS[] = "i2p.router.net.status"; +const char ROUTER_INFO_TUNNELS_PARTICIPATING[] = "i2p.router.net.tunnels.participating"; +// TODO: Probably better to use the standard GetRate instead +const char ROUTER_INFO_TUNNELS_CREATION_SUCCESS[] = "i2p.router.net.tunnels.creationsuccessrate"; +const char ROUTER_INFO_TUNNELS_IN_LIST[] = "i2p.router.net.tunnels.inbound.list"; +const char ROUTER_INFO_TUNNELS_OUT_LIST[] = "i2p.router.net.tunnels.outbound.list"; +const char ROUTER_INFO_BW_IB_1S[] = "i2p.router.net.bw.inbound.1s"; +const char ROUTER_INFO_BW_OB_1S[] = "i2p.router.net.bw.outbound.1s"; // RouterManager requests -const char I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN[] = "Shutdown"; -const char I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN_GRACEFUL[] = "ShutdownGraceful"; -const char I2P_CONTROL_ROUTER_MANAGER_RESEED[] = "Reseed"; +const char ROUTER_MANAGER_SHUTDOWN[] = "Shutdown"; +const char ROUTER_MANAGER_SHUTDOWN_GRACEFUL[] = "ShutdownGraceful"; +const char ROUTER_MANAGER_RESEED[] = "Reseed"; + +} // constants + +/** + * Represents a Json object, provides functionality to convert to string. + */ +class JsonObject { + +public: + JsonObject() = default; + + JsonObject(const std::string& value); + + JsonObject(int value); + + JsonObject(double value); + + JsonObject& operator[](const std::string& key); + + std::string toString() const; + +private: + std::map children; + std::string value; +}; + + +JsonObject tunnelToJsonObject(i2p::tunnel::Tunnel* tunnel); /** * "Null" I2P control implementation, does not do actual networking. @@ -97,9 +139,22 @@ public: * @todo escape quotes */ void setParam(const std::string& param, const std::string& value); + + /** + * Set an ouptut parameter to a specified integer. + */ void setParam(const std::string& param, int value); + + /** + * Set an ouptut parameter to a specified double. + */ void setParam(const std::string& param, double value); + /** + * Set an ouptut parameter to a specified Json object. + */ + void setParam(const std::string& param, const JsonObject& value); + void setError(ErrorCode code); void setId(const std::string& identifier); @@ -113,7 +168,7 @@ public: * the lifetime of this I2PControlSession. */ I2PControlSession(boost::asio::io_service& ios, - const std::string& pass = I2P_CONTROL_DEFAULT_PASSWORD); + const std::string& pass = constants::DEFAULT_PASSWORD); /** * Starts the I2PControlSession. @@ -172,10 +227,18 @@ private: void handleUptime(Response& response); void handleVersion(Response& response); void handleStatus(Response& response); + void handleDatapath(Response& response); void handleNetDbKnownPeers(Response& response); void handleNetDbActivePeers(Response& response); + void handleNetDbFloodfills(Response& response); + void handleNetDbLeaseSets(Response& response); void handleNetStatus(Response& response); + void handleTunnelsParticipating(Response& response); + void handleTunnelsCreationSuccess(Response& response); + void handleTunnelsInList(Response& response); + void handleTunnelsOutList(Response& response); + void handleInBandwidth1S(Response& response); void handleOutBandwidth1S(Response& response); @@ -198,6 +261,7 @@ private: boost::asio::deadline_timer expireTokensTimer; }; +} } } diff --git a/client/i2pcontrol/I2PControlServer.cpp b/client/i2pcontrol/I2PControlServer.cpp index 88017e25..92277747 100644 --- a/client/i2pcontrol/I2PControlServer.cpp +++ b/client/i2pcontrol/I2PControlServer.cpp @@ -8,6 +8,7 @@ namespace i2p { namespace client { +namespace i2pcontrol { I2PControlService::I2PControlService(const std::string& address, int port, const std::string& pass) : m_Session(std::make_shared(m_Service, pass)), @@ -168,3 +169,4 @@ void I2PControlService::HandleResponseSent(const boost::system::error_code& ecod } } +} diff --git a/client/i2pcontrol/I2PControlServer.h b/client/i2pcontrol/I2PControlServer.h index e60ee862..ad09af30 100644 --- a/client/i2pcontrol/I2PControlServer.h +++ b/client/i2pcontrol/I2PControlServer.h @@ -12,6 +12,7 @@ namespace i2p { namespace client { +namespace i2pcontrol { const size_t I2P_CONTROL_MAX_REQUEST_SIZE = 1024; typedef std::array I2PControlBuffer; @@ -51,6 +52,7 @@ private: }; } } +} #endif diff --git a/contrib/certificates/router/orignal_at_mail.i2p.crt b/contrib/certificates/router/orignal_at_mail.i2p.crt deleted file mode 100644 index c1229f3b..00000000 --- a/contrib/certificates/router/orignal_at_mail.i2p.crt +++ /dev/null @@ -1,31 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIFVDCCAzwCCQC2r1XWYtqtAzANBgkqhkiG9w0BAQsFADBsMQswCQYDVQQGEwJY -WDELMAkGA1UECAwCWFgxCzAJBgNVBAcMAlhYMRMwEQYDVQQKDApQdXJwbGUgSTJQ -MQ0wCwYDVQQLDARJMlBEMR8wHQYJKoZIhvcNAQkBFhBvcmlnbmFsQG1haWwuaTJw -MB4XDTE1MDIyMjEzNTgxOFoXDTI1MDIxOTEzNTgxOFowbDELMAkGA1UEBhMCWFgx -CzAJBgNVBAgMAlhYMQswCQYDVQQHDAJYWDETMBEGA1UECgwKUHVycGxlIEkyUDEN -MAsGA1UECwwESTJQRDEfMB0GCSqGSIb3DQEJARYQb3JpZ25hbEBtYWlsLmkycDCC -AiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBALp3D/gdvFjrMm+IE8tHZCWE -hQ6Pp0CCgCGDBC3WQFLqR98bqVPl4UwRG/MKY/LY7Woai06JNmGcpfw0LMoNnHxT -bvKtDRe/8kQdhdLHhgIkWKSbMvTAl7uUdV6FzsPgDR0x7scoFVWEhkF0wfmzGF2V -yr/WCBQejFPu69z03m5tRQ8Xjp2txWV45RawUmFu50bgbZvLCSLfTkIvxmfJzgPN -pJ3sPa/g7TBZl2uEiAu4uaEKvTuuzStOWCGgFaHYFVlTfFXTvmhFMqHfaidtzrlu -H35WGrmIWTDl6uGPC5QkSppvkj73rDj5aEyPzWMz5DN3YeECoVSchN+OJJCM6m7+ -rLFYXghVEp2h+T9O1GBRfcHlQ2E3CrWWvxhmK8dfteJmd501dyNX2paeuIg/aPFO -54/8m2r11uyF29hgY8VWLdXtqvwhKuK36PCzofEwDp9QQX8GRsEV4pZTrn4bDhGo -kb9BF7TZTqtL3uyiRmIyBXrNNiYlA1Xm4fyKRtxl0mrPaUXdgdnCt3KxOAJ8WM2B -7L/kk9U8C/nexHbMxIZfTap49XcUg5dxSO9kOBosIOcCUms8sAzBPDV2tWAByhYF -jI/Tutbd3F0+fvcmTcIFOlGbOxKgO2SfwXjv/44g/3LMK6IAMFB9UOc8KhnnJP0f -uAHvMXn1ahRs4pM1VizLAgMBAAEwDQYJKoZIhvcNAQELBQADggIBAIOxdaXT+wfu -nv/+1hy5T4TlRMNNsuj79ROcy6Mp+JwMG50HjTc0qTlXh8C7nHybDJn4v7DA+Nyn -RxT0J5I+Gqn+Na9TaC9mLeX/lwe8/KomyhBWxjrsyWj1V6v/cLO924S2rtcfzMDm -l3SFh9YHM1KF/R9N1XYBwtMzr3bupWDnE1yycYp1F4sMLr5SMzMQ0svQpQEM2/y5 -kly8+eUzryhm+ag9x1686uEG5gxhQ1eHQoZEaClHUOsV+28+d5If7cqcYx9Hf5Tt -CiVjJQzdxBF+6GeiJtKxnLtevqlkbyIJt6Cm9/7YIy/ovRGF2AKSYN6oCwmZQ6i1 -8nRnFq5zE7O94m+GXconWZxy0wVqA6472HThMi7S+Tk/eLYen2ilGY+KCb9a0FH5 -5MOuWSoJZ8/HfW2VeQmL8EjhWm5F2ybg28wgXK4BOGR3jQi03Fsc+AFidnWxSKo0 -aiJoPgOsfyu8/fnCcAi07kSmjzUKIWskApgcpGQLNXHFK9mtg7+VA8esRnfLlKtP -tJf+nNAPY1sqHfGBzh7WWGWal5RGHF5nEm3ta3oiFF5sMKCJ6C87zVwFkEcRytGC -xOGmiG1O1RPrO5NG7rZUaQ4y1OKl2Y1H+nGONzZ3mvoAOvxEq6JtUnU2kZscpPlk -fpeOSDoGBYJGbIpzDreBDhxaZrwGq36k ------END CERTIFICATE----- diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index a9a51945..95a10d02 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -11,6 +11,7 @@ set(CORE_SRC "util/base64.cpp" "util/util.cpp" "util/Log.cpp" + "util/HTTP.cpp" "tunnel/TransitTunnel.cpp" "tunnel/Tunnel.cpp" "tunnel/TunnelGateway.cpp" diff --git a/core/util/HTTP.cpp b/core/util/HTTP.cpp new file mode 100644 index 00000000..ea8c9dff --- /dev/null +++ b/core/util/HTTP.cpp @@ -0,0 +1,246 @@ +#include "HTTP.h" +#include +#include +#include +#include +#include +#include "Log.h" + +namespace i2p { +namespace util { +namespace http { + +void Request::parseRequestLine(const std::string& line) +{ + std::stringstream ss(line); + ss >> method; + ss >> uri; +} + +void Request::parseHeaderLine(const std::string& line) +{ + const std::size_t pos = line.find_first_of(':'); + headers[boost::trim_copy(line.substr(0, pos))] = boost::trim_copy(line.substr(pos + 1)); +} + +void Request::parseHeader(std::stringstream& ss) +{ + std::string line; + while(std::getline(ss, line) && !boost::trim_copy(line).empty()) + parseHeaderLine(line); + + has_header = boost::trim_copy(line).empty(); + if(!has_header) + header_part = line; + else + header_part = ""; +} + +void Request::setIsComplete() +{ + auto it = headers.find("Content-Length"); + if(it == headers.end()) { + // If Content-Length is not set, assume there is no more content + // TODO: Support chunked transfer, or explictly reject it + is_complete = true; + return; + } + const std::size_t length = std::stoi(it->second); + is_complete = content.size() >= length; +} + +Request::Request(const std::string& data) +{ + if(!data.empty()) + has_data = true; + + std::stringstream ss(data); + + std::string line; + std::getline(ss, line); + + // Assume the request line is always passed in one go + parseRequestLine(line); + + parseHeader(ss); + + if(has_header && ss) { + const std::string current = ss.str(); + content = current.substr(ss.tellg()); + } + + if(has_header) + setIsComplete(); +} + +std::string Request::getMethod() const +{ + return method; +} + +std::string Request::getUri() const +{ + return uri; +} + +std::string Request::getHost() const +{ + return host; +} + +int Request::getPort() const +{ + return port; +} + +std::string Request::getHeader(const std::string& name) const +{ + return headers.at(name); +} + +std::string Request::getContent() const +{ + return content; +} + +bool Request::hasData() const +{ + return has_data; +} + +bool Request::isComplete() const +{ + return is_complete; +} + +void Request::clear() +{ + has_data = false; + has_header = false; + is_complete = false; +} + +void Request::update(const std::string& data) +{ + std::stringstream ss(header_part + data); + if(!has_header) + parseHeader(ss); + + if(has_header && ss) { + const std::string current = ss.str(); + content += current.substr(ss.tellg()); + } + + if(has_header) + setIsComplete(); +} + +Response::Response(int status, const std::string& content) + : status(status), content(content), headers() +{ + +} + +void Response::setHeader(const std::string& name, const std::string& value) +{ + headers[name] = value; +} + +std::string Response::toString() const +{ + std::stringstream ss; + ss << "HTTP/1.1 " << status << ' ' << getStatusMessage() << "\r\n"; + for(auto& pair : headers) + ss << pair.first << ": " << pair.second << "\r\n"; + ss << "\r\n" << content; + return ss.str(); +} + +std::string Response::getStatusMessage() const +{ + switch(status) { + case 105: + return "Name Not Resolved"; + case 200: + return "OK"; + case 400: + return "Bad Request"; + case 404: + return "Not Found"; + case 408: + return "Request Timeout"; + case 500: + return "Internal Server Error"; + case 502: + return "Not Implemented"; + case 504: + return "Gateway Timeout"; + default: + return std::string(); + } +} + +void Response::setContentLength() +{ + setHeader("Content-Length", std::to_string(content.size())); +} + +std::string preprocessContent(const std::string& content, const std::string& path) +{ + const boost::filesystem::path directory(path); // Given path is assumed to be clean + + static const std::regex re( + "<\\!\\-\\-\\s*#include\\s+virtual\\s*\\=\\s*\"([^\"]*)\"\\s*\\-\\->" + ); + + boost::system::error_code e; + + std::string result; + + std::smatch match; + auto it = content.begin(); + while(std::regex_search(it, content.end(), match, re)) { + const auto last = it; + std::advance(it, match.position()); + result.append(last, it); + std::advance(it, match.length()); + + // Read the contents of the included file + std::ifstream ifs( + boost::filesystem::canonical(directory / std::string(match[1]), e).string(), + std::ios_base::in | std::ios_base::binary + ); + if(e || !ifs) + continue; + + std::string data; + ifs.seekg(0, ifs.end); + data.resize(ifs.tellg()); + ifs.seekg(0, ifs.beg); + ifs.read(&data[0], data.size()); + + result += data; + } + + // Append all of the remaining content + result.append(it, content.end()); + + return result; +} + +std::string getMimeType(const std::string& filename) +{ + const std::string ext = filename.substr(filename.find_last_of(".")); + if(ext == ".css") + return "text/css"; + else if(ext == ".js") + return "text/javascript"; + else if(ext == ".html" || ext == ".htm") + return "text/html"; + else + return "application/octet-stream"; +} + +} +} +} diff --git a/core/util/HTTP.h b/core/util/HTTP.h new file mode 100644 index 00000000..3f6415dc --- /dev/null +++ b/core/util/HTTP.h @@ -0,0 +1,104 @@ +#ifndef _HTTP_H__ +#define _HTTP_H__ + +#include +#include +#include + +namespace i2p { +namespace util { +namespace http { + +class Request { + + void parseRequestLine(const std::string& line); + + void parseHeaderLine(const std::string& line); + + void parseHeader(std::stringstream& ss); + + void setIsComplete(); +public: + Request() = default; + + Request(const std::string& data); + + std::string getMethod() const; + + std::string getUri() const; + + std::string getHost() const; + + int getPort() const; + + /** + * @throw std::out_of_range if no such header exists + */ + std::string getHeader(const std::string& name) const; + + std::string getContent() const; + + bool hasData() const; + + bool isComplete() const; + + void clear(); + + void update(const std::string& data); + +private: + std::string header_part; + + std::string method; + std::string uri; + std::string host; + std::string content; + int port; + std::map headers; + bool has_data; + bool has_header; + bool is_complete; +}; + +class Response { +public: + Response() = default; + + Response(int status, const std::string& content = ""); + + /** + * @note overrides existing header values with the same name + */ + void setHeader(const std::string& name, const std::string& value); + + std::string toString() const; + + /** + * @return the message associated with the satus of this response, or the + * empty string if the status number is invalid + */ + std::string getStatusMessage() const; + + void setContentLength(); + +private: + int status; + std::string content; + std::map headers; +}; + +/** + * Handle server side includes. + */ +std::string preprocessContent(const std::string& content, const std::string& path); + +/** + * @return the MIME type based on the extension of the given filename + */ +std::string getMimeType(const std::string& filename); + +} +} +} + +#endif // _HTTP_H__ diff --git a/core/util/util.cpp b/core/util/util.cpp index d55f380b..abc14c2b 100644 --- a/core/util/util.cpp +++ b/core/util/util.cpp @@ -18,7 +18,7 @@ #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__) #include #include -#elif defined(WIN32) +#elif defined(_WIN32) #include #include #include @@ -79,7 +79,7 @@ namespace config { strKey = strKey.substr(0, has_data); } -#ifdef WIN32 +#ifdef _WIN32 boost::to_lower(strKey); if(boost::algorithm::starts_with(strKey, "/")) strKey = "-" + strKey.substr(1); @@ -125,6 +125,11 @@ namespace config { return nDefault; } + bool HasArg(const std::string& strArg) + { + return mapArgs.count(strArg); + } + } namespace filesystem @@ -193,28 +198,34 @@ namespace filesystem return pathTunnelsConfigFile; } + boost::filesystem::path GetWebuiDataDir() + { + return GetDataDir() / "webui"; + } + boost::filesystem::path GetDefaultDataDir() { + // Custom path, or default path: // 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 or /var/lib/i2pd is system=1 - -#ifdef WIN32 + // Unix: ~/.i2pd +#ifdef I2PD_CUSTOM_DATA_PATH + return boost::filesystem::path(std::string(I2PD_CUSTOM_DATA_PATH)); +#else +#ifdef _WIN32 // Windows char localAppData[MAX_PATH]; SHGetFolderPath(NULL, CSIDL_APPDATA, 0, NULL, localAppData); return boost::filesystem::path(std::string(localAppData) + "\\" + appName); #else - if(i2p::util::config::GetArg("-service", 0)) // use system folder - return boost::filesystem::path(std::string ("/var/lib/") + appName); boost::filesystem::path pathRet; char* pszHome = getenv("HOME"); if(pszHome == NULL || strlen(pszHome) == 0) - pathRet = boost::filesystem::path("/"); + pathRet = boost::filesystem::path("/"); else pathRet = boost::filesystem::path(pszHome); -#ifdef MAC_OSX +#ifdef __APPLE__ // Mac pathRet /= "Library/Application Support"; boost::filesystem::create_directory(pathRet); @@ -223,6 +234,7 @@ namespace filesystem // Unix return pathRet / (std::string (".") + appName); #endif +#endif #endif } @@ -251,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 @@ -515,7 +567,7 @@ namespace net { return mtu; } -#elif defined(WIN32) +#elif defined(_WIN32) int GetMTUWindowsIpv4(sockaddr_in inputAddress, int fallback) { ULONG outBufLen = 0; @@ -556,7 +608,7 @@ namespace net { LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr; sockaddr_in* localInterfaceAddress = (sockaddr_in*) lpAddr; if(localInterfaceAddress->sin_addr.S_un.S_addr == inputAddress.sin_addr.S_un.S_addr) { - result = pAddresses->Mtu; + auto result = pAddresses->Mtu; FREE(pAddresses); return result; } @@ -618,7 +670,7 @@ namespace net { found_address = true; } } if (found_address) { - result = pAddresses->Mtu; + auto result = pAddresses->Mtu; FREE(pAddresses); pAddresses = nullptr; return result; diff --git a/core/util/util.h b/core/util/util.h index 29f68de5..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 @@ -80,6 +85,10 @@ namespace util */ boost::filesystem::path GetDefaultDataDir(); + /** + * @return the default directory for webui data + */ + boost::filesystem::path GetWebuiDataDir(); /** * Read a configuration file and store its contents in the given maps. @@ -91,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 diff --git a/doc/BUILDING.md b/doc/BUILDING.md index 107f5939..a6d03452 100644 --- a/doc/BUILDING.md +++ b/doc/BUILDING.md @@ -15,7 +15,22 @@ The client should now reseed by itself. By default, the web console is located at http://localhost:7070/. -For a list of cmake options, see build/BUILD_NOTES.md +For a list of cmake options, see BUILD_NOTES.md + +Installing the webui +==================== + +If you build from source the webui files will automatically be copied to your + i2pd data path. +In some cases (such as when using binaries), you may have to manually install the + webui. +For this, run: + +$ ./i2pd --install=/path/to/webui + +Or, if the current directory contains a folder named "webui": + +$ ./i2pd --install Building Unit Tests =================== @@ -28,3 +43,15 @@ On Ubuntu/Debian based To build the tests, run $ cmake .. -DWITH_TESTS=ON + +CMake Options +============ +Available cmake options: + +* CMAKE_BUILD_TYPE -- build profile (Debug/Release) +* WITH_AESNI -- AES-NI support (ON/OFF) +* WITH_HARDENING -- enable hardening features (ON/OFF) (gcc only) +* WITH_TESTS -- build tests (ON/OFF) +* WITH_BENCHMARK -- build bechmarking code (ON/OFF) +* WITH_OPTIMIZE -- enable optimization flags (ON/OFF) (not for MSVC) +* I2PD_DATA_DIR -- directory where i2pd will store data diff --git a/doc/BUILD_NOTES.md b/doc/BUILD_NOTES.md index c688a839..e3130142 100644 --- a/doc/BUILD_NOTES.md +++ b/doc/BUILD_NOTES.md @@ -1,12 +1,3 @@ -Build notes -=========== - -Available cmake options: - -* CMAKE_BUILD_TYPE -- build profile (Debug/Release) -* WITH_AESNI -- AES-NI support (ON/OFF) -* WITH_HARDENING -- enable hardening features (ON/OFF) (gcc only) - Debian ------ @@ -25,7 +16,7 @@ Optional packages: FreeBSD ------- -Branch 9.X has gcc v4.2, that knows nothing about required c++11 standart. +Branch 9.X has gcc v4.2, that knows nothing about required c++11 standard. Required ports: diff --git a/doc/COMMANDLINE.md b/doc/COMMANDLINE.md index a867113a..7fcbd835 100644 --- a/doc/COMMANDLINE.md +++ b/doc/COMMANDLINE.md @@ -35,3 +35,4 @@ Cmdline options * --conf= - Config file (default: ~/.i2pd/i2p.conf or /var/lib/i2pd/i2p.conf) This parameter will be silently ignored if the specified config file does not exist. Options specified on the command line take precedence over those in the config file. +* --install= - Installs the webui files, see BUILDING.md for details. diff --git a/tests/Utility.cpp b/tests/Utility.cpp index dbbe2bb6..c876d493 100644 --- a/tests/Utility.cpp +++ b/tests/Utility.cpp @@ -1,5 +1,6 @@ #include #include "util/util.h" +#include "util/HTTP.h" BOOST_AUTO_TEST_SUITE(UtilityTests) @@ -88,4 +89,116 @@ BOOST_AUTO_TEST_CASE(ParseUrlPassword) BOOST_CHECK_EQUAL(url("").pass_, ""); } +BOOST_AUTO_TEST_CASE(ParseHTTPRequestNoHeaders) +{ + Request req1("GET /index.html HTTP/1.1"); + Request req2("POST / HTTP/1.0\r\n"); + BOOST_CHECK_EQUAL(req1.getMethod(), "GET"); + BOOST_CHECK_EQUAL(req1.getUri(), "/index.html"); + BOOST_CHECK_EQUAL(req2.getMethod(), "POST"); + BOOST_CHECK_EQUAL(req2.getUri(), "/"); +} + +BOOST_AUTO_TEST_CASE(ParseHTTPRequestWithHeaders) +{ + Request req1( + "GET /index.html HTTP/1.1\r\n" + "Host: localhost\r\n" + ); + Request req2( + "POST / HTTP/1.1\r\n" + "Host: localhost:123 \r\n" + ); + BOOST_CHECK_EQUAL(req1.getHeader("Host"), "localhost"); + BOOST_CHECK_EQUAL(req2.getHeader("Host"), "localhost:123"); +} + +BOOST_AUTO_TEST_CASE(ParseHTTPRequestWithContent) +{ + Request req1( + "GET /index.html HTTP/1.1\r\n" + "Host: localhost\r\n\r\n" + "Random content." + ); + Request req2( + "GET /index.html HTTP/1.0\r\n\r\n" + "Random content.\r\nTest content." + ); + BOOST_CHECK_EQUAL(req1.getContent(), "Random content."); + BOOST_CHECK_EQUAL(req2.getContent(), "Random content.\r\nTest content."); +} + +BOOST_AUTO_TEST_CASE(ParseHTTPRequestWithPartialHeaders) +{ + Request req( + "GET /index.html HTTP/1.1\r\n" + "Host: local" + ); + BOOST_CHECK(req.hasData()); + BOOST_CHECK(!req.isComplete()); + BOOST_CHECK_EQUAL(req.getMethod(), "GET"); + req.update("host\r\n"); + BOOST_CHECK(req.isComplete()); + BOOST_CHECK_EQUAL(req.getHeader("Host"), "localhost"); + req.clear(); + BOOST_CHECK(!req.hasData()); +} + +BOOST_AUTO_TEST_CASE(ParseHTTPRequestHeadersFirst) +{ + Request req( + "GET /index.html HTTP/1.1\r\n" + "Content-Length: 5\r\n" + "Host: localhost\r\n\r\n" + ); + + BOOST_CHECK_EQUAL(req.getMethod(), "GET"); + BOOST_CHECK_EQUAL(req.getHeader("Content-Length"), "5"); + BOOST_CHECK_EQUAL(req.getHeader("Host"), "localhost"); + + BOOST_CHECK(!req.isComplete()); + req.update("ab"); + BOOST_CHECK(!req.isComplete()); + req.update("cde"); + BOOST_CHECK(req.isComplete()); + + BOOST_CHECK_EQUAL(req.getContent(), "abcde"); +} + +BOOST_AUTO_TEST_CASE(HTTPResponseStatusMessage) +{ + BOOST_CHECK_EQUAL(Response(0).getStatusMessage(), ""); + BOOST_CHECK_EQUAL(Response(105).getStatusMessage(), "Name Not Resolved"); + BOOST_CHECK_EQUAL(Response(200).getStatusMessage(), "OK"); + BOOST_CHECK_EQUAL(Response(400).getStatusMessage(), "Bad Request"); + BOOST_CHECK_EQUAL(Response(404).getStatusMessage(), "Not Found"); + BOOST_CHECK_EQUAL(Response(408).getStatusMessage(), "Request Timeout"); + BOOST_CHECK_EQUAL(Response(500).getStatusMessage(), "Internal Server Error"); + BOOST_CHECK_EQUAL(Response(502).getStatusMessage(), "Not Implemented"); + BOOST_CHECK_EQUAL(Response(504).getStatusMessage(), "Gateway Timeout"); +} + +BOOST_AUTO_TEST_CASE(WriteHTTPResponse) +{ + Response rsp(200); + rsp.setHeader("Connection", "close"); + BOOST_CHECK_EQUAL( + rsp.toString(), + "HTTP/1.1 200 OK\r\n" + "Connection: close\r\n\r\n" + ); +} + +BOOST_AUTO_TEST_CASE(WriteHTTPResponseWithContent) +{ + Response rsp(200, "Test content."); + rsp.setHeader("Connection", "close"); + BOOST_CHECK_EQUAL( + rsp.toString(), + "HTTP/1.1 200 OK\r\n" + "Connection: close\r\n\r\n" + "Test content." + ); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/webui/404.html b/webui/404.html new file mode 100644 index 00000000..41174fe6 --- /dev/null +++ b/webui/404.html @@ -0,0 +1,24 @@ + + + +404 - Page not found + + + + + + + +
+

404 - Page not found

+
+ +
+

The page you were looking for could not be found.

+
+ + + + + + diff --git a/webui/css/main.css b/webui/css/main.css new file mode 100644 index 00000000..8aa8b289 --- /dev/null +++ b/webui/css/main.css @@ -0,0 +1,76 @@ +body { + font-family: sans-serif; + margin: 0px; + padding: 0px; +} + +.header { + margin: 0px 150px; + text-align: center; + padding: 2.5em 2em 0px; + border-bottom: 1px solid #EEE; +} + +h1 { + color: #333; + font-weight: 300; +} + +h2 { + color: #ccc; + font-weight: 300; +} + +.content { + margin: 0px 150px auto; + padding: 0px 2em; + line-height: 1.6em; +} + +.content-subhead { + margin: 50px 0px 20px; + font-weight: 300; + color: #888; +} + +#menu { + width: 150px; + position: fixed; + top: 0px; + bottom: 0px; + z-index: 1000; + background: #191818 none repeat scroll 0% 0%; + overflow-y: auto; + display: block; + margin: 0px; + padding: 0px; +} + +.menu-heading { + background: #1f8dd6; + display: block; + padding: 10px; + color: white; + font-variant: small-caps; + font-size: 23px; + font-weight: bold; +} + +.menu-list { + list-style: none; + margin: 0; + padding: 0; +} + +.menu-item { + padding: 0; + margin: 0; + padding: 10px; +} + +.menu-link { + text-decoration: none; + color: #ccc; + font-weight: bold; + font-size: 15px; +} diff --git a/webui/footer.html b/webui/footer.html new file mode 100644 index 00000000..0f372832 --- /dev/null +++ b/webui/footer.html @@ -0,0 +1,6 @@ +
+ +
+ diff --git a/webui/help.html b/webui/help.html new file mode 100644 index 00000000..09f519b2 --- /dev/null +++ b/webui/help.html @@ -0,0 +1,27 @@ + + + +Purple I2P 0.10.0 Webconsole + + + + + + +
+

I2P help

+
+ +
+

Need help? Join us at IRC: #i2pd-dev at irc.freenode.net

+

+ i2pd at GitHub +

+

I2P Project

+
+ + + + + + diff --git a/webui/index.html b/webui/index.html new file mode 100644 index 00000000..688551bd --- /dev/null +++ b/webui/index.html @@ -0,0 +1,101 @@ + + + +Purple I2P 0.10.0 Webconsole + + + + + + + + +
+

i2pd router console

+

Version: , uptime:

+

Network status:

+

+ + + + +

+
+ +
+

+ Tunnels participating: +

+

+ Tunnel create success rate: +

+

+ Active peers: +

+

+ Known peers: +

+

+ Bandwidth: + in Bps / + out Bps +

+
+ + + + + + diff --git a/webui/javascript/I2PControl.js b/webui/javascript/I2PControl.js new file mode 100644 index 00000000..43db6eaf --- /dev/null +++ b/webui/javascript/I2PControl.js @@ -0,0 +1,102 @@ +var I2PControl = I2PControl || {} + +I2PControl.Session = function(password) { + this.token = ""; + this.ready = false; + this.error = false; + this.password = password; +}; + +I2PControl.Session.prototype = { + + request : function(method, params, handler) { + var request = new XMLHttpRequest(); + request.open("POST", "", true); + request.setRequestHeader('Content-Type', 'application/json'); + var self = this; + request.onreadystatechange = function() { + if(this.readyState == 4 && this.status == "200" && this.responseText != "") { + var data = JSON.parse(this.responseText); + if(data.hasOwnProperty("error")) { + if(data["error"]["code"] == -32003 || data["error"]["code"] == -32004) { + // Get a new token and resend the request + self.start(function() { + self.request(method, params, handler); + }); + return; + } + // Cannot fix the error, report it + self.error = data["error"]; + } + handler(data.result, self); + } + }; + if(this.token != "") + params["Token"] = this.token; + + var rpc = { + "id" : 0, + "method" : method , + "params" : params, + "jsonrpc": "2.0" + } + request.send(JSON.stringify(rpc)); + }, + + start : function(onReady) { + var self = this; + + var handleAuthenticate = function(result) { + self.token = result["Token"]; + self.ready = true; + onReady(); + }; + + this.request( + "Authenticate", + {"API" : 1, "Password" : this.password}, + handleAuthenticate + ); + }, + +}; + +I2PControl.statusToString = function(status) { + switch(status) { + case 0: return "OK"; + case 1: return "TESTING"; + case 2: return "FIREWALLED"; + case 3: return "HIDDEN"; + case 4: return "WARN_FIREWALLED_AND_FAST"; + case 5: return "WARN_FIREWALLED_AND_FLOODFILL"; + case 6: return "WARN_FIREWALLED_WITH_INBOUND_TCP"; + case 7: return "WARN_FIREWALLED_WITH_UDP_DISABLED"; + case 8: return "ERROR_I2CP"; + case 9: return "ERROR_CLOCK_SKEW"; + case 10: return "ERROR_PRIVATE_TCP_ADDRESS"; + case 11: return "ERROR_SYMMETRIC_NAT"; + case 12: return "ERROR_UDP_PORT_IN_USE"; + case 13: return "ERROR_NO_ACTIVE_PEERS_CHECK_CONNECTION_AND_FIREWALL"; + case 14: return "ERROR_UDP_DISABLED_AND_TCP_UNSET"; + default: return "UNKNOWN"; + } +}; + +I2PControl.msToString = function(mseconds) { + var seconds = mseconds / 1000; + var numdays = Math.floor(seconds / 86400); + var numhours = Math.floor((seconds % 86400) / 3600); + var numminutes = Math.floor(((seconds % 86400) % 3600) / 60); + var numseconds = ((seconds % 86400) % 3600) % 60; + + return numdays + "d " + numhours + "h " + numminutes + "m " + numseconds + "s"; +} + +I2PControl.updateDocument = function(values) { + + for(id in values) { + if(!values.hasOwnProperty(id)) + continue; + document.getElementById(id).innerHTML = values[id]; + } +}; diff --git a/webui/menu.html b/webui/menu.html new file mode 100644 index 00000000..f8696aef --- /dev/null +++ b/webui/menu.html @@ -0,0 +1,20 @@ + diff --git a/webui/netdb.html b/webui/netdb.html new file mode 100644 index 00000000..321c3751 --- /dev/null +++ b/webui/netdb.html @@ -0,0 +1,59 @@ + + + +Purple I2P 0.10.0 Webconsole + + + + + + + + +
+

i2pd router console

+

Network Database Information

+
+ +
+

Active peers:

+

Known peers:

+

Floodfills:

+
+

LeaseSets:

+
+ + + + + + diff --git a/webui/tunnels.html b/webui/tunnels.html new file mode 100644 index 00000000..53221f17 --- /dev/null +++ b/webui/tunnels.html @@ -0,0 +1,87 @@ + + + +Purple I2P 0.10.0 Webconsole + + + + + + + + +
+

i2pd router console

+

Tunnel Information

+
+ +
+ +

Inbound Tunnels

+ + + + + + + + +
Tunnel IDStatusOverview
+

Outbound Tunnels

+ + + + + + + + +
Tunnel IDStatusOverview
+
+ + + + + +