MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_wss_proxy

Function test_wss_proxy

tests/test_connection.py:4387–4468  ·  view source on GitHub ↗
(node_factory)

Source from the content-addressed store, hash-verified

4385
4386@unittest.skipUnless(RUST, 'RUST is not enabled')
4387def test_wss_proxy(node_factory):
4388 wss_port = node_factory.get_unused_port()
4389 ws_port = node_factory.get_unused_port()
4390 port = node_factory.get_unused_port()
4391 wss_proxy_certs = node_factory.directory + '/wss-proxy-certs'
4392 l1 = node_factory.get_node(options={'addr': ':' + str(port),
4393 'bind-addr': 'ws:127.0.0.1:' + str(ws_port),
4394 'wss-bind-addr': ['127.0.0.1:' + str(wss_port), '[::1]:' + str(wss_port)],
4395 'wss-certs': wss_proxy_certs,
4396 'dev-allow-localhost': None})
4397
4398 # Some depend on ipv4 vs ipv6 behaviour...
4399 for b in l1.rpc.getinfo()['binding']:
4400 if b['type'] == 'ipv4':
4401 assert b == {'type': 'ipv4', 'address': '0.0.0.0', 'port': port}
4402 elif b['type'] == 'ipv6':
4403 assert b == {'type': 'ipv6', 'address': '::', 'port': port}
4404 else:
4405 assert b == {'type': 'websocket',
4406 'address': '127.0.0.1',
4407 'subtype': 'ipv4',
4408 'port': ws_port}
4409
4410 # Adapter to turn web secure socket into a stream "connection"
4411 class BindWebSecureSocket(object):
4412 def __init__(self, hostname, port):
4413 certfile = f'{wss_proxy_certs}/client.pem'
4414 keyfile = f'{wss_proxy_certs}/client-key.pem'
4415 self.ws = websocket.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE, "ssl_version": ssl.PROTOCOL_TLS_CLIENT, "certfile": certfile, "keyfile": keyfile})
4416 self.ws.settimeout(TIMEOUT)
4417 try:
4418 self.ws.connect("wss://" + hostname + ":" + str(port))
4419 except Exception as e:
4420 raise Exception(f"WebSocket connection failed: {e}")
4421 self.recvbuf = bytes()
4422
4423 def send(self, data):
4424 self.ws.send(data, websocket.ABNF.OPCODE_BINARY)
4425
4426 def recv(self, maxlen):
4427 while len(self.recvbuf) < maxlen:
4428 self.recvbuf += self.ws.recv()
4429
4430 ret = self.recvbuf[:maxlen]
4431 self.recvbuf = self.recvbuf[maxlen:]
4432 return ret
4433
4434 # This might happen really early!
4435 l1.daemon.logsearch_start = 0
4436 l1.daemon.wait_for_log(f'Websocket Secure Server Started at 127.0.0.1:{wss_port}')
4437 l1.daemon.logsearch_start = 0
4438 l1.daemon.wait_for_log(fr'Websocket Secure Server Started at \[::1\]:{wss_port}')
4439
4440 wss = BindWebSecureSocket('localhost', wss_port)
4441
4442 lconn = wire.LightningConnection(wss,
4443 wire.PublicKey(bytes.fromhex(l1.info['id'])),
4444 wire.PrivateKey(bytes([1] * 32)),

Callers

nothing calls this directly

Calls 9

shakeMethod · 0.95
read_messageMethod · 0.95
send_messageMethod · 0.95
BindWebSecureSocketClass · 0.85
get_unused_portMethod · 0.80
wait_for_logMethod · 0.80
get_nodeMethod · 0.45
getinfoMethod · 0.45
from_bytesMethod · 0.45

Tested by

no test coverage detected