Skip to content

Commit

Permalink
Fix the DNS handling in StateOnPartial
Browse files Browse the repository at this point in the history
wip fix dns

Add test code

FIX test code

remove unsued tests

cleanup
  • Loading branch information
strseb committed Feb 6, 2025
1 parent cc44821 commit b30ebaa
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/cmake/sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ target_sources(mozillavpn-sources INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/controller.h
${CMAKE_CURRENT_SOURCE_DIR}/controllerimpl.cpp
${CMAKE_CURRENT_SOURCE_DIR}/controllerimpl.h
${CMAKE_CURRENT_SOURCE_DIR}/controller_p.cpp
${CMAKE_CURRENT_SOURCE_DIR}/controller_p.h
${CMAKE_CURRENT_SOURCE_DIR}/daemon/daemon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/daemon/daemon.h
${CMAKE_CURRENT_SOURCE_DIR}/daemon/daemonlocalserverconnection.cpp
Expand Down
29 changes: 5 additions & 24 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

#include "controller.h"

#include <qhostaddress.h>

#include <QFileInfo>
#include <QNetworkInformation>

#include "app.h"
#include "constants.h"
#include "controller_p.h"
#include "controllerimpl.h"
#include "dnshelper.h"
#include "feature/feature.h"
Expand Down Expand Up @@ -46,12 +49,6 @@
# include "platforms/wasm/wasmcontroller.h"
#endif

// The Mullvad proxy services are located at internal IPv4 addresses in the
// 10.124.0.0/20 address range, which is a subset of the 10.0.0.0/8 Class-A
// private address range.
constexpr const char* MULLVAD_PROXY_RANGE = "10.124.0.0";
constexpr const int MULLVAD_PROXY_RANGE_LENGTH = 20;

namespace {
Logger logger("Controller");

Expand All @@ -76,6 +73,8 @@ Controller::Reason stateToReason(Controller::State state) {
}
} // namespace

using namespace ControllerPrivate;

Controller::Controller() {
MZ_COUNT_CTOR(Controller);

Expand Down Expand Up @@ -591,24 +590,6 @@ QList<IPAddress> Controller::getAllowedIPAddressRanges(
return list;
}

// static
QList<IPAddress> Controller::getExtensionProxyAddressRanges(
const Server& exitServer) {
QList<IPAddress> ranges = {
IPAddress(QHostAddress(exitServer.ipv4Gateway()), 32),
IPAddress(QHostAddress{MULLVAD_PROXY_RANGE}, MULLVAD_PROXY_RANGE_LENGTH)};
if (!exitServer.ipv6Gateway().isEmpty()) {
ranges.append(IPAddress(QHostAddress(exitServer.ipv6Gateway()), 128));
}

auto const dns = DNSHelper::getDNSDetails(exitServer.ipv4Gateway());
if (dns.dnsType != "Default" && dns.dnsType != "Custom") {
ranges.append(IPAddress(QHostAddress(dns.ipAddress), 32));
}

return ranges;
}

void Controller::activateNext() {
MozillaVPN* vpn = MozillaVPN::instance();
const Device* device = vpn->deviceModel()->currentDevice(vpn->keys());
Expand Down
4 changes: 0 additions & 4 deletions src/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#include <QTimer>

#include "interfaceconfig.h"
#include "ipaddress.h"
#include "loghandler.h"
#include "models/server.h"
#include "models/serverdata.h"
#include "pinghelper.h"

Expand Down Expand Up @@ -169,8 +167,6 @@ class Controller : public QObject, public LogSerializer {
};
static IPAddressList getExcludedIPAddressRanges();
static QList<IPAddress> getAllowedIPAddressRanges(const Server& server);
static QList<IPAddress> getExtensionProxyAddressRanges(
const Server& exitServer);

enum ServerCoolDownPolicyForSilentSwitch {
eServerCoolDownNeeded,
Expand Down
56 changes: 56 additions & 0 deletions src/controller_p.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* 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 "controller_p.h"

#include <qassert.h>
#include <qhostaddress.h>

#include "dnshelper.h"
#include "ipaddress.h"
#include "models/server.h"
#include "rfc/rfc1112.h"
#include "rfc/rfc1918.h"
#include "rfc/rfc4193.h"
#include "rfc/rfc4291.h"

namespace ControllerPrivate {

QList<IPAddress> getExtensionProxyAddressRanges(
const Server& exitServer, std::optional<const dnsData> dnsServer) {
QList<IPAddress> ranges = {
IPAddress(QHostAddress(exitServer.ipv4Gateway()), 32),
IPAddress(QHostAddress{MULLVAD_PROXY_RANGE}, MULLVAD_PROXY_RANGE_LENGTH)};
if (!exitServer.ipv6Gateway().isEmpty()) {
ranges.append(IPAddress(QHostAddress(exitServer.ipv6Gateway()), 128));
}

const dnsData dns = [&dnsServer, &exitServer]() {
if (dnsServer.has_value()) {
return dnsServer.value();
}
return DNSHelper::getDNSDetails(exitServer.ipv4Gateway());
}();

if (dns.dnsType == "Default") {
// The DNS Adress is already Resolved.
return ranges;
}
if (dns.dnsType != "Custom") {
// It's a vpn-internal DNS, always add it
ranges.append(IPAddress(QHostAddress(dns.ipAddress), 32));
return ranges;
}
Q_ASSERT(dns.dnsType == "Custom");
const QHostAddress addr{dns.ipAddress};
// If it is a custom dns only add it to the tunnel if it is **NOT**
// a "lan" adress
if (!RFC1918::contains(addr) && !RFC4193::contains(addr) &&
!addr.isLoopback()) {
ranges.append(IPAddress(QHostAddress(dns.ipAddress), 32));
}
return ranges;
}

} // namespace ControllerPrivate
33 changes: 33 additions & 0 deletions src/controller_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* 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/. */

#pragma once

#include <QHostAddress>
#include <QList>

#include "dnshelper.h"

class IPAddress;
class Server;

/**
* Controller.cpp is currently mock'ed out of the unit tests
* stopping us from testing it. This namespace should contain
* pure functions so that we can start writing unit-tests for that code.
*
*/
namespace ControllerPrivate {

// The Mullvad proxy services are located at internal IPv4 addresses in the
// 10.124.0.0/20 address range, which is a subset of the 10.0.0.0/8 Class-A
// private address range.
constexpr const char* MULLVAD_PROXY_RANGE = "10.124.0.0";
constexpr const int MULLVAD_PROXY_RANGE_LENGTH = 20;

QList<IPAddress> getExtensionProxyAddressRanges(
const Server& exitServer,
std::optional<const dnsData> dnsServer = std::nullopt);

} // namespace ControllerPrivate
6 changes: 6 additions & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ target_sources(unit_tests PRIVATE
${MZ_SOURCE_DIR}/constants.cpp
${MZ_SOURCE_DIR}/constants.h
${MZ_SOURCE_DIR}/controller.h
${MZ_SOURCE_DIR}/controller_p.h
${MZ_SOURCE_DIR}/controller_p.cpp
${MZ_SOURCE_DIR}/dnshelper.cpp
${MZ_SOURCE_DIR}/dnshelper.h
${MZ_SOURCE_DIR}/dnspingsender.cpp
Expand Down Expand Up @@ -248,6 +250,8 @@ target_sources(unit_tests PRIVATE
${MZ_SOURCE_DIR}/qmlpath.h
${MZ_SOURCE_DIR}/resourceloader.cpp
${MZ_SOURCE_DIR}/resourceloader.h
${MZ_SOURCE_DIR}/rfc/rfc1112.cpp
${MZ_SOURCE_DIR}/rfc/rfc1112.h
${MZ_SOURCE_DIR}/rfc/rfc1918.cpp
${MZ_SOURCE_DIR}/rfc/rfc1918.h
${MZ_SOURCE_DIR}/rfc/rfc4193.cpp
Expand Down Expand Up @@ -325,6 +329,8 @@ target_sources(unit_tests PRIVATE
testaddon.h
testconnectionhealth.cpp
testconnectionhealth.h
testcontroller_p.cpp
testcontroller_p.h
testcommandlineparser.cpp
testcommandlineparser.h
testcryptosettings.cpp
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <QCoreApplication>
#include <QScopeguard>

#include "constants.h"
#include "glean/mzglean.h"
Expand All @@ -13,6 +14,17 @@
#include "networkrequest.h"
#include "settingsholder.h"

#ifdef _WIN32
# include <windows.h>

# include <chrono>
# include <thread>
#else
# include <unistd.h>

# include <cstdio>
#endif

QVector<TestHelper::NetworkConfig> TestHelper::networkConfig;
Controller::State TestHelper::controllerState = Controller::StateInitializing;
QVector<QObject*> TestHelper::testList;
Expand All @@ -29,6 +41,58 @@ QObject* TestHelper::findTest(const QString& name) {
return nullptr;
}

void maybeWaitForDebugger(int argc, char* argv[]) {
bool debuggerFlag = false;
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--debugger") {
debuggerFlag = true;
break;
}
}
if (!debuggerFlag) {
// Not asked to wait for a debugger; just return.
return;
}
#ifdef _WIN32
DWORD pid = GetCurrentProcessId();
#else
pid_t pid = getpid();
#endif
qDebug() << "PID: " << pid << " - waiting for debugger to attach...\n";

#ifdef _WIN32
// Use Win32 API to detect debugger
while (!IsDebuggerPresent()) {
// Sleep 1 second before checking again
std::this_thread::sleep_for(std::chrono::seconds(1));
}
qDebug() << "Debugger attached. Continuing...\n";

return;
#else
// TODO: Check if that also works on macos
while (true) {
QFile file("/proc/self/status");
auto closeFile = qScopeGuard([&]() { file.close(); });

if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return;
}
while (!file.atEnd()) {
QString line = file.readLine();
if (line.startsWith("TracerPid:")) {
QString pidStr = line.mid(QString("TracerPid:").length()).trimmed();
if (pidStr.toInt() != 0) {
qDebug() << "Debugger attached. Continuing...\n";
return;
}
}
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
#endif
}

TestHelper::TestHelper() { testList.append(this); }

// static
Expand Down Expand Up @@ -64,6 +128,7 @@ bool TestHelper::networkRequestGeneric(NetworkRequest* request) {
}

int main(int argc, char* argv[]) {
maybeWaitForDebugger(argc, argv);
#ifdef MZ_DEBUG
LeakDetector leakDetector;
Q_UNUSED(leakDetector);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/testcommandlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void TestCommandLineParser::basic() {
char** argv = (char**)malloc(sizeof(char*) * argc);

for (int i = 0; i < args.length(); ++i) {
argv[i] = strdup(args[i].toLocal8Bit().data());
argv[i] = _strdup(args[i].toLocal8Bit().data());
}

QFETCH(int, result);
Expand Down
Loading

0 comments on commit b30ebaa

Please sign in to comment.