0

I have dotnet core 2.1 application which has to send web requests using several source ip addresses.

I've googled very much and tried find workaround. The most popular is

Can I send webrequest from specified ip address with .NET Framework?

but it doesn't work for dotnet core and BindIPEndPointCallback is never called.

I've also found CurlHandler class which wraps curl functionality

https://github.com/dotnet/corefx/blob/e0ba7aa8026280ee3571179cc06431baf1dfaaac/src/System.Net.Http/src/System/Net/Http/CurlHandler/CurlHandler.cs

and curl itself has --interface option for setting source IP, but CurlHandler class doesn't have any public methods to set CurlOptions.

So the only way I can fix that is using raw sockets, but I don't like it.

Are there any simple solutions for setting web request source ip?

Thank you in advance

Bambaleo
  • 11
  • 3
  • Is there any way you can configure the routing table on your machine instead, so you can just let the operating system handle this ? (Note, depending on what you are doing, this could already work just as you want, without you having any special logic in your code for it) – nos Sep 10 '18 at 14:35
  • There is a single RemoteEndpoint and I need to rotate local endpoints from time to time. Or I may need to run several instances of my app and set different local endpoint for each of them – Bambaleo Sep 10 '18 at 14:43
  • https://github.com/dotnet/corefx/issues/18129 – Soumen Mukherjee Sep 10 '18 at 15:03
  • thi issue is pending for more than year. so it looks like there is no hope – Bambaleo Sep 10 '18 at 15:11
  • It is only Microsoft who is reluctant to implement this, due to the reasons they stated, and also such usage, though reasonable, is rare IMHO. As raw socket is a feasible workaround, there is no urgency either to move this issue forward. Fully managed socket implement in .NET Core should be something in 2.2 or 3.0, so you should wait till those releases and check again. – Lex Li Sep 10 '18 at 17:23
  • @Bambaleo HttpClient now works on top of Sockets. The default handler now is [SocketsHttpHandler](https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs). What you ask though *is* a rare scenario. Handlers actually handle TCP connection pools, saving a lot of time in DNS resolution, HTTPS negotiation. Getting all that to work with a custom interface is probably not the easiest thing. – Panagiotis Kanavos Sep 11 '18 at 09:53

1 Answers1

0

It looks like the simplest way to fix the issue is just using curl as external command

 var command =string.Format("curl --interface {0} {yourCommandHere}", localIpString);

 Process proc = new Process();
 proc.StartInfo.FileName = "/bin/bash";
 proc.StartInfo.Arguments = "-c \" " + command + " \"";
 proc.StartInfo.UseShellExecute = false;
 proc.StartInfo.RedirectStandardOutput = true;
 proc.Start();
 var result = proc.StandardOutput.ReadToEnd();
 Console.Writeline("curl result {0}", result);
Bambaleo
  • 11
  • 3