|
| 1 | + |
| 2 | +# This script tests a list of Electrum servers by attempting to connect to them using SSL. |
| 3 | +# It sends a simple Electrum JSON-RPC request (server.version) and checks if a valid response is received. |
| 4 | +# If the connection or SSL handshake fails, it prints the error. |
| 5 | +# |
| 6 | +# If you see the error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate (_ssl.c:1007) |
| 7 | +# It means the server is using a self-signed SSL certificate, which is not trusted by your system's CA store. |
| 8 | +# This is a security feature in Python's ssl module to prevent man-in-the-middle attacks. |
| 9 | +# |
| 10 | +# To connect anyway (not recommended for production), you could disable certificate verification, |
| 11 | +# but this exposes you to security risks. Only do this for testing with servers you trust. |
| 12 | +# |
| 13 | +# Example of disabling verification (uncomment and use at your own risk): |
| 14 | +# context = ssl._create_unverified_context() |
| 15 | + |
| 16 | +import socket |
| 17 | +import ssl |
| 18 | +import sys |
| 19 | +from typing import List, Tuple |
| 20 | + |
| 21 | +# Mainnet Electrum servers (name, host, port) |
| 22 | +MAINNET_SERVERS: List[Tuple[str, str, int]] = [ |
| 23 | + ("blockstream.info", "blockstream.info", 700), |
| 24 | + ("electrum.blockstream.info", "electrum.blockstream.info", 50002), |
| 25 | + ("bitcoin.lu.ke", "bitcoin.lu.ke", 50002), |
| 26 | + ("electrum.emzy.de", "electrum.emzy.de", 50002), |
| 27 | + ("electrum.bitaroo.net", "electrum.bitaroo.net", 50002), |
| 28 | + ("electrum.diynodes.com", "electrum.diynodes.com", 50022), |
| 29 | + ("fulcrum.sethforprivacy.com", "fulcrum.sethforprivacy.com", 50002), |
| 30 | +] |
| 31 | + |
| 32 | +# Testnet Electrum servers (name, host, port) |
| 33 | +TESTNET_SERVERS: List[Tuple[str, str, int]] = [ |
| 34 | + ("testnet.aranguren.org", "testnet.aranguren.org", 51002), |
| 35 | + ("testnet.qtornado.com", "testnet.qtornado.com", 51002), |
| 36 | +] |
| 37 | + |
| 38 | +def test_electrum_server(host: str, port: int, timeout: float = 5.0, verify_cert: bool = True) -> bool: |
| 39 | + """ |
| 40 | + Try to connect to an Electrum server using SSL. |
| 41 | + Returns True if the connection and handshake succeed and a valid response is received, False otherwise. |
| 42 | + If verify_cert is False, self-signed certificates are accepted (not secure for production). |
| 43 | + """ |
| 44 | + if verify_cert: |
| 45 | + context = ssl.create_default_context() |
| 46 | + else: |
| 47 | + context = ssl._create_unverified_context() |
| 48 | + try: |
| 49 | + with socket.create_connection((host, port), timeout=timeout) as sock: |
| 50 | + with context.wrap_socket(sock, server_hostname=host) as ssock: |
| 51 | + # Send a simple Electrum request (server.version) |
| 52 | + ssock.sendall(b'{"id": 0, "method": "server.version", "params": ["electrum-client", "1.4"]}\n') |
| 53 | + response = ssock.recv(4096) |
| 54 | + if b'server.version' in response or b'result' in response: |
| 55 | + return True |
| 56 | + except Exception as e: |
| 57 | + print(f"Error with {host}:{port} - {e}") |
| 58 | + return False |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + import argparse |
| 63 | + parser = argparse.ArgumentParser(description="Test Electrum servers (mainnet and testnet)") |
| 64 | + parser.add_argument('--no-verify-cert', action='store_true', help='Disable SSL certificate verification (accept self-signed certs)') |
| 65 | + args = parser.parse_args() |
| 66 | + verify_cert = not args.no_verify_cert |
| 67 | + |
| 68 | + print("\nTesting Mainnet Electrum servers:") |
| 69 | + for name, host, port in MAINNET_SERVERS: |
| 70 | + print(f"Testing {name} ({host}:{port})...", end=" ") |
| 71 | + if test_electrum_server(host, port, verify_cert=verify_cert): |
| 72 | + print("OK") |
| 73 | + else: |
| 74 | + print("FAILED") |
| 75 | + |
| 76 | + print("\nTesting Testnet Electrum servers:") |
| 77 | + for name, host, port in TESTNET_SERVERS: |
| 78 | + print(f"Testing {name} ({host}:{port})...", end=" ") |
| 79 | + if test_electrum_server(host, port, verify_cert=verify_cert): |
| 80 | + print("OK") |
| 81 | + else: |
| 82 | + print("FAILED") |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + main() |
0 commit comments