JsBergbau / BindToInterface

With this program you can bind applications to a specific network interface / network adapter. This is very useful if you have multiple (internet) connections and want your program to use a specific one.
GNU Affero General Public License v3.0
113 stars 14 forks source link

Multiple physical NICs #9

Open Aqua1ung opened 1 year ago

Aqua1ung commented 1 year ago

Currently testing BindToInterface on my Ubuntu Kinetic. I have two physical NICs, one wired, and one wireless. Need ssmtp to use the wireless one: BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=./bindToInterface.so /usr/sbin/ssmtp, however, regardless of the value of BIND_INTERFACE, it still uses the wired NIC. (Testing with curl ifconfig.me tells me precisely that.) What am I doing wrong? Should I go to the trouble of messing with the route metrics? Does that not defeat the purpose of using BindToInterface in the first place?

JsBergbau commented 1 year ago

Halo Aqua1ung Is your wireless NIC using another internect connection. It looks like, because this kind of error didn't happen so far. Another thing: Especially in case of errors or malfunction, you should use absolut path for bindToInterface https://github.com/JsBergbau/BindToInterface#specifying-absolute-path

Aqua1ung commented 1 year ago

Halo Aqua1ung Is your wireless NIC using another internect connection. It looks like, because this kind of error didn't happen so far. Another thing: Especially in case of errors or malfunction, you should use absolut path for bindToInterface https://github.com/JsBergbau/BindToInterface#specifying-absolute-path

Yes, both NICs are connected to the Internet: is that not supported? And yes, I am using the absolute path for bindToInterface.

JsBergbau commented 1 year ago

Can you please post the output of BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=<absolute Path to bindToInterface.so> curl ifconfig.me and output of curl --interface wlp1s0 ifconfig.me

Aqua1ung commented 1 year ago
dad@mificommander:~$ curl --interface wlp1s0 ifconfig.me
98.221.xxx.xxx
dad@mificommander:~$ curl --interface wlp1s0 ifconfig.me
98.221.xxx.xxx
dad@mificommander:~$ BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=/usr/lib/bindToInterface.so curl ifconfig.me
172.58.xxx.xxx
dad@mificommander:~$ curl --interface wlp1s0 ifconfig.me
98.221.xxx.xxx
dad@mificommander:~$ BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=/usr/lib/bindToInterface.so curl ifconfig.me
172.58.xxx.xxx
dad@mificommander:~$ curl --interface wlp1s0 ifconfig.me
98.221.xxx.xxx
dad@mificommander:~$ BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=/usr/lib/bindToInterface.so curl ifconfig.me
172.58.xxx.xxx

See also here.

[Edit] Once in a blue moon I also get 98.221.xxx.xxx from BIND_INTERFACE=wlp1s0. All in all, pretty random it seems.

JsBergbau commented 1 year ago

Please uncomment the line //#define DEBUG recompile and post the full output of BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=/usr/lib/bindToInterface.so curl ifconfig.me

Aqua1ung commented 1 year ago

Not sure what "uncomment" would mean in C. Should I remove the whole //# string, or just the //? (Where I come from # means "comment")

JsBergbau commented 1 year ago

Just remove the //. # is the so called preprocessor. It changes the code before it is presented to the compiler. Some kind of effiency measure.

Aqua1ung commented 1 year ago

Here goes:

dad@mificommander:~$ BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=/usr/lib/bindToInterface.so curl ifconfig.me
connecting to: 34.160.111.145:80 
Bound Interface: none. Socket not bound to desired interface (Bound to: none). Binding to interface: wlp1s0
98.221.xxx.xxx 
dad@mificommander:~$ BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=/usr/lib/bindToInterface.so curl ifconfig.me
172.58.xxx.xxx 
dad@mificommander:~$ BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=/usr/lib/bindToInterface.so curl ifconfig.me
172.58.xxx.xxx

[Edit] As far as I can tell, it looks like the BIND_INTERFACE directive only kicks in once in a blue moon, despite being invoked every time. I wonder why that would be? Am I doing something wrong?

JsBergbau commented 1 year ago

It looks like LD_PRELOAD isn't respected every time. That is really strange and seems some kind of Linux issue. I'm using BindToInterface with multiple VPNs and microsocks https://github.com/rofl0r/microsocks and this works like a charm. Could you try to place the library in your home path? I have it there and it works every time.

Aqua1ung commented 1 year ago

Could you try to place the library in your home path? I have it there and it works every time.

You mean move bindToInterface.so to ~/?

Aqua1ung commented 1 year ago

Here goes:

dad@mificommander:/usr/lib$ curl --interface wlp1s0 ifconfig.me
98.221.xxx.xxx
dad@mificommander:/usr/lib$ BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=~/bindToInterface.so curl ifconfig.me
connecting to: 34.160.111.145:80 
Bound Interface: none. Socket not bound to desired interface (Bound to: none). Binding to interface: wlp1s0
98.221.xxx.xxx 
dad@mificommander:/usr/lib$ BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=~/bindToInterface.so curl ifconfig.me
172.58.xxx.xxx
dad@mificommander:/usr/lib$ BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=~/bindToInterface.so curl ifconfig.me
172.58.xxx.xxx
dad@mificommander:/usr/lib$ curl --interface wlp1s0 ifconfig.me
98.221.xxx.xxx

And here's an even more interesting one:

dad@mificommander:/usr/lib$ BIND_INTERFACE=wlp1s0 DNS_OVERRIDE_IP=8.8.8.8 LD_PRELOAD=~/bindToInterface.so curl ifconfig.me
connecting to: 34.160.111.145:80 
Bound Interface: none. Socket not bound to desired interface (Bound to: none). Binding to interface: wlp1s0
172.58.xxx.xxx

[Edit] Also, I've seen some opinions that LD_PRELOAD is unsuitable for this task, and I quote: "Because LD_PRELOAD does not control the route that the processes uses. It will use the first route. And since it always uses the same route, it will default to the interface registered to the route.(which is not what we want)"

JsBergbau commented 1 year ago

Thats really strange and currently I have no idea whats happening there.