Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
Add Tor requests to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Mar 3, 2021
1 parent 2a2429d commit 857f004
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
17 changes: 14 additions & 3 deletions watchtower-plugin/net/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def send_appointment(tower_id, tower, appointment_dict, signature):
return response


def post_request(data, endpoint, tower_id):
def post_request(data, endpoint, tower_id, socks_port=9050):
"""
Sends a post request to the tower.
Expand All @@ -110,13 +110,24 @@ def post_request(data, endpoint, tower_id):
"""

try:
return requests.post(url=endpoint, json=data, timeout=5)
if ".onion" in endpoint:
proxies = {
'http': f'socks5h://127.0.0.1:{socks_port}',
'https': f'socks5h://127.0.0.1:{socks_port}'
}

return requests.post(url=endpoint, json=data, timeout=15, proxies=proxies)
else:
return requests.post(url=endpoint, json=data, timeout=5)

except ConnectTimeout:
message = f"Cannot connect to {tower_id}. Connection timeout"

except ConnectionError:
message = f"Cannot connect to {tower_id}. Tower cannot be reached"
if ".onion" in endpoint:
message = f"Cannot connect to {tower_id}. Trying to connect to a Tor onion address. Are you running Tor?"
else:
message = f"Cannot connect to {tower_id}. Tower cannot be reached"

except (InvalidSchema, MissingSchema, InvalidURL):
message = f"Invalid URL. No schema, or invalid schema, found (url={endpoint}, tower_id={tower_id})"
Expand Down
1 change: 1 addition & 0 deletions watchtower-plugin/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pyln-client
pyln-testing
requests
coincurve
cryptography>=2.8
Expand Down
8 changes: 5 additions & 3 deletions watchtower-plugin/watchtower.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"APPOINTMENTS_FOLDER_NAME": {"value": "appointment_receipts", "type": str, "path": True},
"TOWERS_DB": {"value": "towers", "type": str, "path": True},
"PRIVATE_KEY": {"value": "sk.der", "type": str, "path": True},
"SOCKS_PORT": {"value": 9050, "type": int},
}


Expand All @@ -45,7 +46,7 @@ class WTClient:
"""
Holds all the data regarding the watchtower client.
Fires an additional tread to take care of retries.
Fires an additional thread to take care of retries.
Args:
sk (:obj:`PrivateKey): the user private key. Used to sign appointment sent to the towers.
Expand All @@ -69,6 +70,7 @@ def __init__(self, sk, user_id, config):
self.retrier = Retrier(config.get("MAX_RETRIES"), Queue())
self.config = config
self.lock = Lock()
self.socks_port = config.get("SOCKS_PORT")

# Populate the towers dict with data from the db
for tower_id, tower_info in self.db_manager.load_all_tower_records().items():
Expand Down Expand Up @@ -170,7 +172,7 @@ def register(plugin, tower_id, host=None, port=None):

plugin.log(f"Registering in the Eye of Satoshi (tower_id={tower_id})")

response = process_post_response(post_request(data, register_endpoint, tower_id))
response = process_post_response(post_request(data, register_endpoint, tower_id, socks_port=plugin.wt_client.socks_port))
available_slots = response.get("available_slots")
subscription_expiry = response.get("subscription_expiry")
tower_signature = response.get("subscription_signature")
Expand Down Expand Up @@ -234,7 +236,7 @@ def get_appointment(plugin, tower_id, locator):
get_appointment_endpoint = f"{tower_netaddr}/get_appointment"
plugin.log(f"Requesting appointment from {tower_id}")

response = process_post_response(post_request(data, get_appointment_endpoint, tower_id))
response = process_post_response(post_request(data, get_appointment_endpoint, tower_id, socks_port=plugin.wt_client.socks_port))
return response

except (InvalidParameter, TowerConnectionError, TowerResponseError) as e:
Expand Down

0 comments on commit 857f004

Please sign in to comment.