-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
110 changed files
with
371 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
ARG CLN_VERSION="0.11.1" | ||
ARG CLN_VERSION="23.11.2" | ||
|
||
FROM elementsproject/lightningd:v${CLN_VERSION} | ||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import os | ||
from pyln.testing.fixtures import * # noqa: F401,F403 | ||
import urllib | ||
from ephemeral_port_reserve import reserve | ||
|
||
plugin_path = os.path.join(os.path.dirname(__file__), "prometheus.py") | ||
|
||
|
||
def test_prometheus_starts(node_factory): | ||
l1 = node_factory.get_node() | ||
# Test dynamically | ||
l1.rpc.plugin_start(plugin_path) | ||
l1.rpc.plugin_stop(plugin_path) | ||
l1.rpc.plugin_start(plugin_path) | ||
l1.stop() | ||
# Then statically | ||
l1.daemon.opts["plugin"] = plugin_path | ||
l1.start() | ||
|
||
|
||
def test_prometheus_scrape(node_factory): | ||
"""Test that we can scrape correctly. | ||
""" | ||
l1 = node_factory.get_node(options={'plugin': plugin_path}) | ||
scrape = urllib.request.urlopen("http://localhost:9750") | ||
|
||
|
||
|
||
def test_prometheus_channels(node_factory): | ||
port = reserve() | ||
l1, l2, l3 = node_factory.line_graph( | ||
3, | ||
opts=[ | ||
{}, | ||
{'plugin': plugin_path, 'prometheus-listen': f'127.0.0.1:{port}'}, | ||
{} | ||
] | ||
) | ||
scrape = urllib.request.urlopen(f'http://localhost:{port}') | ||
print(scrape) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# clearnet enforcer plugin | ||
This plugin aims to prefer usage over clearnet connections. | ||
It does so by disconnecing TOR connections when there are known and usable | ||
clearnet addresses. | ||
|
||
# Options | ||
|
||
# Methods | ||
## clearnet-enforce [peer_id] | ||
Tries to enforce clearnet on all peer or on a given peer_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#!/usr/bin/env python3 | ||
import socket | ||
from contextlib import closing | ||
from pyln.client import Plugin, RpcError | ||
|
||
plugin = Plugin() | ||
|
||
|
||
def get_address_type(addrstr: str): | ||
""" I know this can be more sophisticated, but works """ | ||
if ".onion:" in addrstr: | ||
return 'tor' | ||
if addrstr[0].isdigit(): | ||
return 'ipv4' | ||
if addrstr.startswith("["): | ||
return 'ipv6' | ||
return 'dns' | ||
|
||
|
||
# taken from: | ||
# https://stackoverflow.com/questions/19196105/how-to-check-if-a-network-port-is-open | ||
def check_socket(host: str, port: int, timeout: float = None): | ||
""" Checks if a socket can be opened to a host """ | ||
if host.count('.') == 3: | ||
proto = socket.AF_INET | ||
if host.count(':') > 1: | ||
proto = socket.AF_INET6 | ||
with closing(socket.socket(proto, socket.SOCK_STREAM)) as sock: | ||
if timeout is not None: | ||
sock.settimeout(timeout) # seconds (float) | ||
if sock.connect_ex((host, port)) == 0: | ||
return True | ||
else: | ||
return False | ||
|
||
|
||
def clearnet_pid(peer: dict, messages: list): | ||
peer_id = peer['id'] | ||
if not peer['connected']: | ||
messages += [f"Peer is not conencted: {peer_id}"] | ||
return False | ||
if get_address_type(peer['netaddr'][0]) != 'tor': | ||
messages += [f"Already connected via clearnet: {peer_id}"] | ||
return True | ||
|
||
# lets check what gossip knows about this peer | ||
nodes = plugin.rpc.listnodes(peer_id)['nodes'] | ||
if len(nodes) == 0: | ||
messages += [f"Error: No gossip for: {peer_id}"] | ||
return | ||
addrs = [a for a in nodes[0]['addresses'] if not a['type'].startswith("tor")] | ||
if len(addrs) == 0: | ||
messages += [f"Error: No clearnet addresses known for: {peer_id}"] | ||
return | ||
|
||
# now check addrs for open ports | ||
for addr in addrs: | ||
if addr['type'] == 'dns': | ||
messages += [f"TODO: DNS lookups for: {addr['address']}"] | ||
continue | ||
if check_socket(addr['address'], addr['port'], 2.0): | ||
# disconnect | ||
result = plugin.rpc.disconnect(peer_id, True) | ||
if len(result) != 0: | ||
messages += [f"Error: Can't disconnect: {peer_id} {result}"] | ||
continue | ||
|
||
# try clearnet connection | ||
try: | ||
result = plugin.rpc.connect(peer_id, addr['address'], addr['port']) | ||
newtype = result['address']['type'] | ||
if not newtype.startswith('tor'): | ||
messages += [f"Established clearnet connection for: {peer_id} with {newtype}"] | ||
return True | ||
except RpcError: # we got an connection error, try reconnect | ||
messages += [f"Error: Connection failed for: {peer_id} with {addr['type']}"] | ||
try: | ||
result = plugin.rpc.connect(peer_id) # without address | ||
newtype = result['address']['type'] | ||
if not newtype.startswith('tor'): | ||
messages += [f"Established clearnet connection for: {peer_id} with {newtype}"] | ||
return True | ||
except RpcError: # we got a reconnection error | ||
messages += [f"Error: Reconnection failed for: {peer_id}"] | ||
continue | ||
messages += [f"Reconnected: {peer_id} with {newtype}"] | ||
continue | ||
return False | ||
|
||
|
||
@plugin.method("clearnet") | ||
def clearnet(plugin: Plugin, peer_id: str = None): | ||
""" Enforce a clearnet connection on all peers or a given `peer_id`.""" | ||
if peer_id is None: | ||
peers = plugin.rpc.listpeers(peer_id)['peers'] | ||
else: | ||
if not isinstance(peer_id, str) or len(peer_id) != 66: | ||
return f"Error: Invalid peer_id: {peer_id}" | ||
peers = plugin.rpc.listpeers(peer_id)['peers'] | ||
if len(peers) == 0: | ||
return f"Error: peer not found: {peer_id}" | ||
|
||
messages = [] | ||
for peer in peers: | ||
clearnet_pid(peer, messages) | ||
return messages | ||
|
||
|
||
@plugin.init() | ||
def init(options: dict, configuration: dict, plugin: Plugin, **kwargs): | ||
plugin.log(f"clearnet enforcer plugin initialized") | ||
|
||
|
||
plugin.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pyln-client>=0.12 |
Oops, something went wrong.