Files
triangles_v5/src/tor/tor_process.h
T
sami7777 dee0d9ef62 Fix Tor process cleanup: kill orphans on startup, Job Object on Windows
- Add Windows Job Object (JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE) so Tor
  child process is automatically killed when the wallet exits for any
  reason (crash, Task Manager, clean shutdown)
- Replace port-reuse "assume running" path with active orphan cleanup:
  Windows enumerates and kills tor.exe processes, Linux uses PID file
- Move deep-reorg trust-delta check into Reorganize() so short forks
  (<=6 blocks) converge freely while long-range attacks are still blocked

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-22 22:05:09 -07:00

78 lines
2.0 KiB
C++

// Copyright (c) 2024-2025 Triangles developers
// Tor Process Manager - launches and manages an external Tor binary
// Distributed under the MIT/X11 software license
#ifndef TRIANGLES_TOR_PROCESS_H
#define TRIANGLES_TOR_PROCESS_H
#include <string>
#ifdef WIN32
#include <windows.h>
#endif
// Manages starting/stopping an external Tor process
// Provides SOCKS5 proxy for .onion connectivity and v3 hidden service
class CTorProcess
{
private:
std::string torBinaryPath;
std::string torDataDir;
std::string torrcPath;
std::string lastError;
int socksPort;
int hiddenServicePort;
bool hiddenServiceEnabled;
bool running;
#ifdef WIN32
HANDLE hProcess;
HANDLE hJob; // Job Object: kills Tor if wallet crashes/exits
DWORD processId;
// Find and kill an orphaned Tor process from a previous wallet session
bool KillOrphanedTor();
#else
pid_t processId;
#endif
// Find the Tor binary in standard locations
std::string FindTorBinary();
// Generate torrc configuration file
bool WriteTorrc();
// Check if SOCKS port is already in use (another Tor running)
bool IsPortInUse(int port);
public:
CTorProcess();
~CTorProcess();
// Start the Tor process
// Returns true if Tor was started or is already running
bool Start(const std::string& dataDir, int socksPort = 19099, int hsPort = 24112, bool enableHiddenService = true);
// Stop the Tor process
void Stop();
// Check if Tor is running
bool IsRunning();
// Get the SOCKS proxy address
std::string GetSocksProxy() const;
// Get the Tor binary path (for diagnostics)
std::string GetBinaryPath() const { return torBinaryPath; }
std::string GetStartupError() const { return lastError; }
// Singleton access
static CTorProcess* GetInstance();
};
// Global convenience functions
bool StartTorProcess(const std::string& dataDir, int socksPort = 19099, int hsPort = 24112, bool enableHiddenService = true);
void StopTorProcess();
#endif // TRIANGLES_TOR_PROCESS_H