Tor: fail startAutoTor() when the control port cannot be reached

startAutoTor() waited forever for Tor to become ready, with no timeout
and no error to break the loop when the control connection could not be
established at all (external Tor not running, wrong control port). Since
this runs synchronously inside RsLoginHelper::attemptLogin(), a mobile
or headless login on an auto-Tor node would simply never return.

Give up with an explicit error if the control link has not been seen up
once within 30 seconds. Once the control connection has been observed,
keep waiting indefinitely for Tor to bootstrap, as before.
This commit is contained in:
jolavillette 2026-08-31 20:27:49 +02:00
parent 8e5a92298d
commit fb32c62a9a

View File

@ -2162,6 +2162,15 @@ bool RsInit::startAutoTor()
std::string service_id;
RsTor::setupHiddenService();
// If the connection to the Tor control port cannot be established at all
// (e.g. an external Tor that is not running, or a wrong control port),
// fail after a while instead of waiting forever. Once the control link
// has been seen up, wait indefinitely for Tor to bootstrap, as before.
const time_t control_connect_timeout = 30; // seconds
time_t start_time = time(nullptr);
bool control_link_seen = false;
while(RsTor::torStatus() != RsTorStatus::READY && RsTor::getHiddenServiceStatus(service_id) != RsTorHiddenServiceStatus::ONLINE) // runs until some status is reached: either tor works, or it fails.
{
rstime::rs_usleep(0.5*1000*1000) ;
@ -2174,6 +2183,24 @@ bool RsInit::startAutoTor()
std::cerr << "(EE) Tor hidden service cannot be started: " << error_msg << std::endl;
return false;
}
if(!control_link_seen)
switch(RsTor::torConnectivityStatus())
{
case RsTorConnectivityStatus::SOCKET_CONNECTED:
case RsTorConnectivityStatus::AUTHENTICATING:
case RsTorConnectivityStatus::AUTHENTICATED:
case RsTorConnectivityStatus::HIDDEN_SERVICE_READY:
control_link_seen = true;
break;
default:
if(time(nullptr) > start_time + control_connect_timeout)
{
std::cerr << "(EE) Cannot establish a connection to the Tor control port. Giving up." << std::endl;
return false;
}
break;
}
// process Qt event loop to deal with messages of online/offline info
// QCoreApplication::processEvents();
}