| 514 | } |
| 515 | |
| 516 | void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlReply& reply, bool pow_was_enabled) |
| 517 | { |
| 518 | if (reply.code == TOR_REPLY_OK) { |
| 519 | LogDebug(BCLog::TOR, "ADD_ONION successful (PoW defenses %s)", pow_was_enabled ? "enabled" : "disabled"); |
| 520 | for (const std::string &s : reply.lines) { |
| 521 | std::map<std::string,std::string> m = ParseTorReplyMapping(s); |
| 522 | std::map<std::string,std::string>::iterator i; |
| 523 | if ((i = m.find("ServiceID")) != m.end()) |
| 524 | m_service_id = i->second; |
| 525 | if ((i = m.find("PrivateKey")) != m.end()) |
| 526 | m_private_key = i->second; |
| 527 | } |
| 528 | if (m_service_id.empty()) { |
| 529 | LogWarning("tor: Error parsing ADD_ONION parameters:"); |
| 530 | for (const std::string &s : reply.lines) { |
| 531 | LogWarning(" %s", SanitizeString(s)); |
| 532 | } |
| 533 | return; |
| 534 | } |
| 535 | m_service = LookupNumeric(std::string(m_service_id+".onion"), Params().GetDefaultPort()); |
| 536 | LogInfo("Got tor service ID %s, advertising service %s", m_service_id, m_service.ToStringAddrPort()); |
| 537 | if (WriteBinaryFile(GetPrivateKeyFile(), m_private_key)) { |
| 538 | LogDebug(BCLog::TOR, "Cached service private key to %s", fs::PathToString(GetPrivateKeyFile())); |
| 539 | } else { |
| 540 | LogWarning("tor: Error writing service private key to %s", fs::PathToString(GetPrivateKeyFile())); |
| 541 | } |
| 542 | AddLocal(m_service, LOCAL_MANUAL); |
| 543 | // ... onion requested - keep connection open |
| 544 | } else if (reply.code == TOR_REPLY_UNRECOGNIZED) { |
| 545 | LogWarning("tor: Add onion failed with unrecognized command (You probably need to upgrade Tor)"); |
| 546 | } else if (pow_was_enabled && reply.code == TOR_REPLY_SYNTAX_ERROR) { |
| 547 | LogDebug(BCLog::TOR, "ADD_ONION failed with PoW defenses, retrying without"); |
| 548 | _conn.Command(MakeAddOnionCmd(m_private_key, m_target.ToStringAddrPort(), /*enable_pow=*/false), |
| 549 | [this](TorControlConnection& conn, const TorControlReply& reply) { |
| 550 | add_onion_cb(conn, reply, /*pow_was_enabled=*/false); |
| 551 | }); |
| 552 | } else { |
| 553 | LogWarning("tor: Add onion failed; error code %d", reply.code); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply& reply) |
| 558 | { |
no test coverage detected