Skip to content

Commit

Permalink
WireGuard rework for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
outspace committed Sep 17, 2023
1 parent f62076d commit 279692a
Show file tree
Hide file tree
Showing 30 changed files with 2,319 additions and 36 deletions.
11 changes: 5 additions & 6 deletions client/daemon/daemonlocalserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "leakdetector.h"
#include "logger.h"

#ifdef MZ_MACOS
#if defined(MZ_MACOS) || defined(MZ_LINUX)
# include <sys/stat.h>
# include <sys/types.h>
# include <unistd.h>
Expand Down Expand Up @@ -68,20 +68,21 @@ bool DaemonLocalServer::initialize() {
QString DaemonLocalServer::daemonPath() const {
#if defined(MZ_WINDOWS)
return "\\\\.\\pipe\\amneziavpn";
#elif defined(MZ_MACOS)
#endif
#if defined(MZ_MACOS) || defined(MZ_LINUX)
QDir dir("/var/run");
if (!dir.exists()) {
logger.warning() << "/var/run doesn't exist. Fallback /tmp.";
return TMP_PATH;
}

if (dir.exists("amneziavpn")) {
logger.debug() << "/var/run/amneziavpn seems to be usable";
logger.debug() << "/var/run/amnezia seems to be usable";
return VAR_PATH;
}

if (!dir.mkdir("amneziavpn")) {
logger.warning() << "Failed to create /var/run/amneziavpn";
logger.warning() << "Failed to create /var/run/amnezia";
return TMP_PATH;
}

Expand All @@ -92,7 +93,5 @@ QString DaemonLocalServer::daemonPath() const {
}

return VAR_PATH;
#else
# error Unsupported platform
#endif
}
4 changes: 2 additions & 2 deletions client/mozilla/networkwatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#endif

#ifdef MZ_LINUX
//# include "platforms/linux/linuxnetworkwatcher.h"
# include "platforms/linux/linuxnetworkwatcher.h"
#endif

#ifdef MZ_MACOS
Expand Down Expand Up @@ -56,7 +56,7 @@ void NetworkWatcher::initialize() {
#if defined(MZ_WINDOWS)
m_impl = new WindowsNetworkWatcher(this);
#elif defined(MZ_LINUX)
// m_impl = new LinuxNetworkWatcher(this);
m_impl = new LinuxNetworkWatcher(this);
#elif defined(MZ_MACOS)
m_impl = new MacOSNetworkWatcher(this);
#elif defined(MZ_WASM)
Expand Down
2 changes: 1 addition & 1 deletion client/mozilla/shared/ipaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ IPAddress::IPAddress(const QString& ip) {
m_prefixLength = 128;
}
} else {
Q_ASSERT(false);
// Q_ASSERT(false);
}
}

Expand Down
170 changes: 170 additions & 0 deletions client/platforms/linux/daemon/dbustypeslinux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/* 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/. */

#ifndef DBUSTYPESLINUX_H
#define DBUSTYPESLINUX_H

#include <sys/socket.h>

#include <QByteArray>
#include <QDBusArgument>
#include <QHostAddress>
#include <QtDBus/QtDBus>

/* D-Bus metatype for marshalling arguments to the SetLinkDNS method */
class DnsResolver : public QHostAddress {
public:
DnsResolver(const QHostAddress& address = QHostAddress())
: QHostAddress(address) {}

friend QDBusArgument& operator<<(QDBusArgument& args, const DnsResolver& ip) {
args.beginStructure();
if (ip.protocol() == QAbstractSocket::IPv6Protocol) {
Q_IPV6ADDR addrv6 = ip.toIPv6Address();
args << AF_INET6;
args << QByteArray::fromRawData((const char*)&addrv6, sizeof(addrv6));
} else {
quint32 addrv4 = ip.toIPv4Address();
QByteArray data(4, 0);
data[0] = (addrv4 >> 24) & 0xff;
data[1] = (addrv4 >> 16) & 0xff;
data[2] = (addrv4 >> 8) & 0xff;
data[3] = (addrv4 >> 0) & 0xff;
args << AF_INET;
args << data;
}
args.endStructure();
return args;
}
friend const QDBusArgument& operator>>(const QDBusArgument& args,
DnsResolver& ip) {
int family;
QByteArray data;
args.beginStructure();
args >> family >> data;
args.endStructure();
if (family == AF_INET6) {
ip.setAddress(data.constData());
} else if (data.count() >= 4) {
quint32 addrv4 = 0;
addrv4 |= (data[0] << 24);
addrv4 |= (data[1] << 16);
addrv4 |= (data[2] << 8);
addrv4 |= (data[3] << 0);
ip.setAddress(addrv4);
}
return args;
}
};
typedef QList<DnsResolver> DnsResolverList;
Q_DECLARE_METATYPE(DnsResolver);
Q_DECLARE_METATYPE(DnsResolverList);

