def _parse_invitation_token_or_url(raw: str) -> Union[BackendInvitationAddr, InvitationToken]: try: return InvitationToken.from_hex(raw) except ValueError: try: return BackendInvitationAddr.from_url(raw) except ValueError: raise ValueError("Must be an invitation URL or Token")
async def test_proxy_with_websocket(monkeypatch, connection_type, proxy_type): signing_key = SigningKey.generate() device_id = DeviceID("zack@pc1") proxy_events = [] def _event_hook(event): proxy_events.append(event) async with trio.open_nursery() as nursery: target_port = await start_port_watchdog(nursery, _event_hook) proxy_port = await start_proxy_for_websocket(nursery, target_port, _event_hook) if proxy_type == "http_proxy": proxy_url = f"http://127.0.0.1:{proxy_port}" monkeypatch.setitem(os.environ, "http_proxy", proxy_url) else: assert proxy_type == "http_proxy_pac" pac_server_port = await start_pac_server( nursery=nursery, pac_rule=f"PROXY 127.0.0.1:{proxy_port}", event_hook=_event_hook) pac_server_url = f"http://127.0.0.1:{pac_server_port}" monkeypatch.setitem(os.environ, "http_proxy_pac", pac_server_url) # HTTP_PROXY_PAC has priority over HTTP_PROXY monkeypatch.setitem(os.environ, "http_proxy", f"http://127.0.0.1:{target_port}") async with real_clock_timeout(): with pytest.raises(BackendNotAvailable): if connection_type == "authenticated": await connect_as_authenticated( addr=BackendOrganizationAddr.from_url( f"parsec://127.0.0.1:{target_port}/CoolOrg?no_ssl=true&rvk=7NFDS4VQLP3XPCMTSEN34ZOXKGGIMTY2W2JI2SPIHB2P3M6K4YWAssss" ), device_id=device_id, signing_key=signing_key, ) else: assert connection_type == "invited" await connect_as_invited(addr=BackendInvitationAddr.from_url( f"parsec://127.0.0.1:{target_port}/CoolOrg?no_ssl=true&action=claim_user&token=3a50b191122b480ebb113b10216ef343" )) assert proxy_events == [ *(["PAC file retreived from server"] if proxy_type == "http_proxy_pac" else []), "Connected to proxy", "Reaching target through proxy", ] nursery.cancel_scope.cancel()