2020-05-22 16:18:41 +03:00
/*
* Copyright ( c ) 2013 - 2020 , The PurpleI2P Project
*
* 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"
2020-11-17 17:59:40 +03:00
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
2014-08-17 15:20:57 +04:00
// For image and info
# include "version.h"
2016-04-27 03:00:00 +03:00
namespace i2p {
namespace http {
2016-04-27 03:00:00 +03:00
const char * itoopieFavicon =
2016-01-12 21:18:01 +03:00
" data:image/png;base64, "
2016-11-21 00:58:38 +03:00
" iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACx "
" jwv8YQUAAAAJcEhZcwAALiIAAC4iAari3ZIAAAAHdElNRQfgCQsUNSZrkhi1AAAAGXRFWHRTb2Z0 "
" d2FyZQBwYWludC5uZXQgNC4wLjEyQwRr7AAAAoJJREFUOE9jwAUqi4Q1oEwwcDTV1+5sETaBclGB "
" vb09C5QJB6kWpvFQJoOCeLC5kmjEHCgXE2SlyETLi3h6QrkM4VL+ssWSCZUgtopITLKqaOotRTEn "
" cbAkLqAkGtOqLBLVAWLXyWSVFkkmRiqLxuaqiWb/VBYJMAYrwgckJY25VEUzniqKhjU2y+RtCRSP "
" 6lUXy/1jIBV5tlYxZUaFVMq2NInwIi9hO8fSfOEAqDZUoCwal6MulvOvyS7gi69K4j9zxZT/m0ps "
" /28ptvvvquXXryIa7QYMMdTwqi0WNtVi0GIDseXl7TnUxFKfnGlxAGp0+D8j2eH/8Ub7/9e7nf7X "
" +Af/B7rwt6pI0h0l0WhQADOC9DBkhSirpImHNVZKp24ukkyoshGLnN8d5fA/y13t/44Kq/8hlnL/ "
" z7fZ/58f6vcxSNpbVUVFhV1RLNBVTsQzVYZPSwhsCAhkiIfpNMrkbO6TLf071Sfk/5ZSi/+7q6z/ "
" P5ns+v9mj/P/CpuI/20y+aeNGYxZoVoYGmsF3aFMBAAZlCwftnF9ke3//bU2//fXWP8/UGv731Am "
" +V+DdNblSqnUYqhSTKAiYSOqJBrVqiaa+S3UNPr/gmyH/xuKXf63hnn/B8bIP0UxHfEyyeSNQKVM "
" EB1AEB2twhcTLp+gIBJUoyKasEpVJHmqskh8qryovUG/ffCHHRU2q/Tk/YuB6eGPsbExa7ZkpLu1 "
" oLEcVDtuUCgV1w60rQzElpRUE1EVSX0BYidHiInXF4nagNhYQW60EF+ApH1ktni0A1SIITSUgVlZ "
" JHYnlIsfzJjIp9xZKswL5YKBHL+coKJoRDaUSzoozxHVrygQU4JykQADAwAT5b1NHtwZugAAAABJ "
" RU5ErkJggg== " ;
2014-07-16 20:41:40 +04:00
2016-04-27 03:00:00 +03:00
const char * cssStyles =
" <style> \r \n "
" body { font: 100%/1.5em sans-serif; margin: 0; padding: 1.5em; background: #FAFAFA; color: #103456; } \r \n "
2017-12-01 22:18:04 +03:00
" a, .slide label { text-decoration: none; color: #894C84; } \r \n "
" a:hover, .slide label:hover { color: #FAFAFA; background: #894C84; } \r \n "
2020-06-13 18:19:24 +03:00
" a.button { -webkit-appearance: button; -moz-appearance: button; appearance: button; text-decoration: none; \r \n "
" color: initial; padding: 0 5px; border: 1px solid #894C84; } \r \n "
" .header { font-size: 2.5em; text-align: center; margin: 1em 0; color: #894C84; } \r \n "
2020-06-13 18:33:39 +03:00
" .wrapper { margin: 0 auto; padding: 1em; max-width: 58em; } \r \n "
2020-06-15 09:01:17 +03:00
" .menu { float: left; } .menu a, .commands a { display: block; } \r \n "
" .listitem { display: block; font-family: monospace; font-size: 1.2em; white-space: nowrap; } \r \n "
2020-06-15 13:05:01 +03:00
" .tableitem { font-family: monospace; font-size: 1.2em; white-space: nowrap; } \r \n "
2020-11-30 04:10:13 +03:00
" .content { float: left; font-size: 1em; margin-left: 4em; max-width: 45em; overflow: auto; } \r \n "
2020-06-15 09:01:17 +03:00
" .tunnel.established { color: #56B734; } .tunnel.expiring { color: #D3AE3F; } \r \n "
" .tunnel.failed { color: #D33F3F; } .tunnel.building { color: #434343; } \r \n "
2016-04-27 03:00:00 +03:00
" caption { font-size: 1.5em; text-align: center; color: #894C84; } \r \n "
2020-03-19 02:26:55 +03:00
" table { display: table; border-collapse: collapse; text-align: center; } \r \n "
2020-06-13 18:19:24 +03:00
" table.extaddr { text-align: left; } table.services { width: 100%; } \r \n "
2020-06-15 09:01:17 +03:00
" textarea { word-break: break-all; } \r \n "
2020-03-19 02:26:55 +03:00
" .streamdest { width: 120px; max-width: 240px; overflow: hidden; text-overflow: ellipsis;} \r \n "
2020-06-13 18:33:39 +03:00
" .slide div.slidecontent, .slide [type= \" checkbox \" ] { display: none; } \r \n "
" .slide [type= \" checkbox \" ]:checked ~ div.slidecontent { display: block; margin-top: 0; padding: 0; } \r \n "
2017-11-14 21:31:13 +03:00
" .disabled:after { color: #D33F3F; content: \" Disabled \" } \r \n "
" .enabled:after { color: #56B734; content: \" Enabled \" } \r \n "
2020-06-13 18:19:24 +03:00
" @media screen and (max-width: 980px) { \r \n " /* adaptive style */
2020-08-23 22:25:54 +03:00
" body { padding: 1.5em 0 0 0; } \r \n "
2020-06-15 09:01:17 +03:00
" .menu { width: 100%; display: block; float: none; position: unset; font-size: 16px; \r \n "
" text-align: center; } \r \n "
" .menu a, .commands a { padding: 2px; } \r \n "
2020-08-23 22:25:54 +03:00
" .content { float: none; margin-left: unset; margin-top: 16px; max-width: 100%; width: 100%; \r \n "
2020-06-13 18:19:24 +03:00
" text-align: center; } \r \n "
" a, .slide label { /* margin-right: 10px; */ display: block; /* font-size: 18px; */ } \r \n "
2020-06-15 09:01:17 +03:00
" .header { margin: unset; font-size: 1.5em; } small {display: block} \r \n "
2020-06-13 18:19:24 +03:00
" a.button { -webkit-appearance: button; -moz-appearance: button; appearance: button; text-decoration: none; \r \n "
" color: initial; margin-top: 10px; padding: 6px; border: 1px solid #894c84; width: -webkit-fill-available; } \r \n "
2020-06-15 09:01:17 +03:00
" input { width: 35%; text-align: center; padding: 5px; \r \n "
2020-08-23 22:25:54 +03:00
" border: 2px solid #ccc; -webkit-border-radius: 5px; border-radius: 5px; font-size: 18px; } \r \n "
2020-06-13 18:19:24 +03:00
" textarea { width: -webkit-fill-available; height: auto; padding:5px; border:2px solid #ccc; \r \n "
2020-06-15 09:01:17 +03:00
" -webkit-border-radius: 5px; border-radius: 5px; font-size: 12px; } \r \n "
2020-06-13 18:19:24 +03:00
" button[type=submit] { padding: 5px 15px; background: #ccc; border: 0 none; cursor: pointer; \r \n "
2020-06-15 09:01:17 +03:00
" -webkit-border-radius: 5px; border-radius: 5px; position: relative; height: 36px; display: -webkit-inline-box; margin-top: 10px; } \r \n "
2020-06-13 18:19:24 +03:00
" } \r \n " /* adaptive style */
2016-04-27 03:00:00 +03:00
" </style> \r \n " ;
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 " ;
2016-08-09 13:54:47 +03:00
const char HTTP_COMMAND_RELOAD_CONFIG [ ] = " reload_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 " ;
2015-02-21 06:47:36 +03:00
const char HTTP_PARAM_SAM_SESSION_ID [ ] = " id " ;
2016-03-11 13:30:50 +03:00
const char HTTP_PARAM_ADDRESS [ ] = " address " ;
2013-12-10 17:03:22 +04:00
2018-08-19 01:56:31 +03:00
static std : : string ConvertTime ( uint64_t time ) ;
2020-03-05 04:14:39 +03:00
std : : map < uint32_t , uint32_t > HTTPConnection : : m_Tokens ;
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 ) {
s < < num < < " days, " ;
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 ) {
s < < num < < " hours, " ;
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 ) {
s < < num < < " min, " ;
2016-05-19 03:00:00 +03:00
seconds - = num * 60 ;
2016-04-27 03:00:00 +03:00
}
s < < 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 )
s < < numKBytes < < " KiB " ;
else if ( numKBytes < 1024 * 1024 )
s < < numKBytes / 1024 < < " MiB " ;
else
s < < numKBytes / 1024 / 1024 < < " GiB " ;
}
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
{
2016-04-27 03:00:00 +03:00
std : : string state ;
switch ( eState ) {
case i2p : : tunnel : : eTunnelStateBuildReplyReceived :
2017-09-27 23:41:34 +03:00
case i2p : : tunnel : : eTunnelStatePending : state = " building " ; break ;
2016-04-27 03:00:00 +03:00
case i2p : : tunnel : : eTunnelStateBuildFailed :
case i2p : : tunnel : : eTunnelStateTestFailed :
2017-09-27 23:41:34 +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
}
2017-12-01 22:18:04 +03:00
s < < " <span class= \" tunnel " < < state < < " \" > " < < state < < ( ( explr ) ? " (exploratory) " : " " ) < < " </span>, " ;
2020-06-15 13:05:01 +03:00
s < < " " < < ( int ) ( bytes / 1024 ) < < " KiB \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 )
{
if ( level = = " none " | | level = = " error " | | level = = " warn " | | level = = " info " | | level = = " debug " )
i2p : : log : : Logger ( ) . SetLogLevel ( level ) ;
else {
LogPrint ( eLogError , " HTTPServer: unknown loglevel set attempted " ) ;
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
{
2018-11-07 18:07:05 +03:00
std : : string webroot ;
i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
2016-04-27 03:00:00 +03:00
s < <
" <!DOCTYPE html> \r \n "
" <html lang= \" en \" > \r \n " /* TODO: Add support for locale */
2016-06-28 00:00:54 +03:00
" <head> \r \n " /* TODO: Find something to parse html/template system. This is horrible. */
# if (!defined(WIN32))
" <meta charset= \" UTF-8 \" > \r \n "
# else
" <meta charset= \" windows-1251 \" > \r \n "
# endif
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 "
2016-04-27 03:00:00 +03:00
" <title>Purple I2P " VERSION " Webconsole</title> \r \n "
< < cssStyles < <
" </head> \r \n " ;
s < <
" <body> \r \n "
2020-06-13 18:19:24 +03:00
" <div class= \" header \" ><b>i2pd</b> webconsole</div> \r \n "
" <div class= \" wrapper \" > \r \n "
" <div class= \" menu \" > \r \n "
" <a href= \" " < < webroot < < " \" >Main page</a><br> \r \n "
" <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_COMMANDS < < " \" >Router commands</a> \r \n "
" <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_LOCAL_DESTINATIONS < < " \" >Local destinations</a> \r \n " ;
2020-03-19 02:26:55 +03:00
if ( i2p : : context . IsFloodfill ( ) )
2020-06-13 18:19:24 +03:00
s < < " <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_LEASESETS < < " \" >LeaseSets</a> \r \n " ;
2020-03-19 02:26:55 +03:00
s < <
2020-06-13 18:19:24 +03:00
" <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_TUNNELS < < " \" >Tunnels</a> \r \n "
" <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_TRANSIT_TUNNELS < < " \" >Transit tunnels</a> \r \n "
" <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_TRANSPORTS < < " \" >Transports</a> \r \n "
" <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_I2P_TUNNELS < < " \" >I2P tunnels</a> \r \n " ;
2016-08-09 13:54:47 +03:00
if ( i2p : : client : : context . GetSAMBridge ( ) )
2020-06-13 18:19:24 +03:00
s < < " <a href= \" " < < webroot < < " ?page= " < < HTTP_PAGE_SAM_SESSIONS < < " \" >SAM sessions</a> \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
{
2016-04-27 03:00:00 +03:00
s < < " <b>ERROR:</b> " < < string < < " <br> \r \n " ;
2014-07-16 20:41:40 +04:00
}
2013-12-10 17:03:22 +04:00
2021-03-23 22:36:57 +03:00
static void ShowNetworkStatus ( std : : stringstream & s , RouterStatus status )
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
{
case eRouterStatusOK : s < < " OK " ; break ;
case eRouterStatusTesting : s < < " Testing " ; break ;
2017-09-27 23:28:58 +03:00
case eRouterStatusFirewalled : s < < " Firewalled " ; break ;
2021-03-06 16:50:47 +03:00
case eRouterStatusUnknown : s < < " Unknown " ; break ;
case eRouterStatusProxy : s < < " Proxy " ; break ;
2021-03-13 18:28:03 +03:00
case eRouterStatusMesh : s < < " Mesh " ; break ;
2017-09-27 23:28:58 +03:00
case eRouterStatusError :
{
s < < " Error " ;
2016-09-20 04:37:04 +03:00
switch ( i2p : : context . GetError ( ) )
{
case eRouterErrorClockSkew :
2020-10-12 07:29:46 +03:00
s < < " - Clock skew " ;
2016-09-20 04:37:04 +03:00
break ;
2020-10-12 00:51:40 +03:00
case eRouterErrorOffline :
2020-10-12 07:29:46 +03:00
s < < " - Offline " ;
break ;
2021-03-01 20:20:53 +03:00
case eRouterErrorSymmetricNAT :
s < < " - Symmetric NAT " ;
2021-05-15 16:28:16 +03:00
break ;
2017-09-27 23:28:58 +03:00
default : ;
}
2016-09-20 04:37:04 +03:00
break ;
2017-09-27 23:28:58 +03:00
}
2015-02-26 21:44:18 +03:00
default : s < < " Unknown " ;
2017-09-27 23:28:58 +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 )
{
s < < " <b>Uptime:</b> " ;
ShowUptime ( s , i2p : : context . GetUptime ( ) ) ;
2016-01-17 19:10:56 +03:00
s < < " <br> \r \n " ;
2021-03-23 22:36:57 +03:00
s < < " <b>Network status:</b> " ;
ShowNetworkStatus ( s , i2p : : context . GetStatus ( ) ) ;
s < < " <br> \r \n " ;
if ( i2p : : context . SupportsV6 ( ) )
{
s < < " <b>Network status 6:</b> " ;
ShowNetworkStatus ( s , i2p : : context . GetStatusV6 ( ) ) ;
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 ) {
s < < " <b>Stopping in:</b> " ;
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 ;
s < < " <b>Stopping in:</b> " ;
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 )
s < < " <b>Family:</b> " < < family < < " <br> \r \n " ;
2016-01-17 19:10:56 +03:00
s < < " <b>Tunnel creation success rate:</b> " < < i2p : : tunnel : : tunnels . GetTunnelCreationSuccessRate ( ) < < " %<br> \r \n " ;
2016-03-26 17:32:19 +03:00
s < < " <b>Received:</b> " ;
2017-04-27 23:11:37 +03:00
ShowTraffic ( s , i2p : : transport : : transports . GetTotalReceivedBytes ( ) ) ;
2016-03-26 17:32:19 +03:00
s < < " ( " < < ( double ) i2p : : transport : : transports . GetInBandwidth ( ) / 1024 < < " KiB/s)<br> \r \n " ;
s < < " <b>Sent:</b> " ;
2017-04-27 23:11:37 +03:00
ShowTraffic ( s , i2p : : transport : : transports . GetTotalSentBytes ( ) ) ;
2016-03-26 17:32:19 +03:00
s < < " ( " < < ( double ) i2p : : transport : : transports . GetOutBandwidth ( ) / 1024 < < " KiB/s)<br> \r \n " ;
2017-04-27 23:11:37 +03:00
s < < " <b>Transit:</b> " ;
2017-05-02 21:20:00 +03:00
ShowTraffic ( s , i2p : : transport : : transports . GetTotalTransitTransmittedBytes ( ) ) ;
2017-09-27 23:28:58 +03:00
s < < " ( " < < ( double ) i2p : : transport : : transports . GetTransitBandwidth ( ) / 1024 < < " KiB/s)<br> \r \n " ;
2016-11-12 17:49:16 +03:00
s < < " <b>Data path:</b> " < < i2p : : fs : : GetDataDir ( ) < < " <br> \r \n " ;
2020-03-05 04:14:39 +03:00
s < < " <div class='slide'> " ;
if ( ( outputFormat = = OutputFormatEnum : : forWebConsole ) | | ! includeHiddenContent ) {
2020-06-13 18:33:39 +03:00
s < < " <label for= \" slide-info \" >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
}
if ( includeHiddenContent ) {
s < < " <b>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 ( ) )
s < < " <b>Router Family:</b> " < < i2p : : context . GetRouterInfo ( ) . GetProperty ( " family " ) < < " <br> \r \n " ;
2017-09-27 23:28:58 +03:00
s < < " <b>Router Caps:</b> " < < i2p : : context . GetRouterInfo ( ) . GetProperty ( " caps " ) < < " <br> \r \n " ;
2021-05-12 18:48:27 +03:00
s < < " <b>Version:</b> " VERSION " <br> \r \n " ;
2020-03-19 02:26:55 +03:00
s < < " <b>Our external address:</b> " < < " <br> \r \n <table class= \" extaddr \" ><tbody> \r \n " ;
2017-09-27 23:28:58 +03:00
for ( const auto & address : i2p : : context . GetRouterInfo ( ) . GetAddresses ( ) )
{
2020-03-19 02:26:55 +03:00
s < < " <tr> \r \n " ;
2018-08-03 19:07:09 +03:00
if ( address - > IsNTCP2 ( ) & & ! address - > IsPublishedNTCP2 ( ) )
2018-07-18 19:58:29 +03:00
{
2020-03-19 02:26:55 +03:00
s < < " <td>NTCP2 " ;
2018-08-15 00:12:46 +03:00
if ( address - > host . is_v6 ( ) ) s < < " v6 " ;
2020-03-19 02:26:55 +03:00
s < < " </td><td>supported</td> \r \n </tr> \r \n " ;
2018-07-18 19:58:29 +03:00
continue ;
}
2017-09-27 23:28:58 +03:00
switch ( address - > transportStyle )
{
case i2p : : data : : RouterInfo : : eTransportNTCP :
2018-08-03 19:07:09 +03:00
{
2020-03-19 02:26:55 +03:00
s < < " <td>NTCP " ;
2018-08-03 19:07:09 +03:00
if ( address - > IsPublishedNTCP2 ( ) ) s < < " 2 " ;
2018-08-15 00:12:46 +03:00
if ( address - > host . is_v6 ( ) ) s < < " v6 " ;
2020-03-19 02:26:55 +03:00
s < < " </td> \r \n " ;
2018-08-03 19:07:09 +03:00
break ;
}
2017-09-27 23:28:58 +03:00
case i2p : : data : : RouterInfo : : eTransportSSU :
2020-03-19 02:26:55 +03:00
{
s < < " <td>SSU " ;
2017-09-27 23:28:58 +03:00
if ( address - > host . is_v6 ( ) )
2020-03-19 02:26:55 +03:00
s < < " v6 " ;
s < < " </td> \r \n " ;
break ;
}
2017-09-27 23:28:58 +03:00
default :
2020-03-19 02:26:55 +03:00
s < < " <td>Unknown</td> \r \n " ;
2017-09-27 23:28:58 +03:00
}
2020-03-19 02:26:55 +03:00
s < < " <td> " < < address - > host . to_string ( ) < < " : " < < address - > port < < " </td> \r \n </tr> \r \n " ;
2017-09-27 23:28:58 +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 " ;
2020-03-05 04:14:39 +03:00
if ( outputFormat = = OutputFormatEnum : : forQtUi ) {
s < < " <br> " ;
}
s < < " <b>Routers:</b> " < < i2p : : data : : netdb . GetNumRouters ( ) < < " " ;
2016-01-12 06:05:10 +03:00
s < < " <b>Floodfills:</b> " < < i2p : : data : : netdb . GetNumFloodfills ( ) < < " " ;
2016-01-17 19:10:56 +03:00
s < < " <b>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
s < < " <b>Client Tunnels:</b> " < < std : : to_string ( clientTunnelCount ) < < " " ;
2017-11-14 21:31:13 +03:00
s < < " <b>Transit Tunnels:</b> " < < std : : to_string ( transitTunnelCount ) < < " <br> \r \n <br> \r \n " ;
2020-03-05 04:14:39 +03:00
if ( outputFormat = = OutputFormatEnum : : forWebConsole ) {
bool i2pcontrol ; i2p : : config : : GetOption ( " i2pcontrol.enabled " , i2pcontrol ) ;
2020-03-19 02:26:55 +03:00
s < < " <table class= \" services \" ><caption>Services</caption><tbody> \r \n " ;
s < < " <tr><td> " < < " HTTP Proxy " < < " </td><td><div class=' " < < ( ( i2p : : client : : context . GetHttpProxy ( ) ) ? " enabled " : " disabled " ) < < " '></div></td></tr> \r \n " ;
s < < " <tr><td> " < < " SOCKS Proxy " < < " </td><td><div class=' " < < ( ( i2p : : client : : context . GetSocksProxy ( ) ) ? " enabled " : " disabled " ) < < " '></div></td></tr> \r \n " ;
s < < " <tr><td> " < < " BOB " < < " </td><td><div class=' " < < ( ( i2p : : client : : context . GetBOBCommandChannel ( ) ) ? " enabled " : " disabled " ) < < " '></div></td></tr> \r \n " ;
s < < " <tr><td> " < < " SAM " < < " </td><td><div class=' " < < ( ( i2p : : client : : context . GetSAMBridge ( ) ) ? " enabled " : " disabled " ) < < " '></div></td></tr> \r \n " ;
s < < " <tr><td> " < < " I2CP " < < " </td><td><div class=' " < < ( ( i2p : : client : : context . GetI2CPServer ( ) ) ? " enabled " : " disabled " ) < < " '></div></td></tr> \r \n " ;
s < < " <tr><td> " < < " I2PControl " < < " </td><td><div class=' " < < ( ( i2pcontrol ) ? " enabled " : " disabled " ) < < " '></div></td></tr> \r \n " ;
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 ) ;
2020-06-15 09:01:17 +03:00
s < < " <b>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
{
2020-06-15 09:01:17 +03:00
s < < " <br><b>I2CP 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
}
}
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
{
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 " ;
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 ( ) ) ;
2020-06-13 18:33:39 +03:00
s < < " <div class='slide'><label for='slide-b33'><b>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
}
2021-03-10 01:48:42 +03:00
if ( dest - > IsPublic ( ) )
{
std : : string webroot ; i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
auto base32 = dest - > GetIdentHash ( ) . ToBase32 ( ) ;
s < < " <div class='slide'><label for='slide-regaddr'><b>Address registration line</b></label> \r \n <input type= \" checkbox \" id= \" slide-regaddr \" /> \r \n <div class= \" slidecontent \" > \r \n "
" <form method= \" get \" action= \" " < < webroot < < " \" > \r \n "
" <input type= \" hidden \" name= \" cmd \" value= \" " < < HTTP_COMMAND_GET_REG_STRING < < " \" > \r \n "
" <input type= \" hidden \" name= \" token \" value= \" " < < token < < " \" > \r \n "
" <input type= \" hidden \" name= \" b32 \" value= \" " < < base32 < < " \" > \r \n "
" <b>Domain:</b> \r \n <input type= \" text \" maxlength= \" 67 \" name= \" name \" placeholder= \" domain.i2p \" required> \r \n "
" <button type= \" submit \" >Generate</button> \r \n "
" </form> \r \n <small><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 " ;
}
2016-11-18 03:14:25 +03:00
if ( dest - > GetNumRemoteLeaseSets ( ) )
{
2020-03-19 02:26:55 +03:00
s < < " <div class='slide'><label for='slide-lease'><b>LeaseSets:</b> <i> " < < dest - > GetNumRemoteLeaseSets ( )
2020-06-15 13:05:01 +03:00
< < " </i></label> \r \n <input type= \" checkbox \" id= \" slide-lease \" /> \r \n <div class= \" slidecontent \" > \r \n <table><thead><th>Address</th><th>Type</th><th>EncType</th></thead><tbody class= \" tableitem \" > " ;
2016-11-18 03:14:25 +03:00
for ( auto & it : dest - > GetLeaseSets ( ) )
2020-03-19 02:26:55 +03:00
s < < " <tr><td> " < < it . first . ToBase32 ( ) < < " </td><td> " < < ( int ) it . second - > GetStoreType ( ) < < " </td><td> " < < ( int ) it . second - > GetEncryptionType ( ) < < " </td></tr> \r \n " ;
s < < " </tbody></table> \r \n </div> \r \n </div> \r \n <br> \r \n " ;
2017-11-14 21:31:13 +03:00
} else
2020-03-19 02:26:55 +03:00
s < < " <b>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 )
{
2020-06-15 13:05:01 +03:00
s < < " <b>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 \" > " ;
2016-11-18 03:14:25 +03:00
it - > Print ( s ) ;
if ( it - > LatencyIsKnown ( ) )
s < < " ( " < < it - > GetMeanLatency ( ) < < " ms ) " ;
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 " ;
2020-06-15 13:05:01 +03:00
s < < " <b>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 \" > " ;
2016-11-18 03:14:25 +03:00
it - > Print ( s ) ;
if ( it - > LatencyIsKnown ( ) )
s < < " ( " < < it - > GetMeanLatency ( ) < < " ms ) " ;
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
s < < " <b>Tags</b><br> \r \n 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
}
2020-06-13 18:33:39 +03:00
s < < " <div class='slide'><label for='slide-tags'>Outgoing: <i> " < < out_tags < < " </i></label> \r \n <input type= \" checkbox \" id= \" slide-tags \" /> \r \n "
2020-06-15 13:05:01 +03:00
< < " <div class= \" slidecontent \" > \r \n <table> \r \n <thead><th>Destination</th><th>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
s < < " Outgoing: <i>0</i><br> \r \n " ;
s < < " <br> \r \n " ;
2020-04-30 16:21:49 +03:00
auto numECIESx25519Tags = dest - > GetNumIncomingECIESx25519Tags ( ) ;
if ( numECIESx25519Tags > 0 ) {
s < < " <b>ECIESx25519</b><br> \r \n Incoming Tags: <i> " < < numECIESx25519Tags < < " </i><br> \r \n " ;
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 + + ;
}
2020-06-13 18:33:39 +03:00
s < < " <div class='slide'><label for='slide-ecies-sessions'>Tags sessions: <i> " < < ecies_sessions < < " </i></label> \r \n <input type= \" checkbox \" id= \" slide-ecies-sessions \" /> \r \n "
2020-06-15 13:05:01 +03:00
< < " <div class= \" slidecontent \" > \r \n <table> \r \n <thead><th>Destination</th><th>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
s < < " Tags sessions: <i>0</i><br> \r \n " ;
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
{
2016-01-17 19:10:56 +03:00
s < < " <b>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 ) ;
2016-11-18 03:14:25 +03:00
// show streams
2020-03-05 04:14:39 +03:00
s < < " <table> \r \n <caption>Streams</caption> \r \n <thead> \r \n <tr> " ;
s < < " <th style= \" width:25px; \" >StreamID</th> " ;
s < < " <th style= \" width:5px; \" \\ > " ; // Stream closing button column
s < < " <th class= \" streamdest \" >Destination</th> " ;
2016-01-17 19:10:56 +03:00
s < < " <th>Sent</th> " ;
s < < " <th>Received</th> " ;
s < < " <th>Out</th> " ;
s < < " <th>In</th> " ;
s < < " <th>Buf</th> " ;
s < < " <th>RTT</th> " ;
s < < " <th>Window</th> " ;
s < < " <th>Status</th> " ;
2020-06-15 13:05:01 +03:00
s < < " </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= "
< < it - > GetRecvStreamID ( ) < < " &token= " < < token < < " \" title= \" Close stream \" > ✘ </a></td> " ;
} 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
}
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 )
{
s < < " <b>I2CP 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
2017-09-27 23:28:58 +03:00
ShowError ( s , " I2CP session not found " ) ;
2016-11-18 03:14:25 +03:00
}
else
ShowError ( s , " I2CP is not enabled " ) ;
}
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 ( ) )
{
2020-06-15 13:05:01 +03:00
s < < " <b>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
ls . reset ( new i2p : : data : : LeaseSet2 ( storeType , leaseSet - > GetBuffer ( ) , leaseSet - > GetBufferLen ( ) ) ) ;
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 ( ) )
2020-06-15 13:05:01 +03:00
s < < " <div class= \" invalid \" >!! Invalid !! </div> \r \n " ;
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 " ;
2020-03-19 02:26:55 +03:00
s < < " <b>Store type:</b> " < < ( int ) storeType < < " <br> \r \n " ;
s < < " <b>Expires:</b> " < < ConvertTime ( ls - > GetExpirationTime ( ) ) < < " <br> \r \n " ;
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 ( ) ;
s < < " <b>Non Expired Leases: " < < leases . size ( ) < < " </b><br> \r \n " ;
for ( auto & l : leases )
{
s < < " <b>Gateway:</b> " < < l - > tunnelGateway . ToBase64 ( ) < < " <br> \r \n " ;
s < < " <b>TunnelID:</b> " < < l - > tunnelID < < " <br> \r \n " ;
s < < " <b>EndDate:</b> " < < ConvertTime ( l - > endDate ) < < " <br> \r \n " ;
}
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 ( ) )
{
s < < " <b>LeaseSets:</b> not floodfill.<br> \r \n " ;
}
else
{
s < < " <b>LeaseSets:</b> 0<br> \r \n " ;
}
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
{
2020-06-15 09:01:17 +03:00
s < < " <b>Tunnels:</b><br> \r \n " ;
s < < " <b>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 ( ) ;
2020-06-15 09:01:17 +03:00
s < < " <b>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 \" > " ;
2016-04-27 03:00:00 +03:00
it - > Print ( s ) ;
2016-11-15 22:42:18 +03:00
if ( it - > LatencyIsKnown ( ) )
s < < " ( " < < it - > GetMeanLatency ( ) < < " ms ) " ;
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 " ;
s < < " <b>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 \" > " ;
2016-04-27 03:00:00 +03:00
it - > Print ( s ) ;
2016-11-15 22:42:18 +03:00
if ( it - > LatencyIsKnown ( ) )
s < < " ( " < < it - > GetMeanLatency ( ) < < " ms ) " ;
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 ) ;
2016-04-27 03:00:00 +03:00
/* commands */
2020-06-15 09:01:17 +03:00
s < < " <b>Router Commands</b><br> \r \n <br> \r \n <div class= \" commands \" > \r \n " ;
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_RUN_PEER_TEST < < " &token= " < < token < < " \" >Run peer test</a> \r \n " ;
2016-06-14 11:25:51 +03:00
//s << " <a href=\"/?cmd=" << HTTP_COMMAND_RELOAD_CONFIG << "\">Reload config</a><br>\r\n";
2016-04-27 03:00:00 +03:00
if ( i2p : : context . AcceptsTunnels ( ) )
2020-06-15 09:01:17 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_DISABLE_TRANSIT < < " &token= " < < token < < " \" >Decline transit tunnels</a> \r \n " ;
2016-08-09 13:54:47 +03:00
else
2020-06-15 09:01:17 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_ENABLE_TRANSIT < < " &token= " < < token < < " \" >Accept transit tunnels</a> \r \n " ;
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 )
2020-06-15 09:01:17 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_SHUTDOWN_CANCEL < < " &token= " < < token < < " \" >Cancel graceful shutdown</a> \r \n " ;
2017-09-27 23:28:58 +03:00
else
2018-11-07 18:07:05 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_SHUTDOWN_START < < " &token= " < < token < < " \" >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 )
2020-06-15 09:01:17 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_SHUTDOWN_CANCEL < < " &token= " < < token < < " \" >Cancel graceful shutdown</a> \r \n " ;
2017-09-27 23:28:58 +03:00
else
2020-06-15 09:01:17 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_SHUTDOWN_START < < " &token= " < < token < < " \" >Graceful shutdown</a> \r \n " ;
2016-07-26 19:11:52 +03:00
# endif
2020-06-15 09:01:17 +03:00
s < < " <a href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_SHUTDOWN_NOW < < " &token= " < < token < < " \" >Force shutdown</a> \r \n " ;
s < < " </div> " ;
2020-04-26 23:45:30 +03:00
2020-06-15 09:01:17 +03:00
s < < " <br> \r \n <small><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
s < < " <b>Logging level</b><br> \r \n " ;
s < < " <a class= \" button \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=none&token= " < < token < < " \" > none </a> \r \n " ;
s < < " <a class= \" button \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=error&token= " < < token < < " \" > error </a> \r \n " ;
s < < " <a class= \" button \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=warn&token= " < < token < < " \" > warn </a> \r \n " ;
s < < " <a class= \" button \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=info&token= " < < token < < " \" > info </a> \r \n " ;
s < < " <a class= \" button \" href= \" " < < webroot < < " ?cmd= " < < HTTP_COMMAND_LOGLEVEL < < " &level=debug&token= " < < token < < " \" > debug </a><br> \r \n <br> \r \n " ;
uint16_t maxTunnels = GetMaxNumTransitTunnels ( ) ;
s < < " <b>Transit tunnels limit</b><br> \r \n " ;
s < < " <form method= \" get \" action= \" " < < webroot < < " \" > \r \n " ;
s < < " <input type= \" hidden \" name= \" cmd \" value= \" " < < HTTP_COMMAND_LIMITTRANSIT < < " \" > \r \n " ;
s < < " <input type= \" hidden \" name= \" token \" value= \" " < < token < < " \" > \r \n " ;
s < < " <input type= \" number \" min= \" 0 \" max= \" 65535 \" name= \" limit \" value= \" " < < maxTunnels < < " \" > \r \n " ;
s < < " <button type= \" submit \" >Change</button> \r \n " ;
s < < " </form> \r \n <br> \r \n " ;
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
{
2020-03-19 02:26:55 +03:00
if ( i2p : : tunnel : : tunnels . CountTransitTunnels ( ) )
2016-01-13 04:31:25 +03:00
{
2020-06-15 09:01:17 +03:00
s < < " <b>Transit tunnels:</b><br> \r \n <div class= \" list \" > \r \n " ;
2020-03-19 02:26:55 +03:00
for ( const auto & it : i2p : : tunnel : : tunnels . GetTransitTunnels ( ) )
{
2020-06-15 09:01:17 +03:00
s < < " <div class= \" listitem \" > \r \n " ;
2020-03-19 02:26:55 +03:00
if ( std : : dynamic_pointer_cast < i2p : : tunnel : : TransitTunnelGateway > ( it ) )
s < < it - > GetTunnelID ( ) < < " ⇒ " ;
else if ( std : : dynamic_pointer_cast < i2p : : tunnel : : TransitTunnelEndpoint > ( it ) )
s < < " ⇒ " < < it - > GetTunnelID ( ) ;
else
s < < " ⇒ " < < it - > GetTunnelID ( ) < < " ⇒ " ;
2020-06-15 09:01:17 +03:00
s < < " " < < it - > GetNumTransmittedBytes ( ) < < " </div> \r \n " ;
2020-03-19 02:26:55 +03:00
}
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " ;
2020-03-19 02:26:55 +03:00
}
else
{
s < < " <b>Transit tunnels:</b> 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 >
static void ShowNTCPTransports ( std : : stringstream & s , const Sessions & sessions , const std : : string name )
{
std : : stringstream tmp_s , tmp_s6 ; uint16_t cnt = 0 , cnt6 = 0 ;
for ( const auto & it : sessions )
{
2021-02-17 22:26:48 +03:00
if ( it . second & & it . second - > IsEstablished ( ) & & ! it . second - > GetRemoteEndpoint ( ) . address ( ) . is_v6 ( ) )
2018-07-18 19:58:29 +03:00
{
2020-06-15 09:01:17 +03:00
tmp_s < < " <div class= \" listitem \" > \r \n " ;
2018-07-18 19:58:29 +03:00
if ( it . second - > IsOutgoing ( ) ) tmp_s < < " ⇒ " ;
tmp_s < < i2p : : data : : GetIdentHashAbbreviation ( it . second - > GetRemoteIdentity ( ) - > GetIdentHash ( ) ) < < " : "
2021-02-17 22:26:48 +03:00
< < it . second - > GetRemoteEndpoint ( ) . address ( ) . to_string ( ) ;
2018-07-18 19:58:29 +03:00
if ( ! it . second - > IsOutgoing ( ) ) tmp_s < < " ⇒ " ;
tmp_s < < " [ " < < it . second - > GetNumSentBytes ( ) < < " : " < < it . second - > GetNumReceivedBytes ( ) < < " ] " ;
2020-06-15 09:01:17 +03:00
tmp_s < < " </div> \r \n " < < std : : endl ;
2018-07-18 19:58:29 +03:00
cnt + + ;
}
2021-02-17 22:26:48 +03:00
if ( it . second & & it . second - > IsEstablished ( ) & & it . second - > GetRemoteEndpoint ( ) . 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 " ;
2018-07-18 19:58:29 +03:00
if ( it . second - > IsOutgoing ( ) ) tmp_s6 < < " ⇒ " ;
tmp_s6 < < i2p : : data : : GetIdentHashAbbreviation ( it . second - > GetRemoteIdentity ( ) - > GetIdentHash ( ) ) < < " : "
2021-02-17 22:26:48 +03:00
< < " [ " < < it . second - > GetRemoteEndpoint ( ) . address ( ) . to_string ( ) < < " ] " ;
2018-07-18 19:58:29 +03:00
if ( ! it . second - > IsOutgoing ( ) ) tmp_s6 < < " ⇒ " ;
tmp_s6 < < " [ " < < it . second - > GetNumSentBytes ( ) < < " : " < < it . second - > GetNumReceivedBytes ( ) < < " ] " ;
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
{
2020-06-15 09:01:17 +03:00
s < < " <b>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 ( ) )
ShowNTCPTransports ( s , sessions , " NTCP2 " ) ;
2016-08-09 13:54:47 +03:00
}
2016-01-13 04:31:25 +03:00
auto ssuServer = i2p : : transport : : transports . GetSSUServer ( ) ;
if ( ssuServer )
{
2017-01-01 16:58:21 +03:00
auto sessions = ssuServer - > GetSessions ( ) ;
2017-07-08 16:54:12 +03:00
if ( ! sessions . empty ( ) )
2016-01-13 04:31:25 +03:00
{
2020-06-15 09:01:17 +03:00
s < < " <div class='slide'><label for='slide_ssu'><b>SSU</b> ( " < < ( int ) sessions . size ( ) < < " )</label> \r \n <input type= \" checkbox \" id= \" slide_ssu \" /> \r \n <div class= \" slidecontent list \" > " ;
2017-07-08 16:54:12 +03:00
for ( const auto & it : sessions )
{
2020-06-15 09:01:17 +03:00
s < < " <div class= \" listitem \" > \r \n " ;
2017-07-08 16:54:12 +03:00
auto endpoint = it . second - > GetRemoteEndpoint ( ) ;
if ( it . second - > IsOutgoing ( ) ) s < < " ⇒ " ;
s < < endpoint . address ( ) . to_string ( ) < < " : " < < endpoint . port ( ) ;
if ( ! it . second - > IsOutgoing ( ) ) s < < " ⇒ " ;
s < < " [ " < < it . second - > GetNumSentBytes ( ) < < " : " < < it . second - > GetNumReceivedBytes ( ) < < " ] " ;
if ( it . second - > GetRelayTag ( ) )
s < < " [itag: " < < it . second - > GetRelayTag ( ) < < " ] " ;
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " < < std : : endl ;
2017-07-08 16:54:12 +03:00
}
2020-03-19 02:26:55 +03:00
s < < " </div> \r \n </div> \r \n " ;
2016-01-13 04:31:25 +03:00
}
2017-07-08 16:54:12 +03:00
auto sessions6 = ssuServer - > GetSessionsV6 ( ) ;
if ( ! sessions6 . empty ( ) )
2016-01-13 04:31:25 +03:00
{
2020-06-15 09:01:17 +03:00
s < < " <div class='slide'><label for='slide_ssuv6'><b>SSUv6</b> ( " < < ( int ) sessions6 . size ( ) < < " )</label> \r \n <input type= \" checkbox \" id= \" slide_ssuv6 \" /> \r \n <div class= \" slidecontent list \" > " ;
2017-07-08 16:54:12 +03:00
for ( const auto & it : sessions6 )
{
2020-06-15 09:01:17 +03:00
s < < " <div class= \" listitem \" > \r \n " ;
2017-07-08 16:54:12 +03:00
auto endpoint = it . second - > GetRemoteEndpoint ( ) ;
if ( it . second - > IsOutgoing ( ) ) s < < " ⇒ " ;
s < < " [ " < < endpoint . address ( ) . to_string ( ) < < " ]: " < < endpoint . port ( ) ;
if ( ! it . second - > IsOutgoing ( ) ) s < < " ⇒ " ;
s < < " [ " < < it . second - > GetNumSentBytes ( ) < < " : " < < it . second - > GetNumReceivedBytes ( ) < < " ] " ;
if ( it . second - > GetRelayTag ( ) )
s < < " [itag: " < < it . second - > GetRelayTag ( ) < < " ] " ;
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " < < std : : endl ;
2017-07-08 16:54:12 +03:00
}
2020-03-19 02:26:55 +03:00
s < < " </div> \r \n </div> \r \n " ;
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 )
{
2016-04-27 03:00:00 +03:00
ShowError ( s , " SAM disabled " ) ;
return ;
}
2020-03-19 02:26:55 +03:00
if ( sam - > GetSessions ( ) . size ( ) )
2016-04-27 03:00:00 +03:00
{
2020-06-15 09:01:17 +03:00
s < < " <b>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
s < < " <b>SAM Sessions:</b> 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 ) {
ShowError ( s , " SAM disabled " ) ;
return ;
}
2020-06-15 09:01:17 +03:00
2016-04-27 03:00:00 +03:00
auto session = sam - > FindSession ( id ) ;
if ( ! session ) {
ShowError ( s , " SAM session not found " ) ;
return ;
}
2020-06-15 09:01:17 +03:00
std : : string webroot ; i2p : : config : : GetOption ( " http.webroot " , webroot ) ;
s < < " <b>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 " ;
2020-06-15 09:01:17 +03:00
s < < " <b>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 ;
2020-10-26 23:06:19 +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 ) ;
2020-06-15 09:01:17 +03:00
s < < " <b>Client Tunnels:</b><br> \r \n <div class= \" list \" > \r \n " ;
2016-01-14 04:21:53 +03:00
for ( auto & it : i2p : : client : : context . GetClientTunnels ( ) )
{
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-08-09 13:54:47 +03:00
s < < it . second - > GetName ( ) < < " </a> ⇐ " ;
2016-01-14 04:21:53 +03:00
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " < < std : : endl ;
2016-08-09 13:54:47 +03:00
}
2016-10-31 22:42:50 +03:00
auto httpProxy = i2p : : client : : context . GetHttpProxy ( ) ;
if ( httpProxy )
{
auto & ident = httpProxy - > 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-10-31 22:42:50 +03:00
s < < " HTTP Proxy " < < " </a> ⇐ " ;
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
2020-06-15 09:01:17 +03:00
s < < " </div> \r \n " < < std : : endl ;
2017-09-27 23:28:58 +03:00
}
2016-12-28 06:45:51 +03:00
auto socksProxy = i2p : : client : : context . GetSocksProxy ( ) ;
if ( socksProxy )
{
auto & ident = socksProxy - > 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-12-28 06:45:51 +03:00
s < < " SOCKS Proxy " < < " </a> ⇐ " ;
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
2020-06-15 09:01:17 +03:00
s < < " </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 " ;
2017-08-25 02:37:01 +03:00
auto & serverTunnels = i2p : : client : : context . GetServerTunnels ( ) ;
if ( ! serverTunnels . empty ( ) ) {
2020-06-15 09:01:17 +03:00
s < < " <br> \r \n <b>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 ( ) )
{
2020-06-15 09:01:17 +03:00
s < < " <br> \r \n <b>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 ( ) )
{
2020-06-15 09:01:17 +03:00
s < < " <br> \r \n <b>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 ( ) ;
2018-11-07 18:07:05 +03:00
s < < " <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-08-19 01:56:31 +03:00
std : : string ConvertTime ( uint64_t time )
{
2020-06-15 13:05:01 +03:00
lldiv_t divTime = lldiv ( time , 1000 ) ;
2018-08-19 01:56:31 +03:00
time_t t = divTime . quot ;
struct tm * tm = localtime ( & t ) ;
char date [ 128 ] ;
2020-08-23 22:25:54 +03:00
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 ) ;
2018-08-19 01:56:31 +03:00
return date ;
}
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
LogPrint ( eLogWarning , " HTTPServer: auth failure from " , m_Socket - > remote_endpoint ( ) . address ( ) ) ;
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
2016-04-27 03:00:00 +03:00
LogPrint ( eLogDebug , " HTTPServer: request: " , req . uri ) ;
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 ;
}
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 ;
}
}
2016-04-27 03:00:00 +03:00
// Html5 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
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 ;
ShowError ( s , " Unknown page: " + page ) ;
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 ) ;
std : : string redirect = " 5; 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
{
ShowError ( s , " Invalid token " ) ;
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 ( ) ;
2016-05-12 18:38:18 +03:00
else if ( cmd = = HTTP_COMMAND_RELOAD_CONFIG )
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 ) ;
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 = 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 )
{
if ( dest - > DeleteStream ( streamID ) )
2021-03-10 01:48:42 +03:00
s < < " <b>SUCCESS</b>: Stream closed<br> \r \n <br> \r \n " ;
2020-03-05 04:14:39 +03:00
else
2021-03-10 01:48:42 +03:00
s < < " <b>ERROR</b>: Stream not found or already was closed<br> \r \n <br> \r \n " ;
2020-03-05 04:14:39 +03:00
}
else
2021-03-10 01:48:42 +03:00
s < < " <b>ERROR</b>: Destination not found<br> \r \n <br> \r \n " ;
2020-03-05 04:14:39 +03:00
}
else
2021-03-10 01:48:42 +03:00
s < < " <b>ERROR</b>: StreamID can be null<br> \r \n <br> \r \n " ;
2020-03-05 04:14:39 +03:00
s < < " <a href= \" " < < webroot < < " ?page=local_destination&b32= " < < b32 < < " \" >Return to destination page</a><br> \r \n " ;
s < < " <p>You will be redirected back in 5 seconds</b> " ;
redirect = " 5; 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 ) ;
if ( limit > 0 & & limit < = 65535 )
SetMaxNumTransitTunnels ( limit ) ;
else {
2021-03-10 01:48:42 +03:00
s < < " <b>ERROR</b>: Transit tunnels count must not exceed 65535 \r \n <br> \r \n <br> \r \n " ;
s < < " <a href= \" " < < webroot < < " ?page=commands \" >Back to commands list</a> \r \n <br> \r \n " ;
2020-04-26 23:45:30 +03:00
s < < " <p>You will be redirected back in 5 seconds</b> " ;
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 " ] ;
std : : string name = params [ " name " ] ;
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-03-11 15:20:40 +03:00
s < < " <b>SUCCESS</b>:<br> \r \n <form action= \" http://shx5vqsw7usdaunyzr2qmes2fq37oumybpudrd4jjj4e4vk4uusa.b32.i2p/add \" method= \" post \" rel= \" noreferrer \" target= \" _blank \" > \r \n "
" <textarea readonly name= \" record \" cols= \" 80 \" rows= \" 10 \" > " < < out . str ( ) < < " </textarea> \r \n <br> \r \n <br> \r \n "
" <b>Register at reg.i2p:</b> \r \n <br> \r \n "
" <b>Description:</b> \r \n <input type= \" text \" maxlength= \" 64 \" name= \" desc \" placeholder= \" A bit information about service on domain \" > \r \n "
" <input type= \" submit \" value= \" Submit \" > \r \n "
" </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-03-10 01:48:42 +03:00
s < < " <b>ERROR</b>: 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-03-10 01:48:42 +03:00
s < < " <b>ERROR</b>: 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-03-10 01:48:42 +03:00
s < < " <b>ERROR</b>: Such destination is not found \r \n <br> \r \n <br> \r \n " ;
s < < " <a href= \" " < < webroot < < " ?page=local_destination&b32= " < < b32 < < " \" >Return to destination page</a> \r \n " ;
return ;
}
2020-03-05 04:14:39 +03:00
else
{
2016-04-27 03:00:00 +03:00
res . code = 400 ;
ShowError ( s , " Unknown command: " + cmd ) ;
2016-04-27 03:00:00 +03:00
return ;
}
2020-03-05 04:14:39 +03:00
2016-04-27 03:00:00 +03:00
s < < " <b>SUCCESS</b>: Command accepted<br><br> \r \n " ;
2018-11-09 17:42:04 +03:00
s < < " <a href= \" " < < webroot < < " ?page=commands \" >Back to commands list</a><br> \r \n " ;
2018-11-09 16:13:56 +03:00
s < < " <p>You will be redirected in 5 seconds</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 ) ;
LogPrint ( eLogInfo , " HTTPServer: password set to " , pass ) ;
}
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 ( ) ;
2013-12-10 17:03:22 +04:00
}
void HTTPServer : : Stop ( )
{
2016-09-08 16:19:30 +03:00
m_IsRunning = false ;
2013-12-10 17:03:22 +04:00
m_Acceptor . close ( ) ;
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 )
{
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
{
2016-04-27 03:00:00 +03:00
if ( ecode )
2016-09-04 23:42:48 +03:00
{
if ( newSocket ) newSocket - > close ( ) ;
LogPrint ( eLogError , " HTTP Server: error handling accept " , ecode . message ( ) ) ;
if ( ecode ! = boost : : asio : : error : : operation_aborted )
2017-09-27 23:28:58 +03:00
Accept ( ) ;
2016-04-27 03:00:00 +03:00
return ;
2016-09-04 23:42:48 +03:00
}
2016-04-27 03:00:00 +03:00
CreateConnection ( newSocket ) ;
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