2015-11-21 22:26:12 +00:00
# include <iomanip>
2016-04-27 00:00:00 +00:00
# include <sstream>
# include <thread>
# include <memory>
# include <boost/asio.hpp>
2013-12-10 13:03:22 +00:00
# include <boost/bind.hpp>
2016-04-27 00:00:00 +00:00
2015-11-03 14:15:49 +00:00
# include "Base.h"
2016-02-11 00:00:00 +00:00
# include "FS.h"
2014-01-15 00:56:34 +00:00
# include "Log.h"
2016-04-27 00:00:00 +00:00
# include "Config.h"
2013-12-10 13:03:22 +00:00
# include "Tunnel.h"
# include "TransitTunnel.h"
# include "Transports.h"
2014-01-13 02:41:25 +00:00
# include "NetDb.h"
2016-04-27 00:00:00 +00:00
# include "HTTP.h"
2016-04-27 00:00:00 +00:00
# include "LeaseSet.h"
2014-10-05 12:54:59 +00:00
# include "Destination.h"
2014-09-30 17:34:29 +00:00
# include "RouterContext.h"
2014-10-16 00:52:17 +00:00
# include "ClientContext.h"
2014-09-30 17:34:29 +00:00
# include "HTTPServer.h"
2016-04-27 00:00:00 +00:00
# include "Daemon.h"
2016-06-14 18:37:22 +00:00
# include "util.h"
2016-07-26 16:11:52 +00:00
# ifdef WIN32_APP
# include "Win32/Win32App.h"
# endif
2013-12-10 13:03:22 +00:00
2014-08-17 11:20:57 +00:00
// For image and info
# include "version.h"
2016-04-27 00:00:00 +00:00
namespace i2p {
namespace http {
2016-04-27 00:00:00 +00:00
const char * itoopieFavicon =
2016-01-12 18:18:01 +00:00
" data:image/png;base64, "
" iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv "
" 8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYdEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My4wOGVynO "
" EAAAIzSURBVDhPjZNdSFNhGMf3nm3n7OzMs+8JtfJGzdlgoPtoWBrkqc1OsLTMKEY3eZOQbbS6aBVYO "
" oO8CKSLXEulQtZNahAM9Cq6lS533UUaeDEEKcN/79x7kbQT/eDhfPB7/u/7Poej08JqtXoEQbhoMpmG "
" ZFn2stf/h8nEZ4aHue1SiWBlhSCV4n41NBifBINBjina8DyfzOUIVlcJtrYINjcJ3rw1oFAg4HnjHaZ "
" p4/Ppv8zPH0G5XKZNPZibO4lKpYJ8vgOqqv+uKMq/d9Hfz/0sFr3w+/3IZt2YnbWhszOAxUUv0mkCs9 "
" ncyNT6hEL6dYBgY4Ngd5eger+zU7sODHA/mpubzUytj9FofLa0VGv4s9bWCCTJUGSaNvSzXT3stuHDM "
" rc3xEqF4N2CERciURyyHfgqSZKPqfuxUMyC+OKcL4YHyl28nDFAPdqDZMcQ7tPnSfURUt0jMBgMH1nL "
" fkRRDPvcLds3otfhbRTwasaE8b6He43VSrT3QW3tBT3iPdbyN3T7Ibsor988H8OxtiaMx2sB1aBbCRW "
" R1hbQhbqYXh+6QkaJn8DZyzF09x6HeiaOTC6NK9cSsFqkb3aH3cLU+tCAx9l8FoXPBUy9n8LgyCCmS9 "
" MYez0Gm9P2iWna0GOcDp8KY2JhAsnbSQS6Ahh9OgrlklINeM40bWhAkBd4SLIEh8cBURLhOeiBIArVA "
" U4yTRvJItk5PRehQVFaYfpbt9PBtTmdziaXyyUzjaHT/QZBQuKHAA0UxAAAAABJRU5ErkJggg== " ;
2014-07-16 16:41:40 +00:00
2016-04-27 00:00:00 +00: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 "
" a { text-decoration: none; color: #894C84; } \r \n "
" a:hover { color: #FAFAFA; background: #894C84; } \r \n "
" .header { font-size: 2.5em; text-align: center; margin: 1.5em 0; color: #894C84; } \r \n "
" .wrapper { margin: 0 auto; padding: 1em; max-width: 60em; } \r \n "
" .left { float: left; position: absolute; } \r \n "
" .right { float: left; font-size: 1em; margin-left: 13em; max-width: 46em; overflow: auto; } \r \n "
" .tunnel.established { color: #56B734; } \r \n "
" .tunnel.expiring { color: #D3AE3F; } \r \n "
" .tunnel.failed { color: #D33F3F; } \r \n "
" .tunnel.another { color: #434343; } \r \n "
" caption { font-size: 1.5em; text-align: center; color: #894C84; } \r \n "
" table { width: 100%; border-collapse: collapse; text-align: center; } \r \n "
2016-06-30 11:35:30 +00:00
" .private { background: black; color: black; } .private:hover { background: black; color: white } \r \n "
2016-06-30 18:21:37 +00:00
" .slide p, .slide [type='checkbox']{ display:none; } \r \n "
" .slide [type='checkbox']:checked ~ p { display:block; } \r \n "
2016-04-27 00:00:00 +00:00
" </style> \r \n " ;
const char HTTP_PAGE_TUNNELS [ ] = " tunnels " ;
const char HTTP_PAGE_TRANSIT_TUNNELS [ ] = " transit_tunnels " ;
2016-08-09 10:54:47 +00:00
const char HTTP_PAGE_TRANSPORTS [ ] = " transports " ;
2016-04-27 00:00:00 +00:00
const char HTTP_PAGE_LOCAL_DESTINATIONS [ ] = " local_destinations " ;
const char HTTP_PAGE_LOCAL_DESTINATION [ ] = " local_destination " ;
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 00:00:00 +00:00
const char HTTP_PAGE_COMMANDS [ ] = " commands " ;
2016-07-15 17:52:55 +00:00
const char HTTP_PAGE_LEASESETS [ ] = " leasesets " ;
2016-07-12 00:00:00 +00:00
const char HTTP_COMMAND_ENABLE_TRANSIT [ ] = " enable_transit " ;
const char HTTP_COMMAND_DISABLE_TRANSIT [ ] = " disable_transit " ;
2016-04-27 00:00:00 +00:00
const char HTTP_COMMAND_SHUTDOWN_START [ ] = " shutdown_start " ;
const char HTTP_COMMAND_SHUTDOWN_CANCEL [ ] = " shutdown_cancel " ;
const char HTTP_COMMAND_SHUTDOWN_NOW [ ] = " terminate " ;
2016-05-12 15:38:18 +00:00
const char HTTP_COMMAND_RUN_PEER_TEST [ ] = " run_peer_test " ;
2016-08-09 10:54:47 +00:00
const char HTTP_COMMAND_RELOAD_CONFIG [ ] = " reload_config " ;
2015-02-21 03:47:36 +00:00
const char HTTP_PARAM_SAM_SESSION_ID [ ] = " id " ;
2016-03-11 10:30:50 +00:00
const char HTTP_PARAM_ADDRESS [ ] = " address " ;
2013-12-10 13:03:22 +00:00
2016-04-27 00:00:00 +00:00
void ShowUptime ( std : : stringstream & s , int seconds ) {
int num ;
2013-12-10 13:03:22 +00:00
2016-04-27 00:00:00 +00:00
if ( ( num = seconds / 86400 ) > 0 ) {
s < < num < < " days, " ;
2016-05-19 00:00:00 +00:00
seconds - = num * 86400 ;
2013-12-10 13:03:22 +00:00
}
2016-04-27 00:00:00 +00:00
if ( ( num = seconds / 3600 ) > 0 ) {
s < < num < < " hours, " ;
2016-05-19 00:00:00 +00:00
seconds - = num * 3600 ;
2014-07-16 16:41:40 +00:00
}
2016-04-27 00:00:00 +00:00
if ( ( num = seconds / 60 ) > 0 ) {
s < < num < < " min, " ;
2016-05-19 00:00:00 +00:00
seconds - = num * 60 ;
2016-04-27 00:00:00 +00:00
}
s < < seconds < < " seconds " ;
2014-03-29 22:16:23 +00:00
}
2016-04-27 00:00:00 +00:00
void ShowTunnelDetails ( std : : stringstream & s , enum i2p : : tunnel : : TunnelState eState , int bytes )
2014-01-15 00:56:34 +00:00
{
2016-04-27 00:00:00 +00:00
std : : string state ;
switch ( eState ) {
case i2p : : tunnel : : eTunnelStateBuildReplyReceived :
case i2p : : tunnel : : eTunnelStatePending : state = " building " ; break ;
case i2p : : tunnel : : eTunnelStateBuildFailed :
case i2p : : tunnel : : eTunnelStateTestFailed :
case i2p : : tunnel : : eTunnelStateFailed : state = " failed " ; break ;
case i2p : : tunnel : : eTunnelStateExpiring : state = " expiring " ; break ;
case i2p : : tunnel : : eTunnelStateEstablished : state = " established " ; break ;
default : state = " unknown " ; break ;
2014-07-16 16:41:40 +00:00
}
2016-04-27 00:00:00 +00:00
s < < " <span class= \" tunnel " < < state < < " \" > " < < state < < " </span>, " ;
s < < " " < < ( int ) ( bytes / 1024 ) < < " KiB<br> \r \n " ;
2014-07-16 16:41:40 +00:00
}
2016-04-27 00:00:00 +00:00
void ShowPageHead ( std : : stringstream & s )
2014-03-27 19:42:23 +00:00
{
2016-04-27 00:00:00 +00:00
s < <
" <!DOCTYPE html> \r \n "
" <html lang= \" en \" > \r \n " /* TODO: Add support for locale */
2016-06-27 21:00:54 +00: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
2016-06-14 08:25:51 +00:00
" <link rel= \" shortcut icon \" href= \" " < < itoopieFavicon < < " \" > \r \n "
2016-04-27 00:00:00 +00:00
" <title>Purple I2P " VERSION " Webconsole</title> \r \n "
< < cssStyles < <
" </head> \r \n " ;
s < <
" <body> \r \n "
" <div class=header><b>i2pd</b> webconsole</div> \r \n "
" <div class=wrapper> \r \n "
" <div class=left> \r \n "
2016-06-14 08:25:51 +00:00
" <a href= \" / \" >Main page</a><br> \r \n <br> \r \n "
" <a href= \" /?page= " < < HTTP_PAGE_COMMANDS < < " \" >Router commands</a><br> \r \n "
" <a href= \" /?page= " < < HTTP_PAGE_LOCAL_DESTINATIONS < < " \" >Local destinations</a><br> \r \n "
2016-08-09 10:54:47 +00:00
" <a href= \" /?page= " < < HTTP_PAGE_LEASESETS < < " \" >LeaseSets</a><br> \r \n "
2016-06-14 08:25:51 +00:00
" <a href= \" /?page= " < < HTTP_PAGE_TUNNELS < < " \" >Tunnels</a><br> \r \n "
" <a href= \" /?page= " < < HTTP_PAGE_TRANSIT_TUNNELS < < " \" >Transit tunnels</a><br> \r \n "
" <a href= \" /?page= " < < HTTP_PAGE_TRANSPORTS < < " \" >Transports</a><br> \r \n "
2016-08-09 10:54:47 +00:00
" <a href= \" /?page= " < < HTTP_PAGE_I2P_TUNNELS < < " \" >I2P tunnels</a><br> \r \n " ;
if ( i2p : : client : : context . GetSAMBridge ( ) )
s < < " <a href= \" /?page= " < < HTTP_PAGE_SAM_SESSIONS < < " \" >SAM sessions</a><br> \r \n " ;
s < <
2016-04-27 00:00:00 +00:00
" </div> \r \n "
" <div class=right> " ;
2014-03-27 19:42:23 +00:00
}
2016-04-27 00:00:00 +00:00
void ShowPageTail ( std : : stringstream & s )
2013-12-10 13:03:22 +00:00
{
2016-04-27 00:00:00 +00:00
s < <
" </div></div> \r \n "
" </body> \r \n "
" </html> \r \n " ;
2013-12-10 13:03:22 +00:00
}
2016-04-27 00:00:00 +00:00
void ShowError ( std : : stringstream & s , const std : : string & string )
2013-12-10 13:03:22 +00:00
{
2016-04-27 00:00:00 +00:00
s < < " <b>ERROR:</b> " < < string < < " <br> \r \n " ;
2014-07-16 16:41:40 +00:00
}
2013-12-10 13:03:22 +00:00
2016-04-27 00:00:00 +00:00
void ShowStatus ( std : : stringstream & s )
2013-12-10 13:03:22 +00:00
{
2016-04-27 00:00:00 +00:00
s < < " <b>Uptime:</b> " ;
ShowUptime ( s , i2p : : context . GetUptime ( ) ) ;
s < < " <br> \r \n " ;
2016-08-21 00:00:00 +00:00
s < < " <b>Network status:</b> " ;
2015-02-26 18:44:18 +00:00
switch ( i2p : : context . GetStatus ( ) )
{
case eRouterStatusOK : s < < " OK " ; break ;
case eRouterStatusTesting : s < < " Testing " ; break ;
case eRouterStatusFirewalled : s < < " Firewalled " ; break ;
default : s < < " Unknown " ;
}
2016-01-17 16:10:56 +00:00
s < < " <br> \r \n " ;
2016-08-21 00:00:00 +00:00
# if (!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID))
if ( auto remains = Daemon . gracefullShutdownInterval ) {
s < < " <b>Stopping in:</b> " ;
s < < remains < < " seconds " ;
s < < " <br> \r \n " ;
}
# endif
2016-04-28 22:16:11 +00:00
auto family = i2p : : context . GetFamily ( ) ;
if ( family . length ( ) > 0 )
s < < " <b>Family:</b> " < < family < < " <br> \r \n " ;
2016-01-17 16:10:56 +00:00
s < < " <b>Tunnel creation success rate:</b> " < < i2p : : tunnel : : tunnels . GetTunnelCreationSuccessRate ( ) < < " %<br> \r \n " ;
2016-03-26 14:32:19 +00:00
s < < " <b>Received:</b> " ;
s < < std : : fixed < < std : : setprecision ( 2 ) ;
auto numKBytesReceived = ( double ) i2p : : transport : : transports . GetTotalReceivedBytes ( ) / 1024 ;
if ( numKBytesReceived < 1024 )
s < < numKBytesReceived < < " KiB " ;
else if ( numKBytesReceived < 1024 * 1024 )
s < < numKBytesReceived / 1024 < < " MiB " ;
else
s < < numKBytesReceived / 1024 / 1024 < < " GiB " ;
s < < " ( " < < ( double ) i2p : : transport : : transports . GetInBandwidth ( ) / 1024 < < " KiB/s)<br> \r \n " ;
s < < " <b>Sent:</b> " ;
auto numKBytesSent = ( double ) i2p : : transport : : transports . GetTotalSentBytes ( ) / 1024 ;
if ( numKBytesSent < 1024 )
s < < numKBytesSent < < " KiB " ;
else if ( numKBytesSent < 1024 * 1024 )
s < < numKBytesSent / 1024 < < " MiB " ;
else
s < < numKBytesSent / 1024 / 1024 < < " GiB " ;
s < < " ( " < < ( double ) i2p : : transport : : transports . GetOutBandwidth ( ) / 1024 < < " KiB/s)<br> \r \n " ;
2016-02-11 00:00:00 +00:00
s < < " <b>Data path:</b> " < < i2p : : fs : : GetDataDir ( ) < < " <br> \r \n <br> \r \n " ;
2016-06-30 18:21:37 +00:00
s < < " <div class='slide' \r \n ><label for='slide1'>Hidden content. Press on text to see.</label> \r \n <input type='checkbox' id='slide1'/> \r \n <p class='content'> \r \n " ;
s < < " <b>Router Ident:</b> " < < i2p : : context . GetRouterInfo ( ) . GetIdentHashBase64 ( ) < < " <br> \r \n " ;
s < < " <b>Router Family:</b> " < < i2p : : context . GetRouterInfo ( ) . GetProperty ( " family " ) < < " <br> \r \n " ;
s < < " <b>Router Caps:</b> " < < i2p : : context . GetRouterInfo ( ) . GetProperty ( " caps " ) < < " <br> \r \n " ;
2016-01-17 16:10:56 +00:00
s < < " <b>Our external address:</b> " < < " <br> \r \n " ;
2016-08-05 18:23:54 +00:00
for ( const auto & address : i2p : : context . GetRouterInfo ( ) . GetAddresses ( ) )
2014-03-13 04:10:05 +00:00
{
2016-03-21 17:02:51 +00:00
switch ( address - > transportStyle )
2014-03-13 11:43:54 +00:00
{
case i2p : : data : : RouterInfo : : eTransportNTCP :
2016-03-21 17:02:51 +00:00
if ( address - > host . is_v6 ( ) )
2014-10-27 19:08:50 +00:00
s < < " NTCP6 " ;
else
s < < " NTCP " ;
2014-03-13 04:10:05 +00:00
break ;
2014-03-13 11:43:54 +00:00
case i2p : : data : : RouterInfo : : eTransportSSU :
2016-03-21 17:02:51 +00:00
if ( address - > host . is_v6 ( ) )
2014-10-29 22:46:35 +00:00
s < < " SSU6 " ;
else
s < < " SSU " ;
2014-03-13 04:10:05 +00:00
break ;
2014-03-13 11:43:54 +00:00
default :
s < < " Unknown " ;
2014-03-13 04:10:05 +00:00
}
2016-03-21 17:02:51 +00:00
s < < address - > host . to_string ( ) < < " : " < < address - > port < < " <br> \r \n " ;
2014-03-13 04:10:05 +00:00
}
2016-06-30 18:21:37 +00:00
s < < " </p> \r \n </div> \r \n " ;
2016-01-17 16:10:56 +00:00
s < < " <br> \r \n <b>Routers:</b> " < < i2p : : data : : netdb . GetNumRouters ( ) < < " " ;
2016-01-12 03:05:10 +00:00
s < < " <b>Floodfills:</b> " < < i2p : : data : : netdb . GetNumFloodfills ( ) < < " " ;
2016-01-17 16:10:56 +00:00
s < < " <b>LeaseSets:</b> " < < i2p : : data : : netdb . GetNumLeaseSets ( ) < < " <br> \r \n " ;
2016-03-02 14:41:37 +00:00
2016-03-02 15:05:26 +00:00
size_t clientTunnelCount = i2p : : tunnel : : tunnels . CountOutboundTunnels ( ) ;
clientTunnelCount + = i2p : : tunnel : : tunnels . CountInboundTunnels ( ) ;
size_t transitTunnelCount = i2p : : tunnel : : tunnels . CountTransitTunnels ( ) ;
2016-08-09 10:54:47 +00:00
s < < " <b>Client Tunnels:</b> " < < std : : to_string ( clientTunnelCount ) < < " " ;
s < < " <b>Transit Tunnels:</b> " < < std : : to_string ( transitTunnelCount ) < < " <br> \r \n " ;
2014-09-28 20:12:25 +00:00
}
2016-04-27 00:00:00 +00:00
void ShowLocalDestinations ( std : : stringstream & s )
2014-09-28 23:15:04 +00:00
{
2016-01-17 16:10:56 +00:00
s < < " <b>Local Destinations:</b><br> \r \n <br> \r \n " ;
2014-10-16 00:52:17 +00:00
for ( auto & it : i2p : : client : : context . GetDestinations ( ) )
2014-09-28 23:15:04 +00:00
{
2015-04-10 16:11:10 +00:00
auto ident = it . second - > GetIdentHash ( ) ; ;
2016-06-14 08:25:51 +00:00
s < < " <a href= \" /?page= " < < HTTP_PAGE_LOCAL_DESTINATION < < " &b32= " < < ident . ToBase32 ( ) < < " \" > " ;
2016-01-17 16:10:56 +00:00
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) < < " </a><br> \r \n " < < std : : endl ;
2014-09-28 23:15:04 +00:00
}
2016-01-13 01:31:25 +00:00
}
2014-09-30 02:18:32 +00:00
2016-04-27 00:00:00 +00:00
void ShowLocalDestination ( std : : stringstream & s , const std : : string & b32 )
2014-09-30 02:18:32 +00:00
{
2016-01-17 16:10:56 +00:00
s < < " <b>Local Destination:</b><br> \r \n <br> \r \n " ;
2014-09-30 02:18:32 +00:00
i2p : : data : : IdentHash ident ;
2015-04-10 16:11:10 +00:00
ident . FromBase32 ( b32 ) ;
2014-10-16 14:28:44 +00:00
auto dest = i2p : : client : : context . FindLocalDestination ( ident ) ;
2014-09-30 02:18:32 +00:00
if ( dest )
{
2016-03-25 15:48:58 +00:00
s < < " <b>Base64:</b><br> \r \n <textarea readonly= \" readonly \" cols= \" 64 \" rows= \" 11 \" wrap= \" on \" > " ;
2016-01-17 16:10:56 +00:00
s < < dest - > GetIdentity ( ) - > ToBase64 ( ) < < " </textarea><br> \r \n <br> \r \n " ;
s < < " <b>LeaseSets:</b> <i> " < < dest - > GetNumRemoteLeaseSets ( ) < < " </i><br> \r \n " ;
2014-09-30 02:18:32 +00:00
auto pool = dest - > GetTunnelPool ( ) ;
if ( pool )
{
2016-04-27 00:00:00 +00:00
s < < " <b>Inbound tunnels:</b><br> \r \n " ;
for ( auto & it : pool - > GetInboundTunnels ( ) ) {
it - > Print ( s ) ;
ShowTunnelDetails ( s , it - > GetState ( ) , it - > GetNumReceivedBytes ( ) ) ;
2014-09-30 02:18:32 +00:00
}
2016-04-27 00:00:00 +00:00
s < < " <br> \r \n " ;
s < < " <b>Outbound tunnels:</b><br> \r \n " ;
for ( auto & it : pool - > GetOutboundTunnels ( ) ) {
it - > Print ( s ) ;
ShowTunnelDetails ( s , it - > GetState ( ) , it - > GetNumSentBytes ( ) ) ;
2014-09-30 02:18:32 +00:00
}
2016-08-09 10:54:47 +00:00
}
2016-04-27 00:00:00 +00:00
s < < " <br> \r \n " ;
2016-01-24 03:53:19 +00:00
s < < " <b>Tags</b><br>Incoming: " < < dest - > GetNumIncomingTags ( ) < < " <br>Outgoing:<br> " < < std : : endl ;
2016-08-05 18:23:54 +00:00
for ( const auto & it : dest - > GetSessions ( ) )
2016-01-24 03:53:19 +00:00
{
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( it . first ) < < " " ;
s < < it . second - > GetNumOutgoingTags ( ) < < " <br> " < < std : : endl ;
2016-08-09 10:54:47 +00:00
}
2016-01-24 03:53:19 +00:00
s < < " <br> " < < std : : endl ;
2016-01-17 16:10:56 +00:00
// s << "<br>\r\n<b>Streams:</b><br>\r\n";
// for (auto it: dest->GetStreamingDestination ()->GetStreams ())
2016-08-09 10:54:47 +00:00
// {
2016-01-17 16:10:56 +00:00
// s << it.first << "->" << i2p::client::context.GetAddressBook ().ToAddress(it.second->GetRemoteIdentity ()) << " ";
// s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
// s << " [out:" << it.second->GetSendQueueSize () << "][in:" << it.second->GetReceiveQueueSize () << "]";
// s << "[buf:" << it.second->GetSendBufferSize () << "]";
// s << "[RTT:" << it.second->GetRTT () << "]";
// s << "[Window:" << it.second->GetWindowSize () << "]";
// s << "[Status:" << (int)it.second->GetStatus () << "]";
// s << "<br>\r\n"<< std::endl;
2016-08-09 10:54:47 +00:00
// }
2016-01-17 16:10:56 +00:00
s < < " <br> \r \n <table><caption>Streams</caption><tr> " ;
s < < " <th>StreamID</th> " ;
s < < " <th>Destination</th> " ;
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> " ;
s < < " </tr> " ;
2016-08-05 18:23:54 +00:00
for ( const auto & it : dest - > GetAllStreams ( ) )
2016-08-09 10:54:47 +00:00
{
2016-01-17 16:10:56 +00:00
s < < " <tr> " ;
2016-04-05 02:17:04 +00:00
s < < " <td> " < < it - > GetSendStreamID ( ) < < " </td> " ;
s < < " <td> " < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( it - > GetRemoteIdentity ( ) ) < < " </td> " ;
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> " ;
2016-01-17 16:10:56 +00:00
s < < " </tr><br> \r \n " < < std : : endl ;
2016-09-03 17:58:34 +00:00
}
s < < " </table><br> \r \n <table><caption>Forwards</caption><tr> " ;
s < < " <th>Remote Destination</th> " ;
s < < " <th>IBGW</th> " ;
s < < " <th>OBEP</th> " ;
s < < " <th>UDP Converstation</th> " ;
s < < " </th> " ;
auto forward = i2p : : client : : context . GetForwardInfosFor ( dest - > GetIdentHash ( ) ) ;
2016-09-03 20:54:39 +00:00
for ( auto info : forward )
2016-09-03 17:58:34 +00:00
{
s < < " <tr> " ;
2016-09-03 21:53:46 +00:00
s < < " <td> " < < info - > RemoteIdent - > ToBase32 ( ) < < " </td> " ;
2016-09-03 17:58:34 +00:00
s < < " <td> " ;
2016-09-03 21:53:46 +00:00
if ( info - > CurrentIBGW )
2016-09-03 21:58:16 +00:00
s < < info - > CurrentIBGW - > ToBase64 ( ) . c_str ( ) ;
2016-09-03 17:58:34 +00:00
else
s < < " (none) " ;
s < < " </td> " ;
s < < " <td> " ;
2016-09-03 21:53:46 +00:00
if ( info - > CurrentOBEP )
2016-09-03 21:58:16 +00:00
s < < info - > CurrentOBEP - > ToBase64 ( ) . c_str ( ) ;
2016-09-03 17:58:34 +00:00
else
s < < " (none) " ;
s < < " </td> " ;
2016-09-03 21:53:46 +00:00
s < < " <td> " < < info - > LocalEndpoint < < " ⇄ " < < info - > RemoteEndpoint < < " </td> " ;
2016-09-03 17:58:34 +00:00
s < < " </tr><br> \r \n " ;
2016-01-17 16:10:56 +00:00
}
2016-09-03 17:58:34 +00:00
s < < " </table> \r \n " ;
2016-08-09 10:54:47 +00:00
}
2016-01-13 01:31:25 +00:00
}
2016-07-15 17:52:55 +00:00
void ShowLeasesSets ( std : : stringstream & s )
{
2016-08-09 10:54:47 +00:00
s < < " <div id='leasesets'><b>LeaseSets:</b></div><br> " ;
2016-07-15 17:52:55 +00:00
// for each lease set
i2p : : data : : netdb . VisitLeaseSets (
[ & s ] ( const i2p : : data : : IdentHash dest , std : : shared_ptr < i2p : : data : : LeaseSet > leaseSet )
{
// create copy of lease set so we extract leases
i2p : : data : : LeaseSet ls ( leaseSet - > GetBuffer ( ) , leaseSet - > GetBufferLen ( ) ) ;
// begin lease set entry
s < < " <div class='leaseset " ;
if ( ls . IsExpired ( ) )
s < < " expired " ; // additional css class for expired
s < < " '> " ;
// invalid ?
if ( ! ls . IsValid ( ) )
s < < " <div class='invalid'>!! Invalid !! </div> " ;
// ident
s < < " <div class='ident'> " < < dest . ToBase32 ( ) < < " </div> " ;
// LeaseSet time
s < < " <div class='expires'>expires: " < < ls . GetExpirationTime ( ) < < " </div> " ;
// get non expired leases
auto leases = ls . GetNonExpiredLeases ( ) ;
// show non expired leases
s < < " <div class='leasecount'>Non Expired Leases: " < < leases . size ( ) < < " </div> " ;
// for each lease
s < < " <div class='leases'> " ;
for ( auto & l : leases )
{
// begin lease
s < < " <div class='lease'> " ;
// gateway
s < < " <div class='gateway'>Gateway: " < < l - > tunnelGateway . ToBase64 ( ) < < " </div> " ;
// tunnel id
s < < " <div class='tunnelID'>TunnelID: " < < l - > tunnelID < < " </div> " ;
// end date
s < < " <div class='endDate'>EndDate: " < < l - > endDate < < " </div> " ;
// end lease
s < < " </div> " ;
}
// end for each lease
s < < " </div> " ;
// end lease set entry
s < < " </div> " ;
// linebreak
s < < " <br> " ;
}
) ;
// end for each lease set
}
2016-08-09 10:54:47 +00:00
2016-04-27 00:00:00 +00:00
void ShowTunnels ( std : : stringstream & s )
2016-01-13 01:31:25 +00:00
{
2016-01-17 18:18:21 +00:00
s < < " <b>Queue size:</b> " < < i2p : : tunnel : : tunnels . GetQueueSize ( ) < < " <br> \r \n " ;
2016-01-13 01:31:25 +00:00
2016-04-27 00:00:00 +00:00
s < < " <b>Inbound tunnels:</b><br> \r \n " ;
for ( auto & it : i2p : : tunnel : : tunnels . GetInboundTunnels ( ) ) {
it - > Print ( s ) ;
ShowTunnelDetails ( s , it - > GetState ( ) , it - > GetNumReceivedBytes ( ) ) ;
2016-01-13 01:31:25 +00:00
}
2016-04-27 00:00:00 +00:00
s < < " <br> \r \n " ;
s < < " <b>Outbound tunnels:</b><br> \r \n " ;
for ( auto & it : i2p : : tunnel : : tunnels . GetOutboundTunnels ( ) ) {
it - > Print ( s ) ;
ShowTunnelDetails ( s , it - > GetState ( ) , it - > GetNumSentBytes ( ) ) ;
2016-01-13 01:31:25 +00:00
}
2016-04-27 00:00:00 +00:00
s < < " <br> \r \n " ;
2016-08-09 10:54:47 +00:00
}
2015-02-21 03:47:36 +00:00
2016-04-27 00:00:00 +00:00
void ShowCommands ( std : : stringstream & s )
{
/* commands */
s < < " <b>Router Commands</b><br> \r \n " ;
2016-06-14 08:25:51 +00:00
s < < " <a href= \" /?cmd= " < < HTTP_COMMAND_RUN_PEER_TEST < < " \" >Run peer test</a><br> \r \n " ;
//s << " <a href=\"/?cmd=" << HTTP_COMMAND_RELOAD_CONFIG << "\">Reload config</a><br>\r\n";
2016-04-27 00:00:00 +00:00
if ( i2p : : context . AcceptsTunnels ( ) )
2016-07-12 00:00:00 +00:00
s < < " <a href= \" /?cmd= " < < HTTP_COMMAND_DISABLE_TRANSIT < < " \" >Decline transit tunnels</a><br> \r \n " ;
2016-08-09 10:54:47 +00:00
else
2016-07-12 00:00:00 +00:00
s < < " <a href= \" /?cmd= " < < HTTP_COMMAND_ENABLE_TRANSIT < < " \" >Accept transit tunnels</a><br> \r \n " ;
2016-07-09 20:54:11 +00:00
# if (!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID))
2016-07-26 16:11:52 +00:00
if ( Daemon . gracefullShutdownInterval )
2016-08-26 14:00:00 +00:00
s < < " <a href= \" /?cmd= " < < HTTP_COMMAND_SHUTDOWN_CANCEL < < " \" >Cancel gracefull shutdown</a><br> " ;
2016-07-26 16:11:52 +00:00
else
2016-06-14 08:25:51 +00:00
s < < " <a href= \" /?cmd= " < < HTTP_COMMAND_SHUTDOWN_START < < " \" >Start gracefull shutdown</a><br> \r \n " ;
2016-05-11 19:33:53 +00:00
# endif
2016-07-26 16:11:52 +00:00
# ifdef WIN32_APP
s < < " <a href= \" /?cmd= " < < HTTP_COMMAND_SHUTDOWN_START < < " \" >Gracefull shutdown</a><br> \r \n " ;
# endif
s < < " <a href= \" /?cmd= " < < HTTP_COMMAND_SHUTDOWN_NOW < < " \" >Force shutdown</a><br> \r \n " ;
2016-04-27 00:00:00 +00:00
}
2016-04-27 00:00:00 +00:00
void ShowTransitTunnels ( std : : stringstream & s )
2016-01-13 01:31:25 +00:00
{
2016-01-17 16:10:56 +00:00
s < < " <b>Transit tunnels:</b><br> \r \n <br> \r \n " ;
2016-08-05 18:23:54 +00:00
for ( const auto & it : i2p : : tunnel : : tunnels . GetTransitTunnels ( ) )
2016-01-13 01:31:25 +00:00
{
2016-03-02 01:48:56 +00:00
if ( std : : dynamic_pointer_cast < i2p : : tunnel : : TransitTunnelGateway > ( it ) )
2016-08-09 10:54:47 +00:00
s < < it - > GetTunnelID ( ) < < " ⇒ " ;
2016-03-02 01:48:56 +00:00
else if ( std : : dynamic_pointer_cast < i2p : : tunnel : : TransitTunnelEndpoint > ( it ) )
2016-08-09 10:54:47 +00:00
s < < " ⇒ " < < it - > GetTunnelID ( ) ;
2016-01-13 01:31:25 +00:00
else
2016-08-09 10:54:47 +00:00
s < < " ⇒ " < < it - > GetTunnelID ( ) < < " ⇒ " ;
2016-03-02 01:48:56 +00:00
s < < " " < < it - > GetNumTransmittedBytes ( ) < < " <br> \r \n " ;
2016-01-13 01:31:25 +00:00
}
}
2016-04-27 00:00:00 +00:00
void ShowTransports ( std : : stringstream & s )
2016-01-13 01:31:25 +00:00
{
2016-01-17 16:10:56 +00:00
s < < " <b>Transports:</b><br> \r \n <br> \r \n " ;
2016-01-13 01:31:25 +00:00
auto ntcpServer = i2p : : transport : : transports . GetNTCPServer ( ) ;
if ( ntcpServer )
2016-08-09 10:54:47 +00:00
{
2016-01-17 16:10:56 +00:00
s < < " <b>NTCP</b><br> \r \n " ;
2016-08-05 18:23:54 +00:00
for ( const auto & it : ntcpServer - > GetNTCPSessions ( ) )
2016-01-13 01:31:25 +00:00
{
if ( it . second & & it . second - > IsEstablished ( ) )
{
// incoming connection doesn't have remote RI
2016-08-09 10:54:47 +00:00
if ( it . second - > IsOutgoing ( ) ) s < < " ⇒ " ;
2016-01-13 01:31:25 +00:00
s < < i2p : : data : : GetIdentHashAbbreviation ( it . second - > GetRemoteIdentity ( ) - > GetIdentHash ( ) ) < < " : "
< < it . second - > GetSocket ( ) . remote_endpoint ( ) . address ( ) . to_string ( ) ;
2016-08-09 10:54:47 +00:00
if ( ! it . second - > IsOutgoing ( ) ) s < < " ⇒ " ;
2016-01-13 01:31:25 +00:00
s < < " [ " < < it . second - > GetNumSentBytes ( ) < < " : " < < it . second - > GetNumReceivedBytes ( ) < < " ] " ;
2016-01-17 16:10:56 +00:00
s < < " <br> \r \n " < < std : : endl ;
2016-01-13 01:31:25 +00:00
}
}
2016-08-09 10:54:47 +00:00
}
2016-01-13 01:31:25 +00:00
auto ssuServer = i2p : : transport : : transports . GetSSUServer ( ) ;
if ( ssuServer )
{
2016-01-17 16:10:56 +00:00
s < < " <br> \r \n <b>SSU</b><br> \r \n " ;
2016-08-05 18:23:54 +00:00
for ( const auto & it : ssuServer - > GetSessions ( ) )
2016-01-13 01:31:25 +00:00
{
auto endpoint = it . second - > GetRemoteEndpoint ( ) ;
2016-08-09 10:54:47 +00:00
if ( it . second - > IsOutgoing ( ) ) s < < " ⇒ " ;
2016-01-13 01:31:25 +00:00
s < < endpoint . address ( ) . to_string ( ) < < " : " < < endpoint . port ( ) ;
2016-08-09 10:54:47 +00:00
if ( ! it . second - > IsOutgoing ( ) ) s < < " ⇒ " ;
2016-01-13 01:31:25 +00:00
s < < " [ " < < it . second - > GetNumSentBytes ( ) < < " : " < < it . second - > GetNumReceivedBytes ( ) < < " ] " ;
if ( it . second - > GetRelayTag ( ) )
s < < " [itag: " < < it . second - > GetRelayTag ( ) < < " ] " ;
2016-01-17 16:10:56 +00:00
s < < " <br> \r \n " < < std : : endl ;
2016-01-13 01:31:25 +00:00
}
2016-01-17 16:10:56 +00:00
s < < " <br> \r \n <b>SSU6</b><br> \r \n " ;
2016-08-05 18:23:54 +00:00
for ( const auto & it : ssuServer - > GetSessionsV6 ( ) )
2016-01-13 01:31:25 +00:00
{
auto endpoint = it . second - > GetRemoteEndpoint ( ) ;
2016-08-09 10:54:47 +00:00
if ( it . second - > IsOutgoing ( ) ) s < < " ⇒ " ;
2016-01-13 01:31:25 +00:00
s < < endpoint . address ( ) . to_string ( ) < < " : " < < endpoint . port ( ) ;
2016-08-09 10:54:47 +00:00
if ( ! it . second - > IsOutgoing ( ) ) s < < " ⇒ " ;
2016-01-13 01:31:25 +00:00
s < < " [ " < < it . second - > GetNumSentBytes ( ) < < " : " < < it . second - > GetNumReceivedBytes ( ) < < " ] " ;
2016-01-17 16:10:56 +00:00
s < < " <br> \r \n " < < std : : endl ;
2016-01-13 01:31:25 +00:00
}
}
}
2016-08-09 10:54:47 +00:00
2016-04-27 00:00:00 +00:00
void ShowSAMSessions ( std : : stringstream & s )
2015-02-21 03:47:36 +00:00
{
auto sam = i2p : : client : : context . GetSAMBridge ( ) ;
2016-04-27 00:00:00 +00:00
if ( ! sam ) {
ShowError ( s , " SAM disabled " ) ;
return ;
}
s < < " <b>SAM Sessions:</b><br> \r \n <br> \r \n " ;
for ( auto & it : sam - > GetSessions ( ) )
{
2016-06-14 08:25:51 +00:00
s < < " <a href= \" /?page= " < < HTTP_PAGE_SAM_SESSION < < " &sam_id= " < < it . first < < " \" > " ;
2016-04-27 00:00:00 +00:00
s < < it . first < < " </a><br> \r \n " < < std : : endl ;
2016-08-09 10:54:47 +00:00
}
}
2015-02-21 03:47:36 +00:00
2016-04-27 00:00:00 +00:00
void ShowSAMSession ( std : : stringstream & s , const std : : string & id )
2015-02-21 03:47:36 +00:00
{
2016-01-17 16:10:56 +00:00
s < < " <b>SAM Session:</b><br> \r \n <br> \r \n " ;
2015-02-21 03:47:36 +00:00
auto sam = i2p : : client : : context . GetSAMBridge ( ) ;
2016-04-27 00:00:00 +00:00
if ( ! sam ) {
ShowError ( s , " SAM disabled " ) ;
return ;
}
auto session = sam - > FindSession ( id ) ;
if ( ! session ) {
ShowError ( s , " SAM session not found " ) ;
return ;
}
auto & ident = session - > localDestination - > GetIdentHash ( ) ;
2016-06-14 08:25:51 +00:00
s < < " <a href= \" /?page= " < < HTTP_PAGE_LOCAL_DESTINATION < < " &b32= " < < ident . ToBase32 ( ) < < " \" > " ;
2016-04-27 00:00:00 +00:00
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) < < " </a><br> \r \n " ;
s < < " <br> \r \n " ;
s < < " <b>Streams:</b><br> \r \n " ;
2016-08-05 18:23:54 +00:00
for ( const auto & it : session - > ListSockets ( ) )
2015-02-21 03:47:36 +00:00
{
2016-04-27 00:00:00 +00:00
switch ( it - > GetSocketType ( ) )
2015-02-21 03:47:36 +00:00
{
2016-04-27 00:00:00 +00:00
case i2p : : client : : eSAMSocketTypeSession : s < < " session " ; break ;
case i2p : : client : : eSAMSocketTypeStream : s < < " stream " ; break ;
case i2p : : client : : eSAMSocketTypeAcceptor : s < < " acceptor " ; break ;
default : s < < " unknown " ; break ;
2015-02-21 03:47:36 +00:00
}
2016-04-27 00:00:00 +00:00
s < < " [ " < < it - > GetSocket ( ) . remote_endpoint ( ) < < " ] " ;
s < < " <br> \r \n " ;
2016-08-09 10:54:47 +00:00
}
}
2014-09-30 17:34:29 +00:00
2016-04-27 00:00:00 +00:00
void ShowI2PTunnels ( std : : stringstream & s )
2016-01-14 01:21:53 +00:00
{
2016-01-17 16:10:56 +00:00
s < < " <b>Client Tunnels:</b><br> \r \n <br> \r \n " ;
2016-01-14 01:21:53 +00:00
for ( auto & it : i2p : : client : : context . GetClientTunnels ( ) )
{
auto & ident = it . second - > GetLocalDestination ( ) - > GetIdentHash ( ) ;
2016-06-14 08:25:51 +00:00
s < < " <a href= \" /?page= " < < HTTP_PAGE_LOCAL_DESTINATION < < " &b32= " < < ident . ToBase32 ( ) < < " \" > " ;
2016-08-09 10:54:47 +00:00
s < < it . second - > GetName ( ) < < " </a> ⇐ " ;
2016-01-14 01:21:53 +00:00
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
2016-04-06 20:33:23 +00:00
s < < " <br> \r \n " < < std : : endl ;
2016-08-09 10:54:47 +00:00
}
2016-01-17 16:10:56 +00:00
s < < " <br> \r \n <b>Server Tunnels:</b><br> \r \n <br> \r \n " ;
2016-01-14 01:21:53 +00:00
for ( auto & it : i2p : : client : : context . GetServerTunnels ( ) )
{
auto & ident = it . second - > GetLocalDestination ( ) - > GetIdentHash ( ) ;
2016-06-14 08:25:51 +00:00
s < < " <a href= \" /?page= " < < HTTP_PAGE_LOCAL_DESTINATION < < " &b32= " < < ident . ToBase32 ( ) < < " \" > " ;
2016-08-09 10:54:47 +00:00
s < < it . second - > GetName ( ) < < " </a> ⇒ " ;
2016-01-14 01:21:53 +00:00
s < < i2p : : client : : context . GetAddressBook ( ) . ToAddress ( ident ) ;
2016-01-17 23:55:09 +00:00
s < < " : " < < it . second - > GetLocalPort ( ) ;
2016-01-17 16:10:56 +00:00
s < < " </a><br> \r \n " < < std : : endl ;
2016-08-09 10:54:47 +00:00
}
}
2014-09-30 17:34:29 +00:00
2016-04-27 00:00:00 +00:00
HTTPConnection : : HTTPConnection ( std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > socket ) :
m_Socket ( socket ) , m_Timer ( socket - > get_io_service ( ) ) , m_BufferLen ( 0 )
2016-01-13 01:31:25 +00:00
{
2016-04-27 00:00:00 +00:00
/* cache options */
i2p : : config : : GetOption ( " http.auth " , needAuth ) ;
i2p : : config : : GetOption ( " http.user " , user ) ;
i2p : : config : : GetOption ( " http.pass " , pass ) ;
2016-06-01 00:00:00 +00:00
}
2016-01-13 01:31:25 +00:00
2016-04-27 00:00:00 +00:00
void HTTPConnection : : Receive ( )
2015-11-03 14:15:49 +00:00
{
2016-04-27 00:00:00 +00:00
m_Socket - > async_read_some ( boost : : asio : : buffer ( m_Buffer , HTTP_CONNECTION_BUFFER_SIZE ) ,
std : : bind ( & HTTPConnection : : HandleReceive , shared_from_this ( ) ,
std : : placeholders : : _1 , std : : placeholders : : _2 ) ) ;
2015-11-03 14:15:49 +00:00
}
2016-04-27 00:00:00 +00:00
void HTTPConnection : : HandleReceive ( const boost : : system : : error_code & ecode , std : : size_t bytes_transferred )
2014-01-13 02:41:25 +00:00
{
2016-04-27 00:00:00 +00:00
if ( ecode ) {
if ( ecode ! = boost : : asio : : error : : operation_aborted )
Terminate ( ecode ) ;
2014-10-03 18:22:32 +00:00
return ;
2014-10-21 18:28:56 +00:00
}
2016-04-27 00:00:00 +00:00
m_Buffer [ bytes_transferred ] = ' \0 ' ;
m_BufferLen = bytes_transferred ;
RunRequest ( ) ;
Receive ( ) ;
2014-10-21 18:28:56 +00:00
}
2016-04-27 00:00:00 +00:00
void HTTPConnection : : RunRequest ( )
2014-10-21 18:28:56 +00:00
{
2016-04-27 00:00:00 +00:00
HTTPReq request ;
int ret = request . parse ( m_Buffer ) ;
if ( ret < 0 ) {
m_Buffer [ 0 ] = ' \0 ' ;
m_BufferLen = 0 ;
return ; /* error */
2014-07-16 16:41:40 +00:00
}
2016-04-27 00:00:00 +00:00
if ( ret = = 0 )
return ; /* need more data */
2016-04-27 00:00:00 +00:00
2016-04-27 00:00:00 +00:00
HandleRequest ( request ) ;
2014-10-21 18:28:56 +00:00
}
2016-04-27 00:00:00 +00:00
void HTTPConnection : : Terminate ( const boost : : system : : error_code & ecode )
2014-03-27 19:42:23 +00:00
{
2016-04-27 00:00:00 +00: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 19:42:23 +00:00
}
2016-04-27 00:00:00 +00:00
bool HTTPConnection : : CheckAuth ( const HTTPReq & req ) {
/* 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 17:24:23 +00:00
}
2016-04-27 00:00:00 +00:00
/* method #2: 'Authorization' header sent */
if ( req . headers . count ( " Authorization " ) > 0 ) {
std : : string provided = req . headers . find ( " Authorization " ) - > second ;
std : : string expected = user + " : " + pass ;
char b64_creds [ 64 ] ;
std : : size_t len = 0 ;
len = i2p : : data : : ByteStreamToBase64 ( ( unsigned char * ) expected . c_str ( ) , expected . length ( ) , b64_creds , sizeof ( b64_creds ) ) ;
b64_creds [ len ] = ' \0 ' ;
expected = " Basic " ;
expected + = b64_creds ;
if ( provided = = expected )
return true ;
2014-07-16 16:41:40 +00:00
}
2016-04-27 00:00:00 +00:00
LogPrint ( eLogWarning , " HTTPServer: auth failure from " , m_Socket - > remote_endpoint ( ) . address ( ) ) ;
return false ;
2014-03-27 17:24:23 +00:00
}
2016-04-27 00:00:00 +00:00
void HTTPConnection : : HandleRequest ( const HTTPReq & req )
2014-03-27 17:24:23 +00:00
{
2016-04-27 00:00:00 +00:00
std : : stringstream s ;
2016-04-27 00:00:00 +00:00
std : : string content ;
HTTPRes res ;
2016-04-27 00:00:00 +00:00
2016-04-27 00:00:00 +00:00
LogPrint ( eLogDebug , " HTTPServer: request: " , req . uri ) ;
2016-04-27 00:00:00 +00:00
if ( needAuth & & ! CheckAuth ( req ) ) {
res . code = 401 ;
2016-05-24 00:00:00 +00:00
res . add_header ( " WWW-Authenticate " , " Basic realm= \" WebAdmin \" " ) ;
2016-04-27 00:00:00 +00:00
SendReply ( res , content ) ;
return ;
}
2016-04-27 00:00:00 +00:00
// Html5 head start
2016-04-27 00:00:00 +00:00
ShowPageHead ( s ) ;
2016-05-28 00:00:00 +00:00
if ( req . uri . find ( " page= " ) ! = std : : string : : npos ) {
2016-04-27 00:00:00 +00:00
HandlePage ( req , res , s ) ;
2016-05-28 00:00:00 +00:00
} else if ( req . uri . find ( " cmd= " ) ! = std : : string : : npos ) {
2016-04-27 00:00:00 +00:00
HandleCommand ( req , res , s ) ;
2016-05-28 00:00:00 +00:00
} else {
2016-04-27 00:00:00 +00:00
ShowStatus ( s ) ;
2016-06-24 23:07:47 +00:00
res . add_header ( " Refresh " , " 10 " ) ;
2016-05-28 00:00:00 +00:00
}
2016-04-27 00:00:00 +00:00
ShowPageTail ( s ) ;
2016-05-24 00:00:00 +00:00
res . code = 200 ;
2016-04-27 00:00:00 +00:00
content = s . str ( ) ;
SendReply ( res , content ) ;
2014-03-26 19:45:08 +00:00
}
2014-07-16 16:41:40 +00:00
2016-04-27 00:00:00 +00:00
void HTTPConnection : : HandlePage ( const HTTPReq & req , HTTPRes & res , std : : stringstream & s )
2016-04-27 00:00:00 +00:00
{
std : : map < std : : string , std : : string > params ;
std : : string page ( " " ) ;
URL url ;
2016-04-27 00:00:00 +00:00
url . parse ( req . uri ) ;
2016-04-27 00:00:00 +00: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 00:00:00 +00:00
else if ( page = = HTTP_PAGE_COMMANDS )
ShowCommands ( s ) ;
2016-04-27 00:00:00 +00:00
else if ( page = = HTTP_PAGE_TRANSIT_TUNNELS )
ShowTransitTunnels ( s ) ;
else if ( page = = HTTP_PAGE_LOCAL_DESTINATIONS )
ShowLocalDestinations ( s ) ;
else if ( page = = HTTP_PAGE_LOCAL_DESTINATION )
ShowLocalDestination ( s , params [ " b32 " ] ) ;
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 17:52:55 +00:00
else if ( page = = HTTP_PAGE_LEASESETS )
ShowLeasesSets ( s ) ;
2016-04-27 00:00:00 +00:00
else {
res . code = 400 ;
ShowError ( s , " Unknown page: " + page ) ;
return ;
}
2016-04-27 00:00:00 +00:00
}
2016-04-27 00:00:00 +00:00
void HTTPConnection : : HandleCommand ( const HTTPReq & req , HTTPRes & res , std : : stringstream & s )
2016-04-27 00:00:00 +00:00
{
std : : map < std : : string , std : : string > params ;
std : : string cmd ( " " ) ;
URL url ;
2016-04-27 00:00:00 +00:00
url . parse ( req . uri ) ;
2016-04-27 00:00:00 +00:00
url . parse_query ( params ) ;
cmd = params [ " cmd " ] ;
2016-04-27 00:00:00 +00:00
if ( cmd = = HTTP_COMMAND_RUN_PEER_TEST )
i2p : : transport : : transports . PeerTest ( ) ;
2016-05-12 15:38:18 +00:00
else if ( cmd = = HTTP_COMMAND_RELOAD_CONFIG )
i2p : : client : : context . ReloadConfig ( ) ;
2016-07-12 00:00:00 +00:00
else if ( cmd = = HTTP_COMMAND_ENABLE_TRANSIT )
2016-04-27 00:00:00 +00:00
i2p : : context . SetAcceptsTunnels ( true ) ;
2016-07-12 00:00:00 +00:00
else if ( cmd = = HTTP_COMMAND_DISABLE_TRANSIT )
2016-04-27 00:00:00 +00:00
i2p : : context . SetAcceptsTunnels ( false ) ;
2016-04-27 00:00:00 +00:00
else if ( cmd = = HTTP_COMMAND_SHUTDOWN_START ) {
i2p : : context . SetAcceptsTunnels ( false ) ;
2016-07-09 20:54:11 +00:00
# if (!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID))
2016-04-27 00:00:00 +00:00
Daemon . gracefullShutdownInterval = 10 * 60 ;
2016-07-26 16:11:52 +00:00
# endif
# ifdef WIN32_APP
2016-08-09 10:54:47 +00:00
i2p : : win32 : : GracefulShutdown ( ) ;
2016-05-11 19:33:53 +00:00
# endif
2016-04-27 00:00:00 +00:00
} else if ( cmd = = HTTP_COMMAND_SHUTDOWN_CANCEL ) {
i2p : : context . SetAcceptsTunnels ( true ) ;
2016-07-09 20:54:11 +00:00
# if (!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID))
2016-04-27 00:00:00 +00:00
Daemon . gracefullShutdownInterval = 0 ;
2016-05-11 19:33:53 +00:00
# endif
2016-04-27 00:00:00 +00:00
} else if ( cmd = = HTTP_COMMAND_SHUTDOWN_NOW ) {
Daemon . running = false ;
} else {
2016-04-27 00:00:00 +00:00
res . code = 400 ;
ShowError ( s , " Unknown command: " + cmd ) ;
2016-04-27 00:00:00 +00:00
return ;
}
2016-04-27 00:00:00 +00:00
s < < " <b>SUCCESS</b>: Command accepted<br><br> \r \n " ;
2016-05-28 00:00:00 +00:00
s < < " <a href= \" /?page=commands \" >Back to commands list</a><br> \r \n " ;
s < < " <p>You will be redirected in 5 seconds</b> " ;
res . add_header ( " Refresh " , " 5; url=/?page=commands " ) ;
2016-08-09 10:54:47 +00:00
}
2016-04-27 00:00:00 +00:00
2016-04-27 00:00:00 +00:00
void HTTPConnection : : SendReply ( HTTPRes & reply , std : : string & content )
2016-04-14 00:00:00 +00:00
{
2016-05-24 00:00:00 +00:00
reply . add_header ( " Content-Type " , " text/html " ) ;
reply . body = content ;
2016-04-27 00:00:00 +00:00
2016-08-26 14:00:00 +00:00
m_SendBuffer = reply . to_string ( ) ;
boost : : asio : : async_write ( * m_Socket , boost : : asio : : buffer ( m_SendBuffer ) ,
2016-04-27 00:00:00 +00:00
std : : bind ( & HTTPConnection : : Terminate , shared_from_this ( ) , std : : placeholders : : _1 ) ) ;
2016-04-14 00:00:00 +00:00
}
2015-11-30 14:44:32 +00:00
HTTPServer : : HTTPServer ( const std : : string & address , int port ) :
2014-07-16 16:41:40 +00:00
m_Thread ( nullptr ) , m_Work ( m_Service ) ,
2015-11-30 14:44:32 +00:00
m_Acceptor ( m_Service , boost : : asio : : ip : : tcp : : endpoint ( boost : : asio : : ip : : address : : from_string ( address ) , port ) )
2013-12-10 13:03:22 +00:00
{
}
HTTPServer : : ~ HTTPServer ( )
{
Stop ( ) ;
}
void HTTPServer : : Start ( )
{
2016-04-27 00:00:00 +00:00
bool needAuth ; i2p : : config : : GetOption ( " http.auth " , needAuth ) ;
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 02:00:00 +00:00
uint8_t random [ 16 ] ;
2016-04-27 00:00:00 +00:00
char alnum [ ] = " 0123456789 "
" ABCDEFGHIJKLMNOPQRSTUVWXYZ "
" abcdefghijklmnopqrstuvwxyz " ;
2016-07-27 02:00:00 +00: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 00:00:00 +00:00
}
i2p : : config : : SetOption ( " http.pass " , pass ) ;
LogPrint ( eLogInfo , " HTTPServer: password set to " , pass ) ;
}
2015-11-22 22:01:37 +00:00
m_Thread = std : : unique_ptr < std : : thread > ( new std : : thread ( std : : bind ( & HTTPServer : : Run , this ) ) ) ;
2013-12-10 13:03:22 +00:00
m_Acceptor . listen ( ) ;
Accept ( ) ;
}
void HTTPServer : : Stop ( )
{
m_Acceptor . close ( ) ;
m_Service . stop ( ) ;
2016-04-27 00:00:00 +00:00
if ( m_Thread ) {
m_Thread - > join ( ) ;
m_Thread = nullptr ;
}
2013-12-10 13:03:22 +00:00
}
void HTTPServer : : Run ( )
{
m_Service . run ( ) ;
2014-07-16 16:41:40 +00:00
}
2013-12-10 13:03:22 +00:00
void HTTPServer : : Accept ( )
{
2015-11-22 22:01:37 +00:00
auto newSocket = std : : make_shared < boost : : asio : : ip : : tcp : : socket > ( m_Service ) ;
m_Acceptor . async_accept ( * newSocket , boost : : bind ( & HTTPServer : : HandleAccept , this ,
boost : : asio : : placeholders : : error , newSocket ) ) ;
2013-12-10 13:03:22 +00:00
}
2015-11-22 22:01:37 +00:00
void HTTPServer : : HandleAccept ( const boost : : system : : error_code & ecode ,
std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > newSocket )
2013-12-10 13:03:22 +00:00
{
2016-04-27 00:00:00 +00:00
if ( ecode )
return ;
CreateConnection ( newSocket ) ;
Accept ( ) ;
2014-07-16 16:41:40 +00:00
}
2014-03-29 22:16:23 +00:00
2015-11-22 22:01:37 +00:00
void HTTPServer : : CreateConnection ( std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > newSocket )
2014-03-29 22:16:23 +00:00
{
2015-11-22 22:01:37 +00:00
auto conn = std : : make_shared < HTTPConnection > ( newSocket ) ;
2015-04-04 19:44:29 +00:00
conn - > Receive ( ) ;
2014-03-29 22:16:23 +00:00
}
2016-04-27 00:00:00 +00:00
} // http
} // i2p