Browse Source

pyln: Add TOR and SOCKS5 support in pyln.proto.wire.connect

I wanted to talk to TOR-based nodes, so here comes TOR support :-)
fix-mocks
Christian Decker 4 years ago
committed by Rusty Russell
parent
commit
68d08fc7d7
  1. 13
      contrib/pyln-proto/pyln/proto/wire.py
  2. 1
      contrib/pyln-proto/requirements.txt

13
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()

1
contrib/pyln-proto/requirements.txt

@ -3,3 +3,4 @@ cryptography==3.2
coincurve==13.0.0
base58==1.0.2
mypy
pysocks==1.7.*

Loading…
Cancel
Save