2020-05-22 16:18:41 +03:00
/*
2024-02-15 02:53:28 +03:00
* Copyright ( c ) 2013 - 2024 , The PurpleI2P Project
2020-05-22 16:18:41 +03:00
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2015-11-22 01:26:12 +03:00
# include <iomanip>
2016-04-27 03:00:00 +03:00
# include <sstream>
# include <thread>
# include <memory>
# include <boost/asio.hpp>
2018-08-13 19:41:43 +03:00
# include <boost/algorithm/string.hpp>
2016-04-27 03:00:00 +03:00
2015-11-03 17:15:49 +03:00
# include "Base.h"
2016-02-11 03:00:00 +03:00
# include "FS.h"
2014-01-15 04:56:34 +04:00
# include "Log.h"
2016-04-27 03:00:00 +03:00
# include "Config.h"
2013-12-10 17:03:22 +04:00
# include "Tunnel.h"
# include "Transports.h"
2017-04-22 03:04:16 +03:00
# include "NetDb.hpp"
2016-04-27 03:00:00 +03:00
# include "HTTP.h"
2016-04-27 03:00:00 +03:00
# include "LeaseSet.h"
2014-10-05 16:54:59 +04:00
# include "Destination.h"
2014-09-30 21:34:29 +04:00
# include "RouterContext.h"
2014-10-16 04:52:17 +04:00
# include "ClientContext.h"
2014-09-30 21:34:29 +04:00
# include "HTTPServer.h"
2016-04-27 03:00:00 +03:00
# include "Daemon.h"
2016-06-14 21:37:22 +03:00
# include "util.h"
2020-04-30 03:50:31 +03:00
# include "ECIESX25519AEADRatchetSession.h"
2021-05-22 08:39:05 +03:00
# include "I18N.h"
2016-07-26 19:11:52 +03:00
# ifdef WIN32_APP
2020-11-17 17:59:40 +03:00
# include "Win32App.h"
2016-07-26 19:11:52 +03:00
# endif
2013-12-10 17:03:22 +04:00
2021-09-13 13:27:29 +03:00
// For image, style and info
2014-08-17 15:20:57 +04:00
# include "version.h"
2021-09-13 13:27:29 +03:00
# include "HTTPServerResources.h"
2014-08-17 15:20:57 +04:00
2016-04-27 03:00:00 +03:00
namespace i2p {
namespace http {
2021-08-06 20:42:08 +03:00
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 ( ) ;
2021-08-07 01:37:45 +03:00
} else if ( externalCSS . length ( ) ! = 0 ) { // clean up external style if file was removed
externalCSS = " " ;
2021-08-06 20:42:08 +03:00
}
}
2021-05-23 10:50:26 +03:00
static void GetStyles ( std : : stringstream & s )
{
2021-08-06 20:42:08 +03:00
if ( externalCSS . length ( ) ! = 0 )
s < < " <style> \r \n " < < externalCSS < < " </style> \r \n " ;
else
s < < internalCSS ;
2021-05-23 10:50:26 +03:00
}
2016-04-27 03:00:00 +03:00
const char HTTP_PAGE_TUNNELS [ ] = " tunnels " ;
const char HTTP_PAGE_TRANSIT_TUNNELS [ ] = " transit_tunnels " ;
2016-08-09 13:54:47 +03:00
const char HTTP_PAGE_TRANSPORTS [ ] = " transports " ;
2016-04-27 03:00:00 +03:00
const char HTTP_PAGE_LOCAL_DESTINATIONS [ ] = " local_destinations " ;
const char HTTP_PAGE_LOCAL_DESTINATION [ ] = " local_destination " ;
2016-11-18 03:14:25 +03:00
const char HTTP_PAGE_I2CP_LOCAL_DESTINATION [ ] = " i2cp_local_destination " ;
2016-04-27 03:00:00 +03:00
const char HTTP_PAGE_SAM_SESSIONS [ ] = " sam_sessions " ;
const char HTTP_PAGE_SAM_SESSION [ ] = " sam_session " ;
const char HTTP_PAGE_I2P_TUNNELS [ ] = " i2p_tunnels " ;
2016-04-27 03:00:00 +03:00
const char HTTP_PAGE_COMMANDS [ ] = " commands " ;
2016-07-15 20:52:55 +03:00
const char HTTP_PAGE_LEASESETS [ ] = " leasesets " ;
2016-07-12 03:00:00 +03:00
const char HTTP_COMMAND_ENABLE_TRANSIT [ ] = " enable_transit " ;
const char HTTP_COMMAND_DISABLE_TRANSIT [ ] = " disable_transit " ;
2016-04-27 03:00:00 +03:00
const char HTTP_COMMAND_SHUTDOWN_START [ ] = " shutdown_start " ;
const char HTTP_COMMAND_SHUTDOWN_CANCEL [ ] = " shutdown_cancel " ;
2017-09-27 23:41:34 +03:00
const char HTTP_COMMAND_SHUTDOWN_NOW [ ] = " terminate " ;
2016-05-12 18:38:18 +03:00
const char HTTP_COMMAND_RUN_PEER_TEST [ ] = " run_peer_test " ;
2022-10-09 02:06:50 +03:00
const char HTTP_COMMAND_RELOAD_TUNNELS_CONFIG [ ] = " reload_tunnels_config " ;
2017-12-01 22:18:04 +03:00
const char HTTP_COMMAND_LOGLEVEL [ ] = " set_loglevel " ;
2020-03-05 04:14:39 +03:00
const char HTTP_COMMAND_KILLSTREAM [ ] = " closestream " ;
2020-04-26 23:45:30 +03:00
const char HTTP_COMMAND_LIMITTRANSIT [ ] = " limittransit " ;
2021-03-10 01:48:42 +03:00
const char HTTP_COMMAND_GET_REG_STRING [ ] = " get_reg_string " ;
2021-06-27 17:14:45 +03:00
const char HTTP_COMMAND_SETLANGUAGE [ ] = " setlanguage " ;
2021-08-06 20:42:08 +03:00
const char HTTP_COMMAND_RELOAD_CSS [ ] = " reload_css " ;
2023-04-24 14:01:21 +03:00
const char HTTP_COMMAND_EXPIRELEASE [ ] = " expirelease " ;
2013-12-10 17:03:22 +04:00
2021-08-06 20:42:08 +03:00
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 ;
}
2018-08-19 01:56:31 +03:00
2017-09-27 23:28:58 +03:00
static void ShowUptime ( std : : stringstream & s , int seconds )
2016-11-18 03:14:25 +03:00
{
2016-04-27 03:00:00 +03:00
int num ;
2013-12-10 17:03:22 +04:00
2016-04-27 03:00:00 +03:00
if ( ( num = seconds / 86400 ) > 0 ) {
2023-01-18 07:21:36 +03:00
s < < ntr ( " %d day " , " %d days " , num , num ) < < " , " ;
2016-05-19 03:00:00 +03:00
seconds - = num * 86400 ;
2013-12-10 17:03:22 +04:00
}
2016-04-27 03:00:00 +03:00
if ( ( num = seconds / 3600 ) > 0 ) {
2023-01-18 07:21:36 +03:00
s < < ntr ( " %d hour " , " %d hours " , num , num ) < < " , " ;
2016-05-19 03:00:00 +03:00
seconds - = num * 3600 ;
2014-07-16 20:41:40 +04:00
}
2016-04-27 03:00:00 +03:00
if ( ( num = seconds / 60 ) > 0 ) {
2023-01-18 07:21:36 +03:00
s < < ntr ( " %d minute " , " %d minutes " , num , num ) < < " , " ;
2016-05-19 03:00:00 +03:00
seconds - = num * 60 ;
2016-04-27 03:00:00 +03:00
}
2023-01-18 07:21:36 +03:00
s < < ntr ( " %d second " , " %d seconds " , seconds , seconds ) ;
2014-03-30 02:16:23 +04:00
}
2017-04-27 23:11:37 +03:00
static void ShowTraffic ( std : : stringstream & s , uint64_t bytes )
{
s < < std : : fixed < < std : : setprecision ( 2 ) ;
auto numKBytes = ( double ) bytes / 1024 ;
if ( numKBytes < 1024 )
2023-01-18 07:21:36 +03:00
s < < tr ( /* tr: Kibibyte */ " %.2f KiB " , numKBytes ) ;
2017-04-27 23:11:37 +03:00
else if ( numKBytes < 1024 * 1024 )
2023-01-18 07:21:36 +03:00
s < < tr ( /* tr: Mebibyte */ " %.2f MiB " , numKBytes / 1024 ) ;
2017-04-27 23:11:37 +03:00
else
2023-01-18 07:21:36 +03:00
s < < tr ( /* tr: Gibibyte */ " %.2f GiB " , numKBytes / 1024 / 1024 ) ;
2017-04-27 23:11:37 +03:00
}
2017-12-01 22:18:04 +03:00
static void ShowTunnelDetails ( std : : stringstream & s , enum i2p : : tunnel : : TunnelState eState , bool explr , int bytes )
2014-01-15 04:56:34 +04:00
{
2021-05-23 10:50:26 +03:00
std : : string state , stateText ;
2024-02-15 02:53:28 +03:00
switch ( eState )
{
2016-04-27 03:00:00 +03:00
case i2p : : tunnel : : eTunnelStateBuildReplyReceived :
2021-05-23 10:50:26 +03:00
case i2p : : tunnel : : eTunnelStatePending : state = " building " ; break ;
2024-02-15 02:53:28 +03:00
case i2p : : tunnel : : eTunnelStateBuildFailed : state = " failed " ; stateText = " declined " ; break ;
case i2p : : tunnel : : eTunnelStateTestFailed : state = " failed " ; stateText = " test failed " ; break ;
2021-05-23 10:50:26 +03:00
case i2p : : tunnel : : eTunnelStateFailed : state = " failed " ; break ;
case i2p : : tunnel : : eTunnelStateExpiring : state = " expiring " ; break ;
2016-04-27 03:00:00 +03:00
case i2p : : tunnel : : eTunnelStateEstablished : state = " established " ; break ;
default : state = " unknown " ; break ;
2014-07-16 20:41:40 +04:00
}
2024-02-15 02:53:28 +03:00
if ( stateText . empty ( ) ) stateText = tr ( state ) ;
2021-05-23 10:50:26 +03:00
s < < " <span class= \" tunnel " < < state < < " \" > " < < stateText < < ( ( explr ) ? " ( " + tr ( " exploratory " ) + " ) " : " " ) < < " </span>, " ;
2023-01-21 02:03:00 +03:00
ShowTraffic ( s , bytes ) ;
s < < " \r \n " ;
2014-07-16 20:41:40 +04:00
}
2017-12-01 22:18:04 +03:00
static void SetLogLevel ( const std : : string & level )
{
2023-03-31 14:29:04 +03:00
if ( level = = " none " | | level = = " critical " | | level = = " error " | | level = = " warn " | | level = = " info " | | level = = " debug " )
2017-12-01 22:18:04 +03:00
i2p : : log : : Logger ( ) . SetLogLevel ( level ) ;
else {
2021-11-27 22:53:53 +03:00
LogPrint ( eLogError , " HTTPServer: Unknown loglevel set attempted " ) ;
2017-12-01 22:18:04 +03:00
return ;
}
i2p : : log : : Logger ( ) . Reopen ( ) ;
}
2016-11-18 03:14:25 +03:00
static void ShowPageHead ( std : : stringstream & s )
2014-03-27 23:42:23 +04:00
{
2021-05-23 10:50:26 +03:00
std : : string webroot ; i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
// Page language
2021-09-04 21:01:57 +03:00
std : : string currLang = i2p : : client : : context . GetLanguage ( ) - > GetLanguage ( ) ; // get current used language
2021-08-06 20:42:08 +03:00
auto it = i2p : : i18n : : languages . find ( currLang ) ;
std : : string langCode = it - > second . ShortCode ;
2021-05-23 10:50:26 +03:00
2016-04-27 03:00:00 +03:00
s < <
" <!DOCTYPE html> \r \n "
2021-05-23 10:50:26 +03:00
" <html lang= \" " < < langCode < < " \" > \r \n "
2016-06-28 00:00:54 +03:00
" <head> \r \n " /* TODO: Find something to parse html/template system. This is horrible. */
" <meta charset= \" UTF-8 \" > \r \n "
2020-06-13 18:19:24 +03:00
" <meta name= \" viewport \" content= \" width=device-width, initial-scale=1.0 \" > \r \n "
2016-06-14 11:25:51 +03:00
" <link rel= \" shortcut icon \" href= \" " < < itoopieFavicon < < " \" > \r \n "
2022-07-27 00:37:48 +03:00
" <title> " < < tr ( /* tr: Webconsole page title */ " Purple I2P Webconsole " ) < < " </title> \r \n " ;
2021-05-23 10:50:26 +03:00
GetStyles ( s ) ;
2016-04-27 03:00:00 +03:00
s < <
2021-05-23 10:50:26 +03:00
" </head> \r \n "
2016-04-27 03:00:00 +03:00
" <body> \r \n "
2021-05-23 10:50:26 +03:00
" <div class= \" header \" > " < < tr ( " <b>i2pd</b> webconsole " ) < < " </div> \r \n "
2020-06-13 18:19:24 +03:00
" <div class= \" wrapper \" > \r \n "
" <div class= \" menu \" > \r \n "
2021-06-27 17:14:45 +03:00
" <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 "
2021-06-28 12:45:28 +03:00
" <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_LOCAL_DESTINATIONS < < " \" > " < < tr ( " Local Destinations " ) < < " </a><br> \r \n " ;
2020-03-19 02:26:55 +03:00
if ( i2p : : context . IsFloodfill ( ) )
2021-06-27 17:14:45 +03:00
s < < " <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_LEASESETS < < " \" > " < < tr ( " LeaseSets " ) < < " </a><br> \r \n " ;
2020-03-19 02:26:55 +03:00
s < <
2022-05-01 23:25:08 +03:00
" <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 < <
2023-01-19 08:33:03 +03:00
" <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_TRANSPORTS < < " \" > " < < tr ( " Transports " ) < < " </a><br> \r \n "
2021-06-27 17:14:45 +03:00
" <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_I2P_TUNNELS < < " \" > " < < tr ( " I2P tunnels " ) < < " </a><br> \r \n " ;
2016-08-09 13:54:47 +03:00
if ( i2p : : client : : context . GetSAMBridge ( ) )
2021-06-27 17:14:45 +03:00
s < < " <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_SAM_SESSIONS < < " \" > " < < tr ( " SAM sessions " ) < < " </a><br> \r \n " ;
2016-08-09 13:54:47 +03:00
s < <
2016-04-27 03:00:00 +03:00
" </div> \r \n "
2020-06-13 18:19:24 +03:00
" <div class= \" content \" > " ;
2014-03-27 23:42:23 +04:00
}
2016-11-18 03:14:25 +03:00
static void ShowPageTail ( std : : stringstream & s )
2013-12-10 17:03:22 +04:00
{
2016-04-27 03:00:00 +03:00
s < <
2020-06-15 09:01:17 +03:00
" </div> \r \n </div> \r \n "
2016-04-27 03:00:00 +03:00
" </body> \r \n "
" </html> \r \n " ;
2013-12-10 17:03:22 +04:00
}
2016-11-18 03:14:25 +03:00
static void ShowError ( std : : stringstream & s , const std : : string & string )
2013-12-10 17:03:22 +04:00
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " ERROR " ) < < " :</b> " < < string < < " <br> \r \n " ;
2014-07-16 20:41:40 +04:00
}
2013-12-10 17:03:22 +04:00
2023-07-30 15:29:10 +03:00
static void ShowNetworkStatus ( std : : stringstream & s , RouterStatus status , bool testing , RouterError error )
2013-12-10 17:03:22 +04:00
{
2021-03-23 22:36:57 +03:00
switch ( status )
2015-02-26 21:44:18 +03:00
{
2021-05-23 10:50:26 +03:00
case eRouterStatusOK : s < < tr ( " OK " ) ; 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 ;
2022-08-30 04:11:17 +03:00
default : s < < tr ( " Unknown " ) ;
}
2023-07-30 15:29:10 +03:00
if ( testing )
s < < " ( " < < tr ( " Testing " ) < < " ) " ;
2022-08-30 04:11:17 +03:00
if ( error ! = eRouterErrorNone )
{
switch ( error )
2017-09-27 23:28:58 +03:00
{
2022-08-30 04:11:17 +03:00
case eRouterErrorClockSkew :
s < < " - " < < tr ( " Clock skew " ) ;
break ;
case eRouterErrorOffline :
s < < " - " < < tr ( " Offline " ) ;
2016-09-20 04:37:04 +03:00
break ;
2022-08-30 04:11:17 +03:00
case eRouterErrorSymmetricNAT :
s < < " - " < < tr ( " Symmetric NAT " ) ;
break ;
2023-01-07 20:06:26 +03:00
case eRouterErrorFullConeNAT :
s < < " - " < < tr ( " Full cone NAT " ) ;
2023-01-18 07:21:36 +03:00
break ;
2022-12-10 21:22:37 +03:00
case eRouterErrorNoDescriptors :
s < < " - " < < tr ( " No Descriptors " ) ;
2022-12-10 21:57:23 +03:00
break ;
2022-08-30 04:11:17 +03:00
default : ;
2017-09-27 23:28:58 +03:00
}
2022-10-09 20:24:43 +03:00
}
2021-05-15 16:28:16 +03:00
}
2021-03-23 22:36:57 +03:00
void ShowStatus ( std : : stringstream & s , bool includeHiddenContent , i2p : : http : : OutputFormatEnum outputFormat )
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Uptime " ) < < " :</b> " ;
2021-03-23 22:36:57 +03:00
ShowUptime ( s , i2p : : context . GetUptime ( ) ) ;
2016-01-17 19:10:56 +03:00
s < < " <br> \r \n " ;
2021-08-13 18:36:04 +03:00
s < < " <b> " < < tr ( " Network status " ) < < " :</b> " ;
2023-07-30 15:29:10 +03:00
ShowNetworkStatus ( s , i2p : : context . GetStatus ( ) , i2p : : context . GetTesting ( ) , i2p : : context . GetError ( ) ) ;
2021-08-13 18:36:04 +03:00
s < < " <br> \r \n " ;
2021-03-23 22:36:57 +03:00
if ( i2p : : context . SupportsV6 ( ) )
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Network status v6 " ) < < " :</b> " ;
2023-07-30 15:29:10 +03:00
ShowNetworkStatus ( s , i2p : : context . GetStatusV6 ( ) , i2p : : context . GetTestingV6 ( ) , i2p : : context . GetErrorV6 ( ) ) ;
2021-03-23 22:36:57 +03:00
s < < " <br> \r \n " ;
2021-05-15 16:28:16 +03:00
}
2018-04-26 22:28:30 +03:00
# if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY))
2020-11-16 03:38:04 +03:00
if ( auto remains = Daemon . gracefulShutdownInterval ) {
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Stopping in " ) < < " :</b> " ;
2020-11-16 03:38:04 +03:00
ShowUptime ( s , remains ) ;
s < < " <br> \r \n " ;
2020-11-17 17:59:40 +03:00
}
2020-11-16 03:38:04 +03:00
# elif defined(WIN32_APP)
if ( i2p : : win32 : : g_GracefulShutdownEndtime ! = 0 ) {
uint16_t remains = ( i2p : : win32 : : g_GracefulShutdownEndtime - GetTickCount ( ) ) / 1000 ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Stopping in " ) < < " :</b> " ;
2020-11-16 03:38:04 +03:00
ShowUptime ( s , remains ) ;
s < < " <br> \r \n " ;
}
2016-08-21 03:00:00 +03:00
# endif
2016-04-29 01:16:11 +03:00
auto family = i2p : : context . GetFamily ( ) ;
if ( family . length ( ) > 0 )
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Family " ) < < " :</b> " < < family < < " <br> \r \n " ;
s < < " <b> " < < tr ( " Tunnel creation success rate " ) < < " :</b> " < < i2p : : tunnel : : tunnels . GetTunnelCreationSuccessRate ( ) < < " %<br> \r \n " ;
2023-02-01 19:14:56 +03:00
bool isTotalTCSR ;
i2p : : config : : GetOption ( " http.showTotalTCSR " , isTotalTCSR ) ;
if ( isTotalTCSR ) {
s < < " <b> " < < tr ( " Total tunnel creation success rate " ) < < " :</b> " < < i2p : : tunnel : : tunnels . GetTotalTunnelCreationSuccessRate ( ) < < " %<br/> \r \n " ;
2023-02-01 14:06:28 +03:00
}
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Received " ) < < " :</b> " ;
2017-04-27 23:11:37 +03:00
ShowTraffic ( s , i2p : : transport : : transports . GetTotalReceivedBytes ( ) ) ;
2023-01-19 07:46:54 +03:00
s < < " ( " < < tr ( /* tr: Kibibyte/s */ " %.2f KiB/s " , ( double ) i2p : : transport : : transports . GetInBandwidth15s ( ) / 1024 ) < < " )<br> \r \n " ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Sent " ) < < " :</b> " ;
2017-04-27 23:11:37 +03:00
ShowTraffic ( s , i2p : : transport : : transports . GetTotalSentBytes ( ) ) ;
2023-01-19 07:46:54 +03:00
s < < " ( " < < tr ( /* tr: Kibibyte/s */ " %.2f KiB/s " , ( double ) i2p : : transport : : transports . GetOutBandwidth15s ( ) / 1024 ) < < " )<br> \r \n " ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Transit " ) < < " :</b> " ;
2017-05-02 21:20:00 +03:00
ShowTraffic ( s , i2p : : transport : : transports . GetTotalTransitTransmittedBytes ( ) ) ;
2023-01-19 07:46:54 +03:00
s < < " ( " < < tr ( /* tr: Kibibyte/s */ " %.2f KiB/s " , ( double ) i2p : : transport : : transports . GetTransitBandwidth15s ( ) / 1024 ) < < " )<br> \r \n " ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Data path " ) < < " :</b> " < < i2p : : fs : : GetUTF8DataDir ( ) < < " <br> \r \n " ;
2020-03-05 04:14:39 +03:00
s < < " <div class='slide'> " ;
2022-05-01 23:25:08 +03:00
if ( ( outputFormat = = OutputFormatEnum : : forWebConsole ) | | ! includeHiddenContent ) {
2021-05-23 10:50:26 +03:00
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 " ;
2020-03-05 04:14:39 +03:00
}
2023-01-01 13:48:11 +03:00
if ( includeHiddenContent )
2022-12-05 03:21:51 +03:00
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Router Ident " ) < < " :</b> " < < i2p : : context . GetRouterInfo ( ) . GetIdentHashBase64 ( ) < < " <br> \r \n " ;
2020-03-19 02:26:55 +03:00
if ( ! i2p : : context . GetRouterInfo ( ) . GetProperty ( " family " ) . empty ( ) )
2021-05-23 10:50:26 +03:00
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 " ;
2022-12-05 03:21:51 +03:00
auto addresses = i2p : : context . GetRouterInfo ( ) . GetAddresses ( ) ;
if ( addresses )
2023-01-01 13:48:11 +03:00
{
2022-12-05 03:21:51 +03:00
for ( const auto & address : * addresses )
2022-05-01 23:25:08 +03:00
{
2022-12-16 23:12:30 +03:00
if ( ! address ) continue ;
2022-12-05 03:21:51 +03:00
s < < " <tr> \r \n <td> " ;
switch ( address - > transportStyle )
{
case i2p : : data : : RouterInfo : : eTransportNTCP2 :
s < < " NTCP2 " ;
break ;
case i2p : : data : : RouterInfo : : eTransportSSU2 :
s < < " SSU2 " ;
break ;
default :
s < < tr ( " Unknown " ) ;
}
2023-02-01 01:25:41 +03:00
bool v6 = address - > IsV6 ( ) ;
if ( v6 )
2022-12-05 03:21:51 +03:00
{
if ( address - > IsV4 ( ) ) s < < " v4 " ;
s < < " v6 " ;
}
2022-04-18 19:27:57 +03:00
s < < " </td> \r \n " ;
2022-12-05 03:21:51 +03:00
if ( address - > published )
2023-02-01 01:25:41 +03:00
s < < " <td> " < < ( v6 ? " [ " : " " ) < < address - > host . to_string ( ) < < ( v6 ? " ]: " : " : " ) < < address - > port < < " </td> \r \n " ;
2022-12-05 03:21:51 +03:00
else
{
2023-01-18 07:21:36 +03:00
s < < " <td> " < < tr ( /* tr: Shown when router doesn't publish itself and have "Firewalled" state */ " supported " ) ;
2022-12-05 03:21:51 +03:00
if ( address - > port )
s < < " : " < < address - > port ;
s < < " </td> \r \n " ;
}
s < < " </tr> \r \n " ;
2022-05-01 23:25:08 +03:00
}
2023-01-01 13:48:11 +03:00
}
2020-03-19 02:26:55 +03:00
s < < " </tbody></table> \r \n " ;
2020-03-05 04:14:39 +03:00
}
2020-03-19 02:34:45 +03:00
s < < " </div> \r \n </div> \r \n " ;
2022-05-01 23:25:08 +03:00
if ( outputFormat = = OutputFormatEnum : : forQtUi ) {
2020-03-05 04:14:39 +03:00
s < < " <br> " ;
}
2021-05-23 10:50:26 +03:00
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 " ;
2016-03-02 17:41:37 +03:00
2016-03-02 18:05:26 +03:00
size_t clientTunnelCount = i2p : : tunnel : : tunnels . CountOutboundTunnels ( ) ;
clientTunnelCount + = i2p : : tunnel : : tunnels . CountInboundTunnels ( ) ;
size_t transitTunnelCount = i2p : : tunnel : : tunnels . CountTransitTunnels ( ) ;
2016-08-09 13:54:47 +03:00
2021-05-23 10:50:26 +03:00
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 " ;
2017-11-14 21:31:13 +03:00
2022-05-01 23:25:08 +03:00
if ( outputFormat = = OutputFormatEnum : : forWebConsole ) {
2021-08-06 20:42:08 +03:00
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 ) ;
2021-05-23 10:50:26 +03:00
s < < " <table class= \" services \" ><caption> " < < tr ( " Services " ) < < " </caption><tbody> \r \n " ;
2021-08-06 20:42:08 +03:00
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 " ;
2020-03-19 02:26:55 +03:00
s < < " </tbody></table> \r \n " ;
2020-03-05 04:14:39 +03:00
}
2014-09-29 00:12:25 +04:00
}
2017-09-27 23:28:58 +03:00
void ShowLocalDestinations ( std : : stringstream & s )
2014-09-29 03:15:04 +04:00
{
2018-11-07 18:07:05 +03:00
std : : string webroot ; i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Local Destinations " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2014-10-16 04:52:17 +04:00
for ( auto & it : i2p : : client : : context . GetDestinations ( ) )
2014-09-29 03:15:04 +04:00
{
2017-09-27 23:28:58 +03:00
auto ident = it . second - > GetIdentHash ( ) ;
2020-06-15 09:01:17 +03:00
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 ;
2014-09-29 03:15:04 +04:00
}
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-11-18 03:14:25 +03:00
auto i2cpServer = i2p : : client : : context . GetI2CPServer ( ) ;
2017-11-14 21:31:13 +03:00
if ( i2cpServer & & ! ( i2cpServer - > GetSessions ( ) . empty ( ) ) )
2016-11-18 03:14:25 +03:00
{
2021-05-23 10:50:26 +03:00
s < < " <br><b>I2CP " < < tr ( " Local Destinations " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2016-11-18 03:14:25 +03:00
for ( auto & it : i2cpServer - > GetSessions ( ) )
{
auto dest = it . second - > GetDestination ( ) ;
if ( dest )
2017-09-27 23:28:58 +03:00
{
auto ident = dest - > GetIdentHash ( ) ;
2017-12-13 15:17:45 +03:00
auto & name = dest - > GetNickname ( ) ;
2020-06-15 09:01:17 +03:00
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 ;
2017-09-27 23:28:58 +03:00
}
2016-11-18 03:14:25 +03:00
}
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-11-18 03:14:25 +03:00
}
}
2024-02-23 00:07:07 +03:00
static void ShowHop ( std : : stringstream & s , const i2p : : data : : IdentityEx & ident )
{
auto identHash = ident . GetIdentHash ( ) ;
auto router = i2p : : data : : netdb . FindRouter ( identHash ) ;
s < < i2p : : data : : GetIdentHashAbbreviation ( identHash ) ;
if ( router )
s < < " <small style= \" color:gray \" > " < < router - > GetBandwidthCap ( ) < < " </small> " ;
}
2021-03-10 01:48:42 +03:00
static void ShowLeaseSetDestination ( std : : stringstream & s , std : : shared_ptr < const i2p : : client : : LeaseSetDestination > dest , uint32_t token )
2016-11-18 03:14:25 +03:00
{
2023-06-10 04:08:56 +03:00
s < < " <b>Base32:</b><br> \r \n <textarea readonly cols= \" 80 \" rows= \" 1 \" > " ;
s < < dest - > GetIdentHash ( ) . ToBase32 ( ) < < " </textarea><br> \r \n <br> \r \n " ;
2021-03-11 15:20:40 +03:00
s < < " <b>Base64:</b><br> \r \n <textarea readonly cols= \" 80 \" rows= \" 8 \" > " ;
2016-11-18 03:14:25 +03:00
s < < dest - > GetIdentity ( ) - > ToBase64 ( ) < < " </textarea><br> \r \n <br> \r \n " ;
2023-06-10 04:08:56 +03:00
2019-05-14 21:42:10 +03:00
if ( dest - > IsEncryptedLeaseSet ( ) )
{
2019-09-06 18:02:19 +03:00
i2p : : data : : BlindedPublicKey blinded ( dest - > GetIdentity ( ) , dest - > IsPerClientAuth ( ) ) ;
2021-05-23 10:50:26 +03:00
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 " ;
2019-05-15 14:15:10 +03:00
s < < blinded . ToB33 ( ) < < " .b32.i2p<br> \r \n " ;
2020-03-19 02:26:55 +03:00
s < < " </div> \r \n </div> \r \n " ;
2019-05-14 21:42:10 +03:00
}
2022-06-03 19:32:46 +03:00
if ( dest - > IsPublic ( ) & & token & & ! dest - > IsEncryptedLeaseSet ( ) )
2021-03-10 01:48:42 +03:00
{
std : : string webroot ; i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
2021-05-23 10:50:26 +03:00
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 "
2021-03-10 01:48:42 +03:00
" <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 "
2023-04-24 14:01:21 +03:00
" <input type= \" hidden \" name= \" b32 \" value= \" " < < dest - > GetIdentHash ( ) . ToBase32 ( ) < < " \" > \r \n "
2021-05-23 10:50:26 +03:00
" <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 "
2021-05-23 16:30:42 +03:00
" </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 " ;
2021-03-10 01:48:42 +03:00
}
2022-05-01 23:25:08 +03:00
if ( dest - > GetNumRemoteLeaseSets ( ) )
2016-11-18 03:14:25 +03:00
{
2021-05-23 10:50:26 +03:00
s < < " <div class='slide'><label for='slide-lease'><b> " < < tr ( " LeaseSets " ) < < " :</b> <i> " < < dest - > GetNumRemoteLeaseSets ( )
2023-04-24 14:01:21 +03:00
< < " </i></label> \r \n <input type= \" checkbox \" id= \" slide-lease \" /> \r \n <div class= \" slidecontent \" > \r \n "
< < " <table><thead> "
< < " <th> " < < tr ( " Address " ) < < " </th> "
< < " <th style= \" width:5px; \" > </th> " // LeaseSet expiration button column
< < " <th> " < < tr ( " Type " ) < < " </th> "
< < " <th> " < < tr ( " EncType " ) < < " </th> "
< < " </thead><tbody class= \" tableitem \" > " ;
2016-11-18 03:14:25 +03:00
for ( auto & it : dest - > GetLeaseSets ( ) )
2023-04-24 14:01:21 +03:00
{
s < < " <tr> "
< < " <td> " < < it . first . ToBase32 ( ) < < " </td> "
< < " <td><a class= \" button \" href= \" /?cmd= " < < HTTP_COMMAND_EXPIRELEASE < < " &b32= " < < dest - > GetIdentHash ( ) . ToBase32 ( )
< < " &lease= " < < it . first . ToBase32 ( ) < < " &token= " < < token < < " \" title= \" " < < tr ( " Expire LeaseSet " ) < < " \" > ✘ </a></td> "
< < " <td> " < < ( int ) it . second - > GetStoreType ( ) < < " </td> "
< < " <td> " < < ( int ) it . second - > GetEncryptionType ( ) < < " </td> "
< < " </tr> \r \n " ;
}
2020-03-19 02:26:55 +03:00
s < < " </tbody></table> \r \n </div> \r \n </div> \r \n <br> \r \n " ;
2017-11-14 21:31:13 +03:00
} else
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " LeaseSets " ) < < " :</b> <i>0</i><br> \r \n <br> \r \n " ;
2020-06-15 13:05:01 +03:00
2016-11-18 03:14:25 +03:00
auto pool = dest - > GetTunnelPool ( ) ;
if ( pool )
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Inbound tunnels " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2016-11-18 03:14:25 +03:00
for ( auto & it : pool - > GetInboundTunnels ( ) ) {
2020-06-15 13:05:01 +03:00
s < < " <div class= \" listitem \" > " ;
2022-05-01 23:25:08 +03:00
// for each tunnel hop if not zero-hop
if ( it - > GetNumHops ( ) )
{
it - > VisitTunnelHops (
[ & s ] ( std : : shared_ptr < const i2p : : data : : IdentityEx > hopIdent )
{
2024-02-23 00:07:07 +03:00
s < < " ⇒ " ;
ShowHop ( s , * hopIdent ) ;
s < < " " ;
2022-05-01 23:25:08 +03:00
}
) ;
}
s < < " ⇒ " < < it - > GetTunnelID ( ) < < " :me " ;
if ( it - > LatencyIsKnown ( ) )
2023-01-18 07:21:36 +03:00
s < < " ( " < < tr ( /* tr: Milliseconds */ " %dms " , it - > GetMeanLatency ( ) ) < < " ) " ;
2017-12-01 22:18:04 +03:00
ShowTunnelDetails ( s , it - > GetState ( ) , false , it - > GetNumReceivedBytes ( ) ) ;
2020-06-15 13:05:01 +03:00
s < < " </div> \r \n " ;
2016-11-18 03:14:25 +03:00
}
s < < " <br> \r \n " ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Outbound tunnels " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2016-11-18 03:14:25 +03:00
for ( auto & it : pool - > GetOutboundTunnels ( ) ) {
2020-06-15 13:05:01 +03:00
s < < " <div class= \" listitem \" > " ;
2022-05-01 23:25:08 +03:00
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 )
{
2024-02-23 00:07:07 +03:00
s < < " " ;
ShowHop ( s , * hopIdent ) ;
s < < " ⇒ " ;
2022-05-01 23:25:08 +03:00
}
) ;
}
if ( it - > LatencyIsKnown ( ) )
2023-01-18 07:21:36 +03:00
s < < " ( " < < tr ( " %dms " , it - > GetMeanLatency ( ) ) < < " ) " ;
2017-12-01 22:18:04 +03:00
ShowTunnelDetails ( s , it - > GetState ( ) , false , it - > GetNumSentBytes ( ) ) ;
2020-06-15 13:05:01 +03:00
s < < " </div> \r \n " ;
2016-11-18 03:14:25 +03:00
}
}
s < < " <br> \r \n " ;
2020-04-30 16:21:49 +03:00
2023-01-18 07:21:36 +03:00
s < < " <b> " < < tr ( " Tags " ) < < " </b><br> \r \n "
< < tr ( " Incoming " ) < < " : <i> " < < dest - > GetNumIncomingTags ( ) < < " </i><br> \r \n " ;
2017-12-01 22:18:04 +03:00
if ( ! dest - > GetSessions ( ) . empty ( ) ) {
2018-10-01 18:11:17 +03:00
std : : stringstream tmp_s ; uint32_t out_tags = 0 ;
for ( const auto & it : dest - > GetSessions ( ) ) {
2020-03-19 02:26:55 +03:00
tmp_s < < " <tr><td> " < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( it . first ) < < " </td><td> " < < it . second - > GetNumOutgoingTags ( ) < < " </td></tr> \r \n " ;
2020-04-30 16:21:49 +03:00
out_tags + = it . second - > GetNumOutgoingTags ( ) ;
2018-10-01 18:11:17 +03:00
}
2023-01-18 07:21:36 +03:00
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 " ;
2017-12-01 22:18:04 +03:00
} else
2021-05-23 10:50:26 +03:00
s < < tr ( " Outgoing " ) < < " : <i>0</i><br> \r \n " ;
2017-12-01 22:18:04 +03:00
s < < " <br> \r \n " ;
2020-04-30 16:21:49 +03:00
auto numECIESx25519Tags = dest - > GetNumIncomingECIESx25519Tags ( ) ;
if ( numECIESx25519Tags > 0 ) {
2021-05-23 10:50:26 +03:00
s < < " <b>ECIESx25519</b><br> \r \n " < < tr ( " Incoming Tags " ) < < " : <i> " < < numECIESx25519Tags < < " </i><br> \r \n " ;
2020-04-30 16:21:49 +03:00
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 + + ;
}
2023-01-18 07:21:36 +03:00
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 " ;
2020-04-30 16:21:49 +03:00
} else
2021-05-23 10:50:26 +03:00
s < < tr ( " Tags sessions " ) < < " : <i>0</i><br> \r \n " ;
2020-04-30 16:21:49 +03:00
s < < " <br> \r \n " ;
}
2016-01-13 04:31:25 +03:00
}
2014-09-30 06:18:32 +04:00
2020-03-05 04:14:39 +03:00
void ShowLocalDestination ( std : : stringstream & s , const std : : string & b32 , uint32_t token )
2014-09-30 06:18:32 +04:00
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Local Destination " ) < < " :</b><br> \r \n <br> \r \n " ;
2014-09-30 06:18:32 +04:00
i2p : : data : : IdentHash ident ;
2015-04-10 19:11:10 +03:00
ident . FromBase32 ( b32 ) ;
2014-10-16 18:28:44 +04:00
auto dest = i2p : : client : : context . FindLocalDestination ( ident ) ;
2020-03-05 04:14:39 +03:00
2014-09-30 06:18:32 +04:00
if ( dest )
{
2021-03-10 01:48:42 +03:00
ShowLeaseSetDestination ( s , dest , token ) ;
2021-06-28 12:45:28 +03:00
// Print table with streams information
2022-07-27 00:37:48 +03:00
s < < " <table> \r \n <caption> "
< < tr ( " Streams " )
< < " </caption> \r \n <thead> \r \n <tr> "
< < " <th style= \" width:25px; \" >StreamID</th> "
2023-02-15 20:56:44 +03:00
< < " <th style= \" width:5px; \" > </th> " // Stream closing button column
2022-07-27 00:37:48 +03:00
< < " <th class= \" streamdest \" >Destination</th> "
< < " <th>Sent</th> "
< < " <th>Received</th> "
< < " <th>Out</th> "
< < " <th>In</th> "
< < " <th>Buf</th> "
< < " <th>RTT</th> "
< < " <th>Window</th> "
< < " <th>Status</th> "
< < " </tr> \r \n </thead> \r \n <tbody class= \" tableitem \" > \r \n " ;
2016-01-17 19:10:56 +03:00
2016-08-05 21:23:54 +03:00
for ( const auto & it : dest - > GetAllStreams ( ) )
2016-08-09 13:54:47 +03:00
{
2020-03-05 04:14:39 +03:00
auto streamDest = i2p : : client : : context . GetAddressBook ( ) . ToAddress ( it - > GetRemoteIdentity ( ) ) ;
2020-06-15 13:05:01 +03:00
std : : string streamDestShort = streamDest . substr ( 0 , 12 ) + " ….b32.i2p " ;
2016-01-17 19:10:56 +03:00
s < < " <tr> " ;
2020-03-05 04:14:39 +03:00
s < < " <td> " < < it - > GetRecvStreamID ( ) < < " </td> " ;
if ( it - > GetRecvStreamID ( ) ) {
s < < " <td><a class= \" button \" href= \" /?cmd= " < < HTTP_COMMAND_KILLSTREAM < < " &b32= " < < b32 < < " &streamID= "
2021-05-23 10:50:26 +03:00
< < it - > GetRecvStreamID ( ) < < " &token= " < < token < < " \" title= \" " < < tr ( " Close stream " ) < < " \" > ✘ </a></td> " ;
2020-03-05 04:14:39 +03:00
} else {
s < < " <td \\ > " ;
}
2020-06-15 13:05:01 +03:00
s < < " <td class= \" streamdest \" title= \" " < < streamDest < < " \" > " < < streamDestShort < < " </td> " ;
2016-04-05 05:17:04 +03:00
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> " ;
2017-12-01 22:18:04 +03:00
s < < " </tr> \r \n " ;
}
2020-03-05 04:14:39 +03:00
s < < " </tbody> \r \n </table> " ;
2016-08-09 13:54:47 +03:00
}
2023-06-10 04:08:56 +03:00
else
ShowError ( s , tr ( " Such destination is not found " ) ) ;
2016-01-13 04:31:25 +03:00
}
2021-03-10 01:48:42 +03:00
void ShowI2CPLocalDestination ( std : : stringstream & s , const std : : string & id )
2016-11-18 03:14:25 +03:00
{
auto i2cpServer = i2p : : client : : context . GetI2CPServer ( ) ;
if ( i2cpServer )
{
2021-05-23 10:50:26 +03:00
s < < " <b>I2CP " < < tr ( " Local Destination " ) < < " :</b><br> \r \n <br> \r \n " ;
2017-09-27 23:28:58 +03:00
auto it = i2cpServer - > GetSessions ( ) . find ( std : : stoi ( id ) ) ;
2016-11-18 03:14:25 +03:00
if ( it ! = i2cpServer - > GetSessions ( ) . end ( ) )
2021-03-10 01:48:42 +03:00
ShowLeaseSetDestination ( s , it - > second - > GetDestination ( ) , 0 ) ;
2016-11-18 03:14:25 +03:00
else
2021-05-23 10:50:26 +03:00
ShowError ( s , tr ( " I2CP session not found " ) ) ;
2016-11-18 03:14:25 +03:00
}
else
2021-05-23 10:50:26 +03:00
ShowError ( s , tr ( " I2CP is not enabled " ) ) ;
2016-11-18 03:14:25 +03:00
}
2017-09-27 23:28:58 +03:00
void ShowLeasesSets ( std : : stringstream & s )
2016-07-15 20:52:55 +03:00
{
2020-03-19 02:26:55 +03:00
if ( i2p : : data : : netdb . GetNumLeaseSets ( ) )
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " LeaseSets " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2020-03-19 02:26:55 +03:00
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
2023-03-31 14:29:04 +03:00
{
2023-02-18 05:13:50 +03:00
ls . reset ( new i2p : : data : : LeaseSet2 ( storeType ) ) ;
ls - > Update ( leaseSet - > GetBuffer ( ) , leaseSet - > GetBufferLen ( ) , false ) ;
2023-03-31 14:29:04 +03:00
}
2020-03-19 02:26:55 +03:00
if ( ! ls ) return ;
2020-06-15 13:05:01 +03:00
s < < " <div class= \" leaseset listitem " ;
2020-03-19 02:26:55 +03:00
if ( ls - > IsExpired ( ) )
s < < " expired " ; // additional css class for expired
2020-06-15 13:05:01 +03:00
s < < " \" > \r \n " ;
2020-03-19 02:26:55 +03:00
if ( ! ls - > IsValid ( ) )
2021-05-23 10:50:26 +03:00
s < < " <div class= \" invalid \" >!! " < < tr ( " Invalid " ) < < " !! </div> \r \n " ;
2020-06-15 13:05:01 +03:00
s < < " <div class= \" slide \" ><label for= \" slide " < < counter < < " \" > " < < dest . ToBase32 ( ) < < " </label> \r \n " ;
2020-06-13 18:33:39 +03:00
s < < " <input type= \" checkbox \" id= \" slide " < < ( counter + + ) < < " \" /> \r \n <div class= \" slidecontent \" > \r \n " ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Store type " ) < < " :</b> " < < ( int ) storeType < < " <br> \r \n " ;
s < < " <b> " < < tr ( " Expires " ) < < " :</b> " < < ConvertTime ( ls - > GetExpirationTime ( ) ) < < " <br> \r \n " ;
2020-03-19 02:26:55 +03:00
if ( storeType = = i2p : : data : : NETDB_STORE_TYPE_LEASESET | | storeType = = i2p : : data : : NETDB_STORE_TYPE_STANDARD_LEASESET2 )
2019-01-15 02:39:02 +03:00
{
2020-03-19 02:26:55 +03:00
// leases information is available
auto leases = ls - > GetNonExpiredLeases ( ) ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Non Expired Leases " ) < < " : " < < leases . size ( ) < < " </b><br> \r \n " ;
2020-03-19 02:26:55 +03:00
for ( auto & l : leases )
{
2021-05-23 10:50:26 +03:00
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 " ;
2020-03-19 02:26:55 +03:00
}
2019-01-15 02:39:02 +03:00
}
2020-03-19 02:26:55 +03:00
s < < " </div> \r \n </div> \r \n </div> \r \n " ;
}
) ;
// end for each lease set
}
else if ( ! i2p : : context . IsFloodfill ( ) )
{
2023-01-19 07:46:54 +03:00
s < < " <b> " < < tr ( " LeaseSets " ) < < " :</b> " < < tr ( /* Message on LeaseSets page */ " floodfill mode is disabled " ) < < " .<br> \r \n " ;
2020-03-19 02:26:55 +03:00
}
else
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " LeaseSets " ) < < " :</b> 0<br> \r \n " ;
2020-03-19 02:26:55 +03:00
}
2016-07-15 20:52:55 +03:00
}
2016-08-09 13:54:47 +03:00
2017-09-27 23:28:58 +03:00
void ShowTunnels ( std : : stringstream & s )
2016-01-13 04:31:25 +03:00
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Tunnels " ) < < " :</b><br> \r \n " ;
s < < " <b> " < < tr ( " Queue size " ) < < " :</b> " < < i2p : : tunnel : : tunnels . GetQueueSize ( ) < < " <br> \r \n <br> \r \n " ;
2016-01-13 04:31:25 +03:00
2017-12-01 22:18:04 +03:00
auto ExplPool = i2p : : tunnel : : tunnels . GetExploratoryPool ( ) ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Inbound tunnels " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2016-04-27 03:00:00 +03:00
for ( auto & it : i2p : : tunnel : : tunnels . GetInboundTunnels ( ) ) {
2020-06-15 09:01:17 +03:00
s < < " <div class= \" listitem \" > " ;
2022-05-01 23:25:08 +03:00
if ( it - > GetNumHops ( ) )
{
it - > VisitTunnelHops (
[ & s ] ( std : : shared_ptr < const i2p : : data : : IdentityEx > hopIdent )
{
2024-02-23 00:07:07 +03:00
s < < " ⇒ " ;
ShowHop ( s , * hopIdent ) ;
s < < " " ;
2022-05-01 23:25:08 +03:00
}
) ;
}
s < < " ⇒ " < < it - > GetTunnelID ( ) < < " :me " ;
if ( it - > LatencyIsKnown ( ) )
2023-01-18 07:21:36 +03:00
s < < " ( " < < tr ( " %dms " , it - > GetMeanLatency ( ) ) < < " ) " ;
2017-12-01 22:18:04 +03:00
ShowTunnelDetails ( s , it - > GetState ( ) , ( it - > GetTunnelPool ( ) = = ExplPool ) , it - > GetNumReceivedBytes ( ) ) ;
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-01-13 04:31:25 +03:00
}
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n <br> \r \n " ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Outbound tunnels " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2016-04-27 03:00:00 +03:00
for ( auto & it : i2p : : tunnel : : tunnels . GetOutboundTunnels ( ) ) {
2020-06-15 09:01:17 +03:00
s < < " <div class= \" listitem \" > " ;
2022-05-01 23:25:08 +03:00
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 )
{
2024-02-23 00:07:07 +03:00
s < < " " ;
ShowHop ( s , * hopIdent ) ;
s < < " ⇒ " ;
2022-05-01 23:25:08 +03:00
}
) ;
}
if ( it - > LatencyIsKnown ( ) )
2023-01-18 07:21:36 +03:00
s < < " ( " < < tr ( " %dms " , it - > GetMeanLatency ( ) ) < < " ) " ;
2017-12-01 22:18:04 +03:00
ShowTunnelDetails ( s , it - > GetState ( ) , ( it - > GetTunnelPool ( ) = = ExplPool ) , it - > GetNumSentBytes ( ) ) ;
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-01-13 04:31:25 +03:00
}
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-08-09 13:54:47 +03:00
}
2015-02-21 06:47:36 +03:00
2017-09-27 23:28:58 +03:00
static void ShowCommands ( std : : stringstream & s , uint32_t token )
2016-04-27 03:00:00 +03:00
{
2018-11-07 18:07:05 +03:00
std : : string webroot ; i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
2021-06-28 12:45:28 +03:00
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Router commands " ) < < " </b><br> \r \n <br> \r \n <div class= \" commands \" > \r \n " ;
2021-06-27 17:14:45 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_RUN_PEER_TEST < < " &token= " < < token < < " \" > " < < tr ( " Run peer test " ) < < " </a><br> \r \n " ;
2022-10-09 02:06:50 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_RELOAD_TUNNELS_CONFIG < < " &token= " < < token < < " \" > " < < tr ( " Reload tunnels configuration " ) < < " </a><br> \r \n " ;
2021-06-28 12:45:28 +03:00
2016-04-27 03:00:00 +03:00
if ( i2p : : context . AcceptsTunnels ( ) )
2021-06-27 17:14:45 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_DISABLE_TRANSIT < < " &token= " < < token < < " \" > " < < tr ( " Decline transit tunnels " ) < < " </a><br> \r \n " ;
2016-08-09 13:54:47 +03:00
else
2021-06-27 17:14:45 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_ENABLE_TRANSIT < < " &token= " < < token < < " \" > " < < tr ( " Accept transit tunnels " ) < < " </a><br> \r \n " ;
2021-06-28 12:45:28 +03:00
2018-04-26 22:28:30 +03:00
# if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY))
2017-09-27 23:28:58 +03:00
if ( Daemon . gracefulShutdownInterval )
2021-06-27 17:14:45 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_SHUTDOWN_CANCEL < < " &token= " < < token < < " \" > " < < tr ( " Cancel graceful shutdown " ) < < " </a><br> \r \n " ;
2017-09-27 23:28:58 +03:00
else
2021-06-27 17:14:45 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_SHUTDOWN_START < < " &token= " < < token < < " \" > " < < tr ( " Start graceful shutdown " ) < < " </a><br> \r \n " ;
2017-09-27 23:28:58 +03:00
# elif defined(WIN32_APP)
if ( i2p : : util : : DaemonWin32 : : Instance ( ) . isGraceful )
2021-06-27 17:14:45 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_SHUTDOWN_CANCEL < < " &token= " < < token < < " \" > " < < tr ( " Cancel graceful shutdown " ) < < " </a><br> \r \n " ;
2017-09-27 23:28:58 +03:00
else
2021-06-27 17:14:45 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_SHUTDOWN_START < < " &token= " < < token < < " \" > " < < tr ( " Start graceful shutdown " ) < < " </a><br> \r \n " ;
2016-07-26 19:11:52 +03:00
# endif
2021-06-28 12:45:28 +03:00
2021-08-06 20:42:08 +03:00
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 " ;
2020-06-15 09:01:17 +03:00
s < < " </div> " ;
2020-04-26 23:45:30 +03:00
2021-05-23 10:50:26 +03:00
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 " ;
2020-04-26 23:45:30 +03:00
2023-01-19 08:33:03 +03:00
auto loglevel = i2p : : log : : Logger ( ) . GetLogLevel ( ) ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Logging level " ) < < " </b><br> \r \n " ;
2023-03-31 14:29:04 +03:00
s < < " <a class= \" button " < < ( loglevel = = eLogNone ? " selected " : " " ) < < " \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=none&token= " < < token < < " \" > none </a> \r \n " ;
2023-03-31 14:40:07 +03:00
s < < " <a class= \" button " < < ( loglevel = = eLogCritical ? " selected " : " " ) < < " \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=critical&token= " < < token < < " \" > critical </a> \r \n " ;
2023-03-31 14:29:04 +03:00
s < < " <a class= \" button " < < ( loglevel = = eLogError ? " selected " : " " ) < < " \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=error&token= " < < token < < " \" > error </a> \r \n " ;
s < < " <a class= \" button " < < ( loglevel = = eLogWarning ? " selected " : " " ) < < " \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=warn&token= " < < token < < " \" > warn </a> \r \n " ;
s < < " <a class= \" button " < < ( loglevel = = eLogInfo ? " selected " : " " ) < < " \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=info&token= " < < token < < " \" > info </a> \r \n " ;
s < < " <a class= \" button " < < ( loglevel = = eLogDebug ? " selected " : " " ) < < " \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=debug&token= " < < token < < " \" > debug </a><br> \r \n <br> \r \n " ;
2020-04-26 23:45:30 +03:00
2024-02-22 22:22:11 +03:00
uint32_t maxTunnels = i2p : : tunnel : : tunnels . GetMaxNumTransitTunnels ( ) ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Transit tunnels limit " ) < < " </b><br> \r \n " ;
2020-04-26 23:45:30 +03:00
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 " ;
2023-01-19 08:33:03 +03:00
s < < " <input type= \" number \" min= \" 0 \" max= \" " < < TRANSIT_TUNNELS_LIMIT < < " \" name= \" limit \" value= \" " < < maxTunnels < < " \" > \r \n " ;
2021-05-23 10:50:26 +03:00
s < < " <button type= \" submit \" > " < < tr ( " Change " ) < < " </button> \r \n " ;
2020-04-26 23:45:30 +03:00
s < < " </form> \r \n <br> \r \n " ;
2021-06-27 17:14:45 +03:00
2022-07-27 00:37:48 +03:00
// get current used language
std : : string currLang = i2p : : client : : context . GetLanguage ( ) - > GetLanguage ( ) ;
s < < " <b> "
< < tr ( " Change language " )
< < " </b><br> \r \n "
< < " <form method= \" get \" action= \" " < < webroot < < " \" > \r \n "
< < " <input type= \" hidden \" name= \" cmd \" value= \" " < < HTTP_COMMAND_SETLANGUAGE < < " \" > \r \n "
< < " <input type= \" hidden \" name= \" token \" value= \" " < < token < < " \" > \r \n "
< < " <select name= \" lang \" id= \" lang \" > \r \n " ;
2021-06-27 17:14:45 +03:00
for ( const auto & it : i2p : : i18n : : languages )
s < < " <option value= \" " < < it . first < < " \" " < < ( ( it . first . compare ( currLang ) = = 0 ) ? " selected " : " " ) < < " > " < < it . second . LocaleName < < " </option> \r \n " ;
2022-07-27 00:37:48 +03:00
s < < " </select> \r \n "
< < " <button type= \" submit \" > "
< < tr ( " Change " )
< < " </button> \r \n "
< < " </form> \r \n <br> \r \n " ;
2021-06-27 17:14:45 +03:00
2016-04-27 03:00:00 +03:00
}
2017-09-27 23:28:58 +03:00
void ShowTransitTunnels ( std : : stringstream & s )
2016-01-13 04:31:25 +03:00
{
2022-05-01 23:25:08 +03:00
if ( i2p : : tunnel : : tunnels . CountTransitTunnels ( ) )
2016-01-13 04:31:25 +03:00
{
2023-01-21 00:50:53 +03:00
s < < " <b> " < < tr ( " Transit Tunnels " ) < < " :</b><br> \r \n " ;
s < < " <table><thead><th>⇒</th><th>ID</th><th>⇒</th><th> " < < tr ( " Amount " ) < < " </th></thead><tbody class= \" tableitem \" > " ;
2020-03-19 02:26:55 +03:00
for ( const auto & it : i2p : : tunnel : : tunnels . GetTransitTunnels ( ) )
{
if ( std : : dynamic_pointer_cast < i2p : : tunnel : : TransitTunnelGateway > ( it ) )
2023-01-21 00:50:53 +03:00
s < < " <tr><td></td><td> " < < it - > GetTunnelID ( ) < < " </td><td>⇒</td><td> " ;
2020-03-19 02:26:55 +03:00
else if ( std : : dynamic_pointer_cast < i2p : : tunnel : : TransitTunnelEndpoint > ( it ) )
2023-01-21 00:50:53 +03:00
s < < " <tr><td>⇒</td><td> " < < it - > GetTunnelID ( ) < < " </td><td></td><td> " ;
2020-03-19 02:26:55 +03:00
else
2023-01-21 00:50:53 +03:00
s < < " <tr><td>⇒</td><td> " < < it - > GetTunnelID ( ) < < " </td><td>⇒</td><td> " ;
ShowTraffic ( s , it - > GetNumTransmittedBytes ( ) ) ;
s < < " </td></tr> \r \n " ;
2020-03-19 02:26:55 +03:00
}
2023-01-21 00:50:53 +03:00
s < < " </tbody></table> \r \n " ;
2020-03-19 02:26:55 +03:00
}
else
{
2023-01-19 07:46:54 +03:00
s < < " <b> " < < tr ( " Transit Tunnels " ) < < " :</b> " < < tr ( /* Message on transit tunnels page */ " no transit tunnels currently built " ) < < " .<br> \r \n " ;
2016-01-13 04:31:25 +03:00
}
}
2018-07-18 19:58:29 +03:00
template < typename Sessions >
2022-03-28 02:29:50 +03:00
static void ShowTransportSessions ( std : : stringstream & s , const Sessions & sessions , const std : : string name )
2018-07-18 19:58:29 +03:00
{
2023-02-03 01:05:58 +03:00
auto comp = [ ] ( typename Sessions : : mapped_type a , typename Sessions : : mapped_type b )
{ return a - > GetRemoteEndpoint ( ) < b - > GetRemoteEndpoint ( ) ; } ;
std : : set < typename Sessions : : mapped_type , decltype ( comp ) > sortedSessions ( comp ) ;
for ( const auto & it : sessions )
{
auto ret = sortedSessions . insert ( it . second ) ;
if ( ! ret . second )
LogPrint ( eLogError , " HTTPServer: Duplicate remote endpoint detected: " , ( * ret . first ) - > GetRemoteEndpoint ( ) ) ;
}
2018-07-18 19:58:29 +03:00
std : : stringstream tmp_s , tmp_s6 ; uint16_t cnt = 0 , cnt6 = 0 ;
2023-02-03 01:05:58 +03:00
for ( const auto & it : sortedSessions )
2018-07-18 19:58:29 +03:00
{
2023-02-03 01:05:58 +03:00
auto endpoint = it - > GetRemoteEndpoint ( ) ;
if ( it & & it - > IsEstablished ( ) & & endpoint . address ( ) . is_v4 ( ) )
2018-07-18 19:58:29 +03:00
{
2020-06-15 09:01:17 +03:00
tmp_s < < " <div class= \" listitem \" > \r \n " ;
2023-02-03 01:05:58 +03:00
if ( it - > IsOutgoing ( ) ) tmp_s < < " ⇒ " ;
tmp_s < < i2p : : data : : GetIdentHashAbbreviation ( it - > GetRemoteIdentity ( ) - > GetIdentHash ( ) ) < < " : "
2022-07-31 16:47:04 +03:00
< < endpoint . address ( ) . to_string ( ) < < " : " < < endpoint . port ( ) ;
2023-02-03 01:05:58 +03:00
if ( ! it - > IsOutgoing ( ) ) tmp_s < < " ⇒ " ;
tmp_s < < " [ " < < it - > GetNumSentBytes ( ) < < " : " < < it - > GetNumReceivedBytes ( ) < < " ] " ;
if ( it - > GetRelayTag ( ) )
tmp_s < < " [itag: " < < it - > GetRelayTag ( ) < < " ] " ;
if ( it - > GetSendQueueSize ( ) > 0 )
tmp_s < < " [queue: " < < it - > GetSendQueueSize ( ) < < " ] " ;
2023-03-29 22:54:53 +03:00
if ( it - > IsSlow ( ) ) tmp_s < < " [slow] " ;
2020-06-15 09:01:17 +03:00
tmp_s < < " </div> \r \n " < < std : : endl ;
2018-07-18 19:58:29 +03:00
cnt + + ;
}
2023-02-03 01:05:58 +03:00
if ( it & & it - > IsEstablished ( ) & & endpoint . address ( ) . is_v6 ( ) )
2018-07-18 19:58:29 +03:00
{
2020-06-15 09:01:17 +03:00
tmp_s6 < < " <div class= \" listitem \" > \r \n " ;
2023-02-03 01:05:58 +03:00
if ( it - > IsOutgoing ( ) ) tmp_s6 < < " ⇒ " ;
tmp_s6 < < i2p : : data : : GetIdentHashAbbreviation ( it - > GetRemoteIdentity ( ) - > GetIdentHash ( ) ) < < " : "
2022-07-31 16:47:04 +03:00
< < " [ " < < endpoint . address ( ) . to_string ( ) < < " ]: " < < endpoint . port ( ) ;
2023-02-03 01:05:58 +03:00
if ( ! it - > IsOutgoing ( ) ) tmp_s6 < < " ⇒ " ;
tmp_s6 < < " [ " < < it - > GetNumSentBytes ( ) < < " : " < < it - > GetNumReceivedBytes ( ) < < " ] " ;
if ( it - > GetRelayTag ( ) )
tmp_s6 < < " [itag: " < < it - > GetRelayTag ( ) < < " ] " ;
if ( it - > GetSendQueueSize ( ) > 0 )
tmp_s6 < < " [queue: " < < it - > GetSendQueueSize ( ) < < " ] " ;
2020-06-15 09:01:17 +03:00
tmp_s6 < < " </div> \r \n " < < std : : endl ;
2018-07-18 19:58:29 +03:00
cnt6 + + ;
}
}
if ( ! tmp_s . str ( ) . empty ( ) )
{
2020-03-19 02:26:55 +03:00
s < < " <div class='slide'><label for='slide_ " < < boost : : algorithm : : to_lower_copy ( name ) < < " '><b> " < < name
2020-06-15 09:01:17 +03:00
< < " </b> ( " < < cnt < < " )</label> \r \n <input type= \" checkbox \" id= \" slide_ " < < boost : : algorithm : : to_lower_copy ( name ) < < " \" /> \r \n <div class= \" slidecontent list \" > "
2020-03-19 02:26:55 +03:00
< < tmp_s . str ( ) < < " </div> \r \n </div> \r \n " ;
2018-07-18 19:58:29 +03:00
}
if ( ! tmp_s6 . str ( ) . empty ( ) )
{
2020-03-19 02:26:55 +03:00
s < < " <div class='slide'><label for='slide_ " < < boost : : algorithm : : to_lower_copy ( name ) < < " v6'><b> " < < name
2020-06-15 09:01:17 +03:00
< < " v6</b> ( " < < cnt6 < < " )</label> \r \n <input type= \" checkbox \" id= \" slide_ " < < boost : : algorithm : : to_lower_copy ( name ) < < " v6 \" /> \r \n <div class= \" slidecontent list \" > "
2020-03-19 02:26:55 +03:00
< < tmp_s6 . str ( ) < < " </div> \r \n </div> \r \n " ;
2018-07-18 19:58:29 +03:00
}
}
2017-09-27 23:28:58 +03:00
void ShowTransports ( std : : stringstream & s )
2016-01-13 04:31:25 +03:00
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Transports " ) < < " :</b><br> \r \n " ;
2018-07-18 19:58:29 +03:00
auto ntcp2Server = i2p : : transport : : transports . GetNTCP2Server ( ) ;
if ( ntcp2Server )
{
auto sessions = ntcp2Server - > GetNTCP2Sessions ( ) ;
if ( ! sessions . empty ( ) )
2022-03-28 02:29:50 +03:00
ShowTransportSessions ( s , sessions , " NTCP2 " ) ;
2016-08-09 13:54:47 +03:00
}
2022-03-28 02:29:50 +03:00
auto ssu2Server = i2p : : transport : : transports . GetSSU2Server ( ) ;
if ( ssu2Server )
{
auto sessions = ssu2Server - > GetSSU2Sessions ( ) ;
if ( ! sessions . empty ( ) )
ShowTransportSessions ( s , sessions , " SSU2 " ) ;
}
2016-01-13 04:31:25 +03:00
}
2016-08-09 13:54:47 +03:00
2017-09-27 23:28:58 +03:00
void ShowSAMSessions ( std : : stringstream & s )
2015-02-21 06:47:36 +03:00
{
2018-11-07 18:07:05 +03:00
std : : string webroot ; i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
2015-02-21 06:47:36 +03:00
auto sam = i2p : : client : : context . GetSAMBridge ( ) ;
2020-03-19 02:26:55 +03:00
if ( ! sam )
{
2021-05-23 10:50:26 +03:00
ShowError ( s , tr ( " SAM disabled " ) ) ;
2016-04-27 03:00:00 +03:00
return ;
}
2020-03-19 02:26:55 +03:00
2022-05-01 23:25:08 +03:00
if ( sam - > GetSessions ( ) . size ( ) )
2016-04-27 03:00:00 +03:00
{
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " SAM sessions " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2020-03-19 02:26:55 +03:00
for ( auto & it : sam - > GetSessions ( ) )
{
2021-04-27 04:11:36 +03:00
auto & name = it . second - > GetLocalDestination ( ) - > GetNickname ( ) ;
2020-06-15 09:01:17 +03:00
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 ;
2020-03-19 02:26:55 +03:00
}
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-08-09 13:54:47 +03:00
}
2020-03-19 02:26:55 +03:00
else
2023-01-19 07:46:54 +03:00
s < < " <b> " < < tr ( " SAM sessions " ) < < " :</b> " < < tr ( /* Message on SAM sessions page */ " no sessions currently running " ) < < " .<br> \r \n " ;
2016-08-09 13:54:47 +03:00
}
2015-02-21 06:47:36 +03:00
2021-03-10 01:48:42 +03:00
void ShowSAMSession ( std : : stringstream & s , const std : : string & id )
2015-02-21 06:47:36 +03:00
{
auto sam = i2p : : client : : context . GetSAMBridge ( ) ;
2016-04-27 03:00:00 +03:00
if ( ! sam ) {
2021-05-23 10:50:26 +03:00
ShowError ( s , tr ( " SAM disabled " ) ) ;
2016-04-27 03:00:00 +03:00
return ;
}
2020-06-15 09:01:17 +03:00
2016-04-27 03:00:00 +03:00
auto session = sam - > FindSession ( id ) ;
if ( ! session ) {
2021-05-23 10:50:26 +03:00
ShowError ( s , tr ( " SAM session not found " ) ) ;
2016-04-27 03:00:00 +03:00
return ;
}
2020-06-15 09:01:17 +03:00
std : : string webroot ; i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " SAM Session " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2021-04-27 04:11:36 +03:00
auto & ident = session - > GetLocalDestination ( ) - > GetIdentHash ( ) ;
2020-06-15 09:01:17 +03:00
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 " ;
2016-04-27 03:00:00 +03:00
s < < " <br> \r \n " ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " Streams " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2018-04-24 16:45:16 +03:00
for ( const auto & it : sam - > ListSockets ( id ) )
2015-02-21 06:47:36 +03:00
{
2020-06-15 09:01:17 +03:00
s < < " <div class= \" listitem \" > " ;
2016-04-27 03:00:00 +03:00
switch ( it - > GetSocketType ( ) )
2015-02-21 06:47:36 +03:00
{
2016-04-27 03:00:00 +03:00
case i2p : : client : : eSAMSocketTypeSession : s < < " session " ; break ;
case i2p : : client : : eSAMSocketTypeStream : s < < " stream " ; break ;
case i2p : : client : : eSAMSocketTypeAcceptor : s < < " acceptor " ; break ;
2021-05-23 10:50:26 +03:00
case i2p : : client : : eSAMSocketTypeForward : s < < " forward " ; break ;
2016-04-27 03:00:00 +03:00
default : s < < " unknown " ; break ;
2015-02-21 06:47:36 +03:00
}
2016-04-27 03:00:00 +03:00
s < < " [ " < < it - > GetSocket ( ) . remote_endpoint ( ) < < " ] " ;
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-08-09 13:54:47 +03:00
}
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-08-09 13:54:47 +03:00
}
2014-09-30 21:34:29 +04:00
2017-09-27 23:28:58 +03:00
void ShowI2PTunnels ( std : : stringstream & s )
2016-01-14 04:21:53 +03:00
{
2018-11-07 18:07:05 +03:00
std : : string webroot ; i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
2023-06-10 04:08:56 +03:00
auto & clientTunnels = i2p : : client : : context . GetClientTunnels ( ) ;
2016-10-31 22:42:50 +03:00
auto httpProxy = i2p : : client : : context . GetHttpProxy ( ) ;
2016-12-28 06:45:51 +03:00
auto socksProxy = i2p : : client : : context . GetSocksProxy ( ) ;
2023-06-10 04:08:56 +03:00
if ( ! clientTunnels . empty ( ) | | httpProxy | | socksProxy )
2016-12-28 06:45:51 +03:00
{
2023-06-10 04:08:56 +03:00
s < < " <b> " < < tr ( " Client Tunnels " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
if ( ! clientTunnels . empty ( ) )
{
for ( auto & it : clientTunnels )
{
auto & ident = it . second - > GetLocalDestination ( ) - > GetIdentHash ( ) ;
s < < " <div class= \" listitem \" ><a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_LOCAL_DESTINATION < < " &b32= " < < ident . ToBase32 ( ) < < " \" > " ;
s < < it . second - > GetName ( ) < < " </a> ⇐ " ;
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
s < < " </div> \r \n " < < std : : endl ;
}
}
if ( httpProxy )
{
auto & ident = httpProxy - > GetLocalDestination ( ) - > GetIdentHash ( ) ;
s < < " <div class= \" listitem \" ><a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_LOCAL_DESTINATION < < " &b32= " < < ident . ToBase32 ( ) < < " \" > " ;
s < < " HTTP " < < tr ( " Proxy " ) < < " </a> ⇐ " ;
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
s < < " </div> \r \n " < < std : : endl ;
}
if ( socksProxy )
{
auto & ident = socksProxy - > GetLocalDestination ( ) - > GetIdentHash ( ) ;
s < < " <div class= \" listitem \" ><a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_LOCAL_DESTINATION < < " &b32= " < < ident . ToBase32 ( ) < < " \" > " ;
s < < " SOCKS " < < tr ( " Proxy " ) < < " </a> ⇐ " ;
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
s < < " </div> \r \n " < < std : : endl ;
}
s < < " </div> \r \n " ;
2017-08-25 02:37:01 +03:00
}
2020-06-15 09:01:17 +03:00
2017-08-25 02:37:01 +03:00
auto & serverTunnels = i2p : : client : : context . GetServerTunnels ( ) ;
if ( ! serverTunnels . empty ( ) ) {
2021-05-23 10:50:26 +03:00
s < < " <br> \r \n <b> " < < tr ( " Server Tunnels " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2017-08-25 02:37:01 +03:00
for ( auto & it : serverTunnels )
{
auto & ident = it . second - > GetLocalDestination ( ) - > GetIdentHash ( ) ;
2020-06-15 09:01:17 +03:00
s < < " <div class= \" listitem \" ><a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_LOCAL_DESTINATION < < " &b32= " < < ident . ToBase32 ( ) < < " \" > " ;
2017-08-25 02:37:01 +03:00
s < < it . second - > GetName ( ) < < " </a> ⇒ " ;
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
s < < " : " < < it . second - > GetLocalPort ( ) ;
2020-06-15 09:01:17 +03:00
s < < " </a></div> \r \n " < < std : : endl ;
2017-08-25 02:37:01 +03:00
}
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-08-09 13:54:47 +03:00
}
2020-06-15 09:01:17 +03:00
2016-09-12 19:05:01 +03:00
auto & clientForwards = i2p : : client : : context . GetClientForwards ( ) ;
if ( ! clientForwards . empty ( ) )
{
2021-05-23 10:50:26 +03:00
s < < " <br> \r \n <b> " < < tr ( " Client Forwards " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2016-09-12 19:05:01 +03:00
for ( auto & it : clientForwards )
{
auto & ident = it . second - > GetLocalDestination ( ) - > GetIdentHash ( ) ;
2020-06-15 09:01:17 +03:00
s < < " <div class= \" listitem \" ><a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_LOCAL_DESTINATION < < " &b32= " < < ident . ToBase32 ( ) < < " \" > " ;
2016-09-12 19:05:01 +03:00
s < < it . second - > GetName ( ) < < " </a> ⇐ " ;
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " < < std : : endl ;
2016-09-12 19:05:01 +03:00
}
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-09-12 19:05:01 +03:00
}
auto & serverForwards = i2p : : client : : context . GetServerForwards ( ) ;
if ( ! serverForwards . empty ( ) )
{
2021-05-23 10:50:26 +03:00
s < < " <br> \r \n <b> " < < tr ( " Server Forwards " ) < < " :</b><br> \r \n <div class= \" list \" > \r \n " ;
2016-09-12 19:05:01 +03:00
for ( auto & it : serverForwards )
{
auto & ident = it . second - > GetLocalDestination ( ) - > GetIdentHash ( ) ;
2023-01-20 19:52:56 +03:00
s < < " <div class= \" listitem \" ><a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_LOCAL_DESTINATION < < " &b32= " < < ident . ToBase32 ( ) < < " \" > " ;
2016-09-12 19:05:01 +03:00
s < < it . second - > GetName ( ) < < " </a> ⇐ " ;
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " < < std : : endl ;
2016-09-12 19:05:01 +03:00
}
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2016-09-12 19:05:01 +03:00
}
2016-08-09 13:54:47 +03:00
}
2014-09-30 21:34:29 +04:00
2018-02-15 17:51:26 +03:00
HTTPConnection : : HTTPConnection ( std : : string hostname , std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > socket ) :
2019-03-18 04:37:42 +03:00
m_Socket ( socket ) , m_BufferLen ( 0 ) , expected_host ( hostname )
2016-01-13 04:31:25 +03:00
{
2016-04-27 03:00:00 +03:00
/* cache options */
i2p : : config : : GetOption ( " http.auth " , needAuth ) ;
i2p : : config : : GetOption ( " http.user " , user ) ;
i2p : : config : : GetOption ( " http.pass " , pass ) ;
2016-06-01 03:00:00 +03:00
}
2016-01-13 04:31:25 +03:00
2016-04-27 03:00:00 +03:00
void HTTPConnection : : Receive ( )
2015-11-03 17:15:49 +03:00
{
2016-04-27 03:00:00 +03:00
m_Socket - > async_read_some ( boost : : asio : : buffer ( m_Buffer , HTTP_CONNECTION_BUFFER_SIZE ) ,
2020-03-01 13:25:50 +03:00
std : : bind ( & HTTPConnection : : HandleReceive , shared_from_this ( ) ,
std : : placeholders : : _1 , std : : placeholders : : _2 ) ) ;
2015-11-03 17:15:49 +03:00
}
2016-04-27 03:00:00 +03:00
void HTTPConnection : : HandleReceive ( const boost : : system : : error_code & ecode , std : : size_t bytes_transferred )
2014-01-13 06:41:25 +04:00
{
2016-04-27 03:00:00 +03:00
if ( ecode ) {
if ( ecode ! = boost : : asio : : error : : operation_aborted )
Terminate ( ecode ) ;
2014-10-03 22:22:32 +04:00
return ;
2014-10-21 22:28:56 +04:00
}
2016-04-27 03:00:00 +03:00
m_Buffer [ bytes_transferred ] = ' \0 ' ;
m_BufferLen = bytes_transferred ;
RunRequest ( ) ;
Receive ( ) ;
2014-10-21 22:28:56 +04:00
}
2016-04-27 03:00:00 +03:00
void HTTPConnection : : RunRequest ( )
2014-10-21 22:28:56 +04:00
{
2016-04-27 03:00:00 +03:00
HTTPReq request ;
int ret = request . parse ( m_Buffer ) ;
if ( ret < 0 ) {
m_Buffer [ 0 ] = ' \0 ' ;
m_BufferLen = 0 ;
return ; /* error */
2014-07-16 20:41:40 +04:00
}
2016-04-27 03:00:00 +03:00
if ( ret = = 0 )
return ; /* need more data */
2016-04-27 03:00:00 +03:00
2016-04-27 03:00:00 +03:00
HandleRequest ( request ) ;
2014-10-21 22:28:56 +04:00
}
2016-04-27 03:00:00 +03:00
void HTTPConnection : : Terminate ( const boost : : system : : error_code & ecode )
2014-03-27 23:42:23 +04:00
{
2016-04-27 03:00:00 +03:00
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 ( ) ;
2014-03-27 23:42:23 +04:00
}
2020-03-05 04:14:39 +03:00
bool HTTPConnection : : CheckAuth ( const HTTPReq & req )
{
2016-04-27 03:00:00 +03:00
/* 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 ;
2014-03-27 21:24:23 +04:00
}
2016-04-27 03:00:00 +03:00
/* method #2: 'Authorization' header sent */
2017-02-05 06:39:54 +03:00
auto provided = req . GetHeader ( " Authorization " ) ;
2017-09-27 23:28:58 +03:00
if ( provided . length ( ) > 0 )
{
2018-12-21 18:50:23 +03:00
std : : string expected = " Basic " + i2p : : data : : ToBase64Standard ( user + " : " + pass ) ;
if ( expected = = provided ) return true ;
2014-07-16 20:41:40 +04:00
}
2016-04-27 03:00:00 +03:00
2021-11-27 22:53:53 +03:00
LogPrint ( eLogWarning , " HTTPServer: Auth failure from " , m_Socket - > remote_endpoint ( ) . address ( ) ) ;
2016-04-27 03:00:00 +03:00
return false ;
2014-03-27 21:24:23 +04:00
}
2016-04-27 03:00:00 +03:00
void HTTPConnection : : HandleRequest ( const HTTPReq & req )
2014-03-27 21:24:23 +04:00
{
2016-04-27 03:00:00 +03:00
std : : stringstream s ;
2016-04-27 03:00:00 +03:00
std : : string content ;
HTTPRes res ;
2016-04-27 03:00:00 +03:00
2021-11-27 22:53:53 +03:00
LogPrint ( eLogDebug , " HTTPServer: Request: " , req . uri ) ;
2016-04-27 03:00:00 +03:00
2016-04-27 03:00:00 +03:00
if ( needAuth & & ! CheckAuth ( req ) ) {
res . code = 401 ;
2016-05-24 03:00:00 +03:00
res . add_header ( " WWW-Authenticate " , " Basic realm= \" WebAdmin \" " ) ;
2016-04-27 03:00:00 +03:00
SendReply ( res , content ) ;
return ;
}
2022-05-01 23:25:08 +03:00
2018-02-15 17:51:26 +03:00
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 ;
2018-07-10 12:39:21 +03:00
content = " host mismatch " ;
2018-02-15 17:51:26 +03:00
SendReply ( res , content ) ;
return ;
}
}
2022-05-01 23:25:08 +03:00
2021-05-23 10:50:26 +03:00
// HTML head start
2016-04-27 03:00:00 +03:00
ShowPageHead ( s ) ;
2016-05-28 03:00:00 +03:00
if ( req . uri . find ( " page= " ) ! = std : : string : : npos ) {
2016-04-27 03:00:00 +03:00
HandlePage ( req , res , s ) ;
2016-05-28 03:00:00 +03:00
} else if ( req . uri . find ( " cmd= " ) ! = std : : string : : npos ) {
2016-04-27 03:00:00 +03:00
HandleCommand ( req , res , s ) ;
2016-05-28 03:00:00 +03:00
} else {
2020-03-05 04:14:39 +03:00
ShowStatus ( s , true , i2p : : http : : OutputFormatEnum : : forWebConsole ) ;
2016-06-25 02:07:47 +03:00
res . add_header ( " Refresh " , " 10 " ) ;
2016-05-28 03:00:00 +03:00
}
2016-04-27 03:00:00 +03:00
ShowPageTail ( s ) ;
2016-05-24 03:00:00 +03:00
res . code = 200 ;
2016-04-27 03:00:00 +03:00
content = s . str ( ) ;
SendReply ( res , content ) ;
2014-03-26 23:45:08 +04:00
}
2014-07-16 20:41:40 +04:00
2021-08-06 20:42:08 +03:00
std : : map < uint32_t , uint32_t > HTTPConnection : : m_Tokens ;
2020-03-05 04:14:39 +03:00
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 ;
}
2016-04-27 03:00:00 +03:00
void HTTPConnection : : HandlePage ( const HTTPReq & req , HTTPRes & res , std : : stringstream & s )
2017-09-27 23:28:58 +03:00
{
2016-04-27 03:00:00 +03:00
std : : map < std : : string , std : : string > params ;
std : : string page ( " " ) ;
URL url ;
2016-04-27 03:00:00 +03:00
url . parse ( req . uri ) ;
2016-04-27 03:00:00 +03:00
url . parse_query ( params ) ;
page = params [ " page " ] ;
if ( page = = HTTP_PAGE_TRANSPORTS )
ShowTransports ( s ) ;
else if ( page = = HTTP_PAGE_TUNNELS )
ShowTunnels ( s ) ;
2016-04-27 03:00:00 +03:00
else if ( page = = HTTP_PAGE_COMMANDS )
2016-12-15 21:10:12 +03:00
{
2020-03-05 04:14:39 +03:00
uint32_t token = CreateToken ( ) ;
2016-12-15 21:10:12 +03:00
ShowCommands ( s , token ) ;
}
2016-04-27 03:00:00 +03:00
else if ( page = = HTTP_PAGE_TRANSIT_TUNNELS )
ShowTransitTunnels ( s ) ;
else if ( page = = HTTP_PAGE_LOCAL_DESTINATIONS )
2017-09-27 23:28:58 +03:00
ShowLocalDestinations ( s ) ;
2016-04-27 03:00:00 +03:00
else if ( page = = HTTP_PAGE_LOCAL_DESTINATION )
2020-03-05 04:14:39 +03:00
{
uint32_t token = CreateToken ( ) ;
ShowLocalDestination ( s , params [ " b32 " ] , token ) ;
}
2016-11-18 03:14:25 +03:00
else if ( page = = HTTP_PAGE_I2CP_LOCAL_DESTINATION )
ShowI2CPLocalDestination ( s , params [ " i2cp_id " ] ) ;
2016-04-27 03:00:00 +03:00
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 ) ;
2016-07-15 20:52:55 +03:00
else if ( page = = HTTP_PAGE_LEASESETS )
ShowLeasesSets ( s ) ;
2016-04-27 03:00:00 +03:00
else {
res . code = 400 ;
2021-05-23 10:50:26 +03:00
ShowError ( s , tr ( " Unknown page " ) + " : " + page ) ;
2016-04-27 03:00:00 +03:00
return ;
}
2017-09-27 23:28:58 +03:00
}
2016-04-27 03:00:00 +03:00
2016-04-27 03:00:00 +03:00
void HTTPConnection : : HandleCommand ( const HTTPReq & req , HTTPRes & res , std : : stringstream & s )
2016-04-27 03:00:00 +03:00
{
std : : map < std : : string , std : : string > params ;
URL url ;
2016-04-27 03:00:00 +03:00
url . parse ( req . uri ) ;
2016-04-27 03:00:00 +03:00
url . parse_query ( params ) ;
2020-03-05 04:14:39 +03:00
std : : string webroot ; i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
2023-01-21 01:55:43 +03:00
std : : string redirect = std : : to_string ( COMMAND_REDIRECT_TIMEOUT ) + " ; url= " + webroot + " ?page=commands " ;
2016-12-15 21:10:12 +03:00
std : : string token = params [ " token " ] ;
2020-03-05 04:14:39 +03:00
2016-12-15 21:36:52 +03:00
if ( token . empty ( ) | | m_Tokens . find ( std : : stoi ( token ) ) = = m_Tokens . end ( ) )
2016-12-15 21:10:12 +03:00
{
2021-05-23 10:50:26 +03:00
ShowError ( s , tr ( " Invalid token " ) ) ;
2016-12-15 21:10:12 +03:00
return ;
}
2017-09-27 23:28:58 +03:00
std : : string cmd = params [ " cmd " ] ;
2016-04-27 03:00:00 +03:00
if ( cmd = = HTTP_COMMAND_RUN_PEER_TEST )
i2p : : transport : : transports . PeerTest ( ) ;
2022-10-09 02:06:50 +03:00
else if ( cmd = = HTTP_COMMAND_RELOAD_TUNNELS_CONFIG )
2016-05-12 18:38:18 +03:00
i2p : : client : : context . ReloadConfig ( ) ;
2016-07-12 03:00:00 +03:00
else if ( cmd = = HTTP_COMMAND_ENABLE_TRANSIT )
2016-04-27 03:00:00 +03:00
i2p : : context . SetAcceptsTunnels ( true ) ;
2016-07-12 03:00:00 +03:00
else if ( cmd = = HTTP_COMMAND_DISABLE_TRANSIT )
2016-04-27 03:00:00 +03:00
i2p : : context . SetAcceptsTunnels ( false ) ;
2020-03-05 04:14:39 +03:00
else if ( cmd = = HTTP_COMMAND_SHUTDOWN_START )
{
2016-04-27 03:00:00 +03:00
i2p : : context . SetAcceptsTunnels ( false ) ;
2018-04-26 22:28:30 +03:00
# if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY))
2016-10-31 13:27:27 +03:00
Daemon . gracefulShutdownInterval = 10 * 60 ;
2017-09-27 23:28:58 +03:00
# elif defined(WIN32_APP)
2016-08-09 13:54:47 +03:00
i2p : : win32 : : GracefulShutdown ( ) ;
2016-05-11 22:33:53 +03:00
# endif
2020-03-05 04:14:39 +03:00
}
else if ( cmd = = HTTP_COMMAND_SHUTDOWN_CANCEL )
{
2016-04-27 03:00:00 +03:00
i2p : : context . SetAcceptsTunnels ( true ) ;
2022-05-01 23:25:08 +03:00
# if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY))
2016-10-31 13:27:27 +03:00
Daemon . gracefulShutdownInterval = 0 ;
2017-09-27 23:28:58 +03:00
# elif defined(WIN32_APP)
i2p : : win32 : : StopGracefulShutdown ( ) ;
2016-05-11 22:33:53 +03:00
# endif
2020-03-05 04:14:39 +03:00
}
else if ( cmd = = HTTP_COMMAND_SHUTDOWN_NOW )
{
2017-11-13 03:27:09 +03:00
# ifndef WIN32_APP
2016-04-27 03:00:00 +03:00
Daemon . running = false ;
2017-11-13 03:27:09 +03:00
# else
i2p : : win32 : : StopWin32App ( ) ;
# endif
2020-03-05 04:14:39 +03:00
}
else if ( cmd = = HTTP_COMMAND_LOGLEVEL )
{
2017-12-01 22:18:04 +03:00
std : : string level = params [ " level " ] ;
SetLogLevel ( level ) ;
2020-03-05 04:14:39 +03:00
}
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 )
{
2022-05-01 23:25:08 +03:00
if ( dest - > DeleteStream ( streamID ) )
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " SUCCESS " ) < < " </b>: " < < tr ( " Stream closed " ) < < " <br> \r \n <br> \r \n " ;
2020-03-05 04:14:39 +03:00
else
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " ERROR " ) < < " </b>: " < < tr ( " Stream not found or already was closed " ) < < " <br> \r \n <br> \r \n " ;
2020-03-05 04:14:39 +03:00
}
else
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " ERROR " ) < < " </b>: " < < tr ( " Destination not found " ) < < " <br> \r \n <br> \r \n " ;
2020-03-05 04:14:39 +03:00
}
else
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " ERROR " ) < < " </b>: " < < tr ( " StreamID can't be null " ) < < " <br> \r \n <br> \r \n " ;
2020-03-05 04:14:39 +03:00
2021-05-23 10:50:26 +03:00
s < < " <a href= \" " < < webroot < < " ?page=local_destination&b32= " < < b32 < < " \" > " < < tr ( " Return to destination page " ) < < " </a><br> \r \n " ;
2023-01-19 07:46:54 +03:00
s < < " <p> " < < tr ( " You will be redirected in %d seconds " , COMMAND_REDIRECT_TIMEOUT ) < < " </b> " ;
2023-01-21 01:55:43 +03:00
redirect = std : : to_string ( COMMAND_REDIRECT_TIMEOUT ) + " ; url= " + webroot + " ?page=local_destination&b32= " + b32 ;
2020-03-05 04:14:39 +03:00
res . add_header ( " Refresh " , redirect . c_str ( ) ) ;
return ;
}
2023-04-24 14:01:21 +03:00
else if ( cmd = = HTTP_COMMAND_EXPIRELEASE )
{
std : : string b32 = params [ " b32 " ] ;
std : : string lease = params [ " lease " ] ;
i2p : : data : : IdentHash ident , leaseident ;
ident . FromBase32 ( b32 ) ;
leaseident . FromBase32 ( lease ) ;
auto dest = i2p : : client : : context . FindLocalDestination ( ident ) ;
if ( dest )
{
auto leaseset = dest - > FindLeaseSet ( leaseident ) ;
if ( leaseset )
{
leaseset - > ExpireLease ( ) ;
s < < " <b> " < < tr ( " SUCCESS " ) < < " </b>: " < < tr ( " LeaseSet expiration time updated " ) < < " <br> \r \n <br> \r \n " ;
}
else
s < < " <b> " < < tr ( " ERROR " ) < < " </b>: " < < tr ( " LeaseSet is not found or already expired " ) < < " <br> \r \n <br> \r \n " ;
}
else
s < < " <b> " < < tr ( " ERROR " ) < < " </b>: " < < tr ( " Destination not found " ) < < " <br> \r \n <br> \r \n " ;
s < < " <a href= \" " < < webroot < < " ?page=local_destination&b32= " < < b32 < < " \" > " < < tr ( " Return to destination page " ) < < " </a><br> \r \n " ;
s < < " <p> " < < tr ( " You will be redirected in %d seconds " , COMMAND_REDIRECT_TIMEOUT ) < < " </b> " ;
redirect = std : : to_string ( COMMAND_REDIRECT_TIMEOUT ) + " ; url= " + webroot + " ?page=local_destination&b32= " + b32 ;
res . add_header ( " Refresh " , redirect . c_str ( ) ) ;
return ;
}
2020-04-26 23:45:30 +03:00
else if ( cmd = = HTTP_COMMAND_LIMITTRANSIT )
{
uint32_t limit = std : : stoul ( params [ " limit " ] , nullptr ) ;
2023-01-19 07:46:54 +03:00
if ( limit > 0 & & limit < = TRANSIT_TUNNELS_LIMIT )
2023-03-06 04:08:15 +03:00
i2p : : tunnel : : tunnels . SetMaxNumTransitTunnels ( limit ) ;
2020-04-26 23:45:30 +03:00
else {
2023-01-19 07:46:54 +03:00
s < < " <b> " < < tr ( " ERROR " ) < < " </b>: " < < tr ( " Transit tunnels count must not exceed %d " , TRANSIT_TUNNELS_LIMIT ) < < " \r \n <br> \r \n <br> \r \n " ;
2021-05-23 10:50:26 +03:00
s < < " <a href= \" " < < webroot < < " ?page=commands \" > " < < tr ( " Back to commands list " ) < < " </a> \r \n <br> \r \n " ;
2023-01-19 07:46:54 +03:00
s < < " <p> " < < tr ( " You will be redirected in %d seconds " , COMMAND_REDIRECT_TIMEOUT ) < < " </b> " ;
2020-04-26 23:45:30 +03:00
res . add_header ( " Refresh " , redirect . c_str ( ) ) ;
return ;
}
}
2021-03-10 01:48:42 +03:00
else if ( cmd = = HTTP_COMMAND_GET_REG_STRING )
{
std : : string b32 = params [ " b32 " ] ;
2021-06-26 18:59:48 +03:00
std : : string name = i2p : : http : : UrlDecode ( params [ " name " ] ) ;
2021-03-10 01:48:42 +03:00
i2p : : data : : IdentHash ident ;
ident . FromBase32 ( b32 ) ;
auto dest = i2p : : client : : context . FindLocalDestination ( ident ) ;
2021-05-15 16:28:16 +03:00
if ( dest )
2021-03-10 06:10:51 +03:00
{
2021-03-10 01:48:42 +03:00
std : : size_t pos ;
pos = name . find ( " .i2p " ) ;
2021-05-15 16:28:16 +03:00
if ( pos = = ( name . length ( ) - 4 ) )
2021-03-10 06:10:51 +03:00
{
2021-03-10 01:48:42 +03:00
pos = name . find ( " .b32.i2p " ) ;
2021-05-15 16:28:16 +03:00
if ( pos = = std : : string : : npos )
2021-03-10 06:10:51 +03:00
{
2021-03-10 01:48:42 +03:00
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 ;
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " SUCCESS " ) < < " </b>:<br> \r \n <form action= \" http://shx5vqsw7usdaunyzr2qmes2fq37oumybpudrd4jjj4e4vk4uusa.b32.i2p/add \" method= \" post \" rel= \" noreferrer \" target= \" _blank \" > \r \n "
2021-03-11 15:20:40 +03:00
" <textarea readonly name= \" record \" cols= \" 80 \" rows= \" 10 \" > " < < out . str ( ) < < " </textarea> \r \n <br> \r \n <br> \r \n "
2021-05-23 10:50:26 +03:00
" <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 "
2021-03-11 15:20:40 +03:00
" </form> \r \n <br> \r \n " ;
2021-03-10 06:10:51 +03:00
delete [ ] signature ;
delete [ ] sig ;
2021-05-15 16:28:16 +03:00
}
else
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " ERROR " ) < < " </b>: " < < tr ( " Domain can't end with .b32.i2p " ) < < " \r \n <br> \r \n <br> \r \n " ;
2021-05-15 16:28:16 +03:00
}
2021-03-10 06:10:51 +03:00
else
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " ERROR " ) < < " </b>: " < < tr ( " Domain must end with .i2p " ) < < " \r \n <br> \r \n <br> \r \n " ;
2021-05-15 16:28:16 +03:00
}
2021-03-10 06:10:51 +03:00
else
2021-05-23 10:50:26 +03:00
s < < " <b> " < < tr ( " ERROR " ) < < " </b>: " < < tr ( " Such destination is not found " ) < < " \r \n <br> \r \n <br> \r \n " ;
2021-03-10 01:48:42 +03:00
2021-05-23 10:50:26 +03:00
s < < " <a href= \" " < < webroot < < " ?page=local_destination&b32= " < < b32 < < " \" > " < < tr ( " Return to destination page " ) < < " </a> \r \n " ;
2021-03-10 01:48:42 +03:00
return ;
}
2021-06-27 17:14:45 +03:00
else if ( cmd = = HTTP_COMMAND_SETLANGUAGE )
{
std : : string lang = params [ " lang " ] ;
2021-09-04 21:01:57 +03:00
std : : string currLang = i2p : : client : : context . GetLanguage ( ) - > GetLanguage ( ) ;
2021-06-27 17:14:45 +03:00
if ( currLang . compare ( lang ) ! = 0 )
i2p : : i18n : : SetLanguage ( lang ) ;
}
2021-08-06 20:42:08 +03:00
else if ( cmd = = HTTP_COMMAND_RELOAD_CSS )
{
LoadExtCSS ( ) ;
}
2020-03-05 04:14:39 +03:00
else
{
2016-04-27 03:00:00 +03:00
res . code = 400 ;
2021-05-23 10:50:26 +03:00
ShowError ( s , tr ( " Unknown command " ) + " : " + cmd ) ;
2016-04-27 03:00:00 +03:00
return ;
}
2020-03-05 04:14:39 +03:00
2021-05-23 10:50:26 +03:00
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 " ;
2023-01-19 07:46:54 +03:00
s < < " <p> " < < tr ( " You will be redirected in %d seconds " , COMMAND_REDIRECT_TIMEOUT ) < < " </b> " ;
2018-11-09 17:42:04 +03:00
res . add_header ( " Refresh " , redirect . c_str ( ) ) ;
2016-08-09 13:54:47 +03:00
}
2016-04-27 03:00:00 +03:00
2016-04-27 03:00:00 +03:00
void HTTPConnection : : SendReply ( HTTPRes & reply , std : : string & content )
2016-04-14 03:00:00 +03:00
{
2017-02-03 22:13:55 +03:00
reply . add_header ( " X-Frame-Options " , " SAMEORIGIN " ) ;
2020-04-10 05:34:47 +03:00
reply . add_header ( " X-Content-Type-Options " , " nosniff " ) ;
reply . add_header ( " X-XSS-Protection " , " 1; mode=block " ) ;
2016-05-24 03:00:00 +03:00
reply . add_header ( " Content-Type " , " text/html " ) ;
reply . body = content ;
2016-04-27 03:00:00 +03:00
2016-08-26 17:00:00 +03:00
m_SendBuffer = reply . to_string ( ) ;
boost : : asio : : async_write ( * m_Socket , boost : : asio : : buffer ( m_SendBuffer ) ,
2016-04-27 03:00:00 +03:00
std : : bind ( & HTTPConnection : : Terminate , shared_from_this ( ) , std : : placeholders : : _1 ) ) ;
2016-04-14 03:00:00 +03:00
}
2015-11-30 17:44:32 +03:00
HTTPServer : : HTTPServer ( const std : : string & address , int port ) :
2016-09-08 16:19:30 +03:00
m_IsRunning ( false ) , m_Thread ( nullptr ) , m_Work ( m_Service ) ,
2018-02-15 17:51:26 +03:00
m_Acceptor ( m_Service , boost : : asio : : ip : : tcp : : endpoint ( boost : : asio : : ip : : address : : from_string ( address ) , port ) ) ,
m_Hostname ( address )
2013-12-10 17:03:22 +04:00
{
}
HTTPServer : : ~ HTTPServer ( )
{
Stop ( ) ;
}
void HTTPServer : : Start ( )
{
2017-09-27 23:41:34 +03:00
bool needAuth ; i2p : : config : : GetOption ( " http.auth " , needAuth ) ;
2016-04-27 03:00:00 +03:00
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 = = " " ) {
2016-07-27 05:00:00 +03:00
uint8_t random [ 16 ] ;
2016-04-27 03:00:00 +03:00
char alnum [ ] = " 0123456789 "
2017-11-13 03:27:09 +03:00
" ABCDEFGHIJKLMNOPQRSTUVWXYZ "
" abcdefghijklmnopqrstuvwxyz " ;
2016-07-27 05:00:00 +03:00
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 ) ] ;
2016-04-27 03:00:00 +03:00
}
i2p : : config : : SetOption ( " http.pass " , pass ) ;
2021-11-27 22:53:53 +03:00
LogPrint ( eLogInfo , " HTTPServer: Password set to " , pass ) ;
2016-04-27 03:00:00 +03:00
}
2019-05-17 11:04:44 +03:00
2020-05-06 21:54:41 +03:00
m_IsRunning = true ;
m_Thread . reset ( new std : : thread ( std : : bind ( & HTTPServer : : Run , this ) ) ) ;
2020-05-05 02:36:34 +03:00
m_Acceptor . listen ( ) ;
Accept ( ) ;
2021-08-06 20:42:08 +03:00
LoadExtCSS ( ) ;
2013-12-10 17:03:22 +04:00
}
void HTTPServer : : Stop ( )
{
2016-09-08 16:19:30 +03:00
m_IsRunning = false ;
2022-02-18 09:20:06 +03:00
boost : : system : : error_code ec ;
2022-02-18 09:45:35 +03:00
m_Acceptor . cancel ( ec ) ;
2022-02-18 09:20:06 +03:00
if ( ec )
2022-02-18 09:45:35 +03:00
LogPrint ( eLogDebug , " HTTPServer: Error while cancelling operations on acceptor: " , ec . message ( ) ) ;
2013-12-10 17:03:22 +04:00
m_Acceptor . close ( ) ;
2022-02-18 09:20:06 +03:00
2013-12-10 17:03:22 +04:00
m_Service . stop ( ) ;
2017-09-27 23:28:58 +03:00
if ( m_Thread )
2016-09-08 16:19:30 +03:00
{
2016-04-27 03:00:00 +03:00
m_Thread - > join ( ) ;
m_Thread = nullptr ;
}
2013-12-10 17:03:22 +04:00
}
void HTTPServer : : Run ( )
{
2020-12-07 06:22:16 +03:00
i2p : : util : : SetThreadName ( " Webconsole " ) ;
2016-09-08 16:19:30 +03:00
while ( m_IsRunning )
{
try
{
m_Service . run ( ) ;
}
catch ( std : : exception & ex )
{
2023-03-31 15:16:32 +03:00
LogPrint ( eLogError , " HTTPServer: Runtime exception: " , ex . what ( ) ) ;
2017-09-27 23:28:58 +03:00
}
2016-09-08 16:19:30 +03:00
}
2014-07-16 20:41:40 +04:00
}
2013-12-10 17:03:22 +04:00
void HTTPServer : : Accept ( )
{
2015-11-23 01:01:37 +03:00
auto newSocket = std : : make_shared < boost : : asio : : ip : : tcp : : socket > ( m_Service ) ;
2020-09-18 04:11:46 +03:00
m_Acceptor . async_accept ( * newSocket , std : : bind ( & HTTPServer : : HandleAccept , this ,
std : : placeholders : : _1 , newSocket ) ) ;
2013-12-10 17:03:22 +04:00
}
2017-09-27 23:28:58 +03:00
void HTTPServer : : HandleAccept ( const boost : : system : : error_code & ecode ,
2015-11-23 01:01:37 +03:00
std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > newSocket )
2013-12-10 17:03:22 +04:00
{
2022-02-18 09:20:06 +03:00
if ( ! ecode )
CreateConnection ( newSocket ) ;
else
2016-09-04 23:42:48 +03:00
{
2022-05-01 23:25:08 +03:00
if ( newSocket ) newSocket - > close ( ) ;
2022-02-18 09:20:06 +03:00
LogPrint ( eLogError , " HTTP Server: Error handling accept: " , ecode . message ( ) ) ;
2016-09-04 23:42:48 +03:00
}
2016-04-27 03:00:00 +03:00
Accept ( ) ;
2014-07-16 20:41:40 +04:00
}
2014-03-30 02:16:23 +04:00
2015-11-23 01:01:37 +03:00
void HTTPServer : : CreateConnection ( std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > newSocket )
2014-03-30 02:16:23 +04:00
{
2018-02-15 17:51:26 +03:00
auto conn = std : : make_shared < HTTPConnection > ( m_Hostname , newSocket ) ;
2015-04-04 22:44:29 +03:00
conn - > Receive ( ) ;
2014-03-30 02:16:23 +04:00
}
2016-04-27 03:00:00 +03:00
} // http
} // i2p