0

Using the code example provided here : https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110051-configuring-proxy works only with HTTP(S) proxy but not with socks5. Is there support for doing the same for SOCKS 5 ? Thanks

Stoica Nicusor
  • 211
  • 4
  • 9

2 Answers2

1

DotNetBrowser is based on the Chromium engine. Unfortunately, Chromium engine does not support SOCKS 5 authentication, so DotNetBrowser does not support such authentication as well.

There is an issue in the Chromium bug tracker: https://bugs.chromium.org/p/chromium/issues/detail?id=256785

As soon as we switch to a Chromium version where SOCKS 5 authentication support is available, we will add this implementation into DotNetBrowser too.

As the workaround, you can try to create your own implementation of the ‘IProtocolHandler’ interface to handle the requests to the required proxy server using 3rd party libraries. However, please note that the requests cannot be handled in parallel, so the performance may be significantly reduced.

The following article demonstrates how to register the custom protocol handler: https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000134504-custom-protocol-handler

Eugene Yakush
  • 259
  • 4
  • 6
0

Based on Eugene Yakush suggestion I came up with this :

    public class HttpsHandler : IProtocolHandler
{
    public string socksHost = string.Empty;
    public int socksPort = -100;
    public bool socksLogin = false;
    public string socksUsername = string.Empty;
    public string socksPassword = string.Empty;

    //This method should provide the response for the specified request
    private Chilkat.Http http;
    private Chilkat.HttpRequest httpRequest;
    private Chilkat.HttpResponse httpResponse;
    private Chilkat.Spider spider;
    public HttpsHandler()
    {
        this.http = new Chilkat.Http();
        this.spider = new Chilkat.Spider();
    }
    public IUrlResponse Handle(IUrlRequest request)
    {
        http.SocksHostname = socksHost;
        http.SocksPort = socksPort;
        http.SocksVersion = 5;
        if (socksLogin)
        {
            http.SocksUsername = socksUsername;
            http.SocksPassword = socksPassword;
        }
        httpRequest = new Chilkat.HttpRequest();
        httpRequest.HttpVerb = request.Method.ToUpper();
        Console.WriteLine(request.Url);

        if (request.Method.ToUpper() == "POST")
        {

            var xxl = request.PostData;
            PostData post = request.PostData;
            FormData postData = (FormData)post;
            var keys = postData.GetPairKeys();
            foreach (var item in keys)
            {
                var val = postData.GetPairValues(item);
                Console.WriteLine(item + ":" + val[0]);
                httpRequest.AddParam(item, val[0]);
            }
        }
        var heads = request.Headers.GetHeaders();
        foreach (KeyValuePair<string, List<string>> pair in heads)
        {
            httpRequest.AddHeader(pair.Key, pair.Value[0]);
            Console.WriteLine(pair.Key);
            Console.WriteLine(pair.Value[0]);
            Console.WriteLine("===================");
        }
        string path = request.Url;
        string domain = spider.GetUrlDomain(request.Url);
        path = path.Replace("https://" + domain, string.Empty);
        httpRequest.AddHeader("Host", domain);
        httpRequest.Path = path;
        httpResponse = http.SynchronousRequest(domain, 443, true, httpRequest);
        Console.WriteLine(httpResponse.StatusCode);
        if (httpResponse == null)
        {
            return new UrlResponse(Encoding.UTF8.GetBytes(string.Empty), (System.Net.HttpStatusCode)httpResponse.StatusCode);
        }
        return new UrlResponse(Encoding.UTF8.GetBytes(httpResponse.BodyStr), (System.Net.HttpStatusCode)httpResponse.StatusCode);
    }
}

Obviously, it can be simplified. But if someone has the same problem, this is my solution.

Stoica Nicusor
  • 211
  • 4
  • 9