A minimal implementation of proxy for outgoing connections using PySocks,

special arg --tor currently just sets host and port for the socks_proxy.
This commit is contained in:
Lee Miller 2023-03-28 07:01:29 +03:00
parent d7ee73843e
commit 8fd34d879d
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
4 changed files with 47 additions and 3 deletions

View File

@ -75,7 +75,9 @@ class Connection(threading.Thread):
self.s.settimeout(0)
if not self.server:
if self.network == 'ip':
self.send_queue.put(message.Version(self.host, self.port))
self.send_queue.put(message.Version(
('127.0.0.1' if shared.socks_proxy else self.host),
self.port))
else:
self.send_queue.put(message.Version('127.0.0.1', 7656))
while True:
@ -150,8 +152,10 @@ class Connection(threading.Thread):
peer_str = '{0.host_print}:{0.port}'.format(self)
logging.debug('Connecting to %s', peer_str)
timeout = 30 if shared.tor else 10
try:
self.s = socket.create_connection((self.host, self.port), 10)
self.s = socket.create_connection((self.host, self.port), timeout)
self.status = 'connected'
logging.debug('Established TCP connection to %s', peer_str)
except socket.timeout:

View File

@ -8,6 +8,11 @@ import os
import signal
import socket
try:
import socks
except ImportError:
socks = None
from . import i2p, shared
from .advertiser import Advertiser
from .manager import Manager
@ -52,6 +57,14 @@ def parse_arguments(): # pylint: disable=too-many-branches,too-many-statements
'--i2p-transient', action='store_true',
help='Generate new I2P destination on start')
if socks is not None:
parser.add_argument(
'--socks-proxy',
help='SOCKS proxy address in the form <HOST>:<PORT>')
parser.add_argument(
'--tor', action='store_true',
help='The SOCKS proxy is tor, use 127.0.0.1:9050 if not specified')
args = parser.parse_args()
if args.port:
shared.listening_port = args.port
@ -98,6 +111,13 @@ def parse_arguments(): # pylint: disable=too-many-branches,too-many-statements
shared.i2p_sam_port = args.i2p_sam_port
if args.i2p_transient:
shared.i2p_transient = True
if args.socks_proxy:
addr = args.socks_proxy.split(':')
shared.socks_proxy = (addr[0], int(addr[1]))
if args.tor:
shared.tor = True
if not args.socks_proxy:
shared.socks_proxy = ('127.0.0.1', 9050)
def bootstrap_from_dns():
@ -238,6 +258,23 @@ def main():
'Error while creating data directory in: %s',
shared.data_directory, exc_info=True)
if shared.socks_proxy:
# FIXME: the Connection() code would be too complex
# without this monkeypatching
def create_connection(
dst_pair, timeout=None, source_address=None,
proxy_type=socks.PROXY_TYPE_SOCKS5,
proxy_addr=shared.socks_proxy[0],
proxy_port=shared.socks_proxy[1], proxy_rdns=True,
proxy_username=None, proxy_password=None,
socket_options=None
):
return socks.create_connection(
dst_pair, timeout, source_address, proxy_type, proxy_addr,
proxy_port, proxy_rdns, proxy_username, proxy_password,
socket_options)
socket.create_connection = create_connection
if shared.ip_enabled and not shared.trusted_peer:
bootstrap_from_dns()

View File

@ -187,7 +187,7 @@ class Manager(threading.Thread):
'r', newline='', encoding='ascii'
) as src:
reader = csv.reader(src)
shared.core_nodes = {tuple(row) for row in reader}
shared.core_nodes = {(row[0], int(row[1])) for row in reader}
shared.node_pool.update(shared.core_nodes)
with open(

View File

@ -27,6 +27,9 @@ header_length = 24
i2p_dest_obj_type = 0x493250
i2p_dest_obj_version = 1
socks_proxy = None
tor = False
i2p_enabled = False
i2p_transient = False
i2p_sam_host = '127.0.0.1'