Dan Fuller
Dan Fuller

Reputation: 1161

Connecting to a .NET webservice via a SOCKS5 proxy

I'm attempting to connect through a SOCKS5 proxy to a webservice. Currently, my app.config is as follows:

<system.net>
 <defaultProxy enabled="true"  >
  <proxy proxyaddress="http://xxx.xxx.xxx.xxx:yyyy" bypassonlocal="True" />
 </defaultProxy>
</system.net>

This works fine for http proxies, however it just doesn't connect to SOCKS5 ones. I've googled far and wide for an answer and just can't find anyone that's solved this problem, even though I've found the question a couple of times. Anyone have any idea on how I can do this?

Upvotes: 7

Views: 9543

Answers (4)

Majid
Majid

Reputation: 3471

In .NET 6 you can do it easily as follows:

var proxy = new WebProxy
{
    Address = new Uri("socks5://localhost:8080")
};
//proxy.Credentials = new NetworkCredential(); //Used to set Proxy logins. 
var handler = new HttpClientHandler
{
    Proxy = proxy
};
var httpClient = new HttpClient(handler);

or when you can inject IHttpClientFactory (recommended way):

In your ConfigureServices method:

Services.AddHttpClient("WithProxy")
    .ConfigurePrimaryHttpMessageHandler(() =>
    {
        var proxy = new WebProxy
        {
            Address = new Uri("socks5://localhost:8080")
        };
        return new HttpClientHandler
        {
            Proxy = proxy
        };
    });

Then in your controller or anywhere you can inject IHttpClientFactory object, create a new httpClient (with configured proxy) instance as follows:

httpClient = httpClientFactory.CreateClient("WithProxy");

Here is a more detailed description: https://dotnetcoretutorials.com/2021/07/11/socks-proxy-support-in-net/

Upvotes: 6

mqueirozcorreia
mqueirozcorreia

Reputation: 943

If you are able to use HttpClient, you can use SocksSharp.

I just followed the example and it worked for me.

var settings = new ProxySettings()
{
    Host = "xxx.xxx.xxx.xxx",
    Port = yyyy
};

using (var proxyClientHandler = new ProxyClientHandler<Socks5>(settings))
{
    using (var httpClient = new HttpClient(proxyClientHandler))
    {
        var response = await httpClient.GetAsync("http://example.com/");
    }
}

Upvotes: 1

John
John

Reputation: 30518

In theory you can inherit the webservice class and override GetWebRequest and GetWebResponse. Then you may be able to use this c# socks class http://www.codeproject.com/Articles/5954/C-class-for-connecting-via-a-SOCKS-Proxy-Server and convert the webrequest to sockets and back again.

The quickest solution I have been able to find is to set a system wide socks proxy using WideCap. This routes all traffic on the machine via the socks proxy server.

Upvotes: 0

Takuan Daikon
Takuan Daikon

Reputation: 41

Apparently Microsoft did not build SOCKS proxy support into their web classes (see http://windows-tech.info/13/f17246e7dd4a8a2b.php)

I too am in a situation where I'd like to use the built-in .NET web functionality with a SOCKS5 proxy, and have not found any satisfactory solution. I guess my next step is to obtain the source of the .NET classes either through Reflector or from the Mono codebase and refactor it to use the ProxySocket class mentioned in the above link.

This is more work than I wanted, but seems to be the only way.

Upvotes: 2

Related Questions