Rev2054, Outgoing socket binding support

This commit is contained in:
shortcutme 2017-04-14 16:05:42 +02:00
parent c5629adf23
commit fae5d22e86
No known key found for this signature in database
GPG Key ID: 5B63BAE6CB9613AE
4 changed files with 25 additions and 4 deletions

View File

@ -10,7 +10,7 @@ class Config(object):
def __init__(self, argv):
self.version = "0.5.4"
self.rev = 2050
self.rev = 2054
self.argv = argv
self.action = None
self.config_file = "zeronet.conf"

View File

@ -98,8 +98,7 @@ class Connection(object):
raise Exception("Can't connect to onion addresses, no Tor controller present")
self.sock = self.server.tor_manager.createSocket(self.ip, self.port)
else:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.ip, int(self.port)))
self.sock = socket.create_connection((self.ip, int(self.port)))
# Implicit SSL
if self.cert_pin:

View File

@ -116,7 +116,7 @@ if config.stack_size:
if config.msgpack_purepython:
os.environ["MSGPACK_PUREPYTHON"] = "True"
# Socks Proxy monkey patch
# Socket monkey patch
if config.proxy:
from util import SocksProxy
import urllib2
@ -132,6 +132,13 @@ elif config.tor == "always":
config.fileserver_ip = '127.0.0.1' # Do not accept connections anywhere but localhost
SocksProxy.monkeyPatch(*config.tor_proxy.split(":"))
config.disable_udp = True
elif config.bind:
bind = config.bind
if ":" not in config.bind:
bind += ":0"
from util import helper
helper.socketBindMonkeyPatch(*bind.split(":"))
# -- Actions --

View File

@ -179,3 +179,18 @@ def timerCaller(secs, func, *args, **kwargs):
def timer(secs, func, *args, **kwargs):
gevent.spawn_later(secs, timerCaller, secs, func, *args, **kwargs)
def create_connection(address, timeout=None, source_address=None):
if address in config.ip_local:
sock = socket.create_connection_original(address, timeout, source_address)
else:
sock = socket.create_connection_original(address, timeout, socket.bind_addr)
return sock
def socketBindMonkeyPatch(bind_ip, bind_port):
import socket
logging.info("Monkey patching socket to bind to: %s:%s" % (bind_ip, bind_port))
socket.bind_addr = (bind_ip, int(bind_port))
socket.create_connection_original = socket.create_connection
socket.create_connection = create_connection