mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-22 02:01:08 +07:00
feat: decouple TUN name from XrayProtocol to support dual tunnels
This commit is contained in:
@@ -19,12 +19,6 @@
|
|||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
#ifdef Q_OS_MACOS
|
|
||||||
static const QString tunName = "utun22";
|
|
||||||
#else
|
|
||||||
static const QString tunName = "tun2";
|
|
||||||
#endif
|
|
||||||
|
|
||||||
XrayProtocol::XrayProtocol(const QJsonObject &configuration, QObject *parent) : VpnProtocol(configuration, parent)
|
XrayProtocol::XrayProtocol(const QJsonObject &configuration, QObject *parent) : VpnProtocol(configuration, parent)
|
||||||
{
|
{
|
||||||
m_vpnGateway = amnezia::protocols::xray::defaultLocalAddr;
|
m_vpnGateway = amnezia::protocols::xray::defaultLocalAddr;
|
||||||
@@ -34,6 +28,11 @@ XrayProtocol::XrayProtocol(const QJsonObject &configuration, QObject *parent) :
|
|||||||
m_routeMode = static_cast<amnezia::RouteMode>(configuration.value(amnezia::configKey::splitTunnelType).toInt());
|
m_routeMode = static_cast<amnezia::RouteMode>(configuration.value(amnezia::configKey::splitTunnelType).toInt());
|
||||||
m_remoteAddress = NetworkUtilities::getIPAddress(m_rawConfig.value(amnezia::configKey::hostName).toString());
|
m_remoteAddress = NetworkUtilities::getIPAddress(m_rawConfig.value(amnezia::configKey::hostName).toString());
|
||||||
|
|
||||||
|
#ifdef Q_OS_MACOS
|
||||||
|
m_tunName = configuration.value("tunName").toString("utun22");
|
||||||
|
#else
|
||||||
|
m_tunName = configuration.value("tunName").toString("tun2");
|
||||||
|
#endif
|
||||||
const QString primaryDns = configuration.value(amnezia::configKey::dns1).toString();
|
const QString primaryDns = configuration.value(amnezia::configKey::dns1).toString();
|
||||||
m_dnsServers.push_back(QHostAddress(primaryDns));
|
m_dnsServers.push_back(QHostAddress(primaryDns));
|
||||||
if (primaryDns != amnezia::protocols::dns::amneziaDnsIp) {
|
if (primaryDns != amnezia::protocols::dns::amneziaDnsIp) {
|
||||||
@@ -120,12 +119,12 @@ void XrayProtocol::stop()
|
|||||||
{
|
{
|
||||||
qDebug() << "XrayProtocol::stop()";
|
qDebug() << "XrayProtocol::stop()";
|
||||||
|
|
||||||
IpcClient::withInterface([](QSharedPointer<IpcInterfaceReplica> iface) {
|
IpcClient::withInterface([this](QSharedPointer<IpcInterfaceReplica> iface) {
|
||||||
auto restoreResolvers = iface->restoreResolvers();
|
auto restoreResolvers = iface->restoreResolvers();
|
||||||
if (!restoreResolvers.waitForFinished() || !restoreResolvers.returnValue())
|
if (!restoreResolvers.waitForFinished() || !restoreResolvers.returnValue())
|
||||||
qWarning() << "Failed to restore resolvers";
|
qWarning() << "Failed to restore resolvers";
|
||||||
|
|
||||||
auto deleteTun = iface->deleteTun(tunName);
|
auto deleteTun = iface->deleteTun(m_tunName);
|
||||||
if (!deleteTun.waitForFinished() || !deleteTun.returnValue())
|
if (!deleteTun.waitForFinished() || !deleteTun.returnValue())
|
||||||
qWarning() << "Failed to delete tun";
|
qWarning() << "Failed to delete tun";
|
||||||
|
|
||||||
@@ -167,7 +166,7 @@ ErrorCode XrayProtocol::startTun2Socks()
|
|||||||
const QString proxyUrl = QString("socks5://%1:%2@127.0.0.1:%3").arg(m_socksUser, m_socksPassword, QString::number(m_socksPort));
|
const QString proxyUrl = QString("socks5://%1:%2@127.0.0.1:%3").arg(m_socksUser, m_socksPassword, QString::number(m_socksPort));
|
||||||
|
|
||||||
m_tun2socksProcess->setProgram(PermittedProcess::Tun2Socks);
|
m_tun2socksProcess->setProgram(PermittedProcess::Tun2Socks);
|
||||||
m_tun2socksProcess->setArguments({ "-device", QString("tun://%1").arg(tunName), "-proxy", proxyUrl });
|
m_tun2socksProcess->setArguments({ "-device", QString("tun://%1").arg(m_tunName), "-proxy", proxyUrl });
|
||||||
|
|
||||||
connect(
|
connect(
|
||||||
m_tun2socksProcess.data(), &IpcProcessInterfaceReplica::readyReadStandardError, this,
|
m_tun2socksProcess.data(), &IpcProcessInterfaceReplica::readyReadStandardError, this,
|
||||||
@@ -247,13 +246,13 @@ ErrorCode XrayProtocol::setupRouting()
|
|||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
const int inetAdapterIndex = NetworkUtilities::AdapterIndexTo(QHostAddress(m_remoteAddress));
|
const int inetAdapterIndex = NetworkUtilities::AdapterIndexTo(QHostAddress(m_remoteAddress));
|
||||||
#endif
|
#endif
|
||||||
auto createTun = iface->createTun(tunName, amnezia::protocols::xray::defaultLocalAddr);
|
auto createTun = iface->createTun(m_tunName, amnezia::protocols::xray::defaultLocalAddr);
|
||||||
if (!createTun.waitForFinished() || !createTun.returnValue()) {
|
if (!createTun.waitForFinished() || !createTun.returnValue()) {
|
||||||
qCritical() << "Failed to assign IP address for TUN";
|
qCritical() << "Failed to assign IP address for TUN";
|
||||||
return ErrorCode::InternalError;
|
return ErrorCode::InternalError;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto updateResolvers = iface->updateResolvers(tunName, m_dnsServers);
|
auto updateResolvers = iface->updateResolvers(m_tunName, m_dnsServers);
|
||||||
if (!updateResolvers.waitForFinished() || !updateResolvers.returnValue()) {
|
if (!updateResolvers.waitForFinished() || !updateResolvers.returnValue()) {
|
||||||
qCritical() << "Failed to set DNS resolvers for TUN";
|
qCritical() << "Failed to set DNS resolvers for TUN";
|
||||||
return ErrorCode::InternalError;
|
return ErrorCode::InternalError;
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ private:
|
|||||||
int m_tun2socksRetryCount = 0;
|
int m_tun2socksRetryCount = 0;
|
||||||
static constexpr int maxTun2SocksRetries = 5;
|
static constexpr int maxTun2SocksRetries = 5;
|
||||||
static constexpr int tun2socksRetryDelayMs = 400;
|
static constexpr int tun2socksRetryDelayMs = 400;
|
||||||
|
|
||||||
|
QString m_tunName;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // XRAYPROTOCOL_H
|
#endif // XRAYPROTOCOL_H
|
||||||
|
|||||||
Reference in New Issue
Block a user