From 0a312d3b08a40d51acc1952df3ed2f0348f4aa5e Mon Sep 17 00:00:00 2001 From: Forkk Date: Mon, 6 Jan 2014 15:02:58 -0600 Subject: [PATCH] Implement proxy settings --- MultiMC.cpp | 125 +++++++++++++++++---------- MultiMC.h | 5 ++ gui/dialogs/SettingsDialog.cpp | 37 +++++++- gui/dialogs/SettingsDialog.h | 2 + gui/dialogs/SettingsDialog.ui | 151 ++++++++++++++++++++++++++++++++- 5 files changed, 270 insertions(+), 50 deletions(-) diff --git a/MultiMC.cpp b/MultiMC.cpp index 297d08fdb..7c4116935 100644 --- a/MultiMC.cpp +++ b/MultiMC.cpp @@ -198,56 +198,12 @@ MultiMC::MultiMC(int &argc, char **argv, bool root_override) // init the http meta cache initHttpMetaCache(); - // set up a basic autodetected proxy (system default) - QNetworkProxyFactory::setUseSystemConfiguration(true); - - QLOG_INFO() << "Detecting system proxy settings..."; - auto proxies = QNetworkProxyFactory::systemProxyForQuery(); - if (proxies.size() == 1 && proxies[0].type() == QNetworkProxy::NoProxy) - { - QLOG_INFO() << "No proxy found."; - } - else - for (auto proxy : proxies) - { - QString proxyDesc; - if (proxy.type() == QNetworkProxy::NoProxy) - { - QLOG_INFO() << "Using no proxy is an option!"; - continue; - } - switch (proxy.type()) - { - case QNetworkProxy::DefaultProxy: - proxyDesc = "Default proxy: "; - break; - case QNetworkProxy::Socks5Proxy: - proxyDesc = "Socks5 proxy: "; - break; - case QNetworkProxy::HttpProxy: - proxyDesc = "HTTP proxy: "; - break; - case QNetworkProxy::HttpCachingProxy: - proxyDesc = "HTTP caching: "; - break; - case QNetworkProxy::FtpCachingProxy: - proxyDesc = "FTP caching: "; - break; - default: - proxyDesc = "DERP proxy: "; - break; - } - proxyDesc += QString("%3@%1:%2 pass %4") - .arg(proxy.hostName()) - .arg(proxy.port()) - .arg(proxy.user()) - .arg(proxy.password()); - QLOG_INFO() << proxyDesc; - } - // create the global network manager m_qnam.reset(new QNetworkAccessManager(this)); + // init proxy settings + updateProxySettings(); + // launch instance, if that's what should be done // WARNING: disabled until further notice /* @@ -424,6 +380,13 @@ void MultiMC::initGlobalSettings() m_settings->registerSetting({"MinecraftWinWidth", "MCWindowWidth"}, 854); m_settings->registerSetting({"MinecraftWinHeight", "MCWindowHeight"}, 480); + // Proxy Settings + m_settings->registerSetting("ProxyType", "Default"); + m_settings->registerSetting({"ProxyAddr", "ProxyHostName"}, "127.0.0.1"); + m_settings->registerSetting("ProxyPort", 8080); + m_settings->registerSetting({"ProxyUser", "ProxyUsername"}, ""); + m_settings->registerSetting({"ProxyPass", "ProxyPassword"}, ""); + // Memory m_settings->registerSetting({"MinMemAlloc", "MinMemoryAlloc"}, 512); m_settings->registerSetting({"MaxMemAlloc", "MaxMemoryAlloc"}, 1024); @@ -467,6 +430,74 @@ void MultiMC::initHttpMetaCache() m_metacache->Load(); } +void MultiMC::updateProxySettings() +{ + QString proxyTypeStr = settings()->get("ProxyType").toString(); + + // Get the proxy settings from the settings object. + QString addr = settings()->get("ProxyAddr").toString(); + int port = settings()->get("ProxyPort").value(); + QString user = settings()->get("ProxyUser").toString(); + QString pass = settings()->get("ProxyPass").toString(); + + // Set the application proxy settings. + if (proxyTypeStr == "SOCKS5") + { + QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::Socks5Proxy, addr, port, user, pass)); + } + else if (proxyTypeStr == "HTTP") + { + QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::HttpProxy, addr, port, user, pass)); + } + else if (proxyTypeStr == "None") + { + // If we have no proxy set, set no proxy and return. + QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::NoProxy)); + } + else + { + // If we have "Default" selected, set Qt to use the system proxy settings. + QNetworkProxyFactory::setUseSystemConfiguration(true); + } + + QLOG_INFO() << "Detecting proxy settings..."; + QNetworkProxy proxy = QNetworkProxy::applicationProxy(); + if (m_qnam.get()) m_qnam->setProxy(proxy); + QString proxyDesc; + if (proxy.type() == QNetworkProxy::NoProxy) + { + QLOG_INFO() << "Using no proxy is an option!"; + return; + } + switch (proxy.type()) + { + case QNetworkProxy::DefaultProxy: + proxyDesc = "Default proxy: "; + break; + case QNetworkProxy::Socks5Proxy: + proxyDesc = "Socks5 proxy: "; + break; + case QNetworkProxy::HttpProxy: + proxyDesc = "HTTP proxy: "; + break; + case QNetworkProxy::HttpCachingProxy: + proxyDesc = "HTTP caching: "; + break; + case QNetworkProxy::FtpCachingProxy: + proxyDesc = "FTP caching: "; + break; + default: + proxyDesc = "DERP proxy: "; + break; + } + proxyDesc += QString("%3@%1:%2 pass %4") + .arg(proxy.hostName()) + .arg(proxy.port()) + .arg(proxy.user()) + .arg(proxy.password()); + QLOG_INFO() << proxyDesc; +} + std::shared_ptr MultiMC::icons() { if (!m_icons) diff --git a/MultiMC.h b/MultiMC.h index d02099f3f..18c7aab7f 100644 --- a/MultiMC.h +++ b/MultiMC.h @@ -123,6 +123,11 @@ public: void installUpdates(const QString updateFilesDir, UpdateFlags flags = None); + /*! + * Updates the application proxy settings from the settings object. + */ + void updateProxySettings(); + /*! * Opens a json file using either a system default editor, or, if note empty, the editor * specified in the settings diff --git a/gui/dialogs/SettingsDialog.cpp b/gui/dialogs/SettingsDialog.cpp index 549b11b04..091ffb1c3 100644 --- a/gui/dialogs/SettingsDialog.cpp +++ b/gui/dialogs/SettingsDialog.cpp @@ -83,6 +83,8 @@ void SettingsDialog::updateCheckboxStuff() { ui->windowWidthSpinBox->setEnabled(!ui->maximizedCheckBox->isChecked()); ui->windowHeightSpinBox->setEnabled(!ui->maximizedCheckBox->isChecked()); + ui->proxyAddrBox->setEnabled(!ui->proxyNoneBtn->isChecked() && !ui->proxyDefaultBtn->isChecked()); + ui->proxyAuthBox->setEnabled(!ui->proxyNoneBtn->isChecked() && !ui->proxyDefaultBtn->isChecked()); } void SettingsDialog::on_ftbLauncherBrowseBtn_clicked() @@ -202,6 +204,9 @@ void SettingsDialog::on_buttonBox_accepted() { applySettings(MMC->settings().get()); + // Apply proxy settings + MMC->updateProxySettings(); + MMC->settings()->set("SettingsGeometry", saveGeometry().toBase64()); } @@ -210,6 +215,12 @@ void SettingsDialog::on_buttonBox_rejected() MMC->settings()->set("SettingsGeometry", saveGeometry().toBase64()); } +void SettingsDialog::on_proxyNoneBtn_toggled(bool checked) +{ + Q_UNUSED(checked); + updateCheckboxStuff(); +} + void SettingsDialog::refreshUpdateChannelList() { // Stop listening for selection changes. It's going to change a lot while we update it and we don't need to update the @@ -310,6 +321,19 @@ void SettingsDialog::applySettings(SettingsObject *s) s->set("MinecraftWinWidth", ui->windowWidthSpinBox->value()); s->set("MinecraftWinHeight", ui->windowHeightSpinBox->value()); + // Proxy + QString proxyType = "None"; + if (ui->proxyDefaultBtn->isChecked()) proxyType = "Default"; + else if (ui->proxyNoneBtn->isChecked()) proxyType = "None"; + else if (ui->proxySOCKS5Btn->isChecked()) proxyType = "SOCKS5"; + else if (ui->proxyHTTPBtn->isChecked()) proxyType = "HTTP"; + + s->set("ProxyType", proxyType); + s->set("ProxyAddr", ui->proxyAddrEdit->text()); + s->set("ProxyPort", ui->proxyPortEdit->value()); + s->set("ProxyUser", ui->proxyUserEdit->text()); + s->set("ProxyPass", ui->proxyPassEdit->text()); + // Memory s->set("MinMemAlloc", ui->minMemSpinBox->value()); s->set("MaxMemAlloc", ui->maxMemSpinBox->value()); @@ -384,6 +408,18 @@ void SettingsDialog::loadSettings(SettingsObject *s) ui->sortByNameBtn->setChecked(true); } + // Proxy + QString proxyType = s->get("ProxyType").toString(); + if (proxyType == "Default") ui->proxyDefaultBtn->setChecked(true); + else if (proxyType == "None") ui->proxyNoneBtn->setChecked(true); + else if (proxyType == "SOCKS5") ui->proxySOCKS5Btn->setChecked(true); + else if (proxyType == "HTTP") ui->proxyHTTPBtn->setChecked(true); + + ui->proxyAddrEdit->setText(s->get("ProxyAddr").toString()); + ui->proxyPortEdit->setValue(s->get("ProxyPort").value()); + ui->proxyUserEdit->setText(s->get("ProxyUser").toString()); + ui->proxyPassEdit->setText(s->get("ProxyPass").toString()); + // Java Settings ui->javaPathTextBox->setText(s->get("JavaPath").toString()); ui->jvmArgsTextBox->setText(s->get("JvmArgs").toString()); @@ -447,4 +483,3 @@ void SettingsDialog::checkFinished(JavaCheckResult result) "or set the path to the java executable.")); } } - diff --git a/gui/dialogs/SettingsDialog.h b/gui/dialogs/SettingsDialog.h index df67d4927..6039ecab1 100644 --- a/gui/dialogs/SettingsDialog.h +++ b/gui/dialogs/SettingsDialog.h @@ -75,6 +75,8 @@ slots: void checkFinished(JavaCheckResult result); + void on_proxyNoneBtn_toggled(bool checked); + /*! * Updates the list of update channels in the combo box. */ diff --git a/gui/dialogs/SettingsDialog.ui b/gui/dialogs/SettingsDialog.ui index 7d2708cbb..756e2a1fa 100644 --- a/gui/dialogs/SettingsDialog.ui +++ b/gui/dialogs/SettingsDialog.ui @@ -7,7 +7,7 @@ 0 0 526 - 639 + 683 @@ -33,7 +33,7 @@ QTabWidget::Rounded - 0 + 2 @@ -424,6 +424,153 @@ + + + Network settings. + + + Network + + + + + + Proxy + + + + + + Type + + + + + + Uses your system's default proxy settings. + + + Default + + + + + + + None + + + + + + + SOCKS5 + + + + + + + HTTP + + + + + + + + + + Address and Port + + + + + + 127.0.0.1 + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + QAbstractSpinBox::PlusMinus + + + 65535 + + + 8080 + + + + + + + + + + Authentication + + + + + + + + + Username: + + + + + + + Password: + + + + + + + QLineEdit::Password + + + + + + + Note: Proxy username and password are stored in plain text inside MultiMC's configuration file! + + + true + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + Java