Skip to content

Commit

Permalink
Add the ability to set the proxy server on a per-page-object basis.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdeszynski authored and igorshapiro committed Sep 29, 2016
1 parent 69c68f5 commit c6c7fe5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#define PAGE_SETTINGS_JS_ENABLED "javascriptEnabled"
#define PAGE_SETTINGS_XSS_AUDITING "XSSAuditingEnabled"
#define PAGE_SETTINGS_USER_AGENT "userAgent"
#define PAGE_SETTINGS_PROXY "proxy"
#define PAGE_SETTINGS_LOCAL_ACCESS_REMOTE "localToRemoteUrlAccessEnabled"
#define PAGE_SETTINGS_USERNAME "userName"
#define PAGE_SETTINGS_PASSWORD "password"
Expand Down
9 changes: 9 additions & 0 deletions src/phantom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@ void Phantom::setProxy(const QString& ip, const qint64& port, const QString& pro
}
}

QString Phantom::proxy()
{
QNetworkProxy proxy = QNetworkProxy::applicationProxy();
if (proxy.hostName().isEmpty()) {
return NULL;
}
return proxy.hostName() + ":" + QString::number(proxy.port());
}

void Phantom::exit(int code)
{
if (m_config.debug()) {
Expand Down
2 changes: 2 additions & 0 deletions src/phantom.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ public slots:
*/
void setProxy(const QString& ip, const qint64& port = 80, const QString& proxyType = "http", const QString& user = NULL, const QString& password = NULL);

QString proxy();

// exit() will not exit in debug mode. debugExit() will always exit.
void exit(int code = 0);
void debugExit(int code = 0);
Expand Down
17 changes: 17 additions & 0 deletions src/webpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
#include <QDebug>
#include <QImageWriter>
#include <QUuid>
#include <QUrl>
#include <QNetworkProxy>

#include "phantom.h"
#include "networkaccessmanager.h"
Expand Down Expand Up @@ -641,6 +643,21 @@ void WebPage::applySettings(const QVariantMap& def)
m_networkAccessManager->setResourceTimeout(def[PAGE_SETTINGS_RESOURCE_TIMEOUT].toInt());
}

if (def.contains(PAGE_SETTINGS_PROXY)) {
setProxy(def[PAGE_SETTINGS_PROXY].toString());
}
}

void WebPage::setProxy(const QString& proxyUrl)
{
QUrl url(proxyUrl);
qDebug() << "Setting proxy to: " << url.scheme() << url.host() << url.port();
QNetworkProxy::ProxyType type = QNetworkProxy::HttpProxy;
if (url.scheme() == "socks5") {
type = QNetworkProxy::Socks5Proxy;
}
QNetworkProxy proxy(type, url.host(), url.port(), url.userName(), url.password());
m_networkAccessManager->setProxy(proxy);
}

QString WebPage::userAgent() const
Expand Down
2 changes: 2 additions & 0 deletions src/webpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ public slots:

void clearMemoryCache();

void setProxy(const QString& proxyUrl);

signals:
void initialized();
void loadStarted();
Expand Down

0 comments on commit c6c7fe5

Please sign in to comment.