From 7c18293986dddc52ea6273aac1794fc609053bc3 Mon Sep 17 00:00:00 2001 From: JJTech0130 Date: Fri, 7 Apr 2023 19:54:19 -0400 Subject: [PATCH] minor changes for reliablity; trying to get demo to work --- demo.py | 23 ++++++++++++++--------- proxy/proxy.py | 38 +++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/demo.py b/demo.py index 5749476..7980097 100644 --- a/demo.py +++ b/demo.py @@ -4,26 +4,31 @@ from hashlib import sha1 conn1 = apns.APNSConnection() conn1.connect() +conn1.keep_alive() +conn1.set_state(0x01) print(f"Push Token 1: {b64encode(conn1.token).decode()}") + +#while True: +# pass conn1.filter([]) conn1.connect(False) print(f"User Token 1: {b64encode(conn1.token).decode()}") -conn2 = apns.APNSConnection() -conn2.connect() -conn2.filter([]) -print(f"Push Token 2: {b64encode(conn2.token).decode()}") -conn2.connect(False) -print(f"User Token 2: {b64encode(conn2.token).decode()}") +# conn2 = apns.APNSConnection() +# conn2.connect() +# conn2.filter([]) +# print(f"Push Token 2: {b64encode(conn2.token).decode()}") +# conn2.connect(False) +# print(f"User Token 2: {b64encode(conn2.token).decode()}") conn1.filter(["com.apple.madrid"]) -conn2.filter(["com.apple.madrid"]) +# conn2.filter(["com.apple.madrid"]) conn1.send_message(b"\xe5^\xc0c\xe8\xa4\x1e\xbe\x03\x89'\xea\xd5m\x94\x05\xae\xf5\x1bqK\x1aJTH\xa4\xeb8\xb8<\xd7)", "com.apple.madrid", b'bplist00\xdd\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x13\x17\x18\x19ScdrRtPRgdQiRsPRnrQcQUQtScdvRuaRqHQvM\x08\xd6\xf3\xe6\x8d\x04\x18\x95\xfd\xea\xe9\xf50_\x10\x10tel:+16106632676\t\x12=\x12c&_\x10\x1amailto:jjgill07@icloud.com\x10\x01\x10mO\x10\x10UC>\x9f\xce\xa4N\xe0\xba\xe9\xad\x8e_h\xd7hO\x10 \xe5^\xc0c\xe8\xa4\x1e\xbe\x03\x89\'\xea\xd5m\x94\x05\xae\xf5\x1bqK\x1aJTH\xa4\xeb8\xb8<\xd7)_\x10#[macOS,13.2.1,22D68,MacBookPro18,3]O\x10!\x01\x97\xca\\"\xcaI\x82\x0c\xb66C\xa7\x89h\x91\xcd\x18Ozj"\x06u;9\x96\xebrQs|=\x10\x08\x00\x08\x00#\x00\'\x00*\x00-\x00/\x002\x005\x007\x009\x00;\x00?\x00B\x00E\x00G\x00U\x00h\x00i\x00n\x00\x8b\x00\x8d\x00\x8f\x00\xa2\x00\xc5\x00\xeb\x01\x0f\x00\x00\x00\x00\x00\x00\x02\x01\x00\x00\x00\x00\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x11') -while True: - print(conn1.expect_message()) +# while True: +# print(conn1.expect_message()) #print(conn2.expect_message()) #print(conn1.expect_message()) #print(conn2.expect_message()) diff --git a/proxy/proxy.py b/proxy/proxy.py index fa1b611..4dc2dff 100644 --- a/proxy/proxy.py +++ b/proxy/proxy.py @@ -12,8 +12,8 @@ sys.path.append('../') # APNs server to proxy traffic to APNS_HOST = "windows.courier.push.apple.com" APNS_PORT = 5223 -#ALPN = b"apns-security-v3" -ALPN = b"apns-security-v2" +ALPN = b"apns-security-v3" +#ALPN = b"apns-security-v2" #ALPN = b"apns-pack-v1" global_cnt = 0 @@ -27,6 +27,7 @@ def connect() -> tlslite.TLSConnection: #print("Handshaking with APNs") # Handshake with the server if ALPN == b"apns-security-v3": + print("Using v3") ssock.handshakeClientCert(alpn=[ALPN]) else: import albert @@ -51,8 +52,10 @@ def proxy(conn1: tlslite.TLSConnection, conn2: tlslite.TLSConnection, prefix: st while True: # Read data from the first connection data = conn1.read() + #print(prefix, "data: ", data) # If there is no data, the connection has closed if not data: + print(prefix, "Connection closed due to no data") break override = printer.pretty_print_payload(prefix, apns._deserialize_payload_from_buffer(data)) @@ -66,9 +69,12 @@ def proxy(conn1: tlslite.TLSConnection, conn2: tlslite.TLSConnection, prefix: st conn2.write(data) except OSError as e: if e.errno == 9: + print(prefix, "Connection closed due to OSError 9") pass # Probably a connection closed error - except tlslite.TLSAbruptCloseError: - pass + else: + raise e + except tlslite.TLSAbruptCloseError as e: + print(prefix, "Connection closed abruptly: ", e) print("Connection closed") # Close the connections conn1.close() @@ -103,6 +109,8 @@ def serve(): # Create a socket to listen for connections sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + # Allow the socket to be reused + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(("localhost", 5223)) sock.listen() @@ -120,14 +128,22 @@ def serve(): global key key = f.read() + conns = [] # Accept connections - while True: - # Accept a connection - conn, addr = sock.accept() - # Create a thread to handle the connection - #handle(conn) - thread = threading.Thread(target=handle, args=(conn,)) - thread.start() + try: + while True: + # Accept a connection + conn, addr = sock.accept() + conns.append(conn) + # Create a thread to handle the connection + #handle(conn) + thread = threading.Thread(target=handle, args=(conn,)) + thread.start() + except KeyboardInterrupt: + print("Keyboard interrupt, closing sockets") + for conn in conns: + conn.close() + sock.close() if __name__ == "__main__": serve()