pypush-plus-plus/courier.py

33 lines
950 B
Python
Raw Normal View History

2023-04-05 18:52:14 -05:00
import albert
import tlslite
import socket
COURIER_HOST = "10-courier.push.apple.com"
COURIER_PORT = 5223
#ALPN = [b"apns-security-v2"]
ALPN = None
2023-04-05 20:01:07 -05:00
def connect(private_key=None, cert=None):
# If we don't have a private key or certificate, generate one
if private_key is None or cert is None:
2023-04-05 18:52:14 -05:00
private_key, cert = albert.generate_push_cert()
# 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)
# Handshake with the server
sock.handshakeClientCert(cert, private_key, alpn=ALPN)
2023-04-05 20:01:07 -05:00
return sock, private_key, cert
2023-04-05 18:52:14 -05:00
if __name__ == "__main__":
sock = connect()
sock.write(b"Hello World!")
print(sock.read())
sock.close()