diff --git a/apns.py b/apns.py index cdafc1a..3d4b137 100644 --- a/apns.py +++ b/apns.py @@ -165,17 +165,24 @@ class APNSConnection: self._nursery = nursery self.credentials = credentials - async def connect(self): - """Connects to the APNs server and starts the keep alive and queue filler tasks""" + async def _connect_socket(self): sock = await trio.open_tcp_stream(COURIER_HOST, COURIER_PORT) - context = ssl.SSLContext(ssl.PROTOCOL_TLS) + context = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH) context.set_alpn_protocols(["apns-security-v3"]) + + # Turn off certificate verification, for the proxy + context.check_hostname = False + context.verify_mode = ssl.CERT_NONE self.sock = trio.SSLStream(sock, context, server_hostname=COURIER_HOST) await self.sock.do_handshake() + async def connect(self): + """Connects to the APNs server and starts the keep alive and queue filler tasks""" + await self._connect_socket() + logger.info(f"Connected to APNs ({COURIER_HOST})") if self.credentials.cert == "" or self.credentials.private_key == "": diff --git a/development/proxy/proxy_async.py b/development/proxy/proxy_async.py new file mode 100644 index 0000000..b70c34d --- /dev/null +++ b/development/proxy/proxy_async.py @@ -0,0 +1,57 @@ +import sys +sys.path.append("../") +sys.path.append("../../") + +import apns +import trio +import ssl + +import logging +from rich.logging import RichHandler + +logging.basicConfig( + level=logging.NOTSET, + format="%(message)s", + datefmt="[%X]", + handlers=[RichHandler()], +) + +async def main(): + apns.COURIER_HOST = "windows.courier.push.apple.com" + + context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH) + context.set_alpn_protocols(["apns-security-v3"]) + # Set the certificate and private key + context.load_cert_chain("push_certificate_chain.pem", "push_key.pem") + + await trio.serve_ssl_over_tcp(handle_proxy, 5223, context) + +async def handle_proxy(stream: trio.SocketStream): + # Create an APNS connection + # Create 2 tasks, one to read from the client and write to the server, and one to read from the server and write to the client + try: + async with trio.open_nursery() as nursery: + apns_server = apns.APNSConnection(nursery) + await apns_server._connect_socket() + server = apns_server.sock + + nursery.start_soon(read_from_client, stream, server) + nursery.start_soon(read_from_server, stream, server) + except Exception as e: + logging.error(e) + +async def read_from_client(client: trio.SocketStream, server: trio.SocketStream): + while True: + payload = await apns.APNSPayload.read_from_stream(client) + logging.debug(payload) + await payload.write_to_stream(server) + + +async def read_from_server(client: trio.SocketStream, server: trio.SocketStream): + while True: + payload = await apns.APNSPayload.read_from_stream(server) + logging.debug(payload) + await payload.write_to_stream(client) + +if __name__ == "__main__": + trio.run(main) \ No newline at end of file