mirror of
https://github.com/Sneed-Group/pypush-plus-plus
synced 2024-12-24 03:42:43 -06:00
working demo!
This commit is contained in:
parent
1bd21cbbb7
commit
949a34f73f
1 changed files with 102 additions and 71 deletions
105
demo.py
105
demo.py
|
@ -6,56 +6,67 @@ import json
|
||||||
# Open config
|
# Open config
|
||||||
try:
|
try:
|
||||||
with open("config.json", "r") as f:
|
with open("config.json", "r") as f:
|
||||||
config = json.load(f)
|
CONFIG = json.load(f)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
config = {}
|
CONFIG = {}
|
||||||
|
|
||||||
|
def refresh_token():
|
||||||
# If no username is set, prompt for it
|
# If no username is set, prompt for it
|
||||||
if "username" not in config:
|
if "username" not in CONFIG:
|
||||||
config["username"] = input("Enter iCloud username: ")
|
CONFIG["username"] = input("Enter iCloud username: ")
|
||||||
# If no password is set, prompt for it
|
# If no password is set, prompt for it
|
||||||
if "password" not in config:
|
if "password" not in CONFIG:
|
||||||
config["password"] = getpass.getpass("Enter iCloud password: ")
|
CONFIG["password"] = getpass.getpass("Enter iCloud password: ")
|
||||||
# If grandslam authentication is not set, prompt for it
|
# If grandslam authentication is not set, prompt for it
|
||||||
if "use_gsa" not in config:
|
if "use_gsa" not in CONFIG:
|
||||||
config["use_gsa"] = input("Use grandslam authentication? [y/N] ").lower() == "y"
|
CONFIG["use_gsa"] = input("Use grandslam authentication? [y/N] ").lower() == "y"
|
||||||
|
|
||||||
def factor_gen():
|
def factor_gen():
|
||||||
return input("Enter iCloud 2FA code: ")
|
return input("Enter iCloud 2FA code: ")
|
||||||
|
|
||||||
user_id, token = ids._get_auth_token(
|
CONFIG["user_id"], CONFIG["token"] = ids._get_auth_token(
|
||||||
config["username"], config["password"], config["use_gsa"], factor_gen=factor_gen
|
CONFIG["username"], CONFIG["password"], CONFIG["use_gsa"], factor_gen=factor_gen
|
||||||
)
|
)
|
||||||
|
|
||||||
config["user_id"] = user_id
|
def refresh_cert():
|
||||||
config["token"] = token
|
CONFIG["key"], CONFIG["auth_cert"] = ids._get_auth_cert(
|
||||||
|
CONFIG["user_id"], CONFIG["token"]
|
||||||
|
)
|
||||||
|
|
||||||
key, cert = ids._get_auth_cert(user_id, token)
|
def create_connection():
|
||||||
|
conn = apns.APNSConnection()
|
||||||
|
token = conn.connect()
|
||||||
|
conn.filter(['com.apple.madrid'])
|
||||||
|
CONFIG['push'] = {
|
||||||
|
'token': b64encode(token).decode(),
|
||||||
|
'cert': conn.cert,
|
||||||
|
'key': conn.private_key
|
||||||
|
}
|
||||||
|
return conn
|
||||||
|
|
||||||
config["key"] = key
|
def restore_connection():
|
||||||
config["cert"] = cert
|
conn = apns.APNSConnection(CONFIG['push']['key'], CONFIG['push']['cert'])
|
||||||
|
conn.connect(True, b64decode(CONFIG['push']['token']))
|
||||||
conn1 = apns.APNSConnection()
|
conn.filter(['com.apple.madrid'])
|
||||||
conn1.connect()
|
return conn
|
||||||
|
|
||||||
conn1.filter(["com.apple.madrid"])
|
|
||||||
|
|
||||||
|
def refresh_madrid_cert():
|
||||||
info = {
|
info = {
|
||||||
"uri": "mailto:" + config["username"],
|
"uri": "mailto:" + CONFIG["username"],
|
||||||
"user_id": user_id,
|
"user_id": CONFIG['user_id'],
|
||||||
}
|
}
|
||||||
|
|
||||||
resp = None
|
resp = None
|
||||||
try:
|
try:
|
||||||
if "validation_data" in config:
|
if "validation_data" in CONFIG:
|
||||||
resp = ids._register_request(
|
resp = ids._register_request(
|
||||||
b64encode(conn1.token),
|
CONFIG['push']['token'],
|
||||||
info,
|
info,
|
||||||
cert,
|
CONFIG['auth_cert'],
|
||||||
key,
|
CONFIG['key'],
|
||||||
conn1.cert,
|
CONFIG['push']['cert'],
|
||||||
conn1.private_key,
|
CONFIG['push']['key'],
|
||||||
config["validation_data"],
|
CONFIG["validation_data"],
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
@ -71,15 +82,15 @@ if resp is None:
|
||||||
.replace(" ", "")
|
.replace(" ", "")
|
||||||
)
|
)
|
||||||
resp = ids._register_request(
|
resp = ids._register_request(
|
||||||
b64encode(conn1.token),
|
CONFIG['push']['token'],
|
||||||
info,
|
info,
|
||||||
cert,
|
CONFIG['auth_cert'],
|
||||||
key,
|
CONFIG['key'],
|
||||||
conn1.cert,
|
CONFIG['push']['cert'],
|
||||||
conn1.private_key,
|
CONFIG['push']['key'],
|
||||||
validation_data,
|
validation_data,
|
||||||
)
|
)
|
||||||
config["validation_data"] = validation_data
|
CONFIG["validation_data"] = validation_data
|
||||||
|
|
||||||
madrid_cert = x509.load_der_x509_certificate(
|
madrid_cert = x509.load_der_x509_certificate(
|
||||||
resp["services"][0]["users"][0]["cert"]
|
resp["services"][0]["users"][0]["cert"]
|
||||||
|
@ -88,8 +99,28 @@ madrid_cert = (
|
||||||
madrid_cert.public_bytes(serialization.Encoding.PEM).decode("utf-8").strip()
|
madrid_cert.public_bytes(serialization.Encoding.PEM).decode("utf-8").strip()
|
||||||
)
|
)
|
||||||
|
|
||||||
config["madrid_cert"] = madrid_cert
|
CONFIG["madrid_cert"] = madrid_cert
|
||||||
|
|
||||||
|
|
||||||
|
if not 'madrid_cert' in CONFIG:
|
||||||
|
print("No madrid cert")
|
||||||
|
if not 'key' in CONFIG:
|
||||||
|
print("No auth cert")
|
||||||
|
if not 'token' in CONFIG:
|
||||||
|
print("No auth token")
|
||||||
|
refresh_token()
|
||||||
|
refresh_cert()
|
||||||
|
if not 'push' in CONFIG:
|
||||||
|
print("No push conn")
|
||||||
|
conn = create_connection()
|
||||||
|
else:
|
||||||
|
print("restoring push conn")
|
||||||
|
conn = restore_connection()
|
||||||
|
refresh_madrid_cert()
|
||||||
|
print("Got new madrid cert")
|
||||||
|
|
||||||
|
print("Done")
|
||||||
|
|
||||||
# Save config
|
# Save config
|
||||||
with open("config.json", "w") as f:
|
with open("config.json", "w") as f:
|
||||||
json.dump(config, f, indent=4)
|
json.dump(CONFIG, f, indent=4)
|
Loading…
Reference in a new issue