debugged pipe system with tor executable

This commit is contained in:
csoler 2021-12-10 16:29:10 +01:00
parent 7c77cfd603
commit 1571446a2e
4 changed files with 71 additions and 15 deletions

View File

@ -64,7 +64,8 @@ int RsFdBinInterface::read_pending()
char inBuffer[1025]; char inBuffer[1025];
memset(inBuffer,0,1025); memset(inBuffer,0,1025);
ssize_t readbytes = recv(mCLintConnt, inBuffer, sizeof(inBuffer),MSG_DONTWAIT); ssize_t readbytes = read(mCLintConnt, inBuffer, sizeof(inBuffer)); // Needs read instead of recv which is only for sockets.
// Sockets should be set to non blocking by the client process.
if(readbytes == 0) if(readbytes == 0)
{ {

View File

@ -148,6 +148,44 @@ void TorManager::setHiddenServiceDirectory(const std::string &path)
d->hiddenServiceDir += '/'; d->hiddenServiceDir += '/';
} }
static bool test_listening_port(const std::string& address,uint16_t port)
{
int sockfd, portno;
struct sockaddr_in serv_addr;
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
return false;
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
close(sockfd);
return false;
}
int flags = fcntl(sockfd, F_GETFL);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
int res = listen(sockfd,5);
int err = errno;
close(sockfd);
if(!res)
return true;
return false;
}
bool TorManager::setupHiddenService() bool TorManager::setupHiddenService()
{ {
if(d->hiddenService != NULL) if(d->hiddenService != NULL)
@ -208,12 +246,12 @@ bool TorManager::setupHiddenService()
std::cerr << "Testing host address: " << address << ":" << port ; std::cerr << "Testing host address: " << address << ":" << port ;
// if (!QTcpServer().listen(address, port)) if(!test_listening_port(address,port))
// { {
// // XXX error case // XXX error case
// std::cerr << " Failed to open incoming socket" << std::endl; std::cerr << " Failed to open incoming socket" << std::endl;
// return false; return false;
// } }
std::cerr << " OK - Adding hidden service to TorControl." << std::endl; std::cerr << " OK - Adding hidden service to TorControl." << std::endl;
@ -538,6 +576,14 @@ std::string TorManagerPrivate::torExecutablePath() const
return tor_exe_path; return tor_exe_path;
#endif #endif
#ifdef __linux__
// On linux try system-installed tor /usr/bin/tor
if(RsDirUtil::fileExists("/usr/bin/tor"))
return std::string("/usr/bin/tor");
#endif
RsErr() << "Could not find Tor executable anywhere!" ;
// Try $PATH // Try $PATH
return filename.substr(1); return filename.substr(1);
} }

View File

