(node_factory)
| 4434 | |
| 4435 | @unittest.skipUnless(RUST, 'RUST is not enabled') |
| 4436 | def test_wss_proxy(node_factory): |
| 4437 | wss_port = node_factory.get_unused_port() |
| 4438 | ws_port = node_factory.get_unused_port() |
| 4439 | port = node_factory.get_unused_port() |
| 4440 | wss_proxy_certs = node_factory.directory + '/wss-proxy-certs' |
| 4441 | l1 = node_factory.get_node(options={'addr': ':' + str(port), |
| 4442 | 'bind-addr': 'ws:127.0.0.1:' + str(ws_port), |
| 4443 | 'wss-bind-addr': ['127.0.0.1:' + str(wss_port), '[::1]:' + str(wss_port)], |
| 4444 | 'wss-certs': wss_proxy_certs, |
| 4445 | 'dev-allow-localhost': None}) |
| 4446 | |
| 4447 | # Some depend on ipv4 vs ipv6 behaviour... |
| 4448 | for b in l1.rpc.getinfo()['binding']: |
| 4449 | if b['type'] == 'ipv4': |
| 4450 | assert b == {'type': 'ipv4', 'address': '0.0.0.0', 'port': port} |
| 4451 | elif b['type'] == 'ipv6': |
| 4452 | assert b == {'type': 'ipv6', 'address': '::', 'port': port} |
| 4453 | else: |
| 4454 | assert b == {'type': 'websocket', |
| 4455 | 'address': '127.0.0.1', |
| 4456 | 'subtype': 'ipv4', |
| 4457 | 'port': ws_port} |
| 4458 | |
| 4459 | # Adapter to turn web secure socket into a stream "connection" |
| 4460 | class BindWebSecureSocket(object): |
| 4461 | def __init__(self, hostname, port): |
| 4462 | certfile = f'{wss_proxy_certs}/client.pem' |
| 4463 | keyfile = f'{wss_proxy_certs}/client-key.pem' |
| 4464 | self.ws = websocket.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE, "ssl_version": ssl.PROTOCOL_TLS_CLIENT, "certfile": certfile, "keyfile": keyfile}) |
| 4465 | self.ws.settimeout(TIMEOUT) |
| 4466 | try: |
| 4467 | self.ws.connect("wss://" + hostname + ":" + str(port)) |
| 4468 | except Exception as e: |
| 4469 | raise Exception(f"WebSocket connection failed: {e}") |
| 4470 | self.recvbuf = bytes() |
| 4471 | |
| 4472 | def send(self, data): |
| 4473 | self.ws.send(data, websocket.ABNF.OPCODE_BINARY) |
| 4474 | |
| 4475 | def recv(self, maxlen): |
| 4476 | while len(self.recvbuf) < maxlen: |
| 4477 | self.recvbuf += self.ws.recv() |
| 4478 | |
| 4479 | ret = self.recvbuf[:maxlen] |
| 4480 | self.recvbuf = self.recvbuf[maxlen:] |
| 4481 | return ret |
| 4482 | |
| 4483 | # This might happen really early! |
| 4484 | l1.daemon.logsearch_start = 0 |
| 4485 | l1.daemon.wait_for_log(f'Websocket Secure Server Started at 127.0.0.1:{wss_port}') |
| 4486 | l1.daemon.logsearch_start = 0 |
| 4487 | l1.daemon.wait_for_log(fr'Websocket Secure Server Started at \[::1\]:{wss_port}') |
| 4488 | |
| 4489 | wss = BindWebSecureSocket('localhost', wss_port) |
| 4490 | |
| 4491 | lconn = wire.LightningConnection(wss, |
| 4492 | wire.PublicKey(bytes.fromhex(l1.info['id'])), |
| 4493 | wire.PrivateKey(bytes([1] * 32)), |
nothing calls this directly
no test coverage detected