mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-05-08 14:33:23 +00:00
* refactor: move business logic from servers model * refactor: move containersModel initialization * refactor: added protocol ui controller and removed settings class from protocols model * refactor: moved cli management to separate controller * refactor: moved app split to separate controller * refactor: moved site split to separate controller * refactor: moved allowed dns to separate controller * refactor: moved language logic to separate ui controller * refactor: removed Settings from devices model * refactor: moved configs and services api logit to separate core controller * refactor: added a layer with a repository between the storage and controllers * refactor: use child parent system instead of smart pointers for controllers and models initialization * refactor: moved install functions from server controller to install controller * refactor: install controller refactoring * chore: renamed exportController to exportUiController * refactor: separate export controller * refactor: removed VpnConfigurationsController * chore: renamed ServerController to SshSession * refactor: replaced ServerController to SshSession * chore: moved qml controllers to separate folder * chore: include fixes * chore: moved utils from core root to core/utils * chore: include fixes * chore: rename core/utils files to camelCase foramt * chore: include fixes * chore: moved some utils to api and selfhosted folders * chore: include fixes * chore: remove unused file * chore: moved serialization folder to core/utils * chore: include fixes * chore: moved some files from client root to core/utils * chore: include fixes * chore: moved ui utils to ui/utils folder * chore: include fixes * chore: move utils from root to ui/utils * chore: include fixes * chore: moved configurators to core/configurators * chore: include fixes * refactor: moved iap logic from ui controller to core * refactor: moved remaining core logic from ApiConfigsController to SubscriptionController * chore: rename apiNewsController to apiNewsUiController * refactor: moved core logic from news ui controller to core * chore: renamed apiConfigsController to subscriptionUiController * chore: include fixes * refactor: merge ApiSettingsController with SubscriptionUiController * chore: moved ui selfhosted controllers to separate folder * chore: include fixes * chore: rename connectionController to connectiomUiController * refactor: moved core logic from connectionUiController * chore: rename settingsController to settingsUiController * refactor: move core logic from settingsUiController * refactor: moved core controller signal/slot connections to separate class * fix: newsController fixes after refactoring * chore: rename model to camelCase * chore: include fixes * chore: remove unused code * chore: move selfhosted core to separate folder * chore: include fixes * chore: rename importController to importUiController * refactor: move core logic from importUiController * chore: minor fixes * chore: remove prem v1 migration * refactor: remove openvpn over cloak and openvpn over shadowsocks * refactor: removed protocolsForContainer function * refactor: add core models * refactor: replace json with c++ structs for server config * refactor: move getDnsPair to ServerConfigUtils * feat: add admin selfhosted config export test * feat: add multi import test * refactor: use coreController for tests * feat: add few simple tests * chore: qrepos in all core controllers * feat: add test for settings * refactor: remove repo dependency from configurators * chore: moved protocols to core folder * chore: include fixes * refactor: moved containersDefs, defs, apiDefs, protocolsDefs to different places * chore: include fixes * chore: build fixes * chore: build fixes * refactor: remove q repo and interface repo * feat: add test for ui servers model and controller * chore: renamed to camelCase * chore: include fixes * refactor: moved core logic from sites ui controller * fix: fixed api config processing * fix: fixed processed server index processing * refactor: protocol models now use c++ structs instead of json configs * refactor: servers model now use c++ struct instead of json config * fix: fixed default server index processing * fix: fix logs init * fix: fix secure settings load keys * chore: build fixes * fix: fixed clear settings * fix: fixed restore backup * fix: sshSession usage * fix: fixed export functions signatures * fix: return missing part from buildContainerWorker * fix: fixed server description on page home * refactor: add container config helpers functions * refactor: c++ structs instead of json * chore: add dns protocol config struct * refactor: move config utils functions to config structs * feat: add test for selfhosted server setup * refactor: separate resources.qrc * fix: fixed server rename * chore: return nameOverriddenByUser * fix: build fixes * fix: fixed models init * refactor: cleanup models usage * fix: fixed models init * chore: cleanup connections and functions signatures * chore: cleanup updateModel calls * feat: added cache to servers repo * chore: cleanup unused functions * chore: ssxray processing * chore: remove transportProtoWithDefault and portWithDefault functions * chore: removed proto types any and l2tp * refactor: moved some constants * fix: fixed native configs export * refactor: remove json from processConfigWith functions * fix: fixed processed server index usage * fix: qml warning fixes * chore: merge fixes * chore: update tests * fix: fixed xray config processing * fix: fixed split tunneling processing * chore: rename sites controllers and model * chore: rename fixes * chore: minor fixes * chore: remove ability to load backup from "file with connection settings" button * fix: fixed api device revoke * fix: remove full model update when renaming a user * fix: fixed premium/free server rename * fix: fixed selfhosted new server install * fix: fixed updateContainer function * fix: fixed revoke for external premium configs * feat: add native configs qr processing * chore: codestyle fixes * fix: fixed admin config create * chore: again remove ability to load backup from "file with connection settings" button * chore: minor fixes * fix: fixed variables initialization * fix: fixed qml imports * fix: minor fixes * fix: fix vpnConnection function calls * feat: add buckup error handling * fix: fixed admin config revok * fix: fixed selfhosted awg installation * fix: ad visability * feat: add empty check for primary dns * chore: minor fixes
246 lines
8.0 KiB
C++
246 lines
8.0 KiB
C++
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "linuxroutemonitor.h"
|
|
|
|
#include <QNetworkInterface>
|
|
#include <QCoreApplication>
|
|
#include <QProcess>
|
|
#include <QScopeGuard>
|
|
#include <QTimer>
|
|
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <arpa/inet.h>
|
|
#include <linux/netlink.h>
|
|
#include <linux/rtnetlink.h>
|
|
#include <net/if.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/socket.h>
|
|
|
|
#include "core/utils/utilities.h"
|
|
#include "leakdetector.h"
|
|
#include "logger.h"
|
|
#include "core/utils/networkUtilities.h"
|
|
|
|
namespace {
|
|
Logger logger("LinuxRouteMonitor");
|
|
} // namespace
|
|
|
|
|
|
typedef struct wg_allowedip {
|
|
uint16_t family;
|
|
union {
|
|
struct in_addr ip4;
|
|
struct in6_addr ip6;
|
|
};
|
|
uint8_t cidr;
|
|
struct wg_allowedip *next_allowedip;
|
|
} wg_allowedip;
|
|
|
|
constexpr const char* WG_INTERFACE = "amn0";
|
|
|
|
static void nlmsg_append_attr(struct nlmsghdr* nlmsg, size_t maxlen,
|
|
int attrtype, const void* attrdata,
|
|
size_t attrlen);
|
|
static void nlmsg_append_attr32(struct nlmsghdr* nlmsg, size_t maxlen,
|
|
int attrtype, uint32_t value);
|
|
|
|
static bool buildAllowedIp(wg_allowedip* ip, const IPAddress& prefix);
|
|
|
|
|
|
LinuxRouteMonitor::LinuxRouteMonitor(const QString& ifname, QObject* parent)
|
|
: QObject(parent), m_ifname(ifname) {
|
|
MZ_COUNT_CTOR(LinuxRouteMonitor);
|
|
logger.debug() << "LinuxRouteMonitor created.";
|
|
|
|
m_nlsock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
|
|
if (m_nlsock < 0) {
|
|
logger.warning() << "Failed to create netlink socket:" << strerror(errno);
|
|
}
|
|
|
|
struct sockaddr_nl nladdr;
|
|
memset(&nladdr, 0, sizeof(nladdr));
|
|
nladdr.nl_family = AF_NETLINK;
|
|
nladdr.nl_pid = getpid();
|
|
if (bind(m_nlsock, (struct sockaddr*)&nladdr, sizeof(nladdr)) != 0) {
|
|
logger.warning() << "Failed to bind netlink socket:" << strerror(errno);
|
|
}
|
|
|
|
m_notifier = new QSocketNotifier(m_nlsock, QSocketNotifier::Read, this);
|
|
connect(m_notifier, &QSocketNotifier::activated, this,
|
|
&LinuxRouteMonitor::nlsockReady);
|
|
}
|
|
|
|
LinuxRouteMonitor::~LinuxRouteMonitor() {
|
|
MZ_COUNT_DTOR(LinuxRouteMonitor);
|
|
if (m_nlsock >= 0) {
|
|
close(m_nlsock);
|
|
}
|
|
logger.debug() << "WireguardUtilsLinux destroyed.";
|
|
}
|
|
|
|
// Compare memory against zero.
|
|
static int memcmpzero(const void* data, size_t len) {
|
|
const quint8* ptr = static_cast<const quint8*>(data);
|
|
while (len--) {
|
|
if (*ptr++) return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
bool LinuxRouteMonitor::insertRoute(const IPAddress& prefix) {
|
|
logger.debug() << "Adding route to" << prefix.toString();
|
|
|
|
const int flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE | NLM_F_ACK;
|
|
return rtmSendRoute(RTM_NEWROUTE, flags, RTN_UNICAST, prefix);
|
|
}
|
|
|
|
bool LinuxRouteMonitor::deleteRoute(const IPAddress& prefix) {
|
|
logger.debug() << "Removing route to" << prefix.toString();
|
|
|
|
const int flags = NLM_F_REQUEST | NLM_F_ACK;
|
|
return rtmSendRoute(RTM_DELROUTE, flags, RTN_UNICAST, prefix);
|
|
}
|
|
|
|
bool LinuxRouteMonitor::addExclusionRoute(const IPAddress& prefix) {
|
|
logger.debug() << "Adding exclusion route for"
|
|
<< prefix.toString();
|
|
const int flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE | NLM_F_ACK;
|
|
return rtmSendRoute(RTM_NEWROUTE, flags, RTN_THROW, prefix);
|
|
}
|
|
|
|
bool LinuxRouteMonitor::deleteExclusionRoute(const IPAddress& prefix) {
|
|
logger.debug() << "Removing exclusion route for"
|
|
<< prefix.toString();
|
|
const int flags = NLM_F_REQUEST | NLM_F_ACK;
|
|
return rtmSendRoute(RTM_DELROUTE, flags, RTN_THROW, prefix);
|
|
}
|
|
|
|
bool LinuxRouteMonitor::rtmSendRoute(int action, int flags, int type,
|
|
const IPAddress& prefix) {
|
|
constexpr size_t rtm_max_size = sizeof(struct rtmsg) +
|
|
2 * RTA_SPACE(sizeof(uint32_t)) +
|
|
RTA_SPACE(sizeof(struct in6_addr));
|
|
wg_allowedip ip;
|
|
if (!buildAllowedIp(&ip, prefix)) {
|
|
logger.warning() << "Invalid destination prefix";
|
|
return false;
|
|
}
|
|
|
|
char buf[NLMSG_SPACE(rtm_max_size)];
|
|
struct nlmsghdr* nlmsg = reinterpret_cast<struct nlmsghdr*>(buf);
|
|
struct rtmsg* rtm = static_cast<struct rtmsg*>(NLMSG_DATA(nlmsg));
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
nlmsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
|
nlmsg->nlmsg_type = action;
|
|
nlmsg->nlmsg_flags = flags;
|
|
nlmsg->nlmsg_pid = getpid();
|
|
nlmsg->nlmsg_seq = m_nlseq++;
|
|
rtm->rtm_dst_len = ip.cidr;
|
|
rtm->rtm_family = ip.family;
|
|
rtm->rtm_type = type;
|
|
rtm->rtm_table = RT_TABLE_UNSPEC;
|
|
rtm->rtm_protocol = RTPROT_BOOT;
|
|
rtm->rtm_scope = RT_SCOPE_UNIVERSE;
|
|
|
|
if (rtm->rtm_family == AF_INET6) {
|
|
nlmsg_append_attr(nlmsg, sizeof(buf), RTA_DST, &ip.ip6, sizeof(ip.ip6));
|
|
} else {
|
|
nlmsg_append_attr(nlmsg, sizeof(buf), RTA_DST, &ip.ip4, sizeof(ip.ip4));
|
|
}
|
|
|
|
if (rtm->rtm_type == RTN_UNICAST) {
|
|
int index = if_nametoindex(WG_INTERFACE);
|
|
|
|
if (index <= 0) {
|
|
logger.error() << "if_nametoindex() failed:" << strerror(errno);
|
|
return false;
|
|
}
|
|
nlmsg_append_attr32(nlmsg, sizeof(buf), RTA_OIF, index);
|
|
nlmsg_append_attr32(nlmsg, sizeof(buf), RTA_PRIORITY, 1);
|
|
}
|
|
|
|
if (rtm->rtm_type == RTN_THROW) {
|
|
QString gateway = NetworkUtilities::getGatewayAndIface().first;
|
|
if (gateway.isEmpty()) {
|
|
logger.warning() << "No default gateway available, skipping exclusion route";
|
|
return false;
|
|
}
|
|
struct in_addr ip4;
|
|
inet_pton(AF_INET, gateway.toUtf8(), &ip4);
|
|
nlmsg_append_attr(nlmsg, sizeof(buf), RTA_GATEWAY, &ip4, sizeof(ip4));
|
|
nlmsg_append_attr32(nlmsg, sizeof(buf), RTA_PRIORITY, 0);
|
|
rtm->rtm_type = RTN_UNICAST;
|
|
}
|
|
|
|
struct sockaddr_nl nladdr;
|
|
memset(&nladdr, 0, sizeof(nladdr));
|
|
nladdr.nl_family = AF_NETLINK;
|
|
size_t result = sendto(m_nlsock, buf, nlmsg->nlmsg_len, 0,
|
|
(struct sockaddr*)&nladdr, sizeof(nladdr));
|
|
|
|
return (result == nlmsg->nlmsg_len);
|
|
}
|
|
|
|
static void nlmsg_append_attr(struct nlmsghdr* nlmsg, size_t maxlen,
|
|
int attrtype, const void* attrdata,
|
|
size_t attrlen) {
|
|
size_t newlen = NLMSG_ALIGN(nlmsg->nlmsg_len) + RTA_SPACE(attrlen);
|
|
if (newlen <= maxlen) {
|
|
char* buf = reinterpret_cast<char*>(nlmsg) + NLMSG_ALIGN(nlmsg->nlmsg_len);
|
|
struct rtattr* attr = reinterpret_cast<struct rtattr*>(buf);
|
|
attr->rta_type = attrtype;
|
|
attr->rta_len = RTA_LENGTH(attrlen);
|
|
memcpy(RTA_DATA(attr), attrdata, attrlen);
|
|
nlmsg->nlmsg_len = newlen;
|
|
}
|
|
}
|
|
|
|
static void nlmsg_append_attr32(struct nlmsghdr* nlmsg, size_t maxlen,
|
|
int attrtype, uint32_t value) {
|
|
nlmsg_append_attr(nlmsg, maxlen, attrtype, &value, sizeof(value));
|
|
}
|
|
|
|
void LinuxRouteMonitor::nlsockReady() {
|
|
char buf[1024];
|
|
ssize_t len = recv(m_nlsock, buf, sizeof(buf), MSG_DONTWAIT);
|
|
if (len <= 0) {
|
|
return;
|
|
}
|
|
|
|
struct nlmsghdr* nlmsg = (struct nlmsghdr*)buf;
|
|
while (NLMSG_OK(nlmsg, len)) {
|
|
if (nlmsg->nlmsg_type == NLMSG_DONE) {
|
|
return;
|
|
}
|
|
if (nlmsg->nlmsg_type != NLMSG_ERROR) {
|
|
nlmsg = NLMSG_NEXT(nlmsg, len);
|
|
continue;
|
|
}
|
|
struct nlmsgerr* err = static_cast<struct nlmsgerr*>(NLMSG_DATA(nlmsg));
|
|
if (err->error != 0) {
|
|
logger.debug() << "Netlink request failed:" << strerror(-err->error);
|
|
}
|
|
nlmsg = NLMSG_NEXT(nlmsg, len);
|
|
}
|
|
}
|
|
|
|
static bool buildAllowedIp(wg_allowedip* ip,
|
|
const IPAddress& prefix) {
|
|
const char* addrString = qPrintable(prefix.address().toString());
|
|
if (prefix.type() == QAbstractSocket::IPv4Protocol) {
|
|
ip->family = AF_INET;
|
|
ip->cidr = prefix.prefixLength();
|
|
return inet_pton(AF_INET, addrString, &ip->ip4) == 1;
|
|
}
|
|
if (prefix.type() == QAbstractSocket::IPv6Protocol) {
|
|
ip->family = AF_INET6;
|
|
ip->cidr = prefix.prefixLength();
|
|
return inet_pton(AF_INET6, addrString, &ip->ip6) == 1;
|
|
}
|
|
return false;
|
|
}
|