mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1504 lines
59 KiB
1504 lines
59 KiB
5 years ago
|
/*
|
||
3 years ago
|
* Copyright (c) 2013-2022, The PurpleI2P Project
|
||
5 years ago
|
*
|
||
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||
|
*
|
||
|
* See full license text in LICENSE file at top of project tree
|
||
|
*/
|
||
|
|
||
9 years ago
|
#include <iomanip>
|
||
9 years ago
|
#include <sstream>
|
||
|
#include <thread>
|
||
|
#include <memory>
|
||
|
|
||
|
#include <boost/asio.hpp>
|
||
6 years ago
|
#include <boost/algorithm/string.hpp>
|
||
9 years ago
|
|
||
9 years ago
|
#include "Base.h"
|
||
9 years ago
|
#include "FS.h"
|
||
11 years ago
|
#include "Log.h"
|
||
9 years ago
|
#include "Config.h"
|
||
11 years ago
|
#include "Tunnel.h"
|
||
|
#include "Transports.h"
|
||
8 years ago
|
#include "NetDb.hpp"
|
||
9 years ago
|
#include "HTTP.h"
|
||
9 years ago
|
#include "LeaseSet.h"
|
||
10 years ago
|
#include "Destination.h"
|
||
10 years ago
|
#include "RouterContext.h"
|
||
10 years ago
|
#include "ClientContext.h"
|
||
10 years ago
|
#include "HTTPServer.h"
|
||
9 years ago
|
#include "Daemon.h"
|
||
9 years ago
|
#include "util.h"
|
||
5 years ago
|
#include "ECIESX25519AEADRatchetSession.h"
|
||
4 years ago
|
#include "I18N.h"
|
||
|
|
||
8 years ago
|
#ifdef WIN32_APP
|
||
4 years ago
|
#include "Win32App.h"
|
||
8 years ago
|
#endif
|
||
11 years ago
|
|
||
3 years ago
|
// For image, style and info
|
||
10 years ago
|
#include "version.h"
|
||
3 years ago
|
#include "HTTPServerResources.h"
|
||
10 years ago
|
|
||
9 years ago
|
namespace i2p {
|
||
|
namespace http {
|
||
3 years ago
|
static void LoadExtCSS ()
|
||
|
{
|
||
|
std::stringstream s;
|
||
|
std::string styleFile = i2p::fs::DataDirPath ("webconsole/style.css");
|
||
|
if (i2p::fs::Exists(styleFile)) {
|
||
|
std::ifstream f(styleFile, std::ifstream::binary);
|
||
|
s << f.rdbuf();
|
||
|
externalCSS = s.str();
|
||
3 years ago
|
} else if (externalCSS.length() != 0) { // clean up external style if file was removed
|
||
|
externalCSS = "";
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
static void GetStyles (std::stringstream& s)
|
||
|
{
|
||
3 years ago
|
if (externalCSS.length() != 0)
|
||
|
s << "<style>\r\n" << externalCSS << "</style>\r\n";
|
||
|
else
|
||
|
s << internalCSS;
|
||
4 years ago
|
}
|
||
9 years ago
|
|
||
|
const char HTTP_PAGE_TUNNELS[] = "tunnels";
|
||
|
const char HTTP_PAGE_TRANSIT_TUNNELS[] = "transit_tunnels";
|
||
8 years ago
|
const char HTTP_PAGE_TRANSPORTS[] = "transports";
|
||
9 years ago
|
const char HTTP_PAGE_LOCAL_DESTINATIONS[] = "local_destinations";
|
||
|
const char HTTP_PAGE_LOCAL_DESTINATION[] = "local_destination";
|
||
8 years ago
|
const char HTTP_PAGE_I2CP_LOCAL_DESTINATION[] = "i2cp_local_destination";
|
||
9 years ago
|
const char HTTP_PAGE_SAM_SESSIONS[] = "sam_sessions";
|
||
|
const char HTTP_PAGE_SAM_SESSION[] = "sam_session";
|
||
|
const char HTTP_PAGE_I2P_TUNNELS[] = "i2p_tunnels";
|
||
9 years ago
|
const char HTTP_PAGE_COMMANDS[] = "commands";
|
||
8 years ago
|
const char HTTP_PAGE_LEASESETS[] = "leasesets";
|
||
8 years ago
|
const char HTTP_COMMAND_ENABLE_TRANSIT[] = "enable_transit";
|
||
|
const char HTTP_COMMAND_DISABLE_TRANSIT[] = "disable_transit";
|
||
9 years ago
|
const char HTTP_COMMAND_SHUTDOWN_START[] = "shutdown_start";
|
||
|
const char HTTP_COMMAND_SHUTDOWN_CANCEL[] = "shutdown_cancel";
|
||
7 years ago
|
const char HTTP_COMMAND_SHUTDOWN_NOW[] = "terminate";
|
||
9 years ago
|
const char HTTP_COMMAND_RUN_PEER_TEST[] = "run_peer_test";
|
||
8 years ago
|
const char HTTP_COMMAND_RELOAD_CONFIG[] = "reload_config";
|
||
7 years ago
|
const char HTTP_COMMAND_LOGLEVEL[] = "set_loglevel";
|
||
5 years ago
|
const char HTTP_COMMAND_KILLSTREAM[] = "closestream";
|
||
5 years ago
|
const char HTTP_COMMAND_LIMITTRANSIT[] = "limittransit";
|
||
4 years ago
|
const char HTTP_COMMAND_GET_REG_STRING[] = "get_reg_string";
|
||
3 years ago
|
const char HTTP_COMMAND_SETLANGUAGE[] = "setlanguage";
|
||
3 years ago
|
const char HTTP_COMMAND_RELOAD_CSS[] = "reload_css";
|
||
10 years ago
|
const char HTTP_PARAM_SAM_SESSION_ID[] = "id";
|
||
9 years ago
|
const char HTTP_PARAM_ADDRESS[] = "address";
|
||
11 years ago
|
|
||
3 years ago
|
static std::string ConvertTime (uint64_t time)
|
||
|
{
|
||
|
lldiv_t divTime = lldiv(time, 1000);
|
||
|
time_t t = divTime.quot;
|
||
|
struct tm *tm = localtime(&t);
|
||
|
char date[128];
|
||
|
snprintf(date, sizeof(date), "%02d/%02d/%d %02d:%02d:%02d.%03lld", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec, divTime.rem);
|
||
|
return date;
|
||
|
}
|
||
6 years ago
|
|
||
7 years ago
|
static void ShowUptime (std::stringstream& s, int seconds)
|
||
8 years ago
|
{
|
||
9 years ago
|
int num;
|
||
11 years ago
|
|
||
9 years ago
|
if ((num = seconds / 86400) > 0) {
|
||
3 years ago
|
s << num << " " << tr("day", "days", num) << ", ";
|
||
9 years ago
|
seconds -= num * 86400;
|
||
11 years ago
|
}
|
||
9 years ago
|
if ((num = seconds / 3600) > 0) {
|
||
3 years ago
|
s << num << " " << tr("hour", "hours", num) << ", ";
|
||
9 years ago
|
seconds -= num * 3600;
|
||
11 years ago
|
}
|
||
9 years ago
|
if ((num = seconds / 60) > 0) {
|
||
3 years ago
|
s << num << " " << tr("minute", "minutes", num) << ", ";
|
||
9 years ago
|
seconds -= num * 60;
|
||
9 years ago
|
}
|
||
3 years ago
|
s << seconds << " " << tr("second", "seconds", seconds);
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
static void ShowTraffic (std::stringstream& s, uint64_t bytes)
|
||
|
{
|
||
|
s << std::fixed << std::setprecision(2);
|
||
|
auto numKBytes = (double) bytes / 1024;
|
||
|
if (numKBytes < 1024)
|
||
3 years ago
|
s << numKBytes << " " << tr(/* tr: Kibibit */ "KiB");
|
||
8 years ago
|
else if (numKBytes < 1024 * 1024)
|
||
3 years ago
|
s << numKBytes / 1024 << " " << tr(/* tr: Mebibit */ "MiB");
|
||
8 years ago
|
else
|
||
3 years ago
|
s << numKBytes / 1024 / 1024 << " " << tr(/* tr: Gibibit */ "GiB");
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
static void ShowTunnelDetails (std::stringstream& s, enum i2p::tunnel::TunnelState eState, bool explr, int bytes)
|
||
11 years ago
|
{
|
||
4 years ago
|
std::string state, stateText;
|
||
9 years ago
|
switch (eState) {
|
||
|
case i2p::tunnel::eTunnelStateBuildReplyReceived :
|
||
4 years ago
|
case i2p::tunnel::eTunnelStatePending : state = "building"; break;
|
||
9 years ago
|
case i2p::tunnel::eTunnelStateBuildFailed :
|
||
|
case i2p::tunnel::eTunnelStateTestFailed :
|
||
4 years ago
|
case i2p::tunnel::eTunnelStateFailed : state = "failed"; break;
|
||
|
case i2p::tunnel::eTunnelStateExpiring : state = "expiring"; break;
|
||
9 years ago
|
case i2p::tunnel::eTunnelStateEstablished : state = "established"; break;
|
||
|
default: state = "unknown"; break;
|
||
11 years ago
|
}
|
||
4 years ago
|
|
||
|
if (state == "building") stateText = tr("building");
|
||
|
else if (state == "failed") stateText = tr("failed");
|
||
|
else if (state == "expiring") stateText = tr("expiring");
|
||
|
else if (state == "established") stateText = tr("established");
|
||
|
else stateText = tr("unknown");
|
||
|
|
||
|
s << "<span class=\"tunnel " << state << "\"> " << stateText << ((explr) ? " (" + tr("exploratory") + ")" : "") << "</span>, ";
|
||
3 years ago
|
s << " " << (int) (bytes / 1024) << " " << tr(/* tr: Kibibit */ "KiB") << "\r\n";
|
||
11 years ago
|
}
|
||
|
|
||
7 years ago
|
static void SetLogLevel (const std::string& level)
|
||
|
{
|
||
|
if (level == "none" || level == "error" || level == "warn" || level == "info" || level == "debug")
|
||
|
i2p::log::Logger().SetLogLevel(level);
|
||
|
else {
|
||
3 years ago
|
LogPrint(eLogError, "HTTPServer: Unknown loglevel set attempted");
|
||
7 years ago
|
return;
|
||
|
}
|
||
|
i2p::log::Logger().Reopen ();
|
||
|
}
|
||
|
|
||
8 years ago
|
static void ShowPageHead (std::stringstream& s)
|
||
11 years ago
|
{
|
||
4 years ago
|
std::string webroot; i2p::config::GetOption("http.webroot", webroot);
|
||
|
|
||
|
// Page language
|
||
3 years ago
|
std::string currLang = i2p::client::context.GetLanguage ()->GetLanguage(); // get current used language
|
||
3 years ago
|
auto it = i2p::i18n::languages.find(currLang);
|
||
|
std::string langCode = it->second.ShortCode;
|
||
4 years ago
|
|
||
9 years ago
|
s <<
|
||
|
"<!DOCTYPE html>\r\n"
|
||
4 years ago
|
"<html lang=\"" << langCode << "\">\r\n"
|
||
9 years ago
|
" <head>\r\n" /* TODO: Find something to parse html/template system. This is horrible. */
|
||
|
" <meta charset=\"UTF-8\">\r\n"
|
||
5 years ago
|
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\r\n"
|
||
9 years ago
|
" <link rel=\"shortcut icon\" href=\"" << itoopieFavicon << "\">\r\n"
|
||
3 years ago
|
" <title>Purple I2P Webconsole</title>\r\n";
|
||
4 years ago
|
GetStyles(s);
|
||
9 years ago
|
s <<
|
||
4 years ago
|
"</head>\r\n"
|
||
9 years ago
|
"<body>\r\n"
|
||
4 years ago
|
"<div class=\"header\">" << tr("<b>i2pd</b> webconsole") << "</div>\r\n"
|
||
5 years ago
|
"<div class=\"wrapper\">\r\n"
|
||
|
"<div class=\"menu\">\r\n"
|
||
3 years ago
|
" <a href=\"" << webroot << "\">" << tr("Main page") << "</a><br><br>\r\n"
|
||
|
" <a href=\"" << webroot << "?page=" << HTTP_PAGE_COMMANDS << "\">" << tr("Router commands") << "</a><br>\r\n"
|
||
3 years ago
|
" <a href=\"" << webroot << "?page=" << HTTP_PAGE_LOCAL_DESTINATIONS << "\">" << tr("Local Destinations") << "</a><br>\r\n";
|
||
5 years ago
|
if (i2p::context.IsFloodfill ())
|
||
3 years ago
|
s << " <a href=\"" << webroot << "?page=" << HTTP_PAGE_LEASESETS << "\">" << tr("LeaseSets") << "</a><br>\r\n";
|
||
5 years ago
|
s <<
|
||
3 years ago
|
" <a href=\"" << webroot << "?page=" << HTTP_PAGE_TUNNELS << "\">" << tr("Tunnels") << "</a><br>\r\n";
|
||
|
if (i2p::context.AcceptsTunnels () || i2p::tunnel::tunnels.CountTransitTunnels())
|
||
|
s << " <a href=\"" << webroot << "?page=" << HTTP_PAGE_TRANSIT_TUNNELS << "\">" << tr("Transit Tunnels") << "</a><br>\r\n";
|
||
|
s <<
|
||
3 years ago
|
" <a href=\"" << webroot << "?page=" << HTTP_PAGE_TRANSPORTS << "\">" << tr ("Transports") << "</a><br>\r\n"
|
||
|
" <a href=\"" << webroot << "?page=" << HTTP_PAGE_I2P_TUNNELS << "\">" << tr("I2P tunnels") << "</a><br>\r\n";
|
||
8 years ago
|
if (i2p::client::context.GetSAMBridge ())
|
||
3 years ago
|
s << " <a href=\"" << webroot << "?page=" << HTTP_PAGE_SAM_SESSIONS << "\">" << tr("SAM sessions") << "</a><br>\r\n";
|
||
8 years ago
|
s <<
|
||
9 years ago
|
"</div>\r\n"
|
||
5 years ago
|
"<div class=\"content\">";
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
static void ShowPageTail (std::stringstream& s)
|
||
11 years ago
|
{
|
||
9 years ago
|
s <<
|
||
5 years ago
|
"</div>\r\n</div>\r\n"
|
||
9 years ago
|
"</body>\r\n"
|
||
|
"</html>\r\n";
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
static void ShowError(std::stringstream& s, const std::string& string)
|
||
11 years ago
|
{
|
||
4 years ago
|
s << "<b>" << tr("ERROR") << ":</b> " << string << "<br>\r\n";
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
4 years ago
|
static void ShowNetworkStatus (std::stringstream& s, RouterStatus status)
|
||
11 years ago
|
{
|
||
4 years ago
|
switch (status)
|
||
10 years ago
|
{
|
||
4 years ago
|
case eRouterStatusOK: s << tr("OK"); break;
|
||
|
case eRouterStatusTesting: s << tr("Testing"); break;
|
||
|
case eRouterStatusFirewalled: s << tr("Firewalled"); break;
|
||
|
case eRouterStatusUnknown: s << tr("Unknown"); break;
|
||
|
case eRouterStatusProxy: s << tr("Proxy"); break;
|
||
|
case eRouterStatusMesh: s << tr("Mesh"); break;
|
||
7 years ago
|
case eRouterStatusError:
|
||
|
{
|
||
4 years ago
|
s << tr("Error");
|
||
8 years ago
|
switch (i2p::context.GetError ())
|
||
|
{
|
||
|
case eRouterErrorClockSkew:
|
||
4 years ago
|
s << " - " << tr("Clock skew");
|
||
8 years ago
|
break;
|
||
4 years ago
|
case eRouterErrorOffline:
|
||
4 years ago
|
s << " - " << tr("Offline");
|
||
4 years ago
|
break;
|
||
4 years ago
|
case eRouterErrorSymmetricNAT:
|
||
4 years ago
|
s << " - " << tr("Symmetric NAT");
|
||
4 years ago
|
break;
|
||
7 years ago
|
default: ;
|
||
|
}
|
||
8 years ago
|
break;
|
||
7 years ago
|
}
|
||
4 years ago
|
default: s << tr("Unknown");
|
||
7 years ago
|
}
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
void ShowStatus (std::stringstream& s, bool includeHiddenContent, i2p::http::OutputFormatEnum outputFormat)
|
||
|
{
|
||
4 years ago
|
s << "<b>" << tr("Uptime") << ":</b> ";
|
||
4 years ago
|
ShowUptime(s, i2p::context.GetUptime ());
|
||
9 years ago
|
s << "<br>\r\n";
|
||
3 years ago
|
s << "<b>" << tr("Network status") << ":</b> ";
|
||
|
ShowNetworkStatus (s, i2p::context.GetStatus ());
|
||
|
s << "<br>\r\n";
|
||
4 years ago
|
if (i2p::context.SupportsV6 ())
|
||
|
{
|
||
4 years ago
|
s << "<b>" << tr("Network status v6") << ":</b> ";
|
||
4 years ago
|
ShowNetworkStatus (s, i2p::context.GetStatusV6 ());
|
||
|
s << "<br>\r\n";
|
||
4 years ago
|
}
|
||
7 years ago
|
#if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY))
|
||
4 years ago
|
if (auto remains = Daemon.gracefulShutdownInterval) {
|
||
4 years ago
|
s << "<b>" << tr("Stopping in") << ":</b> ";
|
||
4 years ago
|
ShowUptime(s, remains);
|
||
|
s << "<br>\r\n";
|
||
4 years ago
|
}
|
||
4 years ago
|
#elif defined(WIN32_APP)
|
||
|
if (i2p::win32::g_GracefulShutdownEndtime != 0) {
|
||
|
uint16_t remains = (i2p::win32::g_GracefulShutdownEndtime - GetTickCount()) / 1000;
|
||
4 years ago
|
s << "<b>" << tr("Stopping in") << ":</b> ";
|
||
4 years ago
|
ShowUptime(s, remains);
|
||
|
s << "<br>\r\n";
|
||
|
}
|
||
8 years ago
|
#endif
|
||
9 years ago
|
auto family = i2p::context.GetFamily ();
|
||
|
if (family.length () > 0)
|
||
4 years ago
|
s << "<b>"<< tr("Family") << ":</b> " << family << "<br>\r\n";
|
||
|
s << "<b>" << tr("Tunnel creation success rate") << ":</b> " << i2p::tunnel::tunnels.GetTunnelCreationSuccessRate () << "%<br>\r\n";
|
||
|
s << "<b>" << tr("Received") << ":</b> ";
|
||
8 years ago
|
ShowTraffic (s, i2p::transport::transports.GetTotalReceivedBytes ());
|
||
3 years ago
|
s << " (" << (double) i2p::transport::transports.GetInBandwidth () / 1024 << " " << tr(/* tr: Kibibit/s */ "KiB/s") << ")<br>\r\n";
|
||
4 years ago
|
s << "<b>" << tr("Sent") << ":</b> ";
|
||
8 years ago
|
ShowTraffic (s, i2p::transport::transports.GetTotalSentBytes ());
|
||
3 years ago
|
s << " (" << (double) i2p::transport::transports.GetOutBandwidth () / 1024 << " " << tr(/* tr: Kibibit/s */ "KiB/s") << ")<br>\r\n";
|
||
4 years ago
|
s << "<b>" << tr("Transit") << ":</b> ";
|
||
8 years ago
|
ShowTraffic (s, i2p::transport::transports.GetTotalTransitTransmittedBytes ());
|
||
3 years ago
|
s << " (" << (double) i2p::transport::transports.GetTransitBandwidth () / 1024 << " " << tr(/* tr: Kibibit/s */ "KiB/s") << ")<br>\r\n";
|
||
4 years ago
|
s << "<b>" << tr("Data path") << ":</b> " << i2p::fs::GetUTF8DataDir() << "<br>\r\n";
|
||
5 years ago
|
s << "<div class='slide'>";
|
||
3 years ago
|
if ((outputFormat == OutputFormatEnum::forWebConsole) || !includeHiddenContent) {
|
||
4 years ago
|
s << "<label for=\"slide-info\">" << tr("Hidden content. Press on text to see.") << "</label>\r\n<input type=\"checkbox\" id=\"slide-info\" />\r\n<div class=\"slidecontent\">\r\n";
|
||
5 years ago
|
}
|
||
3 years ago
|
if (includeHiddenContent) {
|
||
4 years ago
|
s << "<b>" << tr("Router Ident") << ":</b> " << i2p::context.GetRouterInfo().GetIdentHashBase64() << "<br>\r\n";
|
||
5 years ago
|
if (!i2p::context.GetRouterInfo().GetProperty("family").empty())
|
||
4 years ago
|
s << "<b>" << tr("Router Family") << ":</b> " << i2p::context.GetRouterInfo().GetProperty("family") << "<br>\r\n";
|
||
|
s << "<b>" << tr("Router Caps") << ":</b> " << i2p::context.GetRouterInfo().GetProperty("caps") << "<br>\r\n";
|
||
|
s << "<b>" << tr("Version") << ":</b> " VERSION "<br>\r\n";
|
||
|
s << "<b>"<< tr("Our external address") << ":</b>" << "<br>\r\n<table class=\"extaddr\"><tbody>\r\n";
|
||
7 years ago
|
for (const auto& address : i2p::context.GetRouterInfo().GetAddresses())
|
||
|
{
|
||
3 years ago
|
s << "<tr>\r\n<td>";
|
||
7 years ago
|
switch (address->transportStyle)
|
||
|
{
|
||
|
case i2p::data::RouterInfo::eTransportNTCP:
|
||
3 years ago
|
s << "NTCP2";
|
||
|
break;
|
||
7 years ago
|
case i2p::data::RouterInfo::eTransportSSU:
|
||
3 years ago
|
s << "SSU";
|
||
|
break;
|
||
3 years ago
|
case i2p::data::RouterInfo::eTransportSSU2:
|
||
3 years ago
|
s << "SSU2";
|
||
3 years ago
|
break;
|
||
7 years ago
|
default:
|
||
3 years ago
|
s << tr("Unknown");
|
||
7 years ago
|
}
|
||
3 years ago
|
if (address->IsV6 ())
|
||
3 years ago
|
{
|
||
3 years ago
|
if (address->IsV4 ()) s << "v4";
|
||
3 years ago
|
s << "v6";
|
||
3 years ago
|
}
|
||
3 years ago
|
s << "</td>\r\n";
|
||
|
if (address->published)
|
||
|
s << "<td>" << address->host.to_string() << ":" << address->port << "</td>\r\n";
|
||
|
else
|
||
3 years ago
|
{
|
||
3 years ago
|
s << "<td>" << tr("supported");
|
||
|
if (address->port)
|
||
3 years ago
|
s << " :" << address->port;
|
||
3 years ago
|
s << "</td>\r\n";
|
||
3 years ago
|
}
|
||
3 years ago
|
s << "</tr>\r\n";
|
||
7 years ago
|
}
|
||
5 years ago
|
s << "</tbody></table>\r\n";
|
||
5 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n</div>\r\n";
|
||
3 years ago
|
if (outputFormat == OutputFormatEnum::forQtUi) {
|
||
5 years ago
|
s << "<br>";
|
||
|
}
|
||
4 years ago
|
s << "<b>" << tr("Routers") << ":</b> " << i2p::data::netdb.GetNumRouters () << " ";
|
||
|
s << "<b>" << tr("Floodfills") << ":</b> " << i2p::data::netdb.GetNumFloodfills () << " ";
|
||
|
s << "<b>" << tr("LeaseSets") << ":</b> " << i2p::data::netdb.GetNumLeaseSets () << "<br>\r\n";
|
||
9 years ago
|
|
||
9 years ago
|
size_t clientTunnelCount = i2p::tunnel::tunnels.CountOutboundTunnels();
|
||
|
clientTunnelCount += i2p::tunnel::tunnels.CountInboundTunnels();
|
||
|
size_t transitTunnelCount = i2p::tunnel::tunnels.CountTransitTunnels();
|
||
8 years ago
|
|
||
4 years ago
|
s << "<b>" << tr("Client Tunnels") << ":</b> " << std::to_string(clientTunnelCount) << " ";
|
||
|
s << "<b>" << tr("Transit Tunnels") << ":</b> " << std::to_string(transitTunnelCount) << "<br>\r\n<br>\r\n";
|
||
7 years ago
|
|
||
3 years ago
|
if (outputFormat==OutputFormatEnum::forWebConsole) {
|
||
3 years ago
|
bool httpproxy = i2p::client::context.GetHttpProxy () ? true : false;
|
||
|
bool socksproxy = i2p::client::context.GetSocksProxy () ? true : false;
|
||
|
bool bob = i2p::client::context.GetBOBCommandChannel () ? true : false;
|
||
|
bool sam = i2p::client::context.GetSAMBridge () ? true : false;
|
||
|
bool i2cp = i2p::client::context.GetI2CPServer () ? true : false;
|
||
|
bool i2pcontrol; i2p::config::GetOption("i2pcontrol.enabled", i2pcontrol);
|
||
4 years ago
|
s << "<table class=\"services\"><caption>" << tr("Services") << "</caption><tbody>\r\n";
|
||
3 years ago
|
s << "<tr><td>" << "HTTP " << tr("Proxy") << "</td><td class='" << (httpproxy ? "enabled" : "disabled") << "'>" << (httpproxy ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
|
||
|
s << "<tr><td>" << "SOCKS " << tr("Proxy") << "</td><td class='" << (socksproxy ? "enabled" : "disabled") << "'>" << (socksproxy ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
|
||
|
s << "<tr><td>" << "BOB" << "</td><td class='" << (bob ? "enabled" : "disabled") << "'>" << (bob ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
|
||
|
s << "<tr><td>" << "SAM" << "</td><td class='" << (sam ? "enabled" : "disabled") << "'>" << (sam ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
|
||
|
s << "<tr><td>" << "I2CP" << "</td><td class='" << (i2cp ? "enabled" : "disabled") << "'>" << (i2cp ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
|
||
|
s << "<tr><td>" << "I2PControl" << "</td><td class='" << (i2pcontrol ? "enabled" : "disabled") << "'>" << (i2pcontrol ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
|
||
5 years ago
|
s << "</tbody></table>\r\n";
|
||
5 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
void ShowLocalDestinations (std::stringstream& s)
|
||
10 years ago
|
{
|
||
6 years ago
|
std::string webroot; i2p::config::GetOption("http.webroot", webroot);
|
||
4 years ago
|
s << "<b>" << tr("Local Destinations") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
10 years ago
|
for (auto& it: i2p::client::context.GetDestinations ())
|
||
10 years ago
|
{
|
||
7 years ago
|
auto ident = it.second->GetIdentHash ();
|
||
5 years ago
|
s << "<div class=\"listitem\"><a href=\"" << webroot << "?page=" << HTTP_PAGE_LOCAL_DESTINATION << "&b32=" << ident.ToBase32 () << "\">";
|
||
|
s << i2p::client::context.GetAddressBook ().ToAddress(ident) << "</a></div>\r\n" << std::endl;
|
||
10 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
|
||
|
auto i2cpServer = i2p::client::context.GetI2CPServer ();
|
||
7 years ago
|
if (i2cpServer && !(i2cpServer->GetSessions ().empty ()))
|
||
8 years ago
|
{
|
||
4 years ago
|
s << "<br><b>I2CP "<< tr("Local Destinations") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
8 years ago
|
for (auto& it: i2cpServer->GetSessions ())
|
||
|
{
|
||
|
auto dest = it.second->GetDestination ();
|
||
|
if (dest)
|
||
7 years ago
|
{
|
||
|
auto ident = dest->GetIdentHash ();
|
||
7 years ago
|
auto& name = dest->GetNickname ();
|
||
5 years ago
|
s << "<div class=\"listitem\"><a href=\"" << webroot << "?page=" << HTTP_PAGE_I2CP_LOCAL_DESTINATION << "&i2cp_id=" << it.first << "\">[ ";
|
||
|
s << name << " ]</a> ⇔ " << i2p::client::context.GetAddressBook ().ToAddress(ident) <<"</div>\r\n" << std::endl;
|
||
7 years ago
|
}
|
||
8 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
static void ShowLeaseSetDestination (std::stringstream& s, std::shared_ptr<const i2p::client::LeaseSetDestination> dest, uint32_t token)
|
||
8 years ago
|
{
|
||
4 years ago
|
s << "<b>Base64:</b><br>\r\n<textarea readonly cols=\"80\" rows=\"8\">";
|
||
8 years ago
|
s << dest->GetIdentity ()->ToBase64 () << "</textarea><br>\r\n<br>\r\n";
|
||
6 years ago
|
if (dest->IsEncryptedLeaseSet ())
|
||
|
{
|
||
5 years ago
|
i2p::data::BlindedPublicKey blinded (dest->GetIdentity (), dest->IsPerClientAuth ());
|
||
4 years ago
|
s << "<div class='slide'><label for='slide-b33'><b>" << tr("Encrypted B33 address") << ":</b></label>\r\n<input type=\"checkbox\" id=\"slide-b33\" />\r\n<div class=\"slidecontent\">\r\n";
|
||
6 years ago
|
s << blinded.ToB33 () << ".b32.i2p<br>\r\n";
|
||
5 years ago
|
s << "</div>\r\n</div>\r\n";
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
if (dest->IsPublic() && token && !dest->IsEncryptedLeaseSet ())
|
||
4 years ago
|
{
|
||
|
std::string webroot; i2p::config::GetOption("http.webroot", webroot);
|
||
|
auto base32 = dest->GetIdentHash ().ToBase32 ();
|
||
4 years ago
|
s << "<div class='slide'><label for='slide-regaddr'><b>" << tr("Address registration line") << "</b></label>\r\n<input type=\"checkbox\" id=\"slide-regaddr\" />\r\n<div class=\"slidecontent\">\r\n"
|
||
4 years ago
|
"<form method=\"get\" action=\"" << webroot << "\">\r\n"
|
||
|
" <input type=\"hidden\" name=\"cmd\" value=\"" << HTTP_COMMAND_GET_REG_STRING << "\">\r\n"
|
||
|
" <input type=\"hidden\" name=\"token\" value=\"" << token << "\">\r\n"
|
||
|
" <input type=\"hidden\" name=\"b32\" value=\"" << base32 << "\">\r\n"
|
||
4 years ago
|
" <b>" << tr("Domain") << ":</b>\r\n<input type=\"text\" maxlength=\"67\" name=\"name\" placeholder=\"domain.i2p\" required>\r\n"
|
||
|
" <button type=\"submit\">" << tr("Generate") << "</button>\r\n"
|
||
4 years ago
|
"</form>\r\n<small>" << tr("<b>Note:</b> result string can be used only for registering 2LD domains (example.i2p). For registering subdomains please use i2pd-tools.") << "</small>\r\n</div>\r\n</div>\r\n<br>\r\n";
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
if (dest->GetNumRemoteLeaseSets())
|
||
8 years ago
|
{
|
||
4 years ago
|
s << "<div class='slide'><label for='slide-lease'><b>" << tr("LeaseSets") << ":</b> <i>" << dest->GetNumRemoteLeaseSets ()
|
||
|
<< "</i></label>\r\n<input type=\"checkbox\" id=\"slide-lease\" />\r\n<div class=\"slidecontent\">\r\n<table><thead><th>"<< tr("Address") << "</th><th>" << tr("Type") << "</th><th>" << tr("EncType") << "</th></thead><tbody class=\"tableitem\">";
|
||
8 years ago
|
for(auto& it: dest->GetLeaseSets ())
|
||
5 years ago
|
s << "<tr><td>" << it.first.ToBase32 () << "</td><td>" << (int)it.second->GetStoreType () << "</td><td>" << (int)it.second->GetEncryptionType () <<"</td></tr>\r\n";
|
||
|
s << "</tbody></table>\r\n</div>\r\n</div>\r\n<br>\r\n";
|
||
7 years ago
|
} else
|
||
4 years ago
|
s << "<b>" << tr("LeaseSets") << ":</b> <i>0</i><br>\r\n<br>\r\n";
|
||
5 years ago
|
|
||
8 years ago
|
auto pool = dest->GetTunnelPool ();
|
||
|
if (pool)
|
||
|
{
|
||
4 years ago
|
s << "<b>" << tr("Inbound tunnels") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
8 years ago
|
for (auto & it : pool->GetInboundTunnels ()) {
|
||
5 years ago
|
s << "<div class=\"listitem\">";
|
||
3 years ago
|
// for each tunnel hop if not zero-hop
|
||
|
if (it->GetNumHops ())
|
||
|
{
|
||
|
it->VisitTunnelHops(
|
||
|
[&s](std::shared_ptr<const i2p::data::IdentityEx> hopIdent)
|
||
|
{
|
||
|
s << "⇒ " << i2p::data::GetIdentHashAbbreviation (hopIdent->GetIdentHash ()) << " ";
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
s << "⇒ " << it->GetTunnelID () << ":me";
|
||
|
if (it->LatencyIsKnown())
|
||
3 years ago
|
s << " ( " << it->GetMeanLatency() << tr(/* tr: Milliseconds */ "ms") << " )";
|
||
7 years ago
|
ShowTunnelDetails(s, it->GetState (), false, it->GetNumReceivedBytes ());
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
}
|
||
|
s << "<br>\r\n";
|
||
4 years ago
|
s << "<b>" << tr("Outbound tunnels") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
8 years ago
|
for (auto & it : pool->GetOutboundTunnels ()) {
|
||
5 years ago
|
s << "<div class=\"listitem\">";
|
||
3 years ago
|
s << it->GetTunnelID () << ":me ⇒";
|
||
|
// for each tunnel hop if not zero-hop
|
||
|
if (it->GetNumHops ())
|
||
|
{
|
||
|
it->VisitTunnelHops(
|
||
|
[&s](std::shared_ptr<const i2p::data::IdentityEx> hopIdent)
|
||
|
{
|
||
|
s << " " << i2p::data::GetIdentHashAbbreviation (hopIdent->GetIdentHash ()) << " ⇒";
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
if (it->LatencyIsKnown())
|
||
3 years ago
|
s << " ( " << it->GetMeanLatency() << tr("ms") << " )";
|
||
7 years ago
|
ShowTunnelDetails(s, it->GetState (), false, it->GetNumSentBytes ());
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
}
|
||
|
}
|
||
|
s << "<br>\r\n";
|
||
5 years ago
|
|
||
4 years ago
|
s << "<b>" << tr("Tags") << "</b><br>\r\n" << tr("Incoming") << ": <i>" << dest->GetNumIncomingTags () << "</i><br>\r\n";
|
||
7 years ago
|
if (!dest->GetSessions ().empty ()) {
|
||
6 years ago
|
std::stringstream tmp_s; uint32_t out_tags = 0;
|
||
|
for (const auto& it: dest->GetSessions ()) {
|
||
5 years ago
|
tmp_s << "<tr><td>" << i2p::client::context.GetAddressBook ().ToAddress(it.first) << "</td><td>" << it.second->GetNumOutgoingTags () << "</td></tr>\r\n";
|
||
5 years ago
|
out_tags += it.second->GetNumOutgoingTags ();
|
||
6 years ago
|
}
|
||
4 years ago
|
s << "<div class='slide'><label for='slide-tags'>" << tr("Outgoing") << ": <i>" << out_tags << "</i></label>\r\n<input type=\"checkbox\" id=\"slide-tags\" />\r\n"
|
||
|
<< "<div class=\"slidecontent\">\r\n<table>\r\n<thead><th>" << tr("Destination") << "</th><th>" << tr("Amount") << "</th></thead>\r\n<tbody class=\"tableitem\">\r\n" << tmp_s.str () << "</tbody></table>\r\n</div>\r\n</div>\r\n";
|
||
7 years ago
|
} else
|
||
4 years ago
|
s << tr("Outgoing") << ": <i>0</i><br>\r\n";
|
||
7 years ago
|
s << "<br>\r\n";
|
||
5 years ago
|
|
||
|
auto numECIESx25519Tags = dest->GetNumIncomingECIESx25519Tags ();
|
||
|
if (numECIESx25519Tags > 0) {
|
||
4 years ago
|
s << "<b>ECIESx25519</b><br>\r\n" << tr("Incoming Tags") << ": <i>" << numECIESx25519Tags << "</i><br>\r\n";
|
||
5 years ago
|
if (!dest->GetECIESx25519Sessions ().empty ())
|
||
|
{
|
||
|
std::stringstream tmp_s; uint32_t ecies_sessions = 0;
|
||
|
for (const auto& it: dest->GetECIESx25519Sessions ()) {
|
||
|
tmp_s << "<tr><td>" << i2p::client::context.GetAddressBook ().ToAddress(it.second->GetDestination ()) << "</td><td>" << it.second->GetState () << "</td></tr>\r\n";
|
||
|
ecies_sessions++;
|
||
|
}
|
||
4 years ago
|
s << "<div class='slide'><label for='slide-ecies-sessions'>" << tr("Tags sessions") << ": <i>" << ecies_sessions << "</i></label>\r\n<input type=\"checkbox\" id=\"slide-ecies-sessions\" />\r\n"
|
||
|
<< "<div class=\"slidecontent\">\r\n<table>\r\n<thead><th>" << tr("Destination") << "</th><th>" << tr("Status") << "</th></thead>\r\n<tbody class=\"tableitem\">\r\n" << tmp_s.str () << "</tbody></table>\r\n</div>\r\n</div>\r\n";
|
||
5 years ago
|
} else
|
||
4 years ago
|
s << tr("Tags sessions") << ": <i>0</i><br>\r\n";
|
||
5 years ago
|
s << "<br>\r\n";
|
||
|
}
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
5 years ago
|
void ShowLocalDestination (std::stringstream& s, const std::string& b32, uint32_t token)
|
||
10 years ago
|
{
|
||
4 years ago
|
s << "<b>" << tr("Local Destination") << ":</b><br>\r\n<br>\r\n";
|
||
10 years ago
|
i2p::data::IdentHash ident;
|
||
10 years ago
|
ident.FromBase32 (b32);
|
||
10 years ago
|
auto dest = i2p::client::context.FindLocalDestination (ident);
|
||
5 years ago
|
|
||
10 years ago
|
if (dest)
|
||
|
{
|
||
4 years ago
|
ShowLeaseSetDestination (s, dest, token);
|
||
3 years ago
|
|
||
|
// Print table with streams information
|
||
4 years ago
|
s << "<table>\r\n<caption>" << tr("Streams") << "</caption>\r\n<thead>\r\n<tr>";
|
||
5 years ago
|
s << "<th style=\"width:25px;\">StreamID</th>";
|
||
|
s << "<th style=\"width:5px;\" \\>"; // Stream closing button column
|
||
|
s << "<th class=\"streamdest\">Destination</th>";
|
||
9 years ago
|
s << "<th>Sent</th>";
|
||
|
s << "<th>Received</th>";
|
||
|
s << "<th>Out</th>";
|
||
|
s << "<th>In</th>";
|
||
|
s << "<th>Buf</th>";
|
||
|
s << "<th>RTT</th>";
|
||
|
s << "<th>Window</th>";
|
||
|
s << "<th>Status</th>";
|
||
5 years ago
|
s << "</tr>\r\n</thead>\r\n<tbody class=\"tableitem\">\r\n";
|
||
9 years ago
|
|
||
8 years ago
|
for (const auto& it: dest->GetAllStreams ())
|
||
8 years ago
|
{
|
||
5 years ago
|
auto streamDest = i2p::client::context.GetAddressBook ().ToAddress(it->GetRemoteIdentity ());
|
||
5 years ago
|
std::string streamDestShort = streamDest.substr(0,12) + "….b32.i2p";
|
||
9 years ago
|
s << "<tr>";
|
||
5 years ago
|
s << "<td>" << it->GetRecvStreamID () << "</td>";
|
||
|
if (it->GetRecvStreamID ()) {
|
||
|
s << "<td><a class=\"button\" href=\"/?cmd=" << HTTP_COMMAND_KILLSTREAM << "&b32=" << b32 << "&streamID="
|
||
4 years ago
|
<< it->GetRecvStreamID () << "&token=" << token << "\" title=\"" << tr("Close stream") << "\"> ✘ </a></td>";
|
||
5 years ago
|
} else {
|
||
|
s << "<td \\>";
|
||
|
}
|
||
5 years ago
|
s << "<td class=\"streamdest\" title=\"" << streamDest << "\">" << streamDestShort << "</td>";
|
||
9 years ago
|
s << "<td>" << it->GetNumSentBytes () << "</td>";
|
||
|
s << "<td>" << it->GetNumReceivedBytes () << "</td>";
|
||
|
s << "<td>" << it->GetSendQueueSize () << "</td>";
|
||
|
s << "<td>" << it->GetReceiveQueueSize () << "</td>";
|
||
|
s << "<td>" << it->GetSendBufferSize () << "</td>";
|
||
|
s << "<td>" << it->GetRTT () << "</td>";
|
||
|
s << "<td>" << it->GetWindowSize () << "</td>";
|
||
|
s << "<td>" << (int)it->GetStatus () << "</td>";
|
||
7 years ago
|
s << "</tr>\r\n";
|
||
|
}
|
||
5 years ago
|
s << "</tbody>\r\n</table>";
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
4 years ago
|
void ShowI2CPLocalDestination (std::stringstream& s, const std::string& id)
|
||
8 years ago
|
{
|
||
|
auto i2cpServer = i2p::client::context.GetI2CPServer ();
|
||
|
if (i2cpServer)
|
||
|
{
|
||
4 years ago
|
s << "<b>I2CP " << tr("Local Destination") << ":</b><br>\r\n<br>\r\n";
|
||
7 years ago
|
auto it = i2cpServer->GetSessions ().find (std::stoi (id));
|
||
8 years ago
|
if (it != i2cpServer->GetSessions ().end ())
|
||
4 years ago
|
ShowLeaseSetDestination (s, it->second->GetDestination (), 0);
|
||
8 years ago
|
else
|
||
4 years ago
|
ShowError(s, tr("I2CP session not found"));
|
||
8 years ago
|
}
|
||
|
else
|
||
4 years ago
|
ShowError(s, tr("I2CP is not enabled"));
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
void ShowLeasesSets(std::stringstream& s)
|
||
8 years ago
|
{
|
||
5 years ago
|
if (i2p::data::netdb.GetNumLeaseSets ())
|
||
|
{
|
||
4 years ago
|
s << "<b>" << tr("LeaseSets") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
5 years ago
|
int counter = 1;
|
||
|
// for each lease set
|
||
|
i2p::data::netdb.VisitLeaseSets(
|
||
|
[&s, &counter](const i2p::data::IdentHash dest, std::shared_ptr<i2p::data::LeaseSet> leaseSet)
|
||
|
{
|
||
|
// create copy of lease set so we extract leases
|
||
|
auto storeType = leaseSet->GetStoreType ();
|
||
|
std::unique_ptr<i2p::data::LeaseSet> ls;
|
||
|
if (storeType == i2p::data::NETDB_STORE_TYPE_LEASESET)
|
||
|
ls.reset (new i2p::data::LeaseSet (leaseSet->GetBuffer(), leaseSet->GetBufferLen()));
|
||
|
else
|
||
|
ls.reset (new i2p::data::LeaseSet2 (storeType, leaseSet->GetBuffer(), leaseSet->GetBufferLen()));
|
||
|
if (!ls) return;
|
||
5 years ago
|
s << "<div class=\"leaseset listitem";
|
||
5 years ago
|
if (ls->IsExpired())
|
||
|
s << " expired"; // additional css class for expired
|
||
5 years ago
|
s << "\">\r\n";
|
||
5 years ago
|
if (!ls->IsValid())
|
||
4 years ago
|
s << "<div class=\"invalid\">!! " << tr("Invalid") << " !! </div>\r\n";
|
||
5 years ago
|
s << "<div class=\"slide\"><label for=\"slide" << counter << "\">" << dest.ToBase32() << "</label>\r\n";
|
||
5 years ago
|
s << "<input type=\"checkbox\" id=\"slide" << (counter++) << "\" />\r\n<div class=\"slidecontent\">\r\n";
|
||
4 years ago
|
s << "<b>" << tr("Store type") << ":</b> " << (int)storeType << "<br>\r\n";
|
||
|
s << "<b>" << tr("Expires") << ":</b> " << ConvertTime(ls->GetExpirationTime()) << "<br>\r\n";
|
||
5 years ago
|
if (storeType == i2p::data::NETDB_STORE_TYPE_LEASESET || storeType == i2p::data::NETDB_STORE_TYPE_STANDARD_LEASESET2)
|
||
6 years ago
|
{
|
||
5 years ago
|
// leases information is available
|
||
|
auto leases = ls->GetNonExpiredLeases();
|
||
4 years ago
|
s << "<b>" << tr("Non Expired Leases") << ": " << leases.size() << "</b><br>\r\n";
|
||
5 years ago
|
for ( auto & l : leases )
|
||
|
{
|
||
4 years ago
|
s << "<b>" << tr("Gateway") << ":</b> " << l->tunnelGateway.ToBase64() << "<br>\r\n";
|
||
|
s << "<b>" << tr("TunnelID") << ":</b> " << l->tunnelID << "<br>\r\n";
|
||
|
s << "<b>" << tr("EndDate") << ":</b> " << ConvertTime(l->endDate) << "<br>\r\n";
|
||
5 years ago
|
}
|
||
6 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n</div>\r\n</div>\r\n";
|
||
|
}
|
||
|
);
|
||
|
// end for each lease set
|
||
|
}
|
||
|
else if (!i2p::context.IsFloodfill ())
|
||
|
{
|
||
4 years ago
|
s << "<b>" << tr("LeaseSets") << ":</b> " << tr("not floodfill") << ".<br>\r\n";
|
||
5 years ago
|
}
|
||
|
else
|
||
|
{
|
||
4 years ago
|
s << "<b>" << tr("LeaseSets") << ":</b> 0<br>\r\n";
|
||
5 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
void ShowTunnels (std::stringstream& s)
|
||
9 years ago
|
{
|
||
4 years ago
|
s << "<b>" << tr("Tunnels") << ":</b><br>\r\n";
|
||
|
s << "<b>" << tr("Queue size") << ":</b> " << i2p::tunnel::tunnels.GetQueueSize () << "<br>\r\n<br>\r\n";
|
||
9 years ago
|
|
||
7 years ago
|
auto ExplPool = i2p::tunnel::tunnels.GetExploratoryPool ();
|
||
|
|
||
4 years ago
|
s << "<b>" << tr("Inbound tunnels") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
9 years ago
|
for (auto & it : i2p::tunnel::tunnels.GetInboundTunnels ()) {
|
||
5 years ago
|
s << "<div class=\"listitem\">";
|
||
3 years ago
|
if (it->GetNumHops ())
|
||
|
{
|
||
|
it->VisitTunnelHops(
|
||
|
[&s](std::shared_ptr<const i2p::data::IdentityEx> hopIdent)
|
||
|
{
|
||
|
s << "⇒ " << i2p::data::GetIdentHashAbbreviation (hopIdent->GetIdentHash ()) << " ";
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
s << "⇒ " << it->GetTunnelID () << ":me";
|
||
|
if (it->LatencyIsKnown())
|
||
4 years ago
|
s << " ( " << it->GetMeanLatency() << tr("ms") << " )";
|
||
7 years ago
|
ShowTunnelDetails(s, it->GetState (), (it->GetTunnelPool () == ExplPool), it->GetNumReceivedBytes ());
|
||
5 years ago
|
s << "</div>\r\n";
|
||
9 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n<br>\r\n";
|
||
4 years ago
|
s << "<b>" << tr("Outbound tunnels") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
9 years ago
|
for (auto & it : i2p::tunnel::tunnels.GetOutboundTunnels ()) {
|
||
5 years ago
|
s << "<div class=\"listitem\">";
|
||
3 years ago
|
s << it->GetTunnelID () << ":me ⇒";
|
||
|
// for each tunnel hop if not zero-hop
|
||
|
if (it->GetNumHops ())
|
||
|
{
|
||
|
it->VisitTunnelHops(
|
||
|
[&s](std::shared_ptr<const i2p::data::IdentityEx> hopIdent)
|
||
|
{
|
||
|
s << " " << i2p::data::GetIdentHashAbbreviation (hopIdent->GetIdentHash ()) << " ⇒";
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
if (it->LatencyIsKnown())
|
||
4 years ago
|
s << " ( " << it->GetMeanLatency() << tr("ms") << " )";
|
||
7 years ago
|
ShowTunnelDetails(s, it->GetState (), (it->GetTunnelPool () == ExplPool), it->GetNumSentBytes ());
|
||
5 years ago
|
s << "</div>\r\n";
|
||
9 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
7 years ago
|
static void ShowCommands (std::stringstream& s, uint32_t token)
|
||
9 years ago
|
{
|
||
6 years ago
|
std::string webroot; i2p::config::GetOption("http.webroot", webroot);
|
||
3 years ago
|
|
||
4 years ago
|
s << "<b>" << tr("Router commands") << "</b><br>\r\n<br>\r\n<div class=\"commands\">\r\n";
|
||
3 years ago
|
s << " <a href=\"" << webroot << "?cmd=" << HTTP_COMMAND_RUN_PEER_TEST << "&token=" << token << "\">" << tr("Run peer test") << "</a><br>\r\n";
|
||
3 years ago
|
|
||
|
// s << " <a href=\"/?cmd=" << HTTP_COMMAND_RELOAD_CONFIG << "\">Reload config</a><br>\r\n";
|
||
|
|
||
9 years ago
|
if (i2p::context.AcceptsTunnels ())
|
||
3 years ago
|
s << " <a href=\"" << webroot << "?cmd=" << HTTP_COMMAND_DISABLE_TRANSIT << "&token=" << token << "\">" << tr("Decline transit tunnels") << "</a><br>\r\n";
|
||
8 years ago
|
else
|
||
3 years ago
|
s << " <a href=\"" << webroot << "?cmd=" << HTTP_COMMAND_ENABLE_TRANSIT << "&token=" << token << "\">" << tr("Accept transit tunnels") << "</a><br>\r\n";
|
||
3 years ago
|
|
||
7 years ago
|
#if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY))
|
||
7 years ago
|
if (Daemon.gracefulShutdownInterval)
|
||
3 years ago
|
s << " <a href=\"" << webroot << "?cmd=" << HTTP_COMMAND_SHUTDOWN_CANCEL << "&token=" << token << "\">" << tr("Cancel graceful shutdown") << "</a><br>\r\n";
|
||
7 years ago
|
else
|
||
3 years ago
|
s << " <a href=\"" << webroot << "?cmd=" << HTTP_COMMAND_SHUTDOWN_START << "&token=" << token << "\">" << tr("Start graceful shutdown") << "</a><br>\r\n";
|
||
7 years ago
|
#elif defined(WIN32_APP)
|
||
|
if (i2p::util::DaemonWin32::Instance().isGraceful)
|
||
3 years ago
|
s << " <a href=\"" << webroot << "?cmd=" << HTTP_COMMAND_SHUTDOWN_CANCEL << "&token=" << token << "\">" << tr("Cancel graceful shutdown") << "</a><br>\r\n";
|
||
7 years ago
|
else
|
||
3 years ago
|
s << " <a href=\"" << webroot << "?cmd=" << HTTP_COMMAND_SHUTDOWN_START << "&token=" << token << "\">" << tr("Start graceful shutdown") << "</a><br>\r\n";
|
||
8 years ago
|
#endif
|
||
3 years ago
|
|
||
3 years ago
|
s << " <a href=\"" << webroot << "?cmd=" << HTTP_COMMAND_SHUTDOWN_NOW << "&token=" << token << "\">" << tr("Force shutdown") << "</a><br><br>\r\n";
|
||
|
s << " <a href=\"" << webroot << "?cmd=" << HTTP_COMMAND_RELOAD_CSS << "&token=" << token << "\">" << tr("Reload external CSS styles") << "</a>\r\n";
|
||
5 years ago
|
s << "</div>";
|
||
5 years ago
|
|
||
4 years ago
|
s << "<br>\r\n<small>" << tr("<b>Note:</b> any action done here are not persistent and not changes your config files.") << "</small>\r\n<br>\r\n";
|
||
5 years ago
|
|
||
4 years ago
|
s << "<b>" << tr("Logging level") << "</b><br>\r\n";
|
||
5 years ago
|
s << " <a class=\"button\" href=\"" << webroot << "?cmd=" << HTTP_COMMAND_LOGLEVEL << "&level=none&token=" << token << "\"> none </a> \r\n";
|
||
|
s << " <a class=\"button\" href=\"" << webroot << "?cmd=" << HTTP_COMMAND_LOGLEVEL << "&level=error&token=" << token << "\"> error </a> \r\n";
|
||
|
s << " <a class=\"button\" href=\"" << webroot << "?cmd=" << HTTP_COMMAND_LOGLEVEL << "&level=warn&token=" << token << "\"> warn </a> \r\n";
|
||
|
s << " <a class=\"button\" href=\"" << webroot << "?cmd=" << HTTP_COMMAND_LOGLEVEL << "&level=info&token=" << token << "\"> info </a> \r\n";
|
||
|
s << " <a class=\"button\" href=\"" << webroot << "?cmd=" << HTTP_COMMAND_LOGLEVEL << "&level=debug&token=" << token << "\"> debug </a><br>\r\n<br>\r\n";
|
||
|
|
||
|
uint16_t maxTunnels = GetMaxNumTransitTunnels ();
|
||
4 years ago
|
s << "<b>" << tr("Transit tunnels limit") << "</b><br>\r\n";
|
||
5 years ago
|
s << "<form method=\"get\" action=\"" << webroot << "\">\r\n";
|
||
|
s << " <input type=\"hidden\" name=\"cmd\" value=\"" << HTTP_COMMAND_LIMITTRANSIT << "\">\r\n";
|
||
|
s << " <input type=\"hidden\" name=\"token\" value=\"" << token << "\">\r\n";
|
||
|
s << " <input type=\"number\" min=\"0\" max=\"65535\" name=\"limit\" value=\"" << maxTunnels << "\">\r\n";
|
||
4 years ago
|
s << " <button type=\"submit\">" << tr("Change") << "</button>\r\n";
|
||
5 years ago
|
s << "</form>\r\n<br>\r\n";
|
||
3 years ago
|
|
||
3 years ago
|
std::string currLang = i2p::client::context.GetLanguage ()->GetLanguage(); // get current used language
|
||
3 years ago
|
s << "<b>" << tr("Change language") << "</b><br>\r\n";
|
||
|
s << "<form method=\"get\" action=\"" << webroot << "\">\r\n";
|
||
|
s << " <input type=\"hidden\" name=\"cmd\" value=\"" << HTTP_COMMAND_SETLANGUAGE << "\">\r\n";
|
||
|
s << " <input type=\"hidden\" name=\"token\" value=\"" << token << "\">\r\n";
|
||
|
s << " <select name=\"lang\" id=\"lang\">\r\n";
|
||
|
for (const auto& it: i2p::i18n::languages)
|
||
|
s << " <option value=\"" << it.first << "\"" << ((it.first.compare(currLang) == 0) ? " selected" : "") << ">" << it.second.LocaleName << "</option>\r\n";
|
||
|
s << " </select>\r\n";
|
||
|
s << " <button type=\"submit\">" << tr("Change") << "</button>\r\n";
|
||
|
s << "</form>\r\n<br>\r\n";
|
||
|
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
void ShowTransitTunnels (std::stringstream& s)
|
||
9 years ago
|
{
|
||
3 years ago
|
if (i2p::tunnel::tunnels.CountTransitTunnels())
|
||
9 years ago
|
{
|
||
3 years ago
|
s << "<b>" << tr("Transit Tunnels") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
5 years ago
|
for (const auto& it: i2p::tunnel::tunnels.GetTransitTunnels ())
|
||
|
{
|
||
5 years ago
|
s << "<div class=\"listitem\">\r\n";
|
||
5 years ago
|
if (std::dynamic_pointer_cast<i2p::tunnel::TransitTunnelGateway>(it))
|
||
|
s << it->GetTunnelID () << " ⇒ ";
|
||
|
else if (std::dynamic_pointer_cast<i2p::tunnel::TransitTunnelEndpoint>(it))
|
||
|
s << " ⇒ " << it->GetTunnelID ();
|
||
|
else
|
||
|
s << " ⇒ " << it->GetTunnelID () << " ⇒ ";
|
||
5 years ago
|
s << " " << it->GetNumTransmittedBytes () << "</div>\r\n";
|
||
5 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n";
|
||
5 years ago
|
}
|
||
|
else
|
||
|
{
|
||
3 years ago
|
s << "<b>" << tr("Transit Tunnels") << ":</b> " << tr("no transit tunnels currently built") << ".<br>\r\n";
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
template<typename Sessions>
|
||
3 years ago
|
static void ShowTransportSessions (std::stringstream& s, const Sessions& sessions, const std::string name)
|
||
6 years ago
|
{
|
||
|
std::stringstream tmp_s, tmp_s6; uint16_t cnt = 0, cnt6 = 0;
|
||
|
for (const auto& it: sessions )
|
||
|
{
|
||
4 years ago
|
if (it.second && it.second->IsEstablished () && !it.second->GetRemoteEndpoint ().address ().is_v6 ())
|
||
6 years ago
|
{
|
||
5 years ago
|
tmp_s << "<div class=\"listitem\">\r\n";
|
||
6 years ago
|
if (it.second->IsOutgoing ()) tmp_s << " ⇒ ";
|
||
|
tmp_s << i2p::data::GetIdentHashAbbreviation (it.second->GetRemoteIdentity ()->GetIdentHash ()) << ": "
|
||
4 years ago
|
<< it.second->GetRemoteEndpoint ().address ().to_string ();
|
||
6 years ago
|
if (!it.second->IsOutgoing ()) tmp_s << " ⇒ ";
|
||
|
tmp_s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
|
||
5 years ago
|
tmp_s << "</div>\r\n" << std::endl;
|
||
6 years ago
|
cnt++;
|
||
|
}
|
||
4 years ago
|
if (it.second && it.second->IsEstablished () && it.second->GetRemoteEndpoint ().address ().is_v6 ())
|
||
6 years ago
|
{
|
||
5 years ago
|
tmp_s6 << "<div class=\"listitem\">\r\n";
|
||
6 years ago
|
if (it.second->IsOutgoing ()) tmp_s6 << " ⇒ ";
|
||
|
tmp_s6 << i2p::data::GetIdentHashAbbreviation (it.second->GetRemoteIdentity ()->GetIdentHash ()) << ": "
|
||
4 years ago
|
<< "[" << it.second->GetRemoteEndpoint ().address ().to_string () << "]";
|
||
6 years ago
|
if (!it.second->IsOutgoing ()) tmp_s6 << " ⇒ ";
|
||
|
tmp_s6 << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
|
||
5 years ago
|
tmp_s6 << "</div>\r\n" << std::endl;
|
||
6 years ago
|
cnt6++;
|
||
|
}
|
||
|
}
|
||
|
if (!tmp_s.str ().empty ())
|
||
|
{
|
||
5 years ago
|
s << "<div class='slide'><label for='slide_" << boost::algorithm::to_lower_copy(name) << "'><b>" << name
|
||
5 years ago
|
<< "</b> ( " << cnt << " )</label>\r\n<input type=\"checkbox\" id=\"slide_" << boost::algorithm::to_lower_copy(name) << "\" />\r\n<div class=\"slidecontent list\">"
|
||
5 years ago
|
<< tmp_s.str () << "</div>\r\n</div>\r\n";
|
||
6 years ago
|
}
|
||
|
if (!tmp_s6.str ().empty ())
|
||
|
{
|
||
5 years ago
|
s << "<div class='slide'><label for='slide_" << boost::algorithm::to_lower_copy(name) << "v6'><b>" << name
|
||
5 years ago
|
<< "v6</b> ( " << cnt6 << " )</label>\r\n<input type=\"checkbox\" id=\"slide_" << boost::algorithm::to_lower_copy(name) << "v6\" />\r\n<div class=\"slidecontent list\">"
|
||
5 years ago
|
<< tmp_s6.str () << "</div>\r\n</div>\r\n";
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
void ShowTransports (std::stringstream& s)
|
||
9 years ago
|
{
|
||
4 years ago
|
s << "<b>" << tr("Transports") << ":</b><br>\r\n";
|
||
6 years ago
|
auto ntcp2Server = i2p::transport::transports.GetNTCP2Server ();
|
||
|
if (ntcp2Server)
|
||
|
{
|
||
|
auto sessions = ntcp2Server->GetNTCP2Sessions ();
|
||
|
if (!sessions.empty ())
|
||
3 years ago
|
ShowTransportSessions (s, sessions, "NTCP2");
|
||
8 years ago
|
}
|
||
9 years ago
|
auto ssuServer = i2p::transport::transports.GetSSUServer ();
|
||
|
if (ssuServer)
|
||
|
{
|
||
8 years ago
|
auto sessions = ssuServer->GetSessions ();
|
||
7 years ago
|
if (!sessions.empty ())
|
||
9 years ago
|
{
|
||
5 years ago
|
s << "<div class='slide'><label for='slide_ssu'><b>SSU</b> ( " << (int) sessions.size() << " )</label>\r\n<input type=\"checkbox\" id=\"slide_ssu\" />\r\n<div class=\"slidecontent list\">";
|
||
7 years ago
|
for (const auto& it: sessions)
|
||
|
{
|
||
5 years ago
|
s << "<div class=\"listitem\">\r\n";
|
||
7 years ago
|
auto endpoint = it.second->GetRemoteEndpoint ();
|
||
|
if (it.second->IsOutgoing ()) s << " ⇒ ";
|
||
|
s << endpoint.address ().to_string () << ":" << endpoint.port ();
|
||
|
if (!it.second->IsOutgoing ()) s << " ⇒ ";
|
||
|
s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
|
||
|
if (it.second->GetRelayTag ())
|
||
|
s << " [itag:" << it.second->GetRelayTag () << "]";
|
||
5 years ago
|
s << "</div>\r\n" << std::endl;
|
||
7 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n</div>\r\n";
|
||
9 years ago
|
}
|
||
7 years ago
|
auto sessions6 = ssuServer->GetSessionsV6 ();
|
||
|
if (!sessions6.empty ())
|
||
9 years ago
|
{
|
||
5 years ago
|
s << "<div class='slide'><label for='slide_ssuv6'><b>SSUv6</b> ( " << (int) sessions6.size() << " )</label>\r\n<input type=\"checkbox\" id=\"slide_ssuv6\" />\r\n<div class=\"slidecontent list\">";
|
||
7 years ago
|
for (const auto& it: sessions6)
|
||
|
{
|
||
5 years ago
|
s << "<div class=\"listitem\">\r\n";
|
||
7 years ago
|
auto endpoint = it.second->GetRemoteEndpoint ();
|
||
|
if (it.second->IsOutgoing ()) s << " ⇒ ";
|
||
|
s << "[" << endpoint.address ().to_string () << "]:" << endpoint.port ();
|
||
|
if (!it.second->IsOutgoing ()) s << " ⇒ ";
|
||
|
s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
|
||
|
if (it.second->GetRelayTag ())
|
||
|
s << " [itag:" << it.second->GetRelayTag () << "]";
|
||
5 years ago
|
s << "</div>\r\n" << std::endl;
|
||
7 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n</div>\r\n";
|
||
9 years ago
|
}
|
||
|
}
|
||
3 years ago
|
auto ssu2Server = i2p::transport::transports.GetSSU2Server ();
|
||
|
if (ssu2Server)
|
||
|
{
|
||
|
auto sessions = ssu2Server->GetSSU2Sessions ();
|
||
|
if (!sessions.empty ())
|
||
|
ShowTransportSessions (s, sessions, "SSU2");
|
||
|
}
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
void ShowSAMSessions (std::stringstream& s)
|
||
10 years ago
|
{
|
||
6 years ago
|
std::string webroot; i2p::config::GetOption("http.webroot", webroot);
|
||
10 years ago
|
auto sam = i2p::client::context.GetSAMBridge ();
|
||
5 years ago
|
if (!sam)
|
||
|
{
|
||
4 years ago
|
ShowError(s, tr("SAM disabled"));
|
||
9 years ago
|
return;
|
||
|
}
|
||
5 years ago
|
|
||
3 years ago
|
if (sam->GetSessions ().size ())
|
||
9 years ago
|
{
|
||
4 years ago
|
s << "<b>" << tr("SAM sessions") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
5 years ago
|
for (auto& it: sam->GetSessions ())
|
||
|
{
|
||
4 years ago
|
auto& name = it.second->GetLocalDestination ()->GetNickname ();
|
||
5 years ago
|
s << "<div class=\"listitem\"><a href=\"" << webroot << "?page=" << HTTP_PAGE_SAM_SESSION << "&sam_id=" << it.first << "\">";
|
||
|
s << name << " (" << it.first << ")</a></div>\r\n" << std::endl;
|
||
5 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
}
|
||
5 years ago
|
else
|
||
4 years ago
|
s << "<b>" << tr("SAM sessions") << ":</b> " << tr("no sessions currently running") << ".<br>\r\n";
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
4 years ago
|
void ShowSAMSession (std::stringstream& s, const std::string& id)
|
||
10 years ago
|
{
|
||
|
auto sam = i2p::client::context.GetSAMBridge ();
|
||
9 years ago
|
if (!sam) {
|
||
4 years ago
|
ShowError(s, tr("SAM disabled"));
|
||
9 years ago
|
return;
|
||
|
}
|
||
5 years ago
|
|
||
9 years ago
|
auto session = sam->FindSession (id);
|
||
|
if (!session) {
|
||
4 years ago
|
ShowError(s, tr("SAM session not found"));
|
||
9 years ago
|
return;
|
||
|
}
|
||
5 years ago
|
|
||
|
std::string webroot; i2p::config::GetOption("http.webroot", webroot);
|
||
4 years ago
|
s << "<b>" << tr("SAM Session") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
4 years ago
|
auto& ident = session->GetLocalDestination ()->GetIdentHash();
|
||
5 years ago
|
s << "<div class=\"listitem\"><a href=\"" << webroot << "?page=" << HTTP_PAGE_LOCAL_DESTINATION << "&b32=" << ident.ToBase32 () << "\">";
|
||
|
s << i2p::client::context.GetAddressBook ().ToAddress(ident) << "</a></div>\r\n";
|
||
9 years ago
|
s << "<br>\r\n";
|
||
4 years ago
|
s << "<b>" << tr("Streams") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
7 years ago
|
for (const auto& it: sam->ListSockets(id))
|
||
10 years ago
|
{
|
||
5 years ago
|
s << "<div class=\"listitem\">";
|
||
9 years ago
|
switch (it->GetSocketType ())
|
||
10 years ago
|
{
|
||
9 years ago
|
case i2p::client::eSAMSocketTypeSession : s << "session"; break;
|
||
|
case i2p::client::eSAMSocketTypeStream : s << "stream"; break;
|
||
|
case i2p::client::eSAMSocketTypeAcceptor : s << "acceptor"; break;
|
||
4 years ago
|
case i2p::client::eSAMSocketTypeForward : s << "forward"; break;
|
||
9 years ago
|
default: s << "unknown"; break;
|
||
10 years ago
|
}
|
||
9 years ago
|
s << " [" << it->GetSocket ().remote_endpoint() << "]";
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
7 years ago
|
void ShowI2PTunnels (std::stringstream& s)
|
||
9 years ago
|
{
|
||
6 years ago
|
std::string webroot; i2p::config::GetOption("http.webroot", webroot);
|
||
4 years ago
|
s << "<b>" << tr("Client Tunnels") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
9 years ago
|
for (auto& it: i2p::client::context.GetClientTunnels ())
|
||
|
{
|
||
|
auto& ident = it.second->GetLocalDestination ()->GetIdentHash();
|
||
5 years ago
|
s << "<div class=\"listitem\"><a href=\"" << webroot << "?page=" << HTTP_PAGE_LOCAL_DESTINATION << "&b32=" << ident.ToBase32 () << "\">";
|
||
8 years ago
|
s << it.second->GetName () << "</a> ⇐ ";
|
||
9 years ago
|
s << i2p::client::context.GetAddressBook ().ToAddress(ident);
|
||
5 years ago
|
s << "</div>\r\n"<< std::endl;
|
||
8 years ago
|
}
|
||
8 years ago
|
auto httpProxy = i2p::client::context.GetHttpProxy ();
|
||
|
if (httpProxy)
|
||
|
{
|
||
|
auto& ident = httpProxy->GetLocalDestination ()->GetIdentHash();
|
||
5 years ago
|
s << "<div class=\"listitem\"><a href=\"" << webroot << "?page=" << HTTP_PAGE_LOCAL_DESTINATION << "&b32=" << ident.ToBase32 () << "\">";
|
||
4 years ago
|
s << "HTTP " << tr("Proxy") << "</a> ⇐ ";
|
||
8 years ago
|
s << i2p::client::context.GetAddressBook ().ToAddress(ident);
|
||
5 years ago
|
s << "</div>\r\n"<< std::endl;
|
||
7 years ago
|
}
|
||
8 years ago
|
auto socksProxy = i2p::client::context.GetSocksProxy ();
|
||
|
if (socksProxy)
|
||
|
{
|
||
|
auto& ident = socksProxy->GetLocalDestination ()->GetIdentHash();
|
||
5 years ago
|
s << "<div class=\"listitem\"><a href=\"" << webroot << "?page=" << HTTP_PAGE_LOCAL_DESTINATION << "&b32=" << ident.ToBase32 () << "\">";
|
||
4 years ago
|
s << "SOCKS " << tr("Proxy") << "</a> ⇐ ";
|
||
8 years ago
|
s << i2p::client::context.GetAddressBook ().ToAddress(ident);
|
||
5 years ago
|
s << "</div>\r\n"<< std::endl;
|
||
7 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n";
|
||
|
|
||
7 years ago
|
auto& serverTunnels = i2p::client::context.GetServerTunnels ();
|
||
|
if (!serverTunnels.empty ()) {
|
||
4 years ago
|
s << "<br>\r\n<b>" << tr("Server Tunnels") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
7 years ago
|
for (auto& it: serverTunnels)
|
||
|
{
|
||
|
auto& ident = it.second->GetLocalDestination ()->GetIdentHash();
|
||
5 years ago
|
s << "<div class=\"listitem\"><a href=\"" << webroot << "?page=" << HTTP_PAGE_LOCAL_DESTINATION << "&b32=" << ident.ToBase32 () << "\">";
|
||
7 years ago
|
s << it.second->GetName () << "</a> ⇒ ";
|
||
|
s << i2p::client::context.GetAddressBook ().ToAddress(ident);
|
||
|
s << ":" << it.second->GetLocalPort ();
|
||
5 years ago
|
s << "</a></div>\r\n"<< std::endl;
|
||
7 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
}
|
||
5 years ago
|
|
||
8 years ago
|
auto& clientForwards = i2p::client::context.GetClientForwards ();
|
||
|
if (!clientForwards.empty ())
|
||
|
{
|
||
4 years ago
|
s << "<br>\r\n<b>" << tr("Client Forwards") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
8 years ago
|
for (auto& it: clientForwards)
|
||
|
{
|
||
|
auto& ident = it.second->GetLocalDestination ()->GetIdentHash();
|
||
5 years ago
|
s << "<div class=\"listitem\"><a href=\"" << webroot << "?page=" << HTTP_PAGE_LOCAL_DESTINATION << "&b32=" << ident.ToBase32 () << "\">";
|
||
8 years ago
|
s << it.second->GetName () << "</a> ⇐ ";
|
||
|
s << i2p::client::context.GetAddressBook ().ToAddress(ident);
|
||
5 years ago
|
s << "</div>\r\n"<< std::endl;
|
||
8 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
}
|
||
|
auto& serverForwards = i2p::client::context.GetServerForwards ();
|
||
|
if (!serverForwards.empty ())
|
||
|
{
|
||
4 years ago
|
s << "<br>\r\n<b>" << tr("Server Forwards") << ":</b><br>\r\n<div class=\"list\">\r\n";
|
||
8 years ago
|
for (auto& it: serverForwards)
|
||
|
{
|
||
|
auto& ident = it.second->GetLocalDestination ()->GetIdentHash();
|
||
6 years ago
|
s << "<a href=\"" << webroot << "?page=" << HTTP_PAGE_LOCAL_DESTINATION << "&b32=" << ident.ToBase32 () << "\">";
|
||
8 years ago
|
s << it.second->GetName () << "</a> ⇐ ";
|
||
|
s << i2p::client::context.GetAddressBook ().ToAddress(ident);
|
||
5 years ago
|
s << "</div>\r\n"<< std::endl;
|
||
8 years ago
|
}
|
||
5 years ago
|
s << "</div>\r\n";
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
7 years ago
|
HTTPConnection::HTTPConnection (std::string hostname, std::shared_ptr<boost::asio::ip::tcp::socket> socket):
|
||
6 years ago
|
m_Socket (socket), m_BufferLen (0), expected_host(hostname)
|
||
9 years ago
|
{
|
||
9 years ago
|
/* cache options */
|
||
|
i2p::config::GetOption("http.auth", needAuth);
|
||
|
i2p::config::GetOption("http.user", user);
|
||
|
i2p::config::GetOption("http.pass", pass);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
void HTTPConnection::Receive ()
|
||
9 years ago
|
{
|
||
9 years ago
|
m_Socket->async_read_some (boost::asio::buffer (m_Buffer, HTTP_CONNECTION_BUFFER_SIZE),
|
||
5 years ago
|
std::bind(&HTTPConnection::HandleReceive, shared_from_this (),
|
||
|
std::placeholders::_1, std::placeholders::_2));
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
void HTTPConnection::HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
11 years ago
|
{
|
||
9 years ago
|
if (ecode) {
|
||
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate (ecode);
|
||
10 years ago
|
return;
|
||
10 years ago
|
}
|
||
9 years ago
|
m_Buffer[bytes_transferred] = '\0';
|
||
|
m_BufferLen = bytes_transferred;
|
||
|
RunRequest();
|
||
|
Receive ();
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
void HTTPConnection::RunRequest ()
|
||
10 years ago
|
{
|
||
9 years ago
|
HTTPReq request;
|
||
|
int ret = request.parse(m_Buffer);
|
||
|
if (ret < 0) {
|
||
|
m_Buffer[0] = '\0';
|
||
|
m_BufferLen = 0;
|
||
|
return; /* error */
|
||
11 years ago
|
}
|
||
9 years ago
|
if (ret == 0)
|
||
|
return; /* need more data */
|
||
9 years ago
|
|
||
9 years ago
|
HandleRequest (request);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void HTTPConnection::Terminate (const boost::system::error_code& ecode)
|
||
11 years ago
|
{
|
||
9 years ago
|
if (ecode == boost::asio::error::operation_aborted)
|
||
|
return;
|
||
|
boost::system::error_code ignored_ec;
|
||
|
m_Socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
|
||
|
m_Socket->close ();
|
||
11 years ago
|
}
|
||
|
|
||
5 years ago
|
bool HTTPConnection::CheckAuth (const HTTPReq & req)
|
||
|
{
|
||
9 years ago
|
/* method #1: http://user:pass@127.0.0.1:7070/ */
|
||
|
if (req.uri.find('@') != std::string::npos) {
|
||
|
URL url;
|
||
|
if (url.parse(req.uri) && url.user == user && url.pass == pass)
|
||
|
return true;
|
||
11 years ago
|
}
|
||
9 years ago
|
/* method #2: 'Authorization' header sent */
|
||
8 years ago
|
auto provided = req.GetHeader ("Authorization");
|
||
7 years ago
|
if (provided.length () > 0)
|
||
|
{
|
||
6 years ago
|
std::string expected = "Basic " + i2p::data::ToBase64Standard (user + ":" + pass);
|
||
|
if (expected == provided) return true;
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
3 years ago
|
LogPrint(eLogWarning, "HTTPServer: Auth failure from ", m_Socket->remote_endpoint().address ());
|
||
9 years ago
|
return false;
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
void HTTPConnection::HandleRequest (const HTTPReq & req)
|
||
11 years ago
|
{
|
||
9 years ago
|
std::stringstream s;
|
||
9 years ago
|
std::string content;
|
||
|
HTTPRes res;
|
||
9 years ago
|
|
||
3 years ago
|
LogPrint(eLogDebug, "HTTPServer: Request: ", req.uri);
|
||
9 years ago
|
|
||
9 years ago
|
if (needAuth && !CheckAuth(req)) {
|
||
|
res.code = 401;
|
||
9 years ago
|
res.add_header("WWW-Authenticate", "Basic realm=\"WebAdmin\"");
|
||
9 years ago
|
SendReply(res, content);
|
||
|
return;
|
||
|
}
|
||
3 years ago
|
|
||
7 years ago
|
bool strictheaders;
|
||
|
i2p::config::GetOption("http.strictheaders", strictheaders);
|
||
|
if (strictheaders)
|
||
|
{
|
||
|
std::string http_hostname;
|
||
|
i2p::config::GetOption("http.hostname", http_hostname);
|
||
|
std::string host = req.GetHeader("Host");
|
||
|
auto idx = host.find(':');
|
||
|
/* strip out port so it's just host */
|
||
|
if (idx != std::string::npos && idx > 0)
|
||
|
{
|
||
|
host = host.substr(0, idx);
|
||
|
}
|
||
|
if (!(host == expected_host || host == http_hostname))
|
||
|
{
|
||
|
/* deny request as it's from a non whitelisted hostname */
|
||
|
res.code = 403;
|
||
6 years ago
|
content = "host mismatch";
|
||
7 years ago
|
SendReply(res, content);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
4 years ago
|
// HTML head start
|
||
9 years ago
|
ShowPageHead (s);
|
||
9 years ago
|
if (req.uri.find("page=") != std::string::npos) {
|
||
9 years ago
|
HandlePage (req, res, s);
|
||
9 years ago
|
} else if (req.uri.find("cmd=") != std::string::npos) {
|
||
9 years ago
|
HandleCommand (req, res, s);
|
||
9 years ago
|
} else {
|
||
5 years ago
|
ShowStatus (s, true, i2p::http::OutputFormatEnum::forWebConsole);
|
||
9 years ago
|
res.add_header("Refresh", "10");
|
||
9 years ago
|
}
|
||
9 years ago
|
ShowPageTail (s);
|
||
9 years ago
|
|
||
|
res.code = 200;
|
||
9 years ago
|
content = s.str ();
|
||
|
SendReply (res, content);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
3 years ago
|
std::map<uint32_t, uint32_t> HTTPConnection::m_Tokens;
|
||
|
|
||
5 years ago
|
uint32_t HTTPConnection::CreateToken ()
|
||
|
{
|
||
|
uint32_t token;
|
||
|
RAND_bytes ((uint8_t *)&token, 4);
|
||
|
token &= 0x7FFFFFFF; // clear first bit
|
||
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
||
|
for (auto it = m_Tokens.begin (); it != m_Tokens.end (); )
|
||
|
{
|
||
|
if (ts > it->second + TOKEN_EXPIRATION_TIMEOUT)
|
||
|
it = m_Tokens.erase (it);
|
||
|
else
|
||
|
++it;
|
||
|
}
|
||
|
m_Tokens[token] = ts;
|
||
|
return token;
|
||
|
}
|
||
|
|
||
9 years ago
|
void HTTPConnection::HandlePage (const HTTPReq& req, HTTPRes& res, std::stringstream& s)
|
||
7 years ago
|
{
|
||
9 years ago
|
std::map<std::string, std::string> params;
|
||
|
std::string page("");
|
||
|
URL url;
|
||
|
|
||
9 years ago
|
url.parse(req.uri);
|
||
9 years ago
|
url.parse_query(params);
|
||
|
page = params["page"];
|
||
|
|
||
|
if (page == HTTP_PAGE_TRANSPORTS)
|
||
|
ShowTransports (s);
|
||
|
else if (page == HTTP_PAGE_TUNNELS)
|
||
|
ShowTunnels (s);
|
||
9 years ago
|
else if (page == HTTP_PAGE_COMMANDS)
|
||
8 years ago
|
{
|
||
5 years ago
|
uint32_t token = CreateToken ();
|
||
8 years ago
|
ShowCommands (s, token);
|
||
|
}
|
||
9 years ago
|
else if (page == HTTP_PAGE_TRANSIT_TUNNELS)
|
||
|
ShowTransitTunnels (s);
|
||
|
else if (page == HTTP_PAGE_LOCAL_DESTINATIONS)
|
||
7 years ago
|
ShowLocalDestinations (s);
|
||
9 years ago
|
else if (page == HTTP_PAGE_LOCAL_DESTINATION)
|
||
5 years ago
|
{
|
||
|
uint32_t token = CreateToken ();
|
||
|
ShowLocalDestination (s, params["b32"], token);
|
||
|
}
|
||
8 years ago
|
else if (page == HTTP_PAGE_I2CP_LOCAL_DESTINATION)
|
||
|
ShowI2CPLocalDestination (s, params["i2cp_id"]);
|
||
9 years ago
|
else if (page == HTTP_PAGE_SAM_SESSIONS)
|
||
|
ShowSAMSessions (s);
|
||
|
else if (page == HTTP_PAGE_SAM_SESSION)
|
||
|
ShowSAMSession (s, params["sam_id"]);
|
||
|
else if (page == HTTP_PAGE_I2P_TUNNELS)
|
||
|
ShowI2PTunnels (s);
|
||
8 years ago
|
else if (page == HTTP_PAGE_LEASESETS)
|
||
|
ShowLeasesSets(s);
|
||
9 years ago
|
else {
|
||
|
res.code = 400;
|
||
4 years ago
|
ShowError(s, tr("Unknown page") + ": " + page);
|
||
9 years ago
|
return;
|
||
|
}
|
||
7 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
void HTTPConnection::HandleCommand (const HTTPReq& req, HTTPRes& res, std::stringstream& s)
|
||
9 years ago
|
{
|
||
|
std::map<std::string, std::string> params;
|
||
|
URL url;
|
||
|
|
||
9 years ago
|
url.parse(req.uri);
|
||
9 years ago
|
url.parse_query(params);
|
||
|
|
||
5 years ago
|
std::string webroot; i2p::config::GetOption("http.webroot", webroot);
|
||
|
std::string redirect = "5; url=" + webroot + "?page=commands";
|
||
8 years ago
|
std::string token = params["token"];
|
||
5 years ago
|
|
||
8 years ago
|
if (token.empty () || m_Tokens.find (std::stoi (token)) == m_Tokens.end ())
|
||
8 years ago
|
{
|
||
4 years ago
|
ShowError(s, tr("Invalid token"));
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
std::string cmd = params["cmd"];
|
||
9 years ago
|
if (cmd == HTTP_COMMAND_RUN_PEER_TEST)
|
||
|
i2p::transport::transports.PeerTest ();
|
||
9 years ago
|
else if (cmd == HTTP_COMMAND_RELOAD_CONFIG)
|
||
|
i2p::client::context.ReloadConfig ();
|
||
8 years ago
|
else if (cmd == HTTP_COMMAND_ENABLE_TRANSIT)
|
||
9 years ago
|
i2p::context.SetAcceptsTunnels (true);
|
||
8 years ago
|
else if (cmd == HTTP_COMMAND_DISABLE_TRANSIT)
|
||
9 years ago
|
i2p::context.SetAcceptsTunnels (false);
|
||
5 years ago
|
else if (cmd == HTTP_COMMAND_SHUTDOWN_START)
|
||
|
{
|
||
9 years ago
|
i2p::context.SetAcceptsTunnels (false);
|
||
7 years ago
|
#if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY))
|
||
8 years ago
|
Daemon.gracefulShutdownInterval = 10*60;
|
||
7 years ago
|
#elif defined(WIN32_APP)
|
||
8 years ago
|
i2p::win32::GracefulShutdown ();
|
||
9 years ago
|
#endif
|
||
5 years ago
|
}
|
||
|
else if (cmd == HTTP_COMMAND_SHUTDOWN_CANCEL)
|
||
|
{
|
||
9 years ago
|
i2p::context.SetAcceptsTunnels (true);
|
||
3 years ago
|
#if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY))
|
||
8 years ago
|
Daemon.gracefulShutdownInterval = 0;
|
||
7 years ago
|
#elif defined(WIN32_APP)
|
||
|
i2p::win32::StopGracefulShutdown ();
|
||
9 years ago
|
#endif
|
||
5 years ago
|
}
|
||
|
else if (cmd == HTTP_COMMAND_SHUTDOWN_NOW)
|
||
|
{
|
||
7 years ago
|
#ifndef WIN32_APP
|
||
9 years ago
|
Daemon.running = false;
|
||
7 years ago
|
#else
|
||
|
i2p::win32::StopWin32App ();
|
||
|
#endif
|
||
5 years ago
|
}
|
||
|
else if (cmd == HTTP_COMMAND_LOGLEVEL)
|
||
|
{
|
||
7 years ago
|
std::string level = params["level"];
|
||
|
SetLogLevel (level);
|
||
5 years ago
|
}
|
||
|
else if (cmd == HTTP_COMMAND_KILLSTREAM)
|
||
|
{
|
||
|
std::string b32 = params["b32"];
|
||
|
uint32_t streamID = std::stoul(params["streamID"], nullptr);
|
||
|
|
||
|
i2p::data::IdentHash ident;
|
||
|
ident.FromBase32 (b32);
|
||
|
auto dest = i2p::client::context.FindLocalDestination (ident);
|
||
|
|
||
|
if (streamID)
|
||
|
{
|
||
|
if (dest)
|
||
|
{
|
||
3 years ago
|
if (dest->DeleteStream (streamID))
|
||
4 years ago
|
s << "<b>" << tr("SUCCESS") << "</b>: " << tr("Stream closed") << "<br>\r\n<br>\r\n";
|
||
5 years ago
|
else
|
||
4 years ago
|
s << "<b>" << tr("ERROR") << "</b>: " << tr("Stream not found or already was closed") << "<br>\r\n<br>\r\n";
|
||
5 years ago
|
}
|
||
|
else
|
||
4 years ago
|
s << "<b>" << tr("ERROR") << "</b>: " << tr("Destination not found") << "<br>\r\n<br>\r\n";
|
||
5 years ago
|
}
|
||
|
else
|
||
4 years ago
|
s << "<b>" << tr("ERROR") << "</b>: " << tr("StreamID can't be null") << "<br>\r\n<br>\r\n";
|
||
5 years ago
|
|
||
4 years ago
|
s << "<a href=\"" << webroot << "?page=local_destination&b32=" << b32 << "\">" << tr("Return to destination page") << "</a><br>\r\n";
|
||
3 years ago
|
s << "<p>" << tr("You will be redirected in 5 seconds") << "</b>";
|
||
5 years ago
|
redirect = "5; url=" + webroot + "?page=local_destination&b32=" + b32;
|
||
|
res.add_header("Refresh", redirect.c_str());
|
||
|
return;
|
||
|
}
|
||
5 years ago
|
else if (cmd == HTTP_COMMAND_LIMITTRANSIT)
|
||
|
{
|
||
|
uint32_t limit = std::stoul(params["limit"], nullptr);
|
||
|
if (limit > 0 && limit <= 65535)
|
||
|
SetMaxNumTransitTunnels (limit);
|
||
|
else {
|
||
4 years ago
|
s << "<b>" << tr("ERROR") << "</b>: " << tr("Transit tunnels count must not exceed 65535") << "\r\n<br>\r\n<br>\r\n";
|
||
|
s << "<a href=\"" << webroot << "?page=commands\">" << tr("Back to commands list") << "</a>\r\n<br>\r\n";
|
||
3 years ago
|
s << "<p>" << tr("You will be redirected in 5 seconds") << "</b>";
|
||
5 years ago
|
res.add_header("Refresh", redirect.c_str());
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
4 years ago
|
else if (cmd == HTTP_COMMAND_GET_REG_STRING)
|
||
|
{
|
||
|
std::string b32 = params["b32"];
|
||
3 years ago
|
std::string name = i2p::http::UrlDecode(params["name"]);
|
||
4 years ago
|
|
||
|
i2p::data::IdentHash ident;
|
||
|
ident.FromBase32 (b32);
|
||
|
auto dest = i2p::client::context.FindLocalDestination (ident);
|
||
|
|
||
4 years ago
|
if (dest)
|
||
4 years ago
|
{
|
||
4 years ago
|
std::size_t pos;
|
||
|
pos = name.find (".i2p");
|
||
4 years ago
|
if (pos == (name.length () - 4))
|
||
4 years ago
|
{
|
||
4 years ago
|
pos = name.find (".b32.i2p");
|
||
4 years ago
|
if (pos == std::string::npos)
|
||
4 years ago
|
{
|
||
4 years ago
|
auto signatureLen = dest->GetIdentity ()->GetSignatureLen ();
|
||
|
uint8_t * signature = new uint8_t[signatureLen];
|
||
|
char * sig = new char[signatureLen*2];
|
||
|
std::stringstream out;
|
||
|
|
||
|
out << name << "=" << dest->GetIdentity ()->ToBase64 ();
|
||
|
dest->Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), signature);
|
||
|
auto len = i2p::data::ByteStreamToBase64 (signature, signatureLen, sig, signatureLen*2);
|
||
|
sig[len] = 0;
|
||
|
out << "#!sig=" << sig;
|
||
4 years ago
|
s << "<b>" << tr("SUCCESS") << "</b>:<br>\r\n<form action=\"http://shx5vqsw7usdaunyzr2qmes2fq37oumybpudrd4jjj4e4vk4uusa.b32.i2p/add\" method=\"post\" rel=\"noreferrer\" target=\"_blank\">\r\n"
|
||
4 years ago
|
"<textarea readonly name=\"record\" cols=\"80\" rows=\"10\">" << out.str () << "</textarea>\r\n<br>\r\n<br>\r\n"
|
||
4 years ago
|
"<b>" << tr("Register at reg.i2p") << ":</b>\r\n<br>\r\n"
|
||
|
"<b>" << tr("Description") << ":</b>\r\n<input type=\"text\" maxlength=\"64\" name=\"desc\" placeholder=\"" << tr("A bit information about service on domain") << "\">\r\n"
|
||
|
"<input type=\"submit\" value=\"" << tr("Submit") << "\">\r\n"
|
||
4 years ago
|
"</form>\r\n<br>\r\n";
|
||
4 years ago
|
delete[] signature;
|
||
|
delete[] sig;
|
||
4 years ago
|
}
|
||
|
else
|
||
4 years ago
|
s << "<b>" << tr("ERROR") << "</b>: " << tr("Domain can't end with .b32.i2p") << "\r\n<br>\r\n<br>\r\n";
|
||
4 years ago
|
}
|
||
4 years ago
|
else
|
||
4 years ago
|
s << "<b>" << tr("ERROR") << "</b>: " << tr("Domain must end with .i2p") << "\r\n<br>\r\n<br>\r\n";
|
||
4 years ago
|
}
|
||
4 years ago
|
else
|
||
4 years ago
|
s << "<b>" << tr("ERROR") << "</b>: " << tr("Such destination is not found") << "\r\n<br>\r\n<br>\r\n";
|
||
4 years ago
|
|
||
4 years ago
|
s << "<a href=\"" << webroot << "?page=local_destination&b32=" << b32 << "\">" << tr("Return to destination page") << "</a>\r\n";
|
||
4 years ago
|
return;
|
||
|
}
|
||
3 years ago
|
else if (cmd == HTTP_COMMAND_SETLANGUAGE)
|
||
|
{
|
||
|
std::string lang = params["lang"];
|
||
3 years ago
|
std::string currLang = i2p::client::context.GetLanguage ()->GetLanguage();
|
||
3 years ago
|
|
||
|
if (currLang.compare(lang) != 0)
|
||
|
i2p::i18n::SetLanguage(lang);
|
||
|
}
|
||
3 years ago
|
else if (cmd == HTTP_COMMAND_RELOAD_CSS)
|
||
|
{
|
||
|
LoadExtCSS();
|
||
|
}
|
||
5 years ago
|
else
|
||
|
{
|
||
9 years ago
|
res.code = 400;
|
||
4 years ago
|
ShowError(s, tr("Unknown command") + ": " + cmd);
|
||
9 years ago
|
return;
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
s << "<b>" << tr("SUCCESS") << "</b>: " << tr("Command accepted") << "<br><br>\r\n";
|
||
|
s << "<a href=\"" << webroot << "?page=commands\">" << tr("Back to commands list") << "</a><br>\r\n";
|
||
|
s << "<p>" << tr("You will be redirected in 5 seconds") << "</b>";
|
||
6 years ago
|
res.add_header("Refresh", redirect.c_str());
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
void HTTPConnection::SendReply (HTTPRes& reply, std::string& content)
|
||
9 years ago
|
{
|
||
8 years ago
|
reply.add_header("X-Frame-Options", "SAMEORIGIN");
|
||
5 years ago
|
reply.add_header("X-Content-Type-Options", "nosniff");
|
||
|
reply.add_header("X-XSS-Protection", "1; mode=block");
|
||
9 years ago
|
reply.add_header("Content-Type", "text/html");
|
||
|
reply.body = content;
|
||
9 years ago
|
|
||
8 years ago
|
m_SendBuffer = reply.to_string();
|
||
|
boost::asio::async_write (*m_Socket, boost::asio::buffer(m_SendBuffer),
|
||
9 years ago
|
std::bind (&HTTPConnection::Terminate, shared_from_this (), std::placeholders::_1));
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
HTTPServer::HTTPServer (const std::string& address, int port):
|
||
8 years ago
|
m_IsRunning (false), m_Thread (nullptr), m_Work (m_Service),
|
||
7 years ago
|
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port)),
|
||
|
m_Hostname(address)
|
||
11 years ago
|
{
|
||
|
}
|
||
|
|
||
|
HTTPServer::~HTTPServer ()
|
||
|
{
|
||
|
Stop ();
|
||
|
}
|
||
|
|
||
|
void HTTPServer::Start ()
|
||
|
{
|
||
7 years ago
|
bool needAuth; i2p::config::GetOption("http.auth", needAuth);
|
||
9 years ago
|
std::string user; i2p::config::GetOption("http.user", user);
|
||
|
std::string pass; i2p::config::GetOption("http.pass", pass);
|
||
|
/* generate pass if needed */
|
||
|
if (needAuth && pass == "") {
|
||
8 years ago
|
uint8_t random[16];
|
||
9 years ago
|
char alnum[] = "0123456789"
|
||
7 years ago
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||
|
"abcdefghijklmnopqrstuvwxyz";
|
||
8 years ago
|
pass.resize(sizeof(random));
|
||
|
RAND_bytes(random, sizeof(random));
|
||
|
for (size_t i = 0; i < sizeof(random); i++) {
|
||
|
pass[i] = alnum[random[i] % (sizeof(alnum) - 1)];
|
||
9 years ago
|
}
|
||
|
i2p::config::SetOption("http.pass", pass);
|
||
3 years ago
|
LogPrint(eLogInfo, "HTTPServer: Password set to ", pass);
|
||
9 years ago
|
}
|
||
6 years ago
|
|
||
5 years ago
|
m_IsRunning = true;
|
||
|
m_Thread.reset (new std::thread (std::bind (&HTTPServer::Run, this)));
|
||
5 years ago
|
m_Acceptor.listen ();
|
||
|
Accept ();
|
||
3 years ago
|
|
||
|
LoadExtCSS();
|
||
11 years ago
|
}
|
||
|
|
||
|
void HTTPServer::Stop ()
|
||
|
{
|
||
8 years ago
|
m_IsRunning = false;
|
||
3 years ago
|
|
||
|
boost::system::error_code ec;
|
||
3 years ago
|
m_Acceptor.cancel(ec);
|
||
3 years ago
|
if (ec)
|
||
3 years ago
|
LogPrint (eLogDebug, "HTTPServer: Error while cancelling operations on acceptor: ", ec.message ());
|
||
11 years ago
|
m_Acceptor.close();
|
||
3 years ago
|
|
||
11 years ago
|
m_Service.stop ();
|
||
7 years ago
|
if (m_Thread)
|
||
8 years ago
|
{
|
||
9 years ago
|
m_Thread->join ();
|
||
|
m_Thread = nullptr;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
void HTTPServer::Run ()
|
||
|
{
|
||
4 years ago
|
i2p::util::SetThreadName("Webconsole");
|
||
|
|
||
8 years ago
|
while (m_IsRunning)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
m_Service.run ();
|
||
|
}
|
||
|
catch (std::exception& ex)
|
||
|
{
|
||
3 years ago
|
LogPrint (eLogError, "HTTPServer: Runtime exception: ", ex.what ());
|
||
7 years ago
|
}
|
||
8 years ago
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
void HTTPServer::Accept ()
|
||
|
{
|
||
9 years ago
|
auto newSocket = std::make_shared<boost::asio::ip::tcp::socket> (m_Service);
|
||
4 years ago
|
m_Acceptor.async_accept (*newSocket, std::bind (&HTTPServer::HandleAccept, this,
|
||
|
std::placeholders::_1, newSocket));
|
||
11 years ago
|
}
|
||
|
|
||
7 years ago
|
void HTTPServer::HandleAccept(const boost::system::error_code& ecode,
|
||
9 years ago
|
std::shared_ptr<boost::asio::ip::tcp::socket> newSocket)
|
||
11 years ago
|
{
|
||
3 years ago
|
if (!ecode)
|
||
|
CreateConnection(newSocket);
|
||
|
else
|
||
8 years ago
|
{
|
||
3 years ago
|
if (newSocket) newSocket->close();
|
||
3 years ago
|
LogPrint(eLogError, "HTTP Server: Error handling accept: ", ecode.message());
|
||
8 years ago
|
}
|
||
9 years ago
|
Accept ();
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
9 years ago
|
void HTTPServer::CreateConnection(std::shared_ptr<boost::asio::ip::tcp::socket> newSocket)
|
||
11 years ago
|
{
|
||
7 years ago
|
auto conn = std::make_shared<HTTPConnection> (m_Hostname, newSocket);
|
||
10 years ago
|
conn->Receive ();
|
||
11 years ago
|
}
|
||
9 years ago
|
} // http
|
||
|
} // i2p
|