Search code examples
qtpyqtsocksqtwebengine

Qt WebEngine set socks5 proxy


I want to set socks5 proxy for my Qt WebEngine app. I use PyQt5.8 , QT5.8.

I set up a socks5 server by danted v1.4.1. I test my socks5 server and it worked good. But when I use it in my app, danted log errors:

error after reading 3 bytes in 0 seconds: client offered no acceptable authentication method

This is my code:

def set_proxy():
    from PyQt5.QtNetwork import QNetworkProxy
    proxy = QNetworkProxy()

    from six.moves.urllib import parse as urlparse
    string_proxy = "socks5://username:password@ip:port"
    urlinfo = urlparse.urlparse(string_proxy)
    proxy = QNetworkProxy()
    if urlinfo.scheme == 'socks5':
        proxy.setType(QNetworkProxy.Socks5Proxy)
    else:
        proxy.setType(QNetworkProxy.NoProxy)
    if urlinfo.hostname != None:
        proxy.setHostName(urlinfo.hostname)
    if urlinfo.port != None:
        proxy.setPort(urlinfo.port)
    if urlinfo.username != None:
        proxy.setUser(urlinfo.username)
    else:
        proxy.setUser('')
    if urlinfo.password != None:
        proxy.setPassword(urlinfo.password)
    else:
        proxy.setPassword('')
    QNetworkProxy.setApplicationProxy(proxy)

Can anyone help me?


update on 2017/03/29

add proxyAuthenticationRequired signal

def set_proxy(string_proxy):
    proxy = QNetworkProxy()
    urlinfo = urlparse.urlparse(string_proxy)
    if urlinfo.scheme == 'socks5':
        proxy.setType(QNetworkProxy.Socks5Proxy)
    elif urlinfo.scheme == 'http':
        proxy.setType(QNetworkProxy.HttpProxy)
    else:
        proxy.setType(QNetworkProxy.NoProxy)
    proxy.setHostName(urlinfo.hostname)
    proxy.setPort(urlinfo.port)
    proxy.setUser(urlinfo.username)
    proxy.setPassword(urlinfo.password)
    QNetworkProxy.setApplicationProxy(proxy)

def handleProxyAuthReq(url, auth, proxyhost):
    auth.setUser(username)
    auth.setPassword(password)

webView = QtWebEngineWidgets.QWebEngineView()
#proxy_string = "http://username:password@ip:port"
proxy_string = "socks5://username:password@ip:port"
set_proxy(proxy_string)
webView.page().proxyAuthenticationRequired.connect(handleProxyAuthReq)

I test it by my Http proxy and it worded. But when I use Socks5 proxy, the proxyAuthenticationRequired signal can not be emited.


Solution

  • QtWebEngine does not handle the username/password information from QNetworkProxy:

    All other proxy settings such as QNetworkProxy::rawHeader(), QNetworkProxy::user(), or QNetworkProxy::password() are ignored.

    You'll need to handle proxyAuthenticationRequired and handle authentication there.


    update on 2017/03/30

    Looks like Chromium does not support authentication with SOCKS proxies.