|
33 | 33 | #include "linuxfirewall.h" |
34 | 34 | #include "logger.h" |
35 | 35 | #include "xray_defs.h" |
| 36 | +#include <QFileInfo> |
36 | 37 | #include <QProcess> |
37 | 38 |
|
38 | 39 | #define BRAND_CODE "amn" |
@@ -109,7 +110,7 @@ int LinuxFirewall::linkChain(LinuxFirewall::IPVersion ip, const QString& chain, |
109 | 110 | // (we can't safely delete all rules at once since rule numbers change) |
110 | 111 | // TODO: occasionally this script results in warnings in logs "Bad rule (does a matching rule exist in the chain?)" - this happens when |
111 | 112 | // 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)); |
113 | 114 | } |
114 | 115 | else |
115 | 116 | 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) |
501 | 502 | logger.debug() << "(" << exitCode << ") $ " << command; |
502 | 503 | if (!out.isEmpty()) |
503 | 504 | logger.info() << out; |
504 | | - if (!err.isEmpty()) |
| 505 | + if (!err.isEmpty() && !ignoreErrors) |
505 | 506 | logger.warning() << err; |
506 | 507 | return exitCode; |
507 | 508 | } |
508 | 509 |
|
509 | 510 | void LinuxFirewall::setupTrafficSplitting() |
510 | 511 | { |
| 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)); |
511 | 521 | auto cGroupDir = "/sys/fs/cgroup/net_cls/" BRAND_CODE "vpnexclusions/"; |
512 | 522 | logger.info() << "Should be setting up cgroup in" << cGroupDir << "for traffic splitting"; |
513 | 523 | execute(QStringLiteral("if [ ! -d %1 ] ; then mkdir %1 ; sleep 0.1 ; echo %2 > %1/net_cls.classid ; fi").arg(cGroupDir).arg(kCGroupId)); |
|
0 commit comments