begin rewriting proxy

This commit is contained in:
JJTech0130 2023-08-18 00:18:30 -04:00
parent d908e9cc37
commit 0ad9531031
No known key found for this signature in database
GPG key ID: 23C92EBCCF8F93D6
2 changed files with 67 additions and 3 deletions

13
apns.py
View file

@ -165,17 +165,24 @@ class APNSConnection:
self._nursery = nursery self._nursery = nursery
self.credentials = credentials self.credentials = credentials
async def connect(self): async def _connect_socket(self):
"""Connects to the APNs server and starts the keep alive and queue filler tasks"""
sock = await trio.open_tcp_stream(COURIER_HOST, COURIER_PORT) 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"]) 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) self.sock = trio.SSLStream(sock, context, server_hostname=COURIER_HOST)
await self.sock.do_handshake() 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})") logger.info(f"Connected to APNs ({COURIER_HOST})")
if self.credentials.cert == "" or self.credentials.private_key == "": if self.credentials.cert == "" or self.credentials.private_key == "":

View file

@ -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)