pypush-plus-plus/courier.py

30 lines
831 B
Python
Raw Normal View History

2023-04-05 23:52:14 +00:00
import socket
2023-04-11 16:23:04 +00:00
import tlslite
COURIER_HOST = "windows.courier.push.apple.com" # TODO: Get this from config
2023-04-05 23:52:14 +00:00
COURIER_PORT = 5223
2023-04-06 16:49:27 +00:00
ALPN = [b"apns-security-v2"]
2023-04-05 23:52:14 +00:00
2023-04-11 16:23:04 +00:00
# Connect to the courier server
def connect(private_key: str, cert: str) -> tlslite.TLSConnection:
2023-04-05 23:52:14 +00:00
# Connect to the courier server
sock = socket.create_connection((COURIER_HOST, COURIER_PORT))
# Wrap the socket in TLS
sock = tlslite.TLSConnection(sock)
# Parse the certificate and private key
cert = tlslite.X509CertChain([tlslite.X509().parse(cert)])
private_key = tlslite.parsePEMKey(private_key, private=True)
2023-04-05 23:52:14 +00:00
# Handshake with the server
sock.handshakeClientCert(cert, private_key, alpn=ALPN)
2023-04-05 23:52:14 +00:00
return sock
2023-04-05 23:52:14 +00:00
2023-04-11 16:23:04 +00:00
2023-04-05 23:52:14 +00:00
if __name__ == "__main__":
sock = connect()
sock.write(b"Hello World!")
print(sock.read())
sock.close()