-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import asyncio | ||
from aioquic.asyncio import QuicConnectionProtocol | ||
from aioquic.asyncio.protocol import QuicStreamHandler | ||
from aioquic.quic.events import StreamDataReceived, StreamReset, ConnectionTerminated | ||
from aioquic.quic.connection import QuicConnection | ||
|
||
class HTTPStreamer: | ||
""" | ||
Allows taking over a HTTP/3 stream. This is a simplified version that | ||
assumes the stream is already established. | ||
""" | ||
def __init__(self, stream_reader, stream_writer): | ||
self.stream_reader = stream_reader | ||
self.stream_writer = stream_writer | ||
|
||
async def read(self, size): | ||
return await self.stream_reader.read(size) | ||
|
||
async def write(self, data): | ||
self.stream_writer.write(data) | ||
await self.stream_writer.drain() | ||
|
||
def close(self): | ||
self.stream_writer.close() | ||
|
||
class StreamCreator: | ||
""" | ||
This class represents an entity capable of creating QUIC streams. | ||
""" | ||
def __init__(self, protocol: QuicConnectionProtocol): | ||
self.protocol = protocol | ||
|
||
async def open_stream(self) -> HTTPStreamer: | ||
reader, writer = await self.protocol._quic.create_stream() | ||
return HTTPStreamer(reader, writer) | ||
|
||
async def open_uni_stream(self) -> HTTPStreamer: | ||
reader, writer = await self.protocol._quic.create_unidirectional_stream() | ||
return HTTPStreamer(reader, writer) | ||
|
||
def local_addr(self): | ||
return self.protocol._quic._local_endpoint | ||
|
||
def remote_addr(self): | ||
return self.protocol._quic._peer_endpoint | ||
|
||
def connection_state(self): | ||
return self.protocol._quic._state | ||
|
||
class Hijacker: | ||
""" | ||
Allows hijacking of the stream creating part of a QuicConnectionProtocol. | ||
""" | ||
def __init__(self, protocol: QuicConnectionProtocol): | ||
self.protocol = protocol | ||
|
||
def stream_creator(self) -> StreamCreator: | ||
return StreamCreator(self.protocol) | ||
|
||
class Body: | ||
""" | ||
The body of a HTTP Request or Response. | ||
""" | ||
def __init__(self, stream: HTTPStreamer): | ||
self.stream = stream | ||
self.was_hijacked = False | ||
|
||
async def read(self, size) -> bytes: | ||
return await self.stream.read(size) | ||
|
||
def http_stream(self) -> HTTPStreamer: | ||
self.was_hijacked = True | ||
return self.stream | ||
|
||
async def close(self): | ||
self.stream.close() | ||
|
||
# Example usage | ||
# async def main(): | ||
# # Example usage - this will vary depending on how you establish your QUIC connection. | ||
# # Replace with actual connection and protocol setup. | ||
# protocol = QuicConnectionProtocol(QuicConnection(...)) | ||
# stream_creator = StreamCreator(protocol) | ||
# http_stream = await stream_creator.open_stream() | ||
# # Now you can read from or write to http_stream as needed. | ||
|
||
# if __name__ == "__main__": | ||
# asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters