summary: use proxy if configuration says to.

All plugins should take care to do this if config
specifies always-use-proxy!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2020-11-12 13:54:06 +10:30
committed by Christian Decker
parent df4b422491
commit 6cd4726369
2 changed files with 16 additions and 4 deletions

View File

@@ -1,3 +1,4 @@
pyln-client>=0.7.3 pyln-client>=0.7.3
requests>=2.0.0 requests>=2.10.0
requests[socks]>=2.10.0
packaging>=14.1 packaging>=14.1

View File

@@ -53,14 +53,15 @@ class PeerThread(threading.Thread):
class PriceThread(threading.Thread): class PriceThread(threading.Thread):
def __init__(self): def __init__(self, proxies):
super().__init__() super().__init__()
self.daemon = True self.daemon = True
self.proxies = proxies
def run(self): def run(self):
while True: while True:
try: try:
r = requests.get('https://www.bitstamp.net/api/v2/ticker/BTC{}'.format(plugin.currency)) r = requests.get('https://www.bitstamp.net/api/v2/ticker/BTC{}'.format(plugin.currency), proxies=self.proxies)
plugin.fiat_per_btc = float(r.json()['last']) plugin.fiat_per_btc = float(r.json()['last'])
except Exception as ex: except Exception as ex:
plugin.log("[PriceThread] " + str(ex), 'warn') plugin.log("[PriceThread] " + str(ex), 'warn')
@@ -232,11 +233,21 @@ def init(options, configuration, plugin):
plugin.persist['availcount'] = 0 plugin.persist['availcount'] = 0
info = plugin.rpc.getinfo() info = plugin.rpc.getinfo()
config = plugin.rpc.listconfigs()
if 'always-use-proxy' in config:
paddr = config['proxy']
# Default port in 9050
if ':' not in paddr:
paddr += ':9050'
proxies = { 'https': 'socks5h://' + paddr,
'http': 'socks5h://' + paddr }
else:
proxies = None
# Measure availability # Measure availability
PeerThread().start() PeerThread().start()
# Try to grab conversion price # Try to grab conversion price
PriceThread().start() PriceThread(proxies).start()
# Prefer IPv4, otherwise take any to give out address. # Prefer IPv4, otherwise take any to give out address.
best_address = None best_address = None