progval / Limnoria

A robust, full-featured, and user/programmer-friendly Python IRC bot, with many existing plugins.
https://docs.limnoria.net/
Other
615 stars 172 forks source link

Bind-IP Support, in Web.titleSnarfer and Others #552

Open MrBenC opened 10 years ago

MrBenC commented 10 years ago

I don't know how conceivable it would be to fix this, but... an example of the problem:

For the sake of this report, pretend the bot in question is connecting to a network from 10.0.0.4, and the server the bots run on has 10.0.0.0/29, or the IPs 10.0.0.2 through 10.0.0.7 assigned to it. 10.0.0.2 is the first IP configured in /etc/network/interfaces, while 10.0.0.3 - 10.0.0.7 are added later.

Quote: @Ben http://myipinfo.net/ @Merovingian Title: Your IP address is 10.0.0.4 - myIPinfo.net - What is my IP address info? @Alice Title: Your IP address is 10.0.0.2 - myIPinfo.net - What is my IP address info? (at myipinfo.net) /End Quote

Note the Merovingian in this example is running Eggdrop and a TCL script, while Alice is Limnoria. The Merovingian is behaving correctly, Alice is not.

I'm not familiar enough with Limnoria's code to create a pull request, but fortunately it would seem (if I understand correctly) that HTTP requests seem to be in the core?

When looking at ~/Limnoria/src/utils/web.py I noticed the use of urllib, urllib2, and httplib . I wonder if perhaps one of these links will be of help?

http://bytes.com/topic/python/answers/45664-controlling-source-ip-address-within-urllib2 http://stackoverflow.com/questions/1150332/source-interface-with-python-and-urllib2

I also came across the following, which should possibly be able to tie into the supybot.protocols.irc.vhost setting: http://docs.python.org/2/library/httplib.html#httplib.HTTPConnection

However, the last link would mean breaking Python 2.6 support, making 2.7 the required minimum version.

MrBenC commented 10 years ago

Forgot to add my main nick at the end, sorry.

Ben ("Ben" at most places, "MrC" on Freenode)

progval commented 10 years ago

Breaking Python 2.6 support is not yet an option (too many people are still using it).

However, it is not hard to prevent a Python version from entering some part of the code (if sys.version_info >= (2, 7, 0):), so that could be done.

MrBenC commented 10 years ago

Giving a little more thought to this, perhaps:

socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) socket.bind((bindaddr, 77777))

Where bindaddr is a string containing the IP to connect from, and 77777 is to port to connect from (might want to randomize that, or maybe not).

Didn't see anything that suggested these options aren't in Python 2.6 at the following link: http://docs.python.org/2/library/socket.html

So it might be possible to implement this, and retain 2.6 support. A quick question in #Python on Freenode suggested socket.bind has been around since 1.x . Not 100% on the socket options specified.

The setsockopt settings are necessary because without those, restarting the bot will take longer due to the bind not being released immediately upon process kill.