Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, configuration):
self.closed = None
self.configuration = configuration
self.stream_queue = []
self._events = []
self._next_stream_bidi = 0 if configuration.is_client else 1
self._next_stream_uni = 2 if configuration.is_client else 3
self._quic_logger = QuicLogger().start_trace(
is_client=configuration.is_client, odcid=b""
)
def client_and_server(
client_kwargs={},
client_options={},
client_patch=lambda x: None,
handshake=True,
server_kwargs={},
server_certfile=SERVER_CERTFILE,
server_keyfile=SERVER_KEYFILE,
server_options={},
server_patch=lambda x: None,
transport_options={},
):
client_configuration = QuicConfiguration(
is_client=True, quic_logger=QuicLogger(), **client_options
)
client_configuration.load_verify_locations(cafile=SERVER_CACERTFILE)
client = QuicConnection(configuration=client_configuration, **client_kwargs)
client._ack_delay = 0
client_patch(client)
server_configuration = QuicConfiguration(
is_client=False, quic_logger=QuicLogger(), **server_options
)
server_configuration.load_cert_chain(server_certfile, server_keyfile)
server = QuicConnection(configuration=server_configuration, **server_kwargs)
server._ack_delay = 0
server_patch(server)
server_keyfile=SERVER_KEYFILE,
server_options={},
server_patch=lambda x: None,
transport_options={},
):
client_configuration = QuicConfiguration(
is_client=True, quic_logger=QuicLogger(), **client_options
)
client_configuration.load_verify_locations(cafile=SERVER_CACERTFILE)
client = QuicConnection(configuration=client_configuration, **client_kwargs)
client._ack_delay = 0
client_patch(client)
server_configuration = QuicConfiguration(
is_client=False, quic_logger=QuicLogger(), **server_options
)
server_configuration.load_cert_chain(server_certfile, server_keyfile)
server = QuicConnection(configuration=server_configuration, **server_kwargs)
server._ack_delay = 0
server_patch(server)
# perform handshake
if handshake:
client.connect(SERVER_ADDR, now=time.time())
for i in range(3):
roundtrip(client, server)
yield client, server
# close
def create_standalone_client(self, **client_options):
client = QuicConnection(
configuration=QuicConfiguration(
is_client=True, quic_logger=QuicLogger(), **client_options
)
)
client._ack_delay = 0
# kick-off handshake
client.connect(SERVER_ADDR, now=time.time())
self.assertEqual(drop(client), 1)
return client
This test ensures handshake success and stream data is successfully sent
and received in the presence of packet loss (randomized 25% in each direction).
"""
data = b"Z" * 65536
server_configuration = QuicConfiguration(
is_client=False, quic_logger=QuicLogger()
)
server_configuration.load_cert_chain(SERVER_CERTFILE, SERVER_KEYFILE)
run(self.run_server(configuration=server_configuration))
response = run(
self.run_client(
"127.0.0.1",
configuration=QuicConfiguration(
is_client=True, quic_logger=QuicLogger()
),
request=data,
)
)
self.assertEqual(response, data)
class SessionTicketStore:
"""
Simple in-memory store for session tickets.
"""
def __init__(self) -> None:
self.tickets: Dict[bytes, SessionTicket] = {}
def add(self, ticket: SessionTicket) -> None:
self.tickets[ticket.ticket] = ticket
def pop(self, label: bytes) -> Optional[SessionTicket]:
return self.tickets.pop(label, None)
class QuicLoggerCustom(QuicLogger):
"""
Custom QUIC logger which writes one trace per file.
"""
def __init__(self, path: str) -> None:
if not os.path.isdir(path):
raise ValueError("QUIC log output directory '%s' does not exist" % path)
self.path = path
super().__init__()
def end_trace(self, trace: QuicLoggerTrace) -> None:
trace_dict = trace.to_dict()
trace_path = os.path.join(
self.path, trace_dict["common_fields"]["ODCID"] + ".qlog"
)
with open(trace_path, "w") as logger_fp:
)
args = parser.parse_args()
logging.basicConfig(
format="%(asctime)s %(levelname)s %(name)s %(message)s",
level=logging.DEBUG if args.verbose else logging.INFO,
)
configuration = QuicConfiguration(
alpn_protocols=["siduck"], is_client=True, max_datagram_frame_size=65536
)
if args.insecure:
configuration.verify_mode = ssl.CERT_NONE
if args.quic_log:
configuration.quic_logger = QuicLogger()
if args.secrets_log:
configuration.secrets_log_file = open(args.secrets_log, "a")
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(
run(configuration=configuration, host=args.host, port=args.port)
)
finally:
if configuration.quic_logger is not None:
with open(args.quic_log, "w") as logger_fp:
json.dump(configuration.quic_logger.to_dict(), logger_fp, indent=4)
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="increase logging verbosity"
)
args = parser.parse_args()
logging.basicConfig(
format="%(asctime)s %(levelname)s %(name)s %(message)s",
level=logging.DEBUG if args.verbose else logging.INFO,
)
# prepare configuration
configuration = QuicConfiguration(is_client=True, alpn_protocols=H3_ALPN)
if args.quic_log:
configuration.quic_logger = QuicLogger()
if args.secrets_log:
configuration.secrets_log_file = open(args.secrets_log, "a")
if args.session_ticket:
try:
with open(args.session_ticket, "rb") as fp:
configuration.session_ticket = pickle.load(fp)
except FileNotFoundError:
pass
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(
run(configuration=configuration, url=args.url, data=args.data)
)
finally:
if configuration.quic_logger is not None:
async def run(servers, tests, quic_log=False, secrets_log_file=None) -> None:
for server in servers:
if server.structured_logging:
server.result |= Result.L
for test_name, test_func in tests:
print("\n=== %s %s ===\n" % (server.name, test_name))
configuration = QuicConfiguration(
alpn_protocols=H3_ALPN + H0_ALPN,
is_client=True,
quic_logger=QuicLogger(),
secrets_log_file=secrets_log_file,
verify_mode=server.verify_mode,
)
if test_name == "test_throughput":
timeout = 60
else:
timeout = 5
try:
await asyncio.wait_for(
test_func(server, configuration), timeout=timeout
)
except Exception as exc:
print(exc)
if quic_log:
with open("%s-%s.qlog" % (server.name, test_name), "w") as logger_fp: