|
12 | 12 | #include <netaddress.h>
|
13 | 13 | #include <netbase.h>
|
14 | 14 | #include <random.h>
|
15 |
| -#include <util/strencodings.h> |
16 | 15 | #include <tinyformat.h>
|
17 | 16 | #include <util/readwritefile.h>
|
18 | 17 | #include <util/sock.h>
|
19 | 18 | #include <util/spanparsing.h>
|
| 19 | +#include <util/strencodings.h> |
20 | 20 | #include <util/system.h>
|
21 | 21 |
|
22 | 22 | #include <chrono>
|
@@ -115,8 +115,19 @@ namespace sam {
|
115 | 115 | Session::Session(const fs::path& private_key_file,
|
116 | 116 | const CService& control_host,
|
117 | 117 | CThreadInterrupt* interrupt)
|
118 |
| - : m_private_key_file(private_key_file), m_control_host(control_host), m_interrupt(interrupt), |
119 |
| - m_control_sock(std::make_unique<Sock>(INVALID_SOCKET)) |
| 118 | + : m_private_key_file{private_key_file}, |
| 119 | + m_control_host{control_host}, |
| 120 | + m_interrupt{interrupt}, |
| 121 | + m_control_sock{std::make_unique<Sock>(INVALID_SOCKET)}, |
| 122 | + m_transient{false} |
| 123 | +{ |
| 124 | +} |
| 125 | + |
| 126 | +Session::Session(const CService& control_host, CThreadInterrupt* interrupt) |
| 127 | + : m_control_host{control_host}, |
| 128 | + m_interrupt{interrupt}, |
| 129 | + m_control_sock{std::make_unique<Sock>(INVALID_SOCKET)}, |
| 130 | + m_transient{true} |
120 | 131 | {
|
121 | 132 | }
|
122 | 133 |
|
@@ -355,29 +366,47 @@ void Session::CreateIfNotCreatedAlready()
|
355 | 366 | return;
|
356 | 367 | }
|
357 | 368 |
|
358 |
| - Log("Creating SAM session with %s", m_control_host.ToString()); |
| 369 | + const auto session_type = m_transient ? "transient" : "persistent"; |
| 370 | + const auto session_id = GetRandHash().GetHex().substr(0, 10); // full is overkill, too verbose in the logs |
| 371 | + |
| 372 | + Log("Creating %s SAM session %s with %s", session_type, session_id, m_control_host.ToString()); |
359 | 373 |
|
360 | 374 | auto sock = Hello();
|
361 | 375 |
|
362 |
| - const auto& [read_ok, data] = ReadBinaryFile(m_private_key_file); |
363 |
| - if (read_ok) { |
364 |
| - m_private_key.assign(data.begin(), data.end()); |
| 376 | + if (m_transient) { |
| 377 | + // The destination (private key) is generated upon session creation and returned |
| 378 | + // in the reply in DESTINATION=. |
| 379 | + const Reply& reply = SendRequestAndGetReply( |
| 380 | + *sock, |
| 381 | + strprintf("SESSION CREATE STYLE=STREAM ID=%s DESTINATION=TRANSIENT", session_id)); |
| 382 | + |
| 383 | + m_private_key = DecodeI2PBase64(reply.Get("DESTINATION")); |
365 | 384 | } else {
|
366 |
| - GenerateAndSavePrivateKey(*sock); |
367 |
| - } |
| 385 | + // Read our persistent destination (private key) from disk or generate |
| 386 | + // one and save it to disk. Then use it when creating the session. |
| 387 | + const auto& [read_ok, data] = ReadBinaryFile(m_private_key_file); |
| 388 | + if (read_ok) { |
| 389 | + m_private_key.assign(data.begin(), data.end()); |
| 390 | + } else { |
| 391 | + GenerateAndSavePrivateKey(*sock); |
| 392 | + } |
368 | 393 |
|
369 |
| - const std::string& session_id = GetRandHash().GetHex().substr(0, 10); // full is an overkill, too verbose in the logs |
370 |
| - const std::string& private_key_b64 = SwapBase64(EncodeBase64(m_private_key)); |
| 394 | + const std::string& private_key_b64 = SwapBase64(EncodeBase64(m_private_key)); |
371 | 395 |
|
372 |
| - SendRequestAndGetReply(*sock, strprintf("SESSION CREATE STYLE=STREAM ID=%s DESTINATION=%s", |
373 |
| - session_id, private_key_b64)); |
| 396 | + SendRequestAndGetReply(*sock, |
| 397 | + strprintf("SESSION CREATE STYLE=STREAM ID=%s DESTINATION=%s", |
| 398 | + session_id, |
| 399 | + private_key_b64)); |
| 400 | + } |
374 | 401 |
|
375 | 402 | m_my_addr = CService(DestBinToAddr(MyDestination()), I2P_SAM31_PORT);
|
376 | 403 | m_session_id = session_id;
|
377 | 404 | m_control_sock = std::move(sock);
|
378 | 405 |
|
379 |
| - LogPrintfCategory(BCLog::I2P, "SAM session created: session id=%s, my address=%s\n", |
380 |
| - m_session_id, m_my_addr.ToString()); |
| 406 | + Log("%s SAM session %s created, my address=%s", |
| 407 | + Capitalize(session_type), |
| 408 | + m_session_id, |
| 409 | + m_my_addr.ToString()); |
381 | 410 | }
|
382 | 411 |
|
383 | 412 | std::unique_ptr<Sock> Session::StreamAccept()
|
|
0 commit comments