@@ -475,18 +475,27 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
475
475
proxyType proxy;
476
476
CAddress addr_bind;
477
477
assert (!addr_bind.IsValid ());
478
+ std::unique_ptr<i2p::sam::Session> i2p_transient_session;
478
479
479
480
if (addrConnect.IsValid ()) {
481
+ const bool use_proxy{GetProxy (addrConnect.GetNetwork (), proxy)};
480
482
bool proxyConnectionFailed = false ;
481
483
482
- if (addrConnect.GetNetwork () == NET_I2P && m_i2p_sam_session. get () != nullptr ) {
484
+ if (addrConnect.GetNetwork () == NET_I2P && use_proxy ) {
483
485
i2p::Connection conn;
484
- if (m_i2p_sam_session->Connect (addrConnect, conn, proxyConnectionFailed)) {
485
- connected = true ;
486
+
487
+ if (m_i2p_sam_session) {
488
+ connected = m_i2p_sam_session->Connect (addrConnect, conn, proxyConnectionFailed);
489
+ } else {
490
+ i2p_transient_session = std::make_unique<i2p::sam::Session>(proxy.proxy , &interruptNet);
491
+ connected = i2p_transient_session->Connect (addrConnect, conn, proxyConnectionFailed);
492
+ }
493
+
494
+ if (connected) {
486
495
sock = std::move (conn.sock );
487
496
addr_bind = CAddress{conn.me , NODE_NONE};
488
497
}
489
- } else if (GetProxy (addrConnect. GetNetwork (), proxy) ) {
498
+ } else if (use_proxy ) {
490
499
sock = CreateSock (proxy.proxy );
491
500
if (!sock) {
492
501
return nullptr ;
@@ -528,7 +537,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
528
537
if (!addr_bind.IsValid ()) {
529
538
addr_bind = GetBindAddress (sock->Get ());
530
539
}
531
- CNode* pnode = new CNode (id, nLocalServices, sock->Release (), addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, addr_bind, pszDest ? pszDest : " " , conn_type, /* inbound_onion */ false );
540
+ CNode* pnode = new CNode (id, nLocalServices, sock->Release (), addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, addr_bind, pszDest ? pszDest : " " , conn_type, /* inbound_onion */ false , std::move (i2p_transient_session) );
532
541
pnode->AddRef ();
533
542
statsClient.inc (" peers.connect" , 1 .0f );
534
543
@@ -571,6 +580,8 @@ void CNode::CloseSocketDisconnect(CConnman* connman)
571
580
572
581
LogPrint (BCLog::NET, " disconnecting peer=%d\n " , id);
573
582
CloseSocket (hSocket);
583
+ m_i2p_sam_session.reset ();
584
+
574
585
statsClient.inc (" peers.disconnect" , 1 .0f );
575
586
}
576
587
@@ -3342,7 +3353,7 @@ bool CConnman::Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_met
3342
3353
}
3343
3354
3344
3355
proxyType i2p_sam;
3345
- if (GetProxy (NET_I2P, i2p_sam)) {
3356
+ if (GetProxy (NET_I2P, i2p_sam) && connOptions. m_i2p_accept_incoming ) {
3346
3357
m_i2p_sam_session = std::make_unique<i2p::sam::Session>(GetDataDir () / " i2p_private_key" ,
3347
3358
i2p_sam.proxy , &interruptNet);
3348
3359
}
@@ -3444,7 +3455,7 @@ bool CConnman::Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_met
3444
3455
// Process messages
3445
3456
threadMessageHandler = std::thread (&util::TraceThread, " msghand" , [this ] { ThreadMessageHandler (); });
3446
3457
3447
- if (connOptions. m_i2p_accept_incoming && m_i2p_sam_session. get () != nullptr ) {
3458
+ if (m_i2p_sam_session) {
3448
3459
threadI2PAcceptIncoming =
3449
3460
std::thread (&util::TraceThread, " i2paccept" , [this , &mn_sync] { ThreadI2PAcceptIncoming (mn_sync); });
3450
3461
}
@@ -4012,17 +4023,18 @@ ServiceFlags CConnman::GetLocalServices() const
4012
4023
4013
4024
unsigned int CConnman::GetReceiveFloodSize () const { return nReceiveFloodSize; }
4014
4025
4015
- CNode::CNode (NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion)
4016
- : nTimeConnected( GetTimeSeconds()) ,
4017
- addr( addrIn) ,
4018
- addrBind( addrBindIn) ,
4026
+ CNode::CNode (NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion, std::unique_ptr<i2p::sam::Session>&& i2p_sam_session )
4027
+ : nTimeConnected{ GetTimeSeconds ()} ,
4028
+ addr{ addrIn} ,
4029
+ addrBind{ addrBindIn} ,
4019
4030
m_addr_name{addrNameIn.empty () ? addr.ToStringIPPort () : addrNameIn},
4020
- m_inbound_onion (inbound_onion),
4021
- nKeyedNetGroup(nKeyedNetGroupIn),
4022
- id(idIn),
4023
- nLocalHostNonce(nLocalHostNonceIn),
4024
- m_conn_type(conn_type_in),
4025
- nLocalServices(nLocalServicesIn)
4031
+ m_inbound_onion{inbound_onion},
4032
+ nKeyedNetGroup{nKeyedNetGroupIn},
4033
+ id{idIn},
4034
+ nLocalHostNonce{nLocalHostNonceIn},
4035
+ m_conn_type{conn_type_in},
4036
+ nLocalServices{nLocalServicesIn},
4037
+ m_i2p_sam_session{std::move (i2p_sam_session)}
4026
4038
{
4027
4039
if (inbound_onion) assert (conn_type_in == ConnectionType::INBOUND);
4028
4040
hSocket = hSocketIn;
0 commit comments