pypush-plus-plus/courier.py

28 lines
822 B
Python
Raw Normal View History

2023-04-05 18:52:14 -05:00
import tlslite
import socket
2023-04-07 15:24:05 -05:00
COURIER_HOST = "1-courier.push.apple.com" # TODO: Get this from config
2023-04-05 18:52:14 -05:00
COURIER_PORT = 5223
2023-04-06 11:49:27 -05:00
ALPN = [b"apns-security-v2"]
2023-04-05 18:52:14 -05:00
# Connect to the courier server
def connect(private_key: str, cert: str) -> tlslite.TLSConnection:
2023-04-05 18:52:14 -05: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 18:52:14 -05:00
# Handshake with the server
sock.handshakeClientCert(cert, private_key, alpn=ALPN)
2023-04-05 18:52:14 -05:00
return sock
2023-04-05 18:52:14 -05:00
if __name__ == "__main__":
sock = connect()
sock.write(b"Hello World!")
print(sock.read())
sock.close()