mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-22 02:01:08 +07:00
fix: replace m_linkDefaultRoutes map with single m_gatewayIfindex
This commit is contained in:
@@ -66,14 +66,8 @@ DnsUtilsLinux::~DnsUtilsLinux() {
|
|||||||
MZ_COUNT_DTOR(DnsUtilsLinux);
|
MZ_COUNT_DTOR(DnsUtilsLinux);
|
||||||
|
|
||||||
if (m_resolver) {
|
if (m_resolver) {
|
||||||
for (auto iterator = m_linkDefaultRoutes.constBegin();
|
if (m_gatewayIfindex > 0)
|
||||||
iterator != m_linkDefaultRoutes.constEnd(); ++iterator) {
|
setLinkDefaultRoute(m_gatewayIfindex, true);
|
||||||
QList<QVariant> argumentList;
|
|
||||||
argumentList << QVariant::fromValue(iterator.key());
|
|
||||||
argumentList << QVariant::fromValue(iterator.value());
|
|
||||||
m_resolver->asyncCallWithArgumentList(QStringLiteral("SetLinkDefaultRoute"),
|
|
||||||
argumentList);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto iterator = m_linkDomains.constBegin();
|
for (auto iterator = m_linkDomains.constBegin();
|
||||||
iterator != m_linkDomains.constEnd(); ++iterator) {
|
iterator != m_linkDomains.constEnd(); ++iterator) {
|
||||||
@@ -93,11 +87,10 @@ DnsUtilsLinux::~DnsUtilsLinux() {
|
|||||||
|
|
||||||
bool DnsUtilsLinux::updateResolvers(const QString& ifname,
|
bool DnsUtilsLinux::updateResolvers(const QString& ifname,
|
||||||
const QList<QHostAddress>& resolvers) {
|
const QList<QHostAddress>& resolvers) {
|
||||||
for (auto iterator = m_linkDefaultRoutes.constBegin();
|
if (m_gatewayIfindex > 0) {
|
||||||
iterator != m_linkDefaultRoutes.constEnd(); ++iterator) {
|
setLinkDefaultRoute(m_gatewayIfindex, true);
|
||||||
setLinkDefaultRoute(iterator.key(), iterator.value());
|
m_gatewayIfindex = 0;
|
||||||
}
|
}
|
||||||
m_linkDefaultRoutes.clear();
|
|
||||||
|
|
||||||
m_ifindex = if_nametoindex(qPrintable(ifname));
|
m_ifindex = if_nametoindex(qPrintable(ifname));
|
||||||
if (m_ifindex <= 0) {
|
if (m_ifindex <= 0) {
|
||||||
@@ -113,7 +106,12 @@ bool DnsUtilsLinux::updateResolvers(const QString& ifname,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateLinkDefaultRoutes();
|
const int gwIdx = NetworkUtilities::getGatewayAndIface().second.index();
|
||||||
|
if (gwIdx > 0 && gwIdx != m_ifindex && gwIdx != m_gatewayIfindex) {
|
||||||
|
m_gatewayIfindex = gwIdx;
|
||||||
|
setLinkDefaultRoute(gwIdx, false);
|
||||||
|
}
|
||||||
|
|
||||||
setLinkDNS(m_ifindex, resolvers);
|
setLinkDNS(m_ifindex, resolvers);
|
||||||
setLinkDefaultRoute(m_ifindex, true);
|
setLinkDefaultRoute(m_ifindex, true);
|
||||||
updateLinkDomains();
|
updateLinkDomains();
|
||||||
@@ -124,11 +122,10 @@ bool DnsUtilsLinux::restoreResolvers() {
|
|||||||
m_pendingIfname.clear();
|
m_pendingIfname.clear();
|
||||||
m_pendingResolvers.clear();
|
m_pendingResolvers.clear();
|
||||||
|
|
||||||
for (auto iterator = m_linkDefaultRoutes.constBegin();
|
if (m_gatewayIfindex > 0) {
|
||||||
iterator != m_linkDefaultRoutes.constEnd(); ++iterator) {
|
setLinkDefaultRoute(m_gatewayIfindex, true);
|
||||||
setLinkDefaultRoute(iterator.key(), iterator.value());
|
m_gatewayIfindex = 0;
|
||||||
}
|
}
|
||||||
m_linkDefaultRoutes.clear();
|
|
||||||
|
|
||||||
for (auto iterator = m_linkDomains.constBegin();
|
for (auto iterator = m_linkDomains.constBegin();
|
||||||
iterator != m_linkDomains.constEnd(); ++iterator) {
|
iterator != m_linkDomains.constEnd(); ++iterator) {
|
||||||
@@ -155,7 +152,7 @@ bool DnsUtilsLinux::restoreResolvers() {
|
|||||||
void DnsUtilsLinux::dnsCallCompleted(QDBusPendingCallWatcher* call) {
|
void DnsUtilsLinux::dnsCallCompleted(QDBusPendingCallWatcher* call) {
|
||||||
QDBusPendingReply<> reply = *call;
|
QDBusPendingReply<> reply = *call;
|
||||||
if (reply.isError()) {
|
if (reply.isError()) {
|
||||||
logger.error() << "Error received from the DBus service";
|
logger.debug() << "DBus call failed (may be transient after systemd-resolved restart)";
|
||||||
}
|
}
|
||||||
delete call;
|
delete call;
|
||||||
}
|
}
|
||||||
@@ -223,23 +220,6 @@ void DnsUtilsLinux::setLinkDefaultRoute(int ifindex, bool enable) {
|
|||||||
SLOT(dnsCallCompleted(QDBusPendingCallWatcher*)));
|
SLOT(dnsCallCompleted(QDBusPendingCallWatcher*)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DnsUtilsLinux::updateLinkDefaultRoutes() {
|
|
||||||
const QNetworkInterface defaultIface = NetworkUtilities::getGatewayAndIface().second;
|
|
||||||
const int ifindex = defaultIface.index();
|
|
||||||
if (ifindex <= 0) {
|
|
||||||
logger.warning() << "Unable to determine default route interface";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ((ifindex == m_ifindex) || m_linkDefaultRoutes.contains(ifindex)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gateway link normally has DefaultRoute=yes. Keep behavior simple:
|
|
||||||
// disable it while VPN DNS is active and restore to yes on teardown.
|
|
||||||
m_linkDefaultRoutes[ifindex] = true;
|
|
||||||
setLinkDefaultRoute(ifindex, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DnsUtilsLinux::updateLinkDomains() {
|
void DnsUtilsLinux::updateLinkDomains() {
|
||||||
if (!m_resolver) return;
|
if (!m_resolver) return;
|
||||||
/* Get the list of search domains, and remove any others that might conspire
|
/* Get the list of search domains, and remove any others that might conspire
|
||||||
@@ -300,9 +280,15 @@ void DnsUtilsLinux::dnsDomainsReceived(QDBusPendingCallWatcher* call) {
|
|||||||
|
|
||||||
/* Add a root search domain for the new interface. */
|
/* Add a root search domain for the new interface. */
|
||||||
if (m_ifindex > 0) {
|
if (m_ifindex > 0) {
|
||||||
QList<DnsLinkDomain> newlist = {root};
|
setLinkDomains(m_ifindex, {root});
|
||||||
setLinkDomains(m_ifindex, newlist);
|
|
||||||
updateLinkDefaultRoutes();
|
/* Disable DefaultRoute on the physical gateway so systemd-resolved
|
||||||
|
* routes all DNS through the VPN interface. */
|
||||||
|
const int gwIdx = NetworkUtilities::getGatewayAndIface().second.index();
|
||||||
|
if (gwIdx > 0 && gwIdx != m_ifindex && gwIdx != m_gatewayIfindex) {
|
||||||
|
m_gatewayIfindex = gwIdx;
|
||||||
|
setLinkDefaultRoute(gwIdx, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ class DnsUtilsLinux final : public DnsUtils {
|
|||||||
void setLinkDNS(int ifindex, const QList<QHostAddress>& resolvers);
|
void setLinkDNS(int ifindex, const QList<QHostAddress>& resolvers);
|
||||||
void setLinkDomains(int ifindex, const QList<DnsLinkDomain>& domains);
|
void setLinkDomains(int ifindex, const QList<DnsLinkDomain>& domains);
|
||||||
void setLinkDefaultRoute(int ifindex, bool enable);
|
void setLinkDefaultRoute(int ifindex, bool enable);
|
||||||
void updateLinkDefaultRoutes();
|
|
||||||
void updateLinkDomains();
|
void updateLinkDomains();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
@@ -42,9 +41,9 @@ class DnsUtilsLinux final : public DnsUtils {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
int m_ifindex = 0;
|
int m_ifindex = 0;
|
||||||
|
int m_gatewayIfindex = 0;
|
||||||
int m_domainRetries = 0;
|
int m_domainRetries = 0;
|
||||||
QMap<int, DnsLinkDomainList> m_linkDomains;
|
QMap<int, DnsLinkDomainList> m_linkDomains;
|
||||||
QMap<int, bool> m_linkDefaultRoutes;
|
|
||||||
QScopedPointer<QDBusInterface> m_resolver;
|
QScopedPointer<QDBusInterface> m_resolver;
|
||||||
QString m_pendingIfname;
|
QString m_pendingIfname;
|
||||||
QList<QHostAddress> m_pendingResolvers;
|
QList<QHostAddress> m_pendingResolvers;
|
||||||
|
|||||||
Reference in New Issue
Block a user