Skip to content

Commit

Permalink
Improve XRay protocol process close (#1318)
Browse files Browse the repository at this point in the history
  • Loading branch information
outspace authored Jan 7, 2025
1 parent e7fa160 commit 767b14b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
35 changes: 16 additions & 19 deletions client/protocols/xrayprotocol.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "xrayprotocol.h"

#include "utilities.h"
#include "containers/containers_defs.h"
#include "core/networkUtilities.h"

#include <QCryptographicHash>
Expand All @@ -22,9 +21,8 @@ XrayProtocol::XrayProtocol(const QJsonObject &configuration, QObject *parent):

XrayProtocol::~XrayProtocol()
{
qDebug() << "XrayProtocol::~XrayProtocol()";
XrayProtocol::stop();
QThread::msleep(200);
m_xrayProcess.close();
}

ErrorCode XrayProtocol::start()
Expand All @@ -36,10 +34,6 @@ ErrorCode XrayProtocol::start()
return lastError();
}

if (Utils::processIsRunning(Utils::executable(xrayExecPath(), true))) {
Utils::killProcessByName(Utils::executable(xrayExecPath(), true));
}

#ifdef QT_DEBUG
m_xrayCfgFile.setAutoRemove(false);
#endif
Expand All @@ -54,9 +48,16 @@ ErrorCode XrayProtocol::start()
qDebug().noquote() << "XrayProtocol::start()"
<< xrayExecPath() << args.join(" ");

m_xrayProcess.setProcessChannelMode(QProcess::MergedChannels);


m_xrayProcess.setProcessChannelMode(QProcess::MergedChannels);
m_xrayProcess.setProgram(xrayExecPath());

if (Utils::processIsRunning(Utils::executable("xray", false))) {
qDebug().noquote() << "kill previos xray";
Utils::killProcessByName(Utils::executable("xray", false));
}

m_xrayProcess.setArguments(args);

connect(&m_xrayProcess, &QProcess::readyReadStandardOutput, this, [this]() {
Expand All @@ -68,13 +69,9 @@ ErrorCode XrayProtocol::start()
connect(&m_xrayProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [this](int exitCode, QProcess::ExitStatus exitStatus) {
qDebug().noquote() << "XrayProtocol finished, exitCode, exitStatus" << exitCode << exitStatus;
setConnectionState(Vpn::ConnectionState::Disconnected);
if (exitStatus != QProcess::NormalExit) {
emit protocolError(amnezia::ErrorCode::XrayExecutableCrashed);
stop();
}
if (exitCode != 0) {
emit protocolError(amnezia::ErrorCode::InternalError);
stop();
if ((exitStatus != QProcess::NormalExit) || (exitCode != 0)) {
emit protocolError(amnezia::ErrorCode::XrayExecutableCrashed);
emit setConnectionState(Vpn::ConnectionState::Error);
}
});

Expand Down Expand Up @@ -177,14 +174,14 @@ void XrayProtocol::stop()
IpcClient::Interface()->StartRoutingIpv6();
#endif
qDebug() << "XrayProtocol::stop()";
m_xrayProcess.terminate();
m_xrayProcess.disconnect();
m_xrayProcess.kill();
m_xrayProcess.waitForFinished(3000);
if (m_t2sProcess) {
m_t2sProcess->stop();
}

#ifdef Q_OS_WIN
Utils::signalCtrl(m_xrayProcess.processId(), CTRL_C_EVENT);
#endif
setConnectionState(Vpn::ConnectionState::Disconnected);
}

QString XrayProtocol::xrayExecPath()
Expand Down
21 changes: 13 additions & 8 deletions ipc/ipctun2socksprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ IpcProcessTun2Socks::IpcProcessTun2Socks(QObject *parent) :
IpcProcessTun2SocksSource(parent),
m_t2sProcess(QSharedPointer<QProcess>(new QProcess()))
{
connect(m_t2sProcess.data(), &QProcess::stateChanged, this, &IpcProcessTun2Socks::stateChanged);
qDebug() << "IpcProcessTun2Socks::IpcProcessTun2Socks()";

}
Expand All @@ -23,8 +22,10 @@ IpcProcessTun2Socks::~IpcProcessTun2Socks()

void IpcProcessTun2Socks::start()
{
connect(m_t2sProcess.data(), &QProcess::stateChanged, this, &IpcProcessTun2Socks::stateChanged);
qDebug() << "IpcProcessTun2Socks::start()";
m_t2sProcess->setProgram(amnezia::permittedProcessPath(static_cast<amnezia::PermittedProcess>(amnezia::PermittedProcess::Tun2Socks)));

QString XrayConStr = "socks5://127.0.0.1:10808";

#ifdef Q_OS_WIN
Expand All @@ -41,7 +42,11 @@ void IpcProcessTun2Socks::start()

m_t2sProcess->setArguments(arguments);

Utils::killProcessByName(m_t2sProcess->program());
if (Utils::processIsRunning(Utils::executable("tun2socks", false))) {
qDebug().noquote() << "kill previos tun2socks";
Utils::killProcessByName(Utils::executable("tun2socks", false));
}

m_t2sProcess->start();

connect(m_t2sProcess.data(), &QProcess::readyReadStandardOutput, this, [this]() {
Expand All @@ -54,12 +59,10 @@ void IpcProcessTun2Socks::start()
connect(m_t2sProcess.data(), QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [this](int exitCode, QProcess::ExitStatus exitStatus) {
qDebug().noquote() << "tun2socks finished, exitCode, exiStatus" << exitCode << exitStatus;
emit setConnectionState(Vpn::ConnectionState::Disconnected);
if (exitStatus != QProcess::NormalExit){
stop();
}
if (exitCode !=0 ){
stop();
if ((exitStatus != QProcess::NormalExit) || (exitCode != 0)) {
emit setConnectionState(Vpn::ConnectionState::Error);
}

});

m_t2sProcess->start();
Expand All @@ -69,6 +72,8 @@ void IpcProcessTun2Socks::start()
void IpcProcessTun2Socks::stop()
{
qDebug() << "IpcProcessTun2Socks::stop()";
m_t2sProcess->close();
m_t2sProcess->disconnect();
m_t2sProcess->kill();
m_t2sProcess->waitForFinished(3000);
}
#endif

0 comments on commit 767b14b

Please sign in to comment.