/* D-Bus metatype for marshalling arguments to the SetLinkDomains method */
class DnsLinkDomain {
public:
DnsLinkDomain(const QString d = "", bool s = false) {
domain = d;
search = s;
};
QString domain;
bool search;

friend QDBusArgument& operator<<(QDBusArgument& args,
const DnsLinkDomain& data) {
args.beginStructure();
args << data.domain << data.search;
args.endStructure();
return args;
}
friend const QDBusArgument& operator>>(const QDBusArgument& args,
DnsLinkDomain& data) {
args.beginStructure();
args >> data.domain >> data.search;
args.endStructure();
return args;
}
bool operator==(const DnsLinkDomain& other) const {
return (domain == other.domain) && (search == other.search);
}
bool operator==(const QString& other) const { return (domain == other); }
};
typedef QList<DnsLinkDomain> DnsLinkDomainList;
Q_DECLARE_METATYPE(DnsLinkDomain);
Q_DECLARE_METATYPE(DnsLinkDomainList);

/* D-Bus metatype for marshalling the Domains property */
class DnsDomain {
public:
DnsDomain() {}
int ifindex = 0;
QString domain = "";
bool search = false;

friend QDBusArgument& operator<<(QDBusArgument& args, const DnsDomain& data) {
args.beginStructure();
args << data.ifindex << data.domain << data.search;
args.endStructure();
return args;
}
friend const QDBusArgument& operator>>(const QDBusArgument& args,
DnsDomain& data) {
args.beginStructure();
args >> data.ifindex >> data.domain >> data.search;
args.endStructure();
return args;
}
};
typedef QList<DnsDomain> DnsDomainList;
Q_DECLARE_METATYPE(DnsDomain);
Q_DECLARE_METATYPE(DnsDomainList);

/* D-Bus metatype for marshalling the freedesktop login manager data. */
class UserData {
public:
QString name;
uint userid;
QDBusObjectPath path;

friend QDBusArgument& operator<<(QDBusArgument& args, const UserData& data) {
args.beginStructure();
args << data.userid << data.name << data.path;
args.endStructure();
return args;
}
friend const QDBusArgument& operator>>(const QDBusArgument& args,
UserData& data) {
args.beginStructure();
args >> data.userid >> data.name >> data.path;
args.endStructure();
return args;
}
};
typedef QList<UserData> UserDataList;
Q_DECLARE_METATYPE(UserData);
Q_DECLARE_METATYPE(UserDataList);

class DnsMetatypeRegistrationProxy {
public:
DnsMetatypeRegistrationProxy() {
qRegisterMetaType<DnsResolver>();
qDBusRegisterMetaType<DnsResolver>();
qRegisterMetaType<DnsResolverList>();
qDBusRegisterMetaType<DnsResolverList>();
qRegisterMetaType<DnsLinkDomain>();
qDBusRegisterMetaType<DnsLinkDomain>();
qRegisterMetaType<DnsLinkDomainList>();
qDBusRegisterMetaType<DnsLinkDomainList>();
qRegisterMetaType<DnsDomain>();
qDBusRegisterMetaType<DnsDomain>();
qRegisterMetaType<DnsDomainList>();
qDBusRegisterMetaType<DnsDomainList>();
qRegisterMetaType<UserData>();
qDBusRegisterMetaType<UserData>();
qRegisterMetaType<UserDataList>();
qDBusRegisterMetaType<UserDataList>();
}
};

#endif // DBUSTYPESLINUX_H
Loading

0 comments on commit 279692a

Please sign in to comment.