Skip to content

Commit 31e740e

Browse files
committed
fix: bad rule xargs, cgroup v2 mkdir, amnvpnrt rt_tables, restart flush
1 parent 9bf974a commit 31e740e

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

client/platforms/linux/daemon/dnsutilslinux.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "core/utils/networkUtilities.h"
1515
#include "leakdetector.h"
1616
#include "logger.h"
17+
#include "router_linux.h"
1718

1819
constexpr const char* DBUS_RESOLVE_SERVICE = "org.freedesktop.resolve1";
1920
constexpr const char* DBUS_RESOLVE_PATH = "/org/freedesktop/resolve1";
@@ -159,6 +160,8 @@ void DnsUtilsLinux::dnsCallCompleted(QDBusPendingCallWatcher* call) {
159160
QDBusPendingReply<> reply = *call;
160161
if (reply.isError()) {
161162
logger.debug() << "DBus call failed (may be transient after systemd-resolved restart)";
163+
logger.debug() << "Restarting resolved to clear its query backlog";
164+
RouterLinux::Instance().flushDns();
162165
scheduleRetry();
163166
}
164167
delete call;

client/platforms/linux/daemon/linuxfirewall.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "linuxfirewall.h"
3434
#include "logger.h"
3535
#include "xray_defs.h"
36+
#include <QFileInfo>
3637
#include <QProcess>
3738

3839
#define BRAND_CODE "amn"
@@ -109,7 +110,7 @@ int LinuxFirewall::linkChain(LinuxFirewall::IPVersion ip, const QString& chain,
109110
// (we can't safely delete all rules at once since rule numbers change)
110111
// TODO: occasionally this script results in warnings in logs "Bad rule (does a matching rule exist in the chain?)" - this happens when
111112
// the e.g OUTPUT chain is empty but this script attempts to delete things from it anyway. It doesn't cause any problems, but we should still fix at some point..
112-
return execute(QStringLiteral("if ! %1 -L %2 -n --line-numbers -t %4 2> /dev/null | awk 'int($1) == 1 && $2 == \"%3\" { found=1 } END { if(found==1) { exit 0 } else { exit 1 } }' ; then %1 -I %2 -j %3 -t %4 && %1 -L %2 -n --line-numbers -t %4 2> /dev/null | awk 'int($1) > 1 && $2 == \"%3\" { print $1; exit }' | xargs %1 -t %4 -D %2 ; fi").arg(cmd, parent, chain, tableName));
113+
return execute(QStringLiteral("if ! %1 -L %2 -n --line-numbers -t %4 2> /dev/null | awk 'int($1) == 1 && $2 == \"%3\" { found=1 } END { if(found==1) { exit 0 } else { exit 1 } }' ; then %1 -I %2 -j %3 -t %4 && %1 -L %2 -n --line-numbers -t %4 2> /dev/null | awk 'int($1) > 1 && $2 == \"%3\" { print $1; exit }' | xargs -r %1 -t %4 -D %2 ; fi").arg(cmd, parent, chain, tableName));
113114
}
114115
else
115116
return execute(QStringLiteral("if ! %1 -C %2 -j %3 -t %4 2> /dev/null ; then %1 -A %2 -j %3 -t %4; fi").arg(cmd, parent, chain, tableName));
@@ -501,13 +502,22 @@ int LinuxFirewall::execute(const QString &command, bool ignoreErrors)
501502
logger.debug() << "(" << exitCode << ") $ " << command;
502503
if (!out.isEmpty())
503504
logger.info() << out;
504-
if (!err.isEmpty())
505+
if (!err.isEmpty() && !ignoreErrors)
505506
logger.warning() << err;
506507
return exitCode;
507508
}
508509

509510
void LinuxFirewall::setupTrafficSplitting()
510511
{
512+
const QString cgroupBase = QStringLiteral("/sys/fs/cgroup/net_cls");
513+
if (!QFileInfo::exists(cgroupBase)) {
514+
logger.warning() << "net_cls cgroup v1 not available, traffic splitting disabled";
515+
return;
516+
}
517+
execute(QStringLiteral(
518+
"if ! grep -qE '^[0-9]+[[:space:]]+%1$' /etc/iproute2/rt_tables 2>/dev/null ; then "
519+
"echo '200 %1' >> /etc/iproute2/rt_tables ; fi"
520+
).arg(kRtableName));
511521
auto cGroupDir = "/sys/fs/cgroup/net_cls/" BRAND_CODE "vpnexclusions/";
512522
logger.info() << "Should be setting up cgroup in" << cGroupDir << "for traffic splitting";
513523
execute(QStringLiteral("if [ ! -d %1 ] ; then mkdir %1 ; sleep 0.1 ; echo %2 > %1/net_cls.classid ; fi").arg(cGroupDir).arg(kCGroupId));

service/server/router_linux.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,17 @@ bool RouterLinux::flushDns()
167167

168168
//check what the dns manager use
169169
if (isServiceActive("nscd.service")) {
170-
qDebug() << "Flushing nscd cache";
171-
p.start("nscd", { "--invalidate=hosts" });
170+
qDebug() << "Restarting nscd.service";
171+
p.start("systemctl", { "restart", "nscd" });
172172
} else if (isServiceActive("systemd-resolved.service")) {
173-
qDebug() << "Flushing systemd-resolved DNS cache";
174-
p.start("resolvectl", { "flush-caches" });
173+
qDebug() << "Restarting systemd-resolved.service";
174+
p.start("systemctl", { "restart", "systemd-resolved" });
175175
} else {
176176
qDebug() << "No suitable DNS manager found.";
177177
return false;
178178
}
179179

180-
p.waitForFinished(3000);
180+
p.waitForFinished();
181181
QByteArray output = p.readAll();
182182
if ((p.exitStatus() != QProcess::NormalExit) || (p.exitCode() != 0)) {
183183
qDebug().noquote() << "Failed to flush DNS: " + output;

0 commit comments

Comments
 (0)