@ -113,6 +113,8 @@ std::string TorProcess::errorMessage() const
int popen3(int fd[3],const char **const cmd,pid_t& pid) int popen3(int fd[3],const char **const cmd,pid_t& pid)
{ {
RsErr() << "Launching Tor in background..." ;
int i, e; int i, e;
int p[3][2]; int p[3][2];
// set all the FDs to invalid // set all the FDs to invalid
@ -141,6 +143,7 @@ int popen3(int fd[3],const char **const cmd,pid_t& pid)
} }
else else
{ {
RsErr() << "Launching sub-process..." ;
// child // child
dup2(p[STDIN_FILENO][0],STDIN_FILENO); dup2(p[STDIN_FILENO][0],STDIN_FILENO);
close(p[STDIN_FILENO][1]); close(p[STDIN_FILENO][1]);
@ -148,16 +151,20 @@ int popen3(int fd[3],const char **const cmd,pid_t& pid)
close(p[STDOUT_FILENO][0]); close(p[STDOUT_FILENO][0]);
dup2(p[STDERR_FILENO][1],STDERR_FILENO); dup2(p[STDERR_FILENO][1],STDERR_FILENO);
close(p[STDERR_FILENO][0]); close(p[STDERR_FILENO][0]);
// here we try and run it // here we try and run it
execv(*cmd,const_cast<char*const*>(cmd)); execv(*cmd,const_cast<char*const*>(cmd));
// if we are there, then we failed to launch our program // if we are there, then we failed to launch our program
perror("Could not launch"); perror("Could not launch");
fprintf(stderr," \"%s\"\n",*cmd); fprintf(stderr," \"%s\"\n",*cmd);
} }
error: error:
// preserve original error
e = errno; e = errno;
// preserve original error
RsErr() << "An error occurred while trying to launch tor in background." ;
for(i=0; i<3; i++) { for(i=0; i<3; i++) {
close(p[i][0]); close(p[i][0]);
close(p[i][1]); close(p[i][1]);
@ -253,8 +260,8 @@ void TorProcess::run()
// We first pushed everything into a vector of strings to save the pointers obtained from string returning methods // We first pushed everything into a vector of strings to save the pointers obtained from string returning methods
// by the time the process is launched. // by the time the process is launched.
for(auto s:args) for(uint32_t i=0;i<args.size();++i)
arguments[n++]= s.c_str(); arguments[n++]= args[i].data();
arguments[n] = nullptr; arguments[n] = nullptr;
@ -267,6 +274,10 @@ void TorProcess::run()
return; // stop the control thread return; // stop the control thread
} }
int flags ;
flags = fcntl(fd[STDOUT_FILENO], F_GETFL); fcntl(fd[STDOUT_FILENO], F_SETFL, flags | O_NONBLOCK);
flags = fcntl(fd[STDERR_FILENO], F_GETFL); fcntl(fd[STDERR_FILENO], F_SETFL, flags | O_NONBLOCK);
RsFdBinInterface stdout_FD(fd[STDOUT_FILENO]); RsFdBinInterface stdout_FD(fd[STDOUT_FILENO]);
RsFdBinInterface stderr_FD(fd[STDERR_FILENO]); RsFdBinInterface stderr_FD(fd[STDERR_FILENO]);
@ -281,7 +292,7 @@ void TorProcess::run()
if((s=stdout_FD.readline(buff,1024))) logMessage(std::string((char*)buff,s)); if((s=stdout_FD.readline(buff,1024))) logMessage(std::string((char*)buff,s));
if((s=stderr_FD.readline(buff,1024))) logMessage(std::string((char*)buff,s)); if((s=stderr_FD.readline(buff,1024))) logMessage(std::string((char*)buff,s));
if(!stdout_FD.isactive() || !stderr_FD.isactive()) if(!stdout_FD.isactive() && !stderr_FD.isactive())
{ {
RsErr() << "Tor process died. Exiting TorControl process." ; RsErr() << "Tor process died. Exiting TorControl process." ;
return; return;
@ -390,13 +401,13 @@ bool TorProcess::ensureFilesExist()
std::string TorProcess::torrcPath() const std::string TorProcess::torrcPath() const
{ {
//return QDir::toNativeSeparators(dataDir) + QDir::separator() + QStringLiteral("torrc"); //return QDir::toNativeSeparators(dataDir) + QDir::separator() + QStringLiteral("torrc");
return mDataDir + "/" + "torrc"; return RsDirUtil::makePath(mDataDir,"torrc");
} }
std::string TorProcess::controlPortFilePath() const std::string TorProcess::controlPortFilePath() const
{ {
//return QDir::toNativeSeparators(dataDir) + QDir::separator() + QStringLiteral("control-port"); //return QDir::toNativeSeparators(dataDir) + QDir::separator() + QStringLiteral("control-port");
return mDataDir + "/" + "control-port"; return RsDirUtil::makePath(mDataDir,"control-port");
} }
bool TorProcess::tryReadControlPort() bool TorProcess::tryReadControlPort()

View File

@ -169,8 +169,6 @@ void TorControlDialog::showLog()
std::cerr << "[TOR DEBUG LOG] " << *it << std::endl; std::cerr << "[TOR DEBUG LOG] " << *it << std::endl;
} }
// torLog_TB->setText(s) ;:
std::cerr << "Connexion Proxy: " << RsTor::socksAddress() << ":" << QString::number(RsTor::socksPort()).toStdString() << std::endl; std::cerr << "Connexion Proxy: " << RsTor::socksAddress() << ":" << QString::number(RsTor::socksPort()).toStdString() << std::endl;
} }