SatSale

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

ssh_tunnel.py (2652B)


      1 import subprocess
      2 import os
      3 import logging
      4 
      5 import config
      6 
      7 
      8 def open_tunnel(host, port):
      9     # If tunnel is required (might make things easier)
     10     try:
     11         command = [
     12             "ssh",
     13             "-q",
     14             "-N",
     15             "-L",
     16             "{}:localhost:{}".format(port, port),
     17             config.tunnel_host,
     18             "-p {}".format(config.tunnel_port),
     19         ]
     20         logging.info("Opening tunnel to {}.".format(" ".join(command)))
     21         return subprocess.Popen(command)
     22 
     23     except Exception as e:
     24         logging.error("FAILED TO OPEN TUNNEL. Exception: {}".format(e))
     25 
     26     return None
     27 
     28 
     29 def clightning_unix_domain_socket_ssh(rpc_file, rpc_store_dir=None):
     30     if rpc_store_dir is None:
     31         rpc_store_dir = os.getcwd()
     32 
     33     local_file = rpc_store_dir + "/lightning-rpc"
     34 
     35     # ssh -nNT -L lightning-rpc:~/.lightning/lightning-rpc config.tunnel_host -p config.tunnel_port
     36     try:
     37         command = [
     38             "ssh",
     39             "-nNT",
     40             "-L",
     41             "{}:{}".format(local_file, rpc_file),
     42             "{}".format(config.tunnel_host),
     43             "-p {}".format(config.tunnel_port),
     44             ]
     45         logging.info("Opening tunnel to {}.".format(" ".join(command)))
     46         tunnel_proc = subprocess.Popen(command)
     47         return tunnel_proc
     48 
     49     except Exception as e:
     50         logging.error(
     51             "FAILED TO OPEN UNIX DOMAIN SOCKET OVER SSH. Exception: {}".format(e)
     52         )
     53 
     54     return None
     55 
     56 
     57 def rm_lightning_rpc_file():
     58     if os.path.exists("lightning-rpc"):
     59         os.remove("lightning-rpc")
     60     return
     61 
     62 
     63 def close_tunnels(ssh_processes):
     64     if ssh_processes is not None:
     65         for proc in ssh_processes:
     66             try:
     67                 proc.kill()
     68             except Exception:
     69                 continue
     70 
     71     if "clightning" in config.payment_methods:
     72         rm_lightning_rpc_file()
     73     return
     74 
     75 
     76 # Open tunnel
     77 def open_tunnels():
     78     ssh_tunnel_processes = []
     79     if config.tunnel_host is not None:
     80         for method in config.payment_methods:
     81             if method['name'] == "bitcoind":
     82                 ssh_tunnel_processes.append(open_tunnel(config.tunnel_host, method['rpcport']))
     83 
     84             # Also for lnd if enabled
     85             if method['name'] == "lnd":
     86                 ssh_tunnel_processes.append(open_tunnel(config.tunnel_host, method['lnd_rpcport']))
     87 
     88             # And if clightning is enabled
     89             if method['name'] == "clightning":
     90                 rm_lightning_rpc_file()
     91                 ssh_tunnel_processes.append(clightning_unix_domain_socket_ssh(method['clightning_rpc_file']))
     92 
     93     return [proc for proc in ssh_tunnel_processes if proc is not None]