implement push tokens

This commit is contained in:
JJTech0130 2023-04-05 20:46:49 -04:00
parent b7df7a3b1e
commit efa7fa6d99
No known key found for this signature in database
GPG key ID: 23C92EBCCF8F93D6
2 changed files with 27 additions and 14 deletions

3
.gitignore vendored
View file

@ -1,6 +1,7 @@
# APNS keys # APNS
push.crt push.crt
push.key push.key
token
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/

36
apns.py
View file

@ -84,18 +84,30 @@ class Payload:
if __name__ == "__main__": if __name__ == "__main__":
import courier import courier
import base64
sock = courier.connect() sock = courier.connect()
# Try and read the token from the file
try:
with open("token", "r") as f:
r = f.read()
if r == "":
raise FileNotFoundError
payload = Payload(7, Fields({1: base64.b64decode(r), 2: 0x01.to_bytes()}))
except FileNotFoundError:
payload = Payload(7, Fields({2: 0x01.to_bytes()})) payload = Payload(7, Fields({2: 0x01.to_bytes()}))
# Send the connect request (with or without the token)
sock.write(payload.to_bytes()) sock.write(payload.to_bytes())
print("recieved: ", Payload.from_stream(sock))
print("recieved: ", Payload.from_stream(sock)) # Read the response
sock.close() resp = Payload.from_stream(sock)
# with socket.create_connection((COURIER_HOST, COURIER_PORT)) as sock: # Check if the response is valid
# with context.wrap_socket(sock, server_hostname=COURIER_HOST) as ssock: if resp.command != 8 or resp.fields.fields[1] != 0x00.to_bytes():
# payload = Payload(7, Fields({2: 0x01.to_bytes()})) raise Exception("Failed to connect")
# #print(payload)
# #print(payload.to_bytes()) # If there's a new token, save it
# #print(Payload.from_bytes(payload.to_bytes())) if 3 in resp.fields.fields:
# ssock.write(payload.to_bytes()) with open("token", "wb") as f:
# print("recieved: ", Payload.from_stream(ssock)) f.write(base64.b64encode(resp.fields.fields[3]))
# print("recieved: ", Payload.from_stream(ssock))