Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions src/daemon/daemon_config.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2018 Canonical, Ltd.
* Copyright (C) 2017-2020 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -31,6 +31,8 @@
#include <multipass/utils.h>

#include <QStandardPaths>
#include <QString>
#include <QUrl>

#include <chrono>
#include <memory>
Expand All @@ -51,6 +53,39 @@ std::string server_name_from(const std::string& server_address)
return "localhost";
return server_name;
}

std::unique_ptr<QNetworkProxy> discover_http_proxy()
{
std::unique_ptr<QNetworkProxy> proxy_ptr{nullptr};

QString http_proxy{qgetenv("http_proxy")};
if (http_proxy.isEmpty())
{
// Some OS's are case senstive
http_proxy = qgetenv("HTTP_PROXY");
}

if (!http_proxy.isEmpty())
{
if (!http_proxy.contains("://"))
{
http_proxy.prepend("http://");
}

QUrl proxy_url{http_proxy};
const auto host = proxy_url.host();
const auto port = proxy_url.port();

auto network_proxy = QNetworkProxy(QNetworkProxy::HttpProxy, host, static_cast<quint16>(port),
proxy_url.userName(), proxy_url.password());

QNetworkProxy::setApplicationProxy(network_proxy);

proxy_ptr = std::make_unique<QNetworkProxy>(network_proxy);
}

return proxy_ptr;
}
} // namespace

mp::DaemonConfig::~DaemonConfig()
Expand Down Expand Up @@ -116,9 +151,12 @@ std::unique_ptr<const mp::DaemonConfig> mp::DaemonConfigBuilder::build()
if (ssh_username.empty())
ssh_username = "ubuntu";

return std::unique_ptr<const DaemonConfig>(
new DaemonConfig{std::move(url_downloader), std::move(factory), std::move(image_hosts), std::move(vault),
std::move(name_generator), std::move(ssh_key_provider), std::move(cert_provider),
std::move(client_cert_store), std::move(update_prompt), multiplexing_logger, cache_directory,
data_directory, server_address, ssh_username, connection_type, image_refresh_timer});
if (network_proxy == nullptr)
network_proxy = discover_http_proxy();

return std::unique_ptr<const DaemonConfig>(new DaemonConfig{
std::move(url_downloader), std::move(factory), std::move(image_hosts), std::move(vault),
std::move(name_generator), std::move(ssh_key_provider), std::move(cert_provider), std::move(client_cert_store),
std::move(update_prompt), multiplexing_logger, std::move(network_proxy), cache_directory, data_directory,
server_address, ssh_username, connection_type, image_refresh_timer});
}
6 changes: 5 additions & 1 deletion src/daemon/daemon_config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Canonical, Ltd.
* Copyright (C) 2017-2020 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -34,6 +34,8 @@
#include <multipass/vm_image_host.h>
#include <multipass/vm_image_vault.h>

#include <QNetworkProxy>

#include <memory>
#include <vector>

Expand All @@ -52,6 +54,7 @@ struct DaemonConfig
const std::unique_ptr<CertStore> client_cert_store;
const std::unique_ptr<UpdatePrompt> update_prompt;
const std::shared_ptr<logging::MultiplexingLogger> logger;
const std::unique_ptr<QNetworkProxy> network_proxy;
const multipass::Path cache_directory;
const multipass::Path data_directory;
const std::string server_address;
Expand All @@ -72,6 +75,7 @@ struct DaemonConfigBuilder
std::unique_ptr<CertStore> client_cert_store;
std::unique_ptr<UpdatePrompt> update_prompt;
std::unique_ptr<logging::Logger> logger;
std::unique_ptr<QNetworkProxy> network_proxy;
multipass::Path cache_directory;
multipass::Path data_directory;
std::string server_address;
Expand Down
5 changes: 4 additions & 1 deletion src/network/url_downloader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2019 Canonical, Ltd.
* Copyright (C) 2017-2020 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -99,6 +99,9 @@ QByteArray download(QNetworkAccessManager* manager, const Time& timeout, QUrl co

const auto msg = reply->errorString().toStdString();

if (reply->error() == QNetworkReply::ProxyAuthenticationRequiredError)
reply->abort();

if (abort_download)
throw mp::AbortedDownloadException{msg};
else
Expand Down
18 changes: 18 additions & 0 deletions tests/test_daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <multipass/vm_image_host.h>
#include <multipass/vm_image_vault.h>

#include "mock_environment_helpers.h"
#include "mock_virtual_machine_factory.h"
#include "stub_cert_store.h"
#include "stub_certprovider.h"
Expand Down Expand Up @@ -367,6 +368,23 @@ TEST_F(Daemon, failed_restart_command_returns_fulfilled_promise)
EXPECT_TRUE(is_ready(status_promise.get_future()));
}

TEST_F(Daemon, proxy_contains_valid_info)
{
QString username{"username"};
QString password{"password"};
QString hostname{"192.168.1.1"};
qint16 port{3128};
QString proxy = QString("%1:%2@%3:%4").arg(username).arg(password).arg(hostname).arg(port);

mpt::SetEnvScope env("http_proxy", proxy.toUtf8());

auto config = config_builder.build();

EXPECT_THAT(config->network_proxy->user(), username);
EXPECT_THAT(config->network_proxy->password(), password);
EXPECT_THAT(config->network_proxy->hostName(), hostname);
EXPECT_THAT(config->network_proxy->port(), port);
}

namespace
{
Expand Down