compatibility with older python

This commit is contained in:
JJTech0130 2023-05-02 17:48:47 -04:00
parent 1eb41fd70a
commit 98c861c625
No known key found for this signature in database
GPG key ID: 23C92EBCCF8F93D6
2 changed files with 15 additions and 15 deletions

14
apns.py
View file

@ -83,11 +83,11 @@ class APNSConnection:
if token is None: if token is None:
payload = _serialize_payload( payload = _serialize_payload(
7, [(2, 0x01.to_bytes()), (5, flags.to_bytes(4))] 7, [(2, 0x01.to_bytes(1, 'big')), (5, flags.to_bytes(4, 'big'))]
) )
else: else:
payload = _serialize_payload( payload = _serialize_payload(
7, [(1, token), (2, 0x01.to_bytes()), (5, flags.to_bytes(4))] 7, [(1, token), (2, 0x01.to_bytes(1, 'big')), (5, flags.to_bytes(4, 'big'))]
) )
self.sock.write(payload) self.sock.write(payload)
@ -97,7 +97,7 @@ class APNSConnection:
if ( if (
payload == None payload == None
or payload[0] != 8 or payload[0] != 8
or _get_field(payload[1], 1) != 0x00.to_bytes() or _get_field(payload[1], 1) != 0x00.to_bytes(1, 'big')
): ):
raise Exception("Failed to connect") raise Exception("Failed to connect")
@ -139,13 +139,13 @@ class APNSConnection:
payload = self.wait_for_packet(0x0B) payload = self.wait_for_packet(0x0B)
if payload[1][0][1] != 0x00.to_bytes(): if payload[1][0][1] != 0x00.to_bytes(1, 'big'):
raise Exception("Failed to send message") raise Exception("Failed to send message")
def set_state(self, state: int): def set_state(self, state: int):
self.sock.write( self.sock.write(
_serialize_payload( _serialize_payload(
0x14, [(1, state.to_bytes(1)), (2, 0x7FFFFFFF.to_bytes(4))] 0x14, [(1, state.to_bytes(1, 'big')), (2, 0x7FFFFFFF.to_bytes(4, 'big'))]
) )
) )
@ -158,7 +158,7 @@ class APNSConnection:
def _serialize_field(id: int, value: bytes) -> bytes: def _serialize_field(id: int, value: bytes) -> bytes:
return id.to_bytes() + len(value).to_bytes(2, "big") + value return id.to_bytes(1, 'big') + len(value).to_bytes(2, "big") + value
def _serialize_payload(id: int, fields: list[(int, bytes)]) -> bytes: def _serialize_payload(id: int, fields: list[(int, bytes)]) -> bytes:
@ -168,7 +168,7 @@ def _serialize_payload(id: int, fields: list[(int, bytes)]) -> bytes:
if fid is not None: if fid is not None:
payload += _serialize_field(fid, value) payload += _serialize_field(fid, value)
return id.to_bytes() + len(payload).to_bytes(4, "big") + payload return id.to_bytes(1, 'big') + len(payload).to_bytes(4, "big") + payload
def _deserialize_field(stream: bytes) -> tuple[int, bytes]: def _deserialize_field(stream: bytes) -> tuple[int, bytes]:

16
ids.py
View file

@ -1,7 +1,7 @@
import plistlib import plistlib
import random import random
import uuid import uuid
import zlib import gzip
from base64 import b64decode, b64encode from base64 import b64decode, b64encode
from datetime import datetime from datetime import datetime
@ -47,13 +47,13 @@ def _create_payload(
return ( return (
nonce nonce
+ len(bag_key).to_bytes(4) + len(bag_key).to_bytes(4, 'big')
+ bag_key.encode() + bag_key.encode()
+ len(query_string).to_bytes(4) + len(query_string).to_bytes(4, 'big')
+ query_string.encode() + query_string.encode()
+ len(payload).to_bytes(4) + len(payload).to_bytes(4, 'big')
+ payload + payload
+ len(push_token).to_bytes(4) + len(push_token).to_bytes(4, 'big')
+ push_token, + push_token,
nonce, nonce,
) )
@ -79,7 +79,7 @@ def sign_payload(
# global_key, global_cert = load_keys() # global_key, global_cert = load_keys()
def _send_request(conn: apns.APNSConnection, bag_key: str, topic: str, body: bytes, keypair: KeyPair, username: str) -> bytes: def _send_request(conn: apns.APNSConnection, bag_key: str, topic: str, body: bytes, keypair: KeyPair, username: str) -> bytes:
body = zlib.compress(body, wbits=16 + zlib.MAX_WBITS) body = gzip.compress(body, mtime=0)
push_token = b64encode(conn.token).decode() push_token = b64encode(conn.token).decode()
@ -133,7 +133,7 @@ def lookup(conn: apns.APNSConnection, self: str, keypair: KeyPair, topic: str, q
query = {"uris": query} query = {"uris": query}
resp = _send_request(conn, "id-query", topic, plistlib.dumps(query), keypair, self) resp = _send_request(conn, "id-query", topic, plistlib.dumps(query), keypair, self)
resp = plistlib.loads(resp) resp = plistlib.loads(resp)
resp = zlib.decompress(resp["b"], 16 + zlib.MAX_WBITS) resp = gzip.decompress(resp["b"])
resp = plistlib.loads(resp) resp = plistlib.loads(resp)
return resp return resp
@ -269,7 +269,7 @@ def _register_request(
} }
body = plistlib.dumps(body) body = plistlib.dumps(body)
body = zlib.compress(body, wbits=16 + zlib.MAX_WBITS) body = gzip.compress(body, mtime=0)
push_sig, push_nonce = sign_payload(push_key, "id-register", "", push_token, body) push_sig, push_nonce = sign_payload(push_key, "id-register", "", push_token, body)
auth_sig, auth_nonce = sign_payload(auth_key, "id-register", "", push_token, body) auth_sig, auth_nonce = sign_payload(auth_key, "id-register", "", push_token, body)