From 68d08fc7d7a078bc70d8593f138a981179d398da Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 7 Dec 2020 11:26:32 +0100 Subject: [PATCH] pyln: Add TOR and SOCKS5 support in pyln.proto.wire.connect I wanted to talk to TOR-based nodes, so here comes TOR support :-) --- contrib/pyln-proto/pyln/proto/wire.py | 13 +++++++++++-- contrib/pyln-proto/requirements.txt | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/contrib/pyln-proto/pyln/proto/wire.py b/contrib/pyln-proto/pyln/proto/wire.py index adb1f0907..62b5cf1b0 100644 --- a/contrib/pyln-proto/pyln/proto/wire.py +++ b/contrib/pyln-proto/pyln/proto/wire.py @@ -6,9 +6,11 @@ from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305 from cryptography.hazmat.primitives.kdf.hkdf import HKDF from .primitives import Secret, PrivateKey, PublicKey from hashlib import sha256 +from typing import Tuple import coincurve import os import socket +import socks import struct import threading @@ -318,7 +320,8 @@ class LightningServerSocket(socket.socket): return (lconn, address) -def connect(local_privkey, node_id, host, port=9735): +def connect(local_privkey, node_id, host: str, port: int = 9735, + socks_addr: Tuple[str, int] = None): if isinstance(node_id, bytes) and len(node_id) == 33: remote_pubkey = PublicKey(node_id) elif isinstance(node_id, ec.EllipticCurvePublicKey): @@ -329,7 +332,13 @@ def connect(local_privkey, node_id, host, port=9735): raise ValueError( "node_id must be either a 33 byte array, or a PublicKey" ) - conn = socket.create_connection((host, port)) + + if socks_addr is None: + conn = socket.create_connection((host, port)) + else: + socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, *socks_addr, True) + conn = socks.socksocket() + conn.connect((host, port)) lconn = LightningConnection(conn, remote_pubkey, local_privkey, is_initiator=True) lconn.shake() diff --git a/contrib/pyln-proto/requirements.txt b/contrib/pyln-proto/requirements.txt index 0f9d06da9..b8b491ddc 100644 --- a/contrib/pyln-proto/requirements.txt +++ b/contrib/pyln-proto/requirements.txt @@ -3,3 +3,4 @@ cryptography==3.2 coincurve==13.0.0 base58==1.0.2 mypy +pysocks==1.7.*