Skip to content

Commit

Permalink
Added more output
Browse files Browse the repository at this point in the history
  • Loading branch information
dchristl committed Dec 23, 2023
1 parent 4880934 commit ccf88dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
32 changes: 14 additions & 18 deletions webserver/mh_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import logging
logger = logging.getLogger()


class ServerHandler(BaseHTTPRequestHandler):
def do_OPTIONS(self):
self.send_response(200, "ok")
Expand All @@ -42,29 +43,28 @@ def do_POST(self):

post_body = self.rfile.read(content_len)

print('Getting with post: ' + str(post_body))
logger.debug('Getting with post: ' + str(post_body))
body = json.loads(post_body)
if "days" in body:
days = body['days']
else:
days = 7
print('Querying for ' + str(days) + ' days')
logger.debug('Querying for ' + str(days) + ' days')
unixEpoch = int(datetime.now().strftime('%s'))
startdate = unixEpoch - (60 * 60 * 24 * days)

dt_object = datetime.fromtimestamp(startdate)

print(dt_object.strftime('%Y-%m-%d %H:%M:%S'))
# Date is always one, because it has no effect
# Date is always 1, because it has no effect
data = {"search": [
{"startDate": 1, "ids": list(body['ids'])}]}

try:
r = requests.post("https://gateway.icloud.com/acsnservice/fetch", auth=getAuth(regenerate=False, second_factor='sms'),
headers=pypush_gsa_icloud.generate_anisette_headers(),
json=data)
print(r.status_code)

logger.debug('Return from fetch service:')
logger.debug(r)
result = json.loads(r.content.decode())
results = result['results']

Expand All @@ -82,10 +82,6 @@ def do_POST(self):
newResults[timestamp] = entry

sorted_map = OrderedDict(sorted(newResults.items(), reverse=True))
for key, value in sorted_map.items():
dt_object = datetime.fromtimestamp(key)
human_readable_format = dt_object.strftime('%Y-%m-%d %H:%M:%S')
print(f"Key: {human_readable_format}")

result["results"] = list(sorted_map.values())
self.send_response(200)
Expand All @@ -98,8 +94,8 @@ def do_POST(self):
self.wfile.write(responseBody.encode())

except requests.exceptions.ConnectTimeout:
print("Timeout to " + anisette +
", is your anisette running and accepting Connections?")
logger.error("Timeout to " + anisette +
", is your anisette running and accepting Connections?")
self.send_response(504)

def getCurrentTimes(self):
Expand All @@ -115,7 +111,7 @@ def getAuth(regenerate=False, second_factor='sms'):
else:
mobileme = pypush_gsa_icloud.icloud_login_mobileme(
second_factor=second_factor)
print('Mobileme' + mobileme)
logger.debug('Mobileme result: ' + mobileme)
j = {'dsid': mobileme['dsid'], 'searchPartyToken': mobileme['delegates']
['com.apple.mobileme']['service-data']['tokens']['searchPartyToken']}
with open(config.getConfigFile(), "w") as f:
Expand All @@ -136,23 +132,23 @@ def getAuth(regenerate=False, second_factor='sms'):
Handler = ServerHandler
httpd = HTTPServer(('localhost', config.PORT), Handler)
if os.path.isfile(config.getCertFile()):
print("Certificate file " + config.getCertFile() + " exists, so using SSL")
logger.info("Certificate file " + config.getCertFile() + " exists, so using SSL")
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile=config.getCertFile(
), keyfile=config.getKeyFile() if os.path.isfile(config.getKeyFile()) else None)

httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)

print("serving at port " + str(config.PORT) + " over HTTPS")
logger.info("serving at port " + str(config.PORT) + " over HTTPS")
else:
print("Certificate file " + config.getCertFile() +
logger.info("Certificate file " + config.getCertFile() +
" not found, so not using SSL")
print("serving at port " + str(config.PORT) + " over HTTP")
logger.info("serving at port " + str(config.PORT) + " over HTTP")

try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
finally:
httpd.server_close()
print('Server stopped')
logger.info('Server stopped')
2 changes: 1 addition & 1 deletion webserver/register/pypush_gsa_icloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def generate_cpd():

def generate_anisette_headers():

logger.info(
logger.debug(
f'Querying {ANISETTE_URL} for an anisette server')
h = json.loads(requests.get(ANISETTE_URL, timeout=5).text)
a = {"X-Apple-I-MD": h["X-Apple-I-MD"],
Expand Down

0 comments on commit ccf88dc

Please sign in to comment.