Tor v3: fix hidden service backend, add key persistence and health monitoring

Codex changes: delegate hidden service management to the actual Tor
backend instead of generating keys the wallet never served. The new
AttachToBackendService() reads the hostname Tor creates, and the
torrc/process plumbing properly gates HiddenService directives behind
the -torhiddenservice flag.

Additional fixes:
- Back up hs_ed25519_secret_key (96 bytes) to wallet.dat so the onion
  identity survives deletion of tor_data/
- Restore the key before Tor starts so the same .onion address is
  regenerated automatically
- Add ThreadTorMaintenance: checks Tor health every 30s, auto-restarts
  with exponential backoff on crash, re-attaches the hidden service
  and re-registers the onion address with AddLocal()
- Seeder maintenance: every 30 min re-announces to peers and refreshes
  known seeder lists (when -torseeder is enabled)
- Clean up ScheduleSeederReannouncement() stub (real work now in thread)
- Respect -torsocks port in onion proxy registration

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-27 00:27:22 -07:00
parent 5ad0bb53b4
commit d242c2f37e
7 changed files with 355 additions and 99 deletions
+28 -17
View File
@@ -48,6 +48,7 @@ CTorProcess* CTorProcess::GetInstance()
CTorProcess::CTorProcess()
: socksPort(19099)
, hiddenServicePort(24112)
, hiddenServiceEnabled(true)
, running(false)
#ifdef WIN32
, hProcess(NULL)
@@ -198,11 +199,13 @@ bool CTorProcess::WriteTorrc()
fs::create_directories(torStateDir);
torrc << "DataDirectory " << torStateDir.string() << "\n";
// V3 hidden service so this node is reachable via .onion
torrc << "HiddenServiceDir " << hsDir.string() << "\n";
torrc << "HiddenServiceVersion 3\n";
torrc << "HiddenServicePort " << hiddenServicePort
<< " 127.0.0.1:" << hiddenServicePort << "\n";
if (hiddenServiceEnabled) {
// V3 hidden service so this node is reachable via .onion
torrc << "HiddenServiceDir " << hsDir.string() << "\n";
torrc << "HiddenServiceVersion 3\n";
torrc << "HiddenServicePort " << hiddenServicePort
<< " 127.0.0.1:" << hiddenServicePort << "\n";
}
// Reduce bandwidth/resource usage for wallet use
torrc << "ClientOnly 1\n";
@@ -213,15 +216,21 @@ bool CTorProcess::WriteTorrc()
torrc.close();
printf("Wrote torrc to %s (SOCKS %d, HS port %d)\n",
torrcPath.c_str(), socksPort, hiddenServicePort);
if (hiddenServiceEnabled) {
printf("Wrote torrc to %s (SOCKS %d, HS port %d)\n",
torrcPath.c_str(), socksPort, hiddenServicePort);
} else {
printf("Wrote torrc to %s (SOCKS %d, hidden service disabled)\n",
torrcPath.c_str(), socksPort);
}
return true;
}
bool CTorProcess::Start(const std::string& dataDir, int socks, int hsPort)
bool CTorProcess::Start(const std::string& dataDir, int socks, int hsPort, bool enableHiddenService)
{
socksPort = socks;
hiddenServicePort = hsPort;
hiddenServiceEnabled = enableHiddenService;
hiddenServicePort = hiddenServiceEnabled ? hsPort : 0;
torDataDir = dataDir;
// Check if something is already listening on our SOCKS port
@@ -314,12 +323,14 @@ bool CTorProcess::Start(const std::string& dataDir, int socks, int hsPort)
printf("Tor SOCKS proxy ready on port %d (took %ds)\n", socksPort, i + 1);
// Read and display the hidden service hostname if available
fs::path hsHostname = fs::path(torDataDir) / "hidden_service" / "hostname";
if (fs::exists(hsHostname)) {
std::ifstream f(hsHostname.string().c_str());
std::string hostname;
if (f.is_open() && std::getline(f, hostname)) {
printf("Tor hidden service: %s\n", hostname.c_str());
if (hiddenServiceEnabled) {
fs::path hsHostname = fs::path(torDataDir) / "hidden_service" / "hostname";
if (fs::exists(hsHostname)) {
std::ifstream f(hsHostname.string().c_str());
std::string hostname;
if (f.is_open() && std::getline(f, hostname)) {
printf("Tor hidden service: %s\n", hostname.c_str());
}
}
}
return true;
@@ -402,9 +413,9 @@ std::string CTorProcess::GetSocksProxy() const
}
// Global convenience functions
bool StartTorProcess(const std::string& dataDir, int socksPort, int hsPort)
bool StartTorProcess(const std::string& dataDir, int socksPort, int hsPort, bool enableHiddenService)
{
return CTorProcess::GetInstance()->Start(dataDir, socksPort, hsPort);
return CTorProcess::GetInstance()->Start(dataDir, socksPort, hsPort, enableHiddenService);
}
void StopTorProcess()