Skip to content

Commit ae1e97c

Browse files
committed
net: use transient I2P session for outbound if -i2pacceptincoming=0
If not accepting I2P connections, then do not create `CConnman::m_i2p_sam_session`. When opening a new outbound I2P connection either use `CConnman::m_i2p_sam_session` like before or create a temporary one and store it in `CNode` for destruction later.
1 parent a1580a0 commit ae1e97c

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/net.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,18 +485,27 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
485485
Proxy proxy;
486486
CAddress addr_bind;
487487
assert(!addr_bind.IsValid());
488+
std::unique_ptr<i2p::sam::Session> i2p_transient_session;
488489

489490
if (addrConnect.IsValid()) {
491+
const bool use_proxy{GetProxy(addrConnect.GetNetwork(), proxy)};
490492
bool proxyConnectionFailed = false;
491493

492-
if (addrConnect.GetNetwork() == NET_I2P && m_i2p_sam_session.get() != nullptr) {
494+
if (addrConnect.GetNetwork() == NET_I2P && use_proxy) {
493495
i2p::Connection conn;
494-
if (m_i2p_sam_session->Connect(addrConnect, conn, proxyConnectionFailed)) {
495-
connected = true;
496+
497+
if (m_i2p_sam_session) {
498+
connected = m_i2p_sam_session->Connect(addrConnect, conn, proxyConnectionFailed);
499+
} else {
500+
i2p_transient_session = std::make_unique<i2p::sam::Session>(proxy.proxy, &interruptNet);
501+
connected = i2p_transient_session->Connect(addrConnect, conn, proxyConnectionFailed);
502+
}
503+
504+
if (connected) {
496505
sock = std::move(conn.sock);
497506
addr_bind = CAddress{conn.me, NODE_NONE};
498507
}
499-
} else if (GetProxy(addrConnect.GetNetwork(), proxy)) {
508+
} else if (use_proxy) {
500509
sock = CreateSock(proxy.proxy);
501510
if (!sock) {
502511
return nullptr;
@@ -547,7 +556,8 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
547556
addr_bind,
548557
pszDest ? pszDest : "",
549558
conn_type,
550-
/*inbound_onion=*/false);
559+
/*inbound_onion=*/false,
560+
std::move(i2p_transient_session));
551561
pnode->AddRef();
552562

553563
// We're making a new connection, harvest entropy from the time (and our peer count)
@@ -2260,7 +2270,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
22602270
}
22612271

22622272
Proxy i2p_sam;
2263-
if (GetProxy(NET_I2P, i2p_sam)) {
2273+
if (GetProxy(NET_I2P, i2p_sam) && connOptions.m_i2p_accept_incoming) {
22642274
m_i2p_sam_session = std::make_unique<i2p::sam::Session>(gArgs.GetDataDirNet() / "i2p_private_key",
22652275
i2p_sam.proxy, &interruptNet);
22662276
}
@@ -2334,7 +2344,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
23342344
// Process messages
23352345
threadMessageHandler = std::thread(&util::TraceThread, "msghand", [this] { ThreadMessageHandler(); });
23362346

2337-
if (connOptions.m_i2p_accept_incoming && m_i2p_sam_session.get() != nullptr) {
2347+
if (m_i2p_sam_session) {
23382348
threadI2PAcceptIncoming =
23392349
std::thread(&util::TraceThread, "i2paccept", [this] { ThreadI2PAcceptIncoming(); });
23402350
}

src/net.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,8 @@ class CConnman
10901090

10911091
/**
10921092
* I2P SAM session.
1093-
* Used to accept incoming and make outgoing I2P connections.
1093+
* Used to accept incoming and make outgoing I2P connections from a persistent
1094+
* address.
10941095
*/
10951096
std::unique_ptr<i2p::sam::Session> m_i2p_sam_session;
10961097

0 commit comments

Comments
 (0)