From 573c79fdbcc332894713aef278c9e5b3e4f6064b Mon Sep 17 00:00:00 2001 From: "d.guskov" Date: Thu, 10 Jul 2014 13:37:57 +0400 Subject: [PATCH] add setProxy function --- src/phantom.cpp | 23 +++++++++++++++++++++++ src/phantom.h | 9 +++++++++ 2 files changed, 32 insertions(+) diff --git a/src/phantom.cpp b/src/phantom.cpp index 1f3179587d..c5ebcd61a6 100644 --- a/src/phantom.cpp +++ b/src/phantom.cpp @@ -392,6 +392,29 @@ bool Phantom::injectJs(const QString &jsFilePath) return Utils::injectJsInFrame(pre + jsFilePath, libraryPath(), m_page->mainFrame()); } +void Phantom::setProxy(const QString &ip, const qint64 &port, const QString &proxyType, const QString &user, const QString &password) +{ + qDebug() << "Set " << proxyType << " proxy to: " << ip << ":" << port; + if (ip.isEmpty()) { + QNetworkProxyFactory::setUseSystemConfiguration(true); + } + else { + QNetworkProxy::ProxyType networkProxyType = QNetworkProxy::HttpProxy; + + if (proxyType == "socks5") { + networkProxyType = QNetworkProxy::Socks5Proxy; + } + // Checking for passed proxy user and password + if(!user.isEmpty() && !password.isEmpty()) { + QNetworkProxy proxy(networkProxyType, ip, port, user, password); + QNetworkProxy::setApplicationProxy(proxy); + } else { + QNetworkProxy proxy(networkProxyType, ip, port); + QNetworkProxy::setApplicationProxy(proxy); + } + } +} + void Phantom::exit(int code) { if (m_config.debug()) { diff --git a/src/phantom.h b/src/phantom.h index 49bed7ffa9..c30b78c844 100644 --- a/src/phantom.h +++ b/src/phantom.h @@ -166,6 +166,15 @@ public slots: */ void clearCookies(); + /** + * Set the application proxy + * @brief setProxy + * @param ip The proxy ip + * @param port The proxy port + * @param proxyType The type of this proxy + */ + void setProxy(const QString &ip, const qint64 &port = 80, const QString &proxyType = "http", const QString &user = NULL, const QString &password = NULL); + // exit() will not exit in debug mode. debugExit() will always exit. void exit(int code = 0); void debugExit(int code = 0);