2020-05-22 16:18:41 +03:00
/*
2023-04-04 02:05:35 +03:00
* Copyright ( c ) 2013 - 2023 , The PurpleI2P Project
2020-05-22 16:18:41 +03:00
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2015-01-07 01:51:10 +03:00
# include <cassert>
2022-05-29 23:59:15 +03:00
# include <boost/algorithm/string.hpp>
2015-11-03 17:15:49 +03:00
# include "Base.h"
2014-08-13 23:25:52 +04:00
# include "Log.h"
2014-10-05 16:54:59 +04:00
# include "Destination.h"
2014-10-16 04:52:17 +04:00
# include "ClientContext.h"
2014-08-13 05:14:19 +04:00
# include "I2PTunnel.h"
2020-12-07 06:22:16 +03:00
# include "util.h"
2014-08-13 05:14:19 +04:00
namespace i2p
{
2014-10-16 18:28:44 +04:00
namespace client
2014-08-13 05:14:19 +04:00
{
2016-07-28 16:25:05 +03:00
/** set standard socket options */
2022-10-09 04:41:28 +03:00
static void I2PTunnelSetSocketOptions ( std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > socket )
2016-07-28 16:25:05 +03:00
{
2016-07-28 16:53:39 +03:00
if ( socket & & socket - > is_open ( ) )
2017-08-31 19:08:22 +03:00
{
2016-07-28 16:53:39 +03:00
boost : : asio : : socket_base : : receive_buffer_size option ( I2P_TUNNEL_CONNECTION_BUFFER_SIZE ) ;
socket - > set_option ( option ) ;
}
2016-07-28 16:25:05 +03:00
}
2017-08-31 19:08:22 +03:00
2015-04-06 21:41:07 +03:00
I2PTunnelConnection : : I2PTunnelConnection ( I2PService * owner , std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > socket ,
2023-06-12 05:10:32 +03:00
std : : shared_ptr < const i2p : : data : : LeaseSet > leaseSet , uint16_t port ) :
2015-01-07 21:09:59 +03:00
I2PServiceHandler ( owner ) , m_Socket ( socket ) , m_RemoteEndpoint ( socket - > remote_endpoint ( ) ) ,
2014-12-05 22:46:59 +03:00
m_IsQuiet ( true )
2014-08-13 05:14:19 +04:00
{
2015-03-13 20:29:27 +03:00
m_Stream = GetOwner ( ) - > GetLocalDestination ( ) - > CreateStream ( leaseSet , port ) ;
2017-08-31 19:08:22 +03:00
}
2014-08-13 05:14:19 +04:00
2015-01-07 21:09:59 +03:00
I2PTunnelConnection : : I2PTunnelConnection ( I2PService * owner ,
2017-10-04 20:15:29 +03:00
std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > socket , std : : shared_ptr < i2p : : stream : : Stream > stream ) :
2015-01-07 21:09:59 +03:00
I2PServiceHandler ( owner ) , m_Socket ( socket ) , m_Stream ( stream ) ,
2015-01-03 04:43:59 +03:00
m_RemoteEndpoint ( socket - > remote_endpoint ( ) ) , m_IsQuiet ( true )
{
}
2015-01-07 21:09:59 +03:00
I2PTunnelConnection : : I2PTunnelConnection ( I2PService * owner , std : : shared_ptr < i2p : : stream : : Stream > stream ,
2022-10-09 04:41:28 +03:00
const boost : : asio : : ip : : tcp : : endpoint & target , bool quiet ,
std : : shared_ptr < boost : : asio : : ssl : : context > sslCtx ) :
I2PServiceHandler ( owner ) , m_Stream ( stream ) , m_RemoteEndpoint ( target ) , m_IsQuiet ( quiet )
2014-08-20 23:03:10 +04:00
{
2022-10-09 04:41:28 +03:00
m_Socket = std : : make_shared < boost : : asio : : ip : : tcp : : socket > ( owner - > GetService ( ) ) ;
if ( sslCtx )
m_SSL = std : : make_shared < boost : : asio : : ssl : : stream < boost : : asio : : ip : : tcp : : socket & > > ( * m_Socket , * sslCtx ) ;
2014-08-20 23:03:10 +04:00
}
2014-08-13 05:14:19 +04:00
I2PTunnelConnection : : ~ I2PTunnelConnection ( )
{
2017-08-31 19:08:22 +03:00
}
2014-12-04 04:37:20 +03:00
void I2PTunnelConnection : : I2PConnect ( const uint8_t * msg , size_t len )
2014-11-24 06:23:17 +03:00
{
2015-01-07 01:51:10 +03:00
if ( m_Stream )
{
if ( msg )
m_Stream - > Send ( msg , len ) ; // connect and send
2017-08-31 19:08:22 +03:00
else
2015-01-07 01:51:10 +03:00
m_Stream - > Send ( m_Buffer , 0 ) ; // connect
}
2014-11-24 06:23:17 +03:00
StreamReceive ( ) ;
Receive ( ) ;
}
2016-12-25 16:56:47 +03:00
2022-10-25 22:30:12 +03:00
boost : : asio : : ip : : address GetLoopbackAddressFor ( const i2p : : data : : IdentHash & addr )
2016-12-25 16:56:47 +03:00
{
boost : : asio : : ip : : address_v4 : : bytes_type bytes ;
const uint8_t * ident = addr ;
bytes [ 0 ] = 127 ;
memcpy ( bytes . data ( ) + 1 , ident , 3 ) ;
boost : : asio : : ip : : address ourIP = boost : : asio : : ip : : address_v4 ( bytes ) ;
return ourIP ;
}
2017-08-31 19:08:22 +03:00
2020-05-14 20:59:52 +03:00
# ifdef __linux__
2022-10-09 04:41:28 +03:00
static void MapToLoopback ( std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > sock , const i2p : : data : : IdentHash & addr )
2016-12-25 16:56:47 +03:00
{
2022-10-09 04:41:28 +03:00
if ( sock )
2022-10-09 20:24:43 +03:00
{
2022-10-09 04:41:28 +03:00
// bind to 127.x.x.x address
// where x.x.x are first three bytes from ident
auto ourIP = GetLoopbackAddressFor ( addr ) ;
boost : : system : : error_code ec ;
sock - > bind ( boost : : asio : : ip : : tcp : : endpoint ( ourIP , 0 ) , ec ) ;
if ( ec )
LogPrint ( eLogError , " I2PTunnel: Can't bind ourIP to " , ourIP . to_string ( ) , " : " , ec . message ( ) ) ;
}
2016-12-25 16:56:47 +03:00
}
2020-05-14 20:59:52 +03:00
# endif
2016-12-25 16:56:47 +03:00
2017-01-13 00:17:11 +03:00
void I2PTunnelConnection : : Connect ( bool isUniqueLocal )
2014-11-24 06:23:17 +03:00
{
2017-08-31 19:08:22 +03:00
if ( m_Socket )
2022-10-09 20:24:43 +03:00
{
2022-10-09 04:41:28 +03:00
I2PTunnelSetSocketOptions ( m_Socket ) ;
2016-12-25 16:56:47 +03:00
# ifdef __linux__
2017-01-13 00:17:11 +03:00
if ( isUniqueLocal & & m_RemoteEndpoint . address ( ) . is_v4 ( ) & &
m_RemoteEndpoint . address ( ) . to_v4 ( ) . to_bytes ( ) [ 0 ] = = 127 )
2016-08-30 00:53:26 +03:00
{
m_Socket - > open ( boost : : asio : : ip : : tcp : : v4 ( ) ) ;
2016-12-25 16:56:47 +03:00
auto ident = m_Stream - > GetRemoteIdentity ( ) - > GetIdentHash ( ) ;
MapToLoopback ( m_Socket , ident ) ;
2016-08-30 00:53:26 +03:00
}
2016-08-29 23:48:37 +03:00
# endif
2018-01-06 07:01:44 +03:00
m_Socket - > async_connect ( m_RemoteEndpoint , std : : bind ( & I2PTunnelConnection : : HandleConnect ,
2014-11-24 06:23:17 +03:00
shared_from_this ( ) , std : : placeholders : : _1 ) ) ;
2016-08-29 23:48:37 +03:00
}
2017-08-31 19:08:22 +03:00
}
2021-02-19 23:15:58 +03:00
void I2PTunnelConnection : : Connect ( const boost : : asio : : ip : : address & localAddress )
{
if ( m_Socket )
{
2021-02-28 17:04:34 +03:00
if ( m_RemoteEndpoint . address ( ) . is_v6 ( ) )
m_Socket - > open ( boost : : asio : : ip : : tcp : : v6 ( ) ) ;
else
m_Socket - > open ( boost : : asio : : ip : : tcp : : v4 ( ) ) ;
2021-02-19 23:15:58 +03:00
boost : : system : : error_code ec ;
m_Socket - > bind ( boost : : asio : : ip : : tcp : : endpoint ( localAddress , 0 ) , ec ) ;
if ( ec )
2021-11-27 23:30:35 +03:00
LogPrint ( eLogError , " I2PTunnel: Can't bind to " , localAddress . to_string ( ) , " : " , ec . message ( ) ) ;
}
2021-02-19 23:15:58 +03:00
Connect ( false ) ;
2021-11-27 23:30:35 +03:00
}
2022-10-09 20:24:43 +03:00
2014-08-14 05:04:23 +04:00
void I2PTunnelConnection : : Terminate ( )
2015-01-07 21:09:59 +03:00
{
if ( Kill ( ) ) return ;
2022-10-09 04:41:28 +03:00
if ( m_SSL ) m_SSL = nullptr ;
2014-10-09 03:44:12 +04:00
if ( m_Stream )
{
m_Stream - > Close ( ) ;
2014-11-23 19:33:58 +03:00
m_Stream . reset ( ) ;
2017-08-31 19:08:22 +03:00
}
2016-12-31 18:50:48 +03:00
boost : : system : : error_code ec ;
m_Socket - > shutdown ( boost : : asio : : ip : : tcp : : socket : : shutdown_send , ec ) ; // avoid RST
2014-08-18 03:14:40 +04:00
m_Socket - > close ( ) ;
2016-12-13 19:01:13 +03:00
2015-01-07 21:09:59 +03:00
Done ( shared_from_this ( ) ) ;
2017-08-31 19:08:22 +03:00
}
2014-08-13 23:25:52 +04:00
void I2PTunnelConnection : : Receive ( )
{
2022-10-09 04:41:28 +03:00
if ( m_SSL )
2022-10-09 16:54:48 +03:00
m_SSL - > async_read_some ( boost : : asio : : buffer ( m_Buffer , I2P_TUNNEL_CONNECTION_BUFFER_SIZE ) ,
2022-10-09 04:41:28 +03:00
std : : bind ( & I2PTunnelConnection : : HandleReceive , shared_from_this ( ) ,
std : : placeholders : : _1 , std : : placeholders : : _2 ) ) ;
2022-10-09 20:24:43 +03:00
else
2022-10-09 04:41:28 +03:00
m_Socket - > async_read_some ( boost : : asio : : buffer ( m_Buffer , I2P_TUNNEL_CONNECTION_BUFFER_SIZE ) ,
std : : bind ( & I2PTunnelConnection : : HandleReceive , shared_from_this ( ) ,
std : : placeholders : : _1 , std : : placeholders : : _2 ) ) ;
2017-08-31 19:08:22 +03:00
}
2022-10-09 04:41:28 +03:00
void I2PTunnelConnection : : HandleReceive ( const boost : : system : : error_code & ecode , std : : size_t bytes_transferred )
2014-08-13 23:25:52 +04:00
{
if ( ecode )
2015-01-03 05:57:37 +03:00
{
2014-10-09 03:44:12 +04:00
if ( ecode ! = boost : : asio : : error : : operation_aborted )
2016-12-13 22:54:48 +03:00
{
2021-11-27 22:53:53 +03:00
LogPrint ( eLogError , " I2PTunnel: Read error: " , ecode . message ( ) ) ;
2014-10-09 03:44:12 +04:00
Terminate ( ) ;
2016-12-13 22:54:48 +03:00
}
2014-08-13 23:25:52 +04:00
}
else
2020-10-06 23:22:40 +03:00
WriteToStream ( m_Buffer , bytes_transferred ) ;
2017-08-31 19:08:22 +03:00
}
2014-08-13 23:25:52 +04:00
2020-10-06 23:22:40 +03:00
void I2PTunnelConnection : : WriteToStream ( const uint8_t * buf , size_t len )
{
if ( m_Stream )
{
auto s = shared_from_this ( ) ;
m_Stream - > AsyncSend ( buf , len ,
[ s ] ( const boost : : system : : error_code & ecode )
{
if ( ! ecode )
s - > Receive ( ) ;
else
s - > Terminate ( ) ;
} ) ;
}
2021-11-27 23:30:35 +03:00
}
2014-08-13 23:25:52 +04:00
void I2PTunnelConnection : : HandleWrite ( const boost : : system : : error_code & ecode )
{
2014-08-18 03:14:40 +04:00
if ( ecode )
{
2021-11-27 22:53:53 +03:00
LogPrint ( eLogError , " I2PTunnel: Write error: " , ecode . message ( ) ) ;
2014-10-09 03:44:12 +04:00
if ( ecode ! = boost : : asio : : error : : operation_aborted )
Terminate ( ) ;
2014-08-18 03:14:40 +04:00
}
else
StreamReceive ( ) ;
2014-08-13 23:25:52 +04:00
}
void I2PTunnelConnection : : StreamReceive ( )
{
if ( m_Stream )
2016-02-15 06:10:56 +03:00
{
if ( m_Stream - > GetStatus ( ) = = i2p : : stream : : eStreamStatusNew | |
2017-10-04 20:15:29 +03:00
m_Stream - > GetStatus ( ) = = i2p : : stream : : eStreamStatusOpen ) // regular
2017-08-31 19:08:22 +03:00
{
2016-02-15 06:10:56 +03:00
m_Stream - > AsyncReceive ( boost : : asio : : buffer ( m_StreamBuffer , I2P_TUNNEL_CONNECTION_BUFFER_SIZE ) ,
std : : bind ( & I2PTunnelConnection : : HandleStreamReceive , shared_from_this ( ) ,
2020-03-01 13:25:50 +03:00
std : : placeholders : : _1 , std : : placeholders : : _2 ) ,
2016-02-15 06:10:56 +03:00
I2P_TUNNEL_CONNECTION_MAX_IDLE ) ;
2017-08-31 19:08:22 +03:00
}
2016-02-15 06:10:56 +03:00
else // closed by peer
{
2021-11-12 19:33:51 +03:00
// get remaining data
2016-02-15 06:10:56 +03:00
auto len = m_Stream - > ReadSome ( m_StreamBuffer , I2P_TUNNEL_CONNECTION_BUFFER_SIZE ) ;
if ( len > 0 ) // still some data
Write ( m_StreamBuffer , len ) ;
else // no more data
2017-08-31 19:08:22 +03:00
Terminate ( ) ;
}
}
}
2014-08-13 05:14:19 +04:00
2014-08-13 23:25:52 +04:00
void I2PTunnelConnection : : HandleStreamReceive ( const boost : : system : : error_code & ecode , std : : size_t bytes_transferred )
{
if ( ecode )
{
2014-10-09 03:44:12 +04:00
if ( ecode ! = boost : : asio : : error : : operation_aborted )
2016-02-15 06:10:56 +03:00
{
2021-11-27 22:53:53 +03:00
LogPrint ( eLogError , " I2PTunnel: Stream read error: " , ecode . message ( ) ) ;
2016-02-15 06:10:56 +03:00
if ( bytes_transferred > 0 )
Write ( m_StreamBuffer , bytes_transferred ) ; // postpone termination
2016-12-23 15:38:41 +03:00
else if ( ecode = = boost : : asio : : error : : timed_out & & m_Stream & & m_Stream - > IsOpen ( ) )
2016-12-17 22:49:51 +03:00
StreamReceive ( ) ;
else
2016-02-15 06:10:56 +03:00
Terminate ( ) ;
2016-10-20 22:20:08 +03:00
}
else
2016-12-17 22:49:51 +03:00
Terminate ( ) ;
2014-08-13 23:25:52 +04:00
}
else
2015-06-02 20:03:22 +03:00
Write ( m_StreamBuffer , bytes_transferred ) ;
}
void I2PTunnelConnection : : Write ( const uint8_t * buf , size_t len )
{
2022-10-09 04:41:28 +03:00
if ( m_SSL )
boost : : asio : : async_write ( * m_SSL , boost : : asio : : buffer ( buf , len ) , boost : : asio : : transfer_all ( ) ,
std : : bind ( & I2PTunnelConnection : : HandleWrite , shared_from_this ( ) , std : : placeholders : : _1 ) ) ;
2022-10-09 20:24:43 +03:00
else
2022-10-09 04:41:28 +03:00
boost : : asio : : async_write ( * m_Socket , boost : : asio : : buffer ( buf , len ) , boost : : asio : : transfer_all ( ) ,
std : : bind ( & I2PTunnelConnection : : HandleWrite , shared_from_this ( ) , std : : placeholders : : _1 ) ) ;
2014-08-13 23:25:52 +04:00
}
2014-08-20 23:03:10 +04:00
void I2PTunnelConnection : : HandleConnect ( const boost : : system : : error_code & ecode )
{
if ( ecode )
{
2021-11-27 22:53:53 +03:00
LogPrint ( eLogError , " I2PTunnel: Connect error: " , ecode . message ( ) ) ;
2014-11-24 06:23:17 +03:00
Terminate ( ) ;
2014-08-20 23:03:10 +04:00
}
else
{
2021-11-27 22:53:53 +03:00
LogPrint ( eLogDebug , " I2PTunnel: Connected " ) ;
2022-10-09 04:41:28 +03:00
if ( m_SSL )
2022-10-09 20:24:43 +03:00
m_SSL - > async_handshake ( boost : : asio : : ssl : : stream_base : : client ,
2022-10-09 04:41:28 +03:00
std : : bind ( & I2PTunnelConnection : : HandleHandshake , shared_from_this ( ) , std : : placeholders : : _1 ) ) ;
2022-10-09 20:24:43 +03:00
else
2022-10-09 04:41:28 +03:00
Established ( ) ;
2014-08-20 23:03:10 +04:00
}
}
2022-10-09 04:41:28 +03:00
void I2PTunnelConnection : : HandleHandshake ( const boost : : system : : error_code & ecode )
{
if ( ecode )
{
LogPrint ( eLogError , " I2PTunnel: Handshake error: " , ecode . message ( ) ) ;
Terminate ( ) ;
}
else
{
LogPrint ( eLogDebug , " I2PTunnel: SSL connected " ) ;
Established ( ) ;
}
}
2022-10-09 20:24:43 +03:00
2022-10-09 04:41:28 +03:00
void I2PTunnelConnection : : Established ( )
{
if ( m_IsQuiet )
StreamReceive ( ) ;
else
{
// send destination first like received from I2P
std : : string dest = m_Stream - > GetRemoteIdentity ( ) - > ToBase64 ( ) ;
dest + = " \n " ;
if ( sizeof ( m_StreamBuffer ) > = dest . size ( ) ) {
memcpy ( m_StreamBuffer , dest . c_str ( ) , dest . size ( ) ) ;
}
HandleStreamReceive ( boost : : system : : error_code ( ) , dest . size ( ) ) ;
}
Receive ( ) ;
2022-10-09 20:24:43 +03:00
}
2017-02-07 05:39:15 +03:00
void I2PClientTunnelConnectionHTTP : : Write ( const uint8_t * buf , size_t len )
{
if ( m_HeaderSent )
I2PTunnelConnection : : Write ( buf , len ) ;
else
{
m_InHeader . clear ( ) ;
m_InHeader . write ( ( const char * ) buf , len ) ;
std : : string line ;
bool endOfHeader = false ;
while ( ! endOfHeader )
{
std : : getline ( m_InHeader , line ) ;
if ( ! m_InHeader . fail ( ) )
{
if ( line = = " \r " ) endOfHeader = true ;
else
2017-08-31 19:08:22 +03:00
{
2017-02-07 05:39:15 +03:00
if ( ! m_ConnectionSent & & ! line . compare ( 0 , 10 , " Connection " ) )
2017-08-31 19:08:22 +03:00
{
2019-02-12 19:20:54 +03:00
/* close connection, if not Connection: (U|u)pgrade (for websocket) */
2020-03-01 13:25:50 +03:00
auto x = line . find ( " pgrade " ) ;
2019-02-12 19:20:54 +03:00
if ( x ! = std : : string : : npos & & std : : tolower ( line [ x - 1 ] ) = = ' u ' )
2019-02-12 01:18:01 +03:00
m_OutHeader < < line < < " \r \n " ;
2019-02-12 19:20:54 +03:00
else
m_OutHeader < < " Connection: close \r \n " ;
2019-02-12 01:18:01 +03:00
2017-02-07 05:39:15 +03:00
m_ConnectionSent = true ;
2017-08-31 19:08:22 +03:00
}
2017-02-07 05:39:15 +03:00
else if ( ! m_ProxyConnectionSent & & ! line . compare ( 0 , 16 , " Proxy-Connection " ) )
2017-08-31 19:08:22 +03:00
{
2017-02-07 05:39:15 +03:00
m_OutHeader < < " Proxy-Connection: close \r \n " ;
m_ProxyConnectionSent = true ;
2017-08-31 19:08:22 +03:00
}
2017-02-07 05:39:15 +03:00
else
2021-09-03 23:25:47 +03:00
m_OutHeader < < line < < " \n " ;
2017-08-31 19:08:22 +03:00
}
2017-02-07 05:39:15 +03:00
}
else
2023-04-19 02:25:15 +03:00
{
// insert incomplete line back
m_InHeader . clear ( ) ;
m_InHeader < < line ;
2017-02-07 05:39:15 +03:00
break ;
2023-06-12 00:32:44 +03:00
}
2017-02-07 05:39:15 +03:00
}
if ( endOfHeader )
{
if ( ! m_ConnectionSent ) m_OutHeader < < " Connection: close \r \n " ;
2020-03-01 13:25:50 +03:00
if ( ! m_ProxyConnectionSent ) m_OutHeader < < " Proxy-Connection: close \r \n " ;
2017-02-07 05:39:15 +03:00
m_OutHeader < < " \r \n " ; // end of header
m_OutHeader < < m_InHeader . str ( ) . substr ( m_InHeader . tellg ( ) ) ; // data right after header
m_InHeader . str ( " " ) ;
m_HeaderSent = true ;
I2PTunnelConnection : : Write ( ( uint8_t * ) m_OutHeader . str ( ) . c_str ( ) , m_OutHeader . str ( ) . length ( ) ) ;
}
2022-08-24 02:06:28 +03:00
else if ( m_OutHeader . tellp ( ) < I2P_TUNNEL_HTTP_MAX_HEADER_SIZE )
2022-08-23 22:11:10 +03:00
StreamReceive ( ) ; // read more header
2022-08-24 02:06:28 +03:00
else
{
2022-10-09 20:24:43 +03:00
LogPrint ( eLogError , " I2PTunnel: HTTP header exceeds max size " , I2P_TUNNEL_HTTP_MAX_HEADER_SIZE ) ;
2022-08-24 02:06:28 +03:00
Terminate ( ) ;
2022-10-09 20:24:43 +03:00
}
2017-02-07 05:39:15 +03:00
}
2017-08-31 19:08:22 +03:00
}
2017-02-07 05:39:15 +03:00
I2PServerTunnelConnectionHTTP : : I2PServerTunnelConnectionHTTP ( I2PService * owner , std : : shared_ptr < i2p : : stream : : Stream > stream ,
2022-10-09 04:41:28 +03:00
const boost : : asio : : ip : : tcp : : endpoint & target , const std : : string & host ,
std : : shared_ptr < boost : : asio : : ssl : : context > sslCtx ) :
I2PTunnelConnection ( owner , stream , target , true , sslCtx ) , m_Host ( host ) ,
2020-10-06 23:22:40 +03:00
m_HeaderSent ( false ) , m_ResponseHeaderSent ( false ) , m_From ( stream - > GetRemoteIdentity ( ) )
2015-06-02 20:03:22 +03:00
{
2022-10-09 04:41:28 +03:00
if ( sslCtx )
2022-10-09 20:24:43 +03:00
SSL_set_tlsext_host_name ( GetSSL ( ) - > native_handle ( ) , host . c_str ( ) ) ;
2015-06-02 20:03:22 +03:00
}
2017-02-07 05:39:15 +03:00
void I2PServerTunnelConnectionHTTP : : Write ( const uint8_t * buf , size_t len )
2015-06-02 23:21:38 +03:00
{
if ( m_HeaderSent )
I2PTunnelConnection : : Write ( buf , len ) ;
else
2017-08-31 19:08:22 +03:00
{
2022-05-29 23:59:15 +03:00
m_InHeader . clear ( ) ;
m_InHeader . write ( ( const char * ) buf , len ) ;
std : : string line ;
bool endOfHeader = false , connection = false ;
while ( ! endOfHeader )
2015-06-03 19:30:15 +03:00
{
2022-05-29 23:59:15 +03:00
std : : getline ( m_InHeader , line ) ;
2023-04-27 04:34:22 +03:00
if ( m_InHeader . fail ( ) ) break ;
if ( ! m_InHeader . eof ( ) )
2022-05-29 23:59:15 +03:00
{
if ( line = = " \r " ) endOfHeader = true ;
else
{
// strip up some headers
static const std : : vector < std : : string > excluded // list of excluded headers
{
2022-10-09 20:24:43 +03:00
" Keep-Alive: " , " X-I2P "
2022-05-29 23:59:15 +03:00
} ;
bool matched = false ;
for ( const auto & it : excluded )
if ( boost : : iequals ( line . substr ( 0 , it . length ( ) ) , it ) )
{
matched = true ;
break ;
}
2023-04-19 02:25:15 +03:00
if ( matched ) continue ;
2022-05-29 23:59:15 +03:00
// replace some headers
if ( ! m_Host . empty ( ) & & boost : : iequals ( line . substr ( 0 , 5 ) , " Host: " ) )
m_OutHeader < < " Host: " < < m_Host < < " \r \n " ; // override host
else if ( boost : : iequals ( line . substr ( 0 , 11 ) , " Connection: " ) )
{
auto x = line . find ( " pgrade " ) ;
if ( x ! = std : : string : : npos & & x & & std : : tolower ( line [ x - 1 ] ) ! = ' u ' ) // upgrade or Upgrade
m_OutHeader < < line < < " \n " ;
else
m_OutHeader < < " Connection: close \r \n " ;
connection = true ;
2022-10-09 20:24:43 +03:00
}
else // forward as is
2022-05-29 23:59:15 +03:00
m_OutHeader < < line < < " \n " ;
}
}
else
2023-06-12 00:32:44 +03:00
{
2023-04-19 02:25:15 +03:00
// insert incomplete line back
m_InHeader . clear ( ) ;
m_InHeader < < line ;
2022-05-29 23:59:15 +03:00
break ;
2023-06-12 00:32:44 +03:00
}
2022-05-29 23:59:15 +03:00
}
if ( endOfHeader )
{
// add Connection if not presented
if ( ! connection )
m_OutHeader < < " Connection: close \r \n " ;
// add X-I2P fields
if ( m_From )
{
m_OutHeader < < X_I2P_DEST_B32 < < " : " < < context . GetAddressBook ( ) . ToAddress ( m_From - > GetIdentHash ( ) ) < < " \r \n " ;
m_OutHeader < < X_I2P_DEST_HASH < < " : " < < m_From - > GetIdentHash ( ) . ToBase64 ( ) < < " \r \n " ;
m_OutHeader < < X_I2P_DEST_B64 < < " : " < < m_From - > ToBase64 ( ) < < " \r \n " ;
}
m_OutHeader < < " \r \n " ; // end of header
m_OutHeader < < m_InHeader . str ( ) . substr ( m_InHeader . tellg ( ) ) ; // data right after header
m_InHeader . str ( " " ) ;
m_From = nullptr ;
m_HeaderSent = true ;
I2PTunnelConnection : : Write ( ( uint8_t * ) m_OutHeader . str ( ) . c_str ( ) , m_OutHeader . str ( ) . length ( ) ) ;
}
2022-08-24 02:06:28 +03:00
else if ( m_OutHeader . tellp ( ) < I2P_TUNNEL_HTTP_MAX_HEADER_SIZE )
StreamReceive ( ) ; // read more header
2022-08-23 22:11:10 +03:00
else
2022-08-24 02:06:28 +03:00
{
2022-10-09 20:24:43 +03:00
LogPrint ( eLogError , " I2PTunnel: HTTP header exceeds max size " , I2P_TUNNEL_HTTP_MAX_HEADER_SIZE ) ;
2022-08-24 02:06:28 +03:00
Terminate ( ) ;
}
2017-08-31 19:08:22 +03:00
}
2015-06-02 23:21:38 +03:00
}
2020-10-06 23:22:40 +03:00
void I2PServerTunnelConnectionHTTP : : WriteToStream ( const uint8_t * buf , size_t len )
{
if ( m_ResponseHeaderSent )
I2PTunnelConnection : : WriteToStream ( buf , len ) ;
else
{
m_InHeader . clear ( ) ;
if ( m_InHeader . str ( ) . empty ( ) ) m_OutHeader . str ( " " ) ; // start of response
m_InHeader . write ( ( const char * ) buf , len ) ;
std : : string line ;
bool endOfHeader = false ;
while ( ! endOfHeader )
{
std : : getline ( m_InHeader , line ) ;
2023-04-27 04:34:22 +03:00
if ( m_InHeader . fail ( ) ) break ;
if ( ! m_InHeader . eof ( ) )
2020-10-06 23:22:40 +03:00
{
if ( line = = " \r " ) endOfHeader = true ;
else
{
2020-10-07 02:24:03 +03:00
static const std : : vector < std : : string > excluded // list of excluded headers
{
" Server: " , " Date: " , " X-Runtime: " , " X-Powered-By: " , " Proxy "
} ;
bool matched = false ;
for ( const auto & it : excluded )
2021-11-27 23:30:35 +03:00
if ( ! line . compare ( 0 , it . length ( ) , it ) )
2020-10-07 02:24:03 +03:00
{
matched = true ;
2021-11-27 23:30:35 +03:00
break ;
}
2020-10-07 02:24:03 +03:00
if ( ! matched )
m_OutHeader < < line < < " \n " ;
2020-10-06 23:22:40 +03:00
}
}
else
2023-06-12 00:32:44 +03:00
{
2023-04-19 02:25:15 +03:00
// insert incomplete line back
m_InHeader . clear ( ) ;
m_InHeader < < line ;
2020-10-06 23:22:40 +03:00
break ;
2023-06-12 00:32:44 +03:00
}
2020-10-06 23:22:40 +03:00
}
if ( endOfHeader )
{
m_OutHeader < < " \r \n " ; // end of header
m_OutHeader < < m_InHeader . str ( ) . substr ( m_InHeader . tellg ( ) ) ; // data right after header
m_InHeader . str ( " " ) ;
m_ResponseHeaderSent = true ;
I2PTunnelConnection : : WriteToStream ( ( uint8_t * ) m_OutHeader . str ( ) . c_str ( ) , m_OutHeader . str ( ) . length ( ) ) ;
m_OutHeader . str ( " " ) ;
2021-11-27 23:30:35 +03:00
}
2020-10-06 23:22:40 +03:00
else
Receive ( ) ;
2021-11-27 23:30:35 +03:00
}
2020-10-06 23:22:40 +03:00
}
2021-11-27 23:30:35 +03:00
2016-02-22 22:33:21 +03:00
I2PTunnelConnectionIRC : : I2PTunnelConnectionIRC ( I2PService * owner , std : : shared_ptr < i2p : : stream : : Stream > stream ,
2022-10-10 18:02:19 +03:00
const boost : : asio : : ip : : tcp : : endpoint & target , const std : : string & webircpass ,
std : : shared_ptr < boost : : asio : : ssl : : context > sslCtx ) :
I2PTunnelConnection ( owner , stream , target , true , sslCtx ) , m_From ( stream - > GetRemoteIdentity ( ) ) ,
2017-10-04 20:15:29 +03:00
m_NeedsWebIrc ( webircpass . length ( ) ? true : false ) , m_WebircPass ( webircpass )
{
}
void I2PTunnelConnectionIRC : : Write ( const uint8_t * buf , size_t len )
{
2016-10-31 16:46:59 +03:00
m_OutPacket . str ( " " ) ;
2017-10-04 20:15:29 +03:00
if ( m_NeedsWebIrc )
2016-10-31 16:46:59 +03:00
{
2017-10-04 20:15:29 +03:00
m_NeedsWebIrc = false ;
2022-10-09 20:24:43 +03:00
m_OutPacket < < " WEBIRC " < < m_WebircPass < < " cgiirc " < < context . GetAddressBook ( ) . ToAddress ( m_From - > GetIdentHash ( ) )
2022-10-09 04:41:28 +03:00
< < " " < < GetSocket ( ) - > local_endpoint ( ) . address ( ) < < std : : endl ;
2017-10-04 20:15:29 +03:00
}
2016-03-04 09:37:38 +03:00
2017-10-04 20:15:29 +03:00
m_InPacket . clear ( ) ;
m_InPacket . write ( ( const char * ) buf , len ) ;
2017-08-31 19:08:22 +03:00
2017-10-04 20:15:29 +03:00
while ( ! m_InPacket . eof ( ) & & ! m_InPacket . fail ( ) )
{
2016-10-31 16:46:59 +03:00
std : : string line ;
2017-10-04 20:15:29 +03:00
std : : getline ( m_InPacket , line ) ;
if ( line . length ( ) = = 0 & & m_InPacket . eof ( ) )
m_InPacket . str ( " " ) ;
auto pos = line . find ( " USER " ) ;
if ( ! pos ) // start of line
{
pos = line . find ( " " ) ;
pos + + ;
pos = line . find ( " " , pos ) ;
pos + + ;
auto nextpos = line . find ( " " , pos ) ;
m_OutPacket < < line . substr ( 0 , pos ) ;
m_OutPacket < < context . GetAddressBook ( ) . ToAddress ( m_From - > GetIdentHash ( ) ) ;
m_OutPacket < < line . substr ( nextpos ) < < ' \n ' ;
}
2016-10-31 16:46:59 +03:00
else
2017-10-04 20:15:29 +03:00
m_OutPacket < < line < < ' \n ' ;
}
I2PTunnelConnection : : Write ( ( uint8_t * ) m_OutPacket . str ( ) . c_str ( ) , m_OutPacket . str ( ) . length ( ) ) ;
}
2016-02-22 22:33:21 +03:00
2019-05-17 11:04:44 +03:00
/* This handler tries to establish a connection with the desired server and dies if it fails to do so */
2015-01-08 05:28:54 +03:00
class I2PClientTunnelHandler : public I2PServiceHandler , public std : : enable_shared_from_this < I2PClientTunnelHandler >
{
public :
2019-03-28 16:57:34 +03:00
I2PClientTunnelHandler ( I2PClientTunnel * parent , std : : shared_ptr < const Address > address ,
2023-06-12 05:10:32 +03:00
uint16_t destinationPort , std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > socket ) :
2019-03-28 16:57:34 +03:00
I2PServiceHandler ( parent ) , m_Address ( address ) ,
2015-03-13 20:29:27 +03:00
m_DestinationPort ( destinationPort ) , m_Socket ( socket ) { } ;
2015-01-08 05:28:54 +03:00
void Handle ( ) ;
void Terminate ( ) ;
private :
void HandleStreamRequestComplete ( std : : shared_ptr < i2p : : stream : : Stream > stream ) ;
2019-03-28 16:57:34 +03:00
std : : shared_ptr < const Address > m_Address ;
2023-06-12 05:10:32 +03:00
uint16_t m_DestinationPort ;
2015-04-06 21:41:07 +03:00
std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > m_Socket ;
2015-01-08 05:28:54 +03:00
} ;
void I2PClientTunnelHandler : : Handle ( )
{
2017-08-31 19:08:22 +03:00
GetOwner ( ) - > CreateStream (
std : : bind ( & I2PClientTunnelHandler : : HandleStreamRequestComplete , shared_from_this ( ) , std : : placeholders : : _1 ) ,
2019-03-28 16:57:34 +03:00
m_Address , m_DestinationPort ) ;
2015-01-08 05:28:54 +03:00
}
void I2PClientTunnelHandler : : HandleStreamRequestComplete ( std : : shared_ptr < i2p : : stream : : Stream > stream )
{
if ( stream )
{
if ( Kill ( ) ) return ;
2021-11-27 22:53:53 +03:00
LogPrint ( eLogDebug , " I2PTunnel: New connection " ) ;
2015-01-08 05:28:54 +03:00
auto connection = std : : make_shared < I2PTunnelConnection > ( GetOwner ( ) , m_Socket , stream ) ;
GetOwner ( ) - > AddHandler ( connection ) ;
connection - > I2PConnect ( ) ;
Done ( shared_from_this ( ) ) ;
}
else
{
2015-12-18 15:12:46 +03:00
LogPrint ( eLogError , " I2PTunnel: Client Tunnel Issue when creating the stream, check the previous warnings for more info. " ) ;
2015-01-08 05:28:54 +03:00
Terminate ( ) ;
}
}
void I2PClientTunnelHandler : : Terminate ( )
{
if ( Kill ( ) ) return ;
if ( m_Socket )
{
m_Socket - > close ( ) ;
m_Socket = nullptr ;
}
Done ( shared_from_this ( ) ) ;
}
2017-08-31 19:08:22 +03:00
I2PClientTunnel : : I2PClientTunnel ( const std : : string & name , const std : : string & destination ,
2023-06-12 05:10:32 +03:00
const std : : string & address , uint16_t port , std : : shared_ptr < ClientDestination > localDestination , uint16_t destinationPort ) :
2017-08-31 19:08:22 +03:00
TCPIPAcceptor ( address , port , localDestination ) , m_Name ( name ) , m_Destination ( destination ) ,
2021-09-26 23:25:12 +03:00
m_DestinationPort ( destinationPort ) , m_KeepAliveInterval ( 0 )
2016-01-14 04:21:53 +03:00
{
2017-08-31 19:08:22 +03:00
}
2014-08-13 05:14:19 +04:00
2014-08-13 23:25:52 +04:00
void I2PClientTunnel : : Start ( )
{
2015-01-08 15:39:35 +03:00
TCPIPAcceptor : : Start ( ) ;
2019-03-28 16:57:34 +03:00
GetAddress ( ) ;
2021-09-26 23:25:12 +03:00
if ( m_KeepAliveInterval )
ScheduleKeepAliveTimer ( ) ;
2014-08-13 23:25:52 +04:00
}
void I2PClientTunnel : : Stop ( )
{
2015-01-08 05:49:35 +03:00
TCPIPAcceptor : : Stop ( ) ;
2019-03-28 16:57:34 +03:00
m_Address = nullptr ;
2021-09-26 23:25:12 +03:00
if ( m_KeepAliveTimer ) m_KeepAliveTimer - > cancel ( ) ;
2014-08-13 23:25:52 +04:00
}
2021-09-26 23:25:12 +03:00
void I2PClientTunnel : : SetKeepAliveInterval ( uint32_t keepAliveInterval )
{
m_KeepAliveInterval = keepAliveInterval ;
if ( m_KeepAliveInterval )
m_KeepAliveTimer . reset ( new boost : : asio : : deadline_timer ( GetLocalDestination ( ) - > GetService ( ) ) ) ;
2021-11-27 23:30:35 +03:00
}
2015-01-03 04:17:01 +03:00
/* HACK: maybe we should create a caching IdentHash provider in AddressBook */
2020-03-01 13:25:50 +03:00
std : : shared_ptr < const Address > I2PClientTunnel : : GetAddress ( )
2015-01-03 04:17:01 +03:00
{
2019-03-28 16:57:34 +03:00
if ( ! m_Address )
2015-01-03 04:17:01 +03:00
{
2019-03-28 16:57:34 +03:00
m_Address = i2p : : client : : context . GetAddressBook ( ) . GetAddress ( m_Destination ) ;
if ( ! m_Address )
2015-12-18 15:12:46 +03:00
LogPrint ( eLogWarning , " I2PTunnel: Remote destination " , m_Destination , " not found " ) ;
2015-01-03 04:17:01 +03:00
}
2019-03-28 16:57:34 +03:00
return m_Address ;
2015-01-03 04:17:01 +03:00
}
2015-04-06 21:41:07 +03:00
std : : shared_ptr < I2PServiceHandler > I2PClientTunnel : : CreateHandler ( std : : shared_ptr < boost : : asio : : ip : : tcp : : socket > socket )
2014-08-13 05:14:19 +04:00
{
2019-03-28 16:57:34 +03:00
auto address = GetAddress ( ) ;
if ( address )
2020-03-01 13:25:50 +03:00
return std : : make_shared < I2PClientTunnelHandler > ( this , address , m_DestinationPort , socket ) ;
2014-08-13 05:14:19 +04:00
else
2015-01-08 05:49:35 +03:00
return nullptr ;
2014-10-15 20:07:06 +04:00
}
2021-09-26 23:25:12 +03:00
void I2PClientTunnel : : ScheduleKeepAliveTimer ( )
{
if ( m_KeepAliveTimer )
{
2022-05-09 10:59:25 +03:00
m_KeepAliveTimer - > expires_from_now ( boost : : posix_time : : seconds ( m_KeepAliveInterval ) ) ;
2021-09-26 23:25:12 +03:00
m_KeepAliveTimer - > async_wait ( std : : bind ( & I2PClientTunnel : : HandleKeepAliveTimer ,
this , std : : placeholders : : _1 ) ) ;
2021-11-27 23:30:35 +03:00
}
}
2021-09-26 23:25:12 +03:00
void I2PClientTunnel : : HandleKeepAliveTimer ( const boost : : system : : error_code & ecode )
{
if ( ecode ! = boost : : asio : : error : : operation_aborted )
{
if ( m_Address & & m_Address - > IsValid ( ) )
2021-11-27 23:30:35 +03:00
{
2021-09-26 23:25:12 +03:00
if ( m_Address - > IsIdentHash ( ) )
GetLocalDestination ( ) - > SendPing ( m_Address - > identHash ) ;
else
GetLocalDestination ( ) - > SendPing ( m_Address - > blindedPublicKey ) ;
2021-11-27 23:30:35 +03:00
}
2021-09-26 23:25:12 +03:00
ScheduleKeepAliveTimer ( ) ;
}
2021-11-27 23:30:35 +03:00
}
2017-08-31 19:08:22 +03:00
I2PServerTunnel : : I2PServerTunnel ( const std : : string & name , const std : : string & address ,
2023-06-12 05:10:32 +03:00
uint16_t port , std : : shared_ptr < ClientDestination > localDestination , uint16_t inport , bool gzip ) :
2017-01-13 00:17:11 +03:00
I2PService ( localDestination ) , m_IsUniqueLocal ( true ) , m_Name ( name ) , m_Address ( address ) , m_Port ( port ) , m_IsAccessList ( false )
2014-08-20 23:03:10 +04:00
{
2023-06-12 05:12:07 +03:00
m_PortDestination = localDestination - > GetStreamingDestination ( inport ) ;
2023-06-12 00:32:44 +03:00
if ( ! m_PortDestination ) // default destination
2023-06-12 05:12:07 +03:00
m_PortDestination = localDestination - > CreateStreamingDestination ( inport , gzip ) ;
2014-08-20 23:03:10 +04:00
}
2016-07-28 18:16:29 +03:00
2014-08-20 23:03:10 +04:00
void I2PServerTunnel : : Start ( )
{
2017-08-31 19:08:22 +03:00
m_Endpoint . port ( m_Port ) ;
2015-06-02 20:18:41 +03:00
boost : : system : : error_code ec ;
auto addr = boost : : asio : : ip : : address : : from_string ( m_Address , ec ) ;
2017-08-31 19:08:22 +03:00
if ( ! ec )
2015-06-02 20:18:41 +03:00
{
m_Endpoint . address ( addr ) ;
Accept ( ) ;
}
else
{
auto resolver = std : : make_shared < boost : : asio : : ip : : tcp : : resolver > ( GetService ( ) ) ;
2017-08-31 19:08:22 +03:00
resolver - > async_resolve ( boost : : asio : : ip : : tcp : : resolver : : query ( m_Address , " " ) ,
std : : bind ( & I2PServerTunnel : : HandleResolve , this ,
2015-06-02 20:18:41 +03:00
std : : placeholders : : _1 , std : : placeholders : : _2 , resolver ) ) ;
2017-08-31 19:08:22 +03:00
}
2014-08-20 23:03:10 +04:00
}
void I2PServerTunnel : : Stop ( )
{
2022-08-16 21:06:13 +03:00
if ( m_PortDestination )
m_PortDestination - > ResetAcceptor ( ) ;
auto localDestination = GetLocalDestination ( ) ;
if ( localDestination )
localDestination - > StopAcceptingStreams ( ) ;
2022-10-09 20:24:43 +03:00
2015-01-07 21:09:59 +03:00
ClearHandlers ( ) ;
2017-08-31 19:08:22 +03:00
}
2014-08-20 23:03:10 +04:00
2017-08-31 19:08:22 +03:00
void I2PServerTunnel : : HandleResolve ( const boost : : system : : error_code & ecode , boost : : asio : : ip : : tcp : : resolver : : iterator it ,
2016-10-10 15:59:48 +03:00
std : : shared_ptr < boost : : asio : : ip : : tcp : : resolver > resolver )
2017-08-31 19:08:22 +03:00
{
2015-06-02 20:18:41 +03:00
if ( ! ecode )
2017-08-31 19:08:22 +03:00
{
2021-08-13 20:31:04 +03:00
bool found = false ;
boost : : asio : : ip : : tcp : : endpoint ep ;
2021-08-13 20:54:23 +03:00
if ( m_LocalAddress )
2021-11-27 23:30:35 +03:00
{
2021-08-13 20:54:23 +03:00
boost : : asio : : ip : : tcp : : resolver : : iterator end ;
while ( it ! = end )
2021-11-27 23:30:35 +03:00
{
2021-08-13 20:54:23 +03:00
ep = * it ;
if ( ! ep . address ( ) . is_unspecified ( ) )
2021-08-13 20:31:04 +03:00
{
2021-08-13 20:54:23 +03:00
if ( ep . address ( ) . is_v4 ( ) )
2021-11-27 23:30:35 +03:00
{
if ( m_LocalAddress - > is_v4 ( ) ) found = true ;
2021-08-13 20:54:23 +03:00
}
else if ( ep . address ( ) . is_v6 ( ) )
2021-08-13 20:31:04 +03:00
{
2021-08-13 20:54:23 +03:00
if ( i2p : : util : : net : : IsYggdrasilAddress ( ep . address ( ) ) )
{
if ( i2p : : util : : net : : IsYggdrasilAddress ( * m_LocalAddress ) )
found = true ;
2021-11-27 23:30:35 +03:00
}
else if ( m_LocalAddress - > is_v6 ( ) )
2021-08-13 20:31:04 +03:00
found = true ;
2021-08-13 20:54:23 +03:00
}
2021-11-27 23:30:35 +03:00
}
2021-08-13 20:54:23 +03:00
if ( found ) break ;
it + + ;
}
2021-11-27 23:30:35 +03:00
}
2021-08-13 20:54:23 +03:00
else
{
found = true ;
ep = * it ; // first available
2021-11-27 23:30:35 +03:00
}
2021-08-13 20:31:04 +03:00
if ( ! found )
{
2021-08-13 20:54:23 +03:00
LogPrint ( eLogError , " I2PTunnel: Unable to resolve to compatible address " ) ;
2021-08-13 20:31:04 +03:00
return ;
2021-11-27 23:30:35 +03:00
}
2021-08-13 20:31:04 +03:00
auto addr = ep . address ( ) ;
2021-11-27 22:53:53 +03:00
LogPrint ( eLogInfo , " I2PTunnel: Server tunnel " , ( * it ) . host_name ( ) , " has been resolved to " , addr ) ;
2015-06-02 20:18:41 +03:00
m_Endpoint . address ( addr ) ;
2017-08-31 19:08:22 +03:00
Accept ( ) ;
}
2015-06-02 20:18:41 +03:00
else
2015-12-18 15:12:46 +03:00
LogPrint ( eLogError , " I2PTunnel: Unable to resolve server tunnel address: " , ecode . message ( ) ) ;
2015-06-02 20:18:41 +03:00
}
2015-03-16 21:52:42 +03:00
void I2PServerTunnel : : SetAccessList ( const std : : set < i2p : : data : : IdentHash > & accessList )
{
m_AccessList = accessList ;
2017-08-31 19:08:22 +03:00
m_IsAccessList = true ;
2015-03-16 21:52:42 +03:00
}
2021-02-19 23:15:58 +03:00
void I2PServerTunnel : : SetLocalAddress ( const std : : string & localAddress )
{
boost : : system : : error_code ec ;
auto addr = boost : : asio : : ip : : address : : from_string ( localAddress , ec ) ;
if ( ! ec )
m_LocalAddress . reset ( new boost : : asio : : ip : : address ( addr ) ) ;
else
2021-11-27 22:53:53 +03:00
LogPrint ( eLogError , " I2PTunnel: Can't set local address " , localAddress ) ;
2021-11-27 23:30:35 +03:00
}
2022-10-09 04:41:28 +03:00
void I2PServerTunnel : : SetSSL ( bool ssl )
{
if ( ssl )
2022-10-09 20:24:43 +03:00
{
2022-10-09 04:41:28 +03:00
m_SSLCtx = std : : make_shared < boost : : asio : : ssl : : context > ( boost : : asio : : ssl : : context : : sslv23 ) ;
m_SSLCtx - > set_verify_mode ( boost : : asio : : ssl : : context : : verify_none ) ;
2022-10-09 20:24:43 +03:00
}
2022-10-09 04:41:28 +03:00
else
m_SSLCtx = nullptr ;
2022-10-09 20:24:43 +03:00
}
2014-08-20 23:03:10 +04:00
void I2PServerTunnel : : Accept ( )
{
2015-03-03 22:52:16 +03:00
if ( m_PortDestination )
m_PortDestination - > SetAcceptor ( std : : bind ( & I2PServerTunnel : : HandleAccept , this , std : : placeholders : : _1 ) ) ;
2017-08-31 19:08:22 +03:00
auto localDestination = GetLocalDestination ( ) ;
2014-10-01 18:58:28 +04:00
if ( localDestination )
2015-03-03 22:52:16 +03:00
{
if ( ! localDestination - > IsAcceptingStreams ( ) ) // set it as default if not set yet
localDestination - > AcceptStreams ( std : : bind ( & I2PServerTunnel : : HandleAccept , this , std : : placeholders : : _1 ) ) ;
}
2014-10-01 18:58:28 +04:00
else
2015-12-18 15:12:46 +03:00
LogPrint ( eLogError , " I2PTunnel: Local destination not set for server tunnel " ) ;
2014-08-20 23:03:10 +04:00
}
2014-11-23 19:33:58 +03:00
void I2PServerTunnel : : HandleAccept ( std : : shared_ptr < i2p : : stream : : Stream > stream )
2014-08-20 23:03:10 +04:00
{
if ( stream )
2017-08-31 19:08:22 +03:00
{
2015-03-16 21:52:42 +03:00
if ( m_IsAccessList )
{
2015-11-03 17:15:49 +03:00
if ( ! m_AccessList . count ( stream - > GetRemoteIdentity ( ) - > GetIdentHash ( ) ) )
2015-03-16 21:52:42 +03:00
{
2015-12-18 15:12:46 +03:00
LogPrint ( eLogWarning , " I2PTunnel: Address " , stream - > GetRemoteIdentity ( ) - > GetIdentHash ( ) . ToBase32 ( ) , " is not in white list. Incoming connection dropped " ) ;
2015-03-16 21:52:42 +03:00
stream - > Close ( ) ;
return ;
}
}
2017-01-13 21:47:51 +03:00
// new connection
auto conn = CreateI2PConnection ( stream ) ;
AddHandler ( conn ) ;
2021-02-19 23:15:58 +03:00
if ( m_LocalAddress )
conn - > Connect ( * m_LocalAddress ) ;
2021-11-27 23:30:35 +03:00
else
2021-02-19 23:15:58 +03:00
conn - > Connect ( m_IsUniqueLocal ) ;
2017-08-31 19:08:22 +03:00
}
2014-08-20 23:03:10 +04:00
}
2015-05-20 23:00:09 +03:00
2017-01-13 21:47:51 +03:00
std : : shared_ptr < I2PTunnelConnection > I2PServerTunnel : : CreateI2PConnection ( std : : shared_ptr < i2p : : stream : : Stream > stream )
2015-06-02 20:03:22 +03:00
{
2022-10-09 04:41:28 +03:00
return std : : make_shared < I2PTunnelConnection > ( this , stream , GetEndpoint ( ) , true , m_SSLCtx ) ;
2017-08-31 19:08:22 +03:00
2015-06-02 20:03:22 +03:00
}
2017-08-31 19:08:22 +03:00
I2PServerTunnelHTTP : : I2PServerTunnelHTTP ( const std : : string & name , const std : : string & address ,
2023-06-12 05:10:32 +03:00
uint16_t port , std : : shared_ptr < ClientDestination > localDestination ,
const std : : string & host , uint16_t inport , bool gzip ) :
2017-08-31 19:08:22 +03:00
I2PServerTunnel ( name , address , port , localDestination , inport , gzip ) ,
2016-11-02 17:12:54 +03:00
m_Host ( host )
2015-06-02 20:03:22 +03:00
{
}
2017-01-13 21:47:51 +03:00
std : : shared_ptr < I2PTunnelConnection > I2PServerTunnelHTTP : : CreateI2PConnection ( std : : shared_ptr < i2p : : stream : : Stream > stream )
2015-05-20 23:00:09 +03:00
{
2022-10-09 04:41:28 +03:00
return std : : make_shared < I2PServerTunnelConnectionHTTP > ( this , stream , GetEndpoint ( ) , m_Host , GetSSLCtx ( ) ) ;
2015-05-20 23:00:09 +03:00
}
2016-02-22 22:33:21 +03:00
2017-10-04 20:15:29 +03:00
I2PServerTunnelIRC : : I2PServerTunnelIRC ( const std : : string & name , const std : : string & address ,
2023-06-12 05:10:32 +03:00
uint16_t port , std : : shared_ptr < ClientDestination > localDestination ,
const std : : string & webircpass , uint16_t inport , bool gzip ) :
2017-10-04 20:15:29 +03:00
I2PServerTunnel ( name , address , port , localDestination , inport , gzip ) ,
m_WebircPass ( webircpass )
{
}
std : : shared_ptr < I2PTunnelConnection > I2PServerTunnelIRC : : CreateI2PConnection ( std : : shared_ptr < i2p : : stream : : Stream > stream )
{
2022-10-10 18:02:19 +03:00
return std : : make_shared < I2PTunnelConnectionIRC > ( this , stream , GetEndpoint ( ) , m_WebircPass , GetSSLCtx ( ) ) ;
2017-10-04 20:15:29 +03:00
}
2016-09-03 16:38:53 +03:00
}
}
2022-05-29 23:59:15 +03:00