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];
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)
{

View File

@ -148,6 +148,44 @@ void TorManager::setHiddenServiceDirectory(const std::string &path)
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()
{
if(d->hiddenService != NULL)
@ -208,12 +246,12 @@ bool TorManager::setupHiddenService()
std::cerr << "Testing host address: " << address << ":" << port ;
// if (!QTcpServer().listen(address, port))
// {
// // XXX error case
// std::cerr << " Failed to open incoming socket" << std::endl;
// return false;
// }
if(!test_listening_port(address,port))
{
// XXX error case
std::cerr << " Failed to open incoming socket" << std::endl;
return false;
}
std::cerr << " OK - Adding hidden service to TorControl." << std::endl;
@ -538,6 +576,14 @@ std::string TorManagerPrivate::torExecutablePath() const
return tor_exe_path;
#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
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)
{
RsErr() << "Launching Tor in background..." ;
int i, e;
int p[3][2];
// set all the FDs to invalid
@ -141,6 +143,7 @@ int popen3(int fd[3],const char **const cmd,pid_t& pid)
}
else
{
RsErr() << "Launching sub-process..." ;
// child
dup2(p[STDIN_FILENO][0],STDIN_FILENO);
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]);
dup2(p[STDERR_FILENO][1],STDERR_FILENO);
close(p[STDERR_FILENO][0]);
// here we try and run it
execv(*cmd,const_cast<char*const*>(cmd));
// if we are there, then we failed to launch our program
perror("Could not launch");
fprintf(stderr," \"%s\"\n",*cmd);
}
error:
// preserve original error
e = errno;
// preserve original error
RsErr() << "An error occurred while trying to launch tor in background." ;
for(i=0; i<3; i++) {
close(p[i][0]);
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
// by the time the process is launched.
for(auto s:args)
arguments[n++]= s.c_str();
for(uint32_t i=0;i<args.size();++i)
arguments[n++]= args[i].data();
arguments[n] = nullptr;
@ -267,6 +274,10 @@ void TorProcess::run()
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 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=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." ;
return;
@ -390,13 +401,13 @@ bool TorProcess::ensureFilesExist()
std::string TorProcess::torrcPath() const
{
//return QDir::toNativeSeparators(dataDir) + QDir::separator() + QStringLiteral("torrc");
return mDataDir + "/" + "torrc";
return RsDirUtil::makePath(mDataDir,"torrc");
}
std::string TorProcess::controlPortFilePath() const
{
//return QDir::toNativeSeparators(dataDir) + QDir::separator() + QStringLiteral("control-port");
return mDataDir + "/" + "control-port";
return RsDirUtil::makePath(mDataDir,"control-port");
}
bool TorProcess::tryReadControlPort()

View File

@ -169,8 +169,6 @@ void TorControlDialog::showLog()
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;
}