pypush-plus-plus/ids/query.py

94 lines
6.8 KiB
Python
Raw Normal View History

2023-05-09 19:36:33 +00:00
import gzip
import plistlib
import random
from base64 import b64encode
import apns
import bags
2023-05-09 21:03:27 +00:00
2023-05-09 22:01:32 +00:00
from ._helpers import USER_AGENT, KeyPair
from . import signing
2023-05-09 19:36:33 +00:00
def _send_request(
conn: apns.APNSConnection,
bag_key: str,
topic: str,
body: bytes,
keypair: KeyPair,
2023-05-09 22:01:32 +00:00
self_uri: str,
2023-05-09 19:36:33 +00:00
) -> bytes:
body = gzip.compress(body, mtime=0)
push_token = b64encode(conn.token).decode()
# Sign the request
# signature, nonce = _sign_payload(keypair.key, bag_key, "", push_token, body)
headers = {
2023-05-09 22:01:32 +00:00
"x-id-self-uri": self_uri,
2023-05-09 19:36:33 +00:00
"User-Agent": USER_AGENT,
"x-protocol-version": "1630",
}
2023-05-09 22:01:32 +00:00
print(headers)
2023-05-09 19:36:33 +00:00
signing.add_id_signature(headers, body, bag_key, keypair, push_token)
# print(headers)
msg_id = random.randbytes(16)
req = {
"cT": "application/x-apple-plist",
"U": msg_id,
"c": 96,
"ua": USER_AGENT,
"u": bags.ids_bag()[bag_key],
"h": headers,
"v": 2,
"b": body,
}
2023-05-09 22:01:32 +00:00
print(req)
2023-05-09 19:36:33 +00:00
conn.send_message(topic, plistlib.dumps(req, fmt=plistlib.FMT_BINARY))
# resp = conn.wait_for_packet(0x0A)
def check_response(x):
if x[0] != 0x0A:
return False
resp_body = apns._get_field(x[1], 3)
if resp_body is None:
return False
resp_body = plistlib.loads(resp_body)
return resp_body["U"] == msg_id
# Lambda to check if the response is the one we want
# conn.incoming_queue.find(check_response)
payload = conn.incoming_queue.wait_pop_find(check_response)
# conn._send_ack(apns._get_field(payload[1], 4))
resp = apns._get_field(payload[1], 3)
return plistlib.loads(resp)
# Performs an IDS lookup
# conn: an active APNs connection. must be connected and have a push token. will be filtered to the IDS topic
# self: the user's email address
# keypair: a KeyPair object containing the user's private key and certificate
# topic: the IDS topic to query
# query: a list of URIs to query
2023-05-09 22:43:51 +00:00
def lookup_n(
2023-05-09 22:01:32 +00:00
conn: apns.APNSConnection, self_uri: str, id_keypair: KeyPair, topic: str, query: list[str]
2023-05-09 19:36:33 +00:00
) -> any:
conn.filter([topic])
query = {"uris": query}
2023-05-09 22:01:32 +00:00
resp = _send_request(conn, "id-query", topic, plistlib.dumps(query), id_keypair, self_uri)
2023-05-09 19:36:33 +00:00
# resp = plistlib.loads(resp)
# print(resp)
resp = gzip.decompress(resp["b"])
resp = plistlib.loads(resp)
return resp
2023-05-09 22:43:51 +00:00
def lookup(conn: apns.APNSConnection, self_uri: str, id_keypair: KeyPair, topic: str, query: list[str]) -> any:
import oldids
#id_keypair = KeyPair("-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEA6fNjLPobeiQEbeDzYResvK2oC9+MsGyog36jo1o7pm8AeIth\nSzZ7caM8ThM/37i9DGJyDsnl6yqg1SxhyW4Fm8Evq2Mm6eYh6YwzRvppQoFqXNQO\nrEgjpQAW+D31V5OvHRprwX6qDVRprNF8gtaGYYjTbQudzYwpzpCIwbUu+1IqfojF\ngzTR/bxfdTbJnlaqWbOjFF7WrSZrP18nGaVkbM3rBS7egRZH1WlG14gO31YNbNg3\ndzOz9hQJHehHfHrSyZam3h6nda8tA7LJzVpTGCo7PJMC4IVyvQf7S2N3BlMJ4cen\nemzaDIOW9b/FCuvENkY2LPuDIT1hQs3pOoWHSQIDAQABAoIBABPAmCLDwh6jnFkf\nmUTdEBlFCy9ncjQyFF83yb6gv3ELpa1HzVDhmnYLe2u3Hdk4eoOpaypa+wXKLVaa\nPu5YEvKl0q3EexRb+QiELQ8k1M7H6PBJ+iwrEhFcCtRuPMDmZ+5L3QWy+U4TTrHH\n5RyR2row6HLoPGxOlXgKhXVfZAZVbgsSG8dbbuoP+U9cCrSU5TH2yIa64Gm7XrvF\n0aEo+J6nMAzw4jUUYY/y8gCU89p5utNDpxXZNva8CO0GpkooZ0nDAOnU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