forked from smpanaro/icloud-tabs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tabs.py
executable file
·202 lines (173 loc) · 6.28 KB
/
tabs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
# Format of request:
# {
# "apns-token": "<data>",
# "apps": [
# {
# "bundle-id": "com.apple.Safari",
# "keys": [
# {
# "data": "<data>",
# "name": ""
# }
# ],
# "kvstore-id": "com.apple.Safari.SyncedTabs",
# "registry-version": ""
# }
# ],
# "service-id":"iOS"
# }
# Format of "keys" <data> blob:
# {
# "DeviceName": "",
# "LastModified": "YYYY-MM-DDTHH:MM:SSZ",
# "Tabs": [
# {
# "Title": "",
# "URL": ""
# }
# ]
# }
import urllib2
import StringIO
import gzip
import plistlib
import biplist
import base64
import time, datetime
import uuid
import pprint
import os
from update_config import *
# Request Header Constants
ICLOUD_SERVICE = "keyvalueservice.icloud.com"
USER_AGENT = "SyncedDefaults/91.30 (Mac OS X 10.9.1 (13B42))" #"SyncedDefaults/43.27 (Mac OS X 10.8.4 (12E55))"
X_MME_CLIENT_INFO = "<Macmini3,1> <Mac OS X;10.9.1;13B42> <com.apple.SyncedDefaults/91.30>" #"<MacBookAir4,2> <Mac OS X;10.8.4;12E55> <com.apple.SyncedDefaults/43.27>"
X_APPLE_REQUEST_UUID = str(uuid.uuid4()).upper()
X_APPLE_SCHEDULER_ID = "com.apple.syncedpreferences.browser"
#what a pile, replace with http://docs.python-requests.org/en/latest/ someday
class iCloudNodeRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_330(self, req, fp, code, msg, headers):
'''330 received when apple wants us to redirect to a specific server node for a service after requesting something from its top-level CNAME'''
mme_server = headers['X-Apple-MMe-Host'] #icloud server node: `p<00-NN>-exampleservice.icloud.com`
newhdr = req.headers
newhdr['Host'] = mme_server
newurl = "%s://%s%s" % ( req.get_type(), mme_server, req.get_selector() )
newreq = urllib2.Request(newurl, req.data, newhdr)
return urllib2.urlopen(newreq)
icl_urlopen = urllib2.build_opener(iCloudNodeRedirectHandler)
def generate_plist(with_payload, tabs, uuid, name, registry_version="need-something-here"):
b = {
"DeviceName": name,
"LastModified": datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),
"Tabs": tabs
}
# Write b into a binary plist and base64 encode it.
out = StringIO.StringIO()
biplist.writePlist(b, out)
#plistlib.dump(b, out, fmt=plistlib.FMT_BINARY) #py3.4
# There is a required 12 byte header here. Don't know what it's supposed to contain.
b_encoded = "".join(map(chr, [1, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0])) + out.getvalue()
#codesign --display --entitlements - /Applications/Safari.app
#https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/iCloudFundametals.html
#http://www.undsoversum.de/2012/09/11/about-apns-tokens-and-duplicate-udids/
p = {
#"apns-token": plistlib.Data(APNS_TOKEN),
"apps": [
{
"bundle-id": "com.apple.Safari",
"keys": [
{
"data": plistlib.Data(b_encoded), #bytes(b_encoded) py3.4
"name": uuid # a unique id for your device
}
],
"kvstore-id": "com.apple.Safari.SyncedTabs",
"registry-version": registry_version, # no idea
}
],
"service-id":"iOS"
}
if with_payload == False:
p["apps"][0].pop("keys")
# Write p into a regular plist and return its string value.
out = StringIO.StringIO()
plistlib.writePlist(p, out)
return out.getvalue()
def dogzip(s):
out = StringIO.StringIO()
f = gzip.GzipFile(fileobj=out, mode='w')
f.write(s)
f.close()
return out.getvalue()
def ungzip(s):
data = StringIO.StringIO(s)
gzipper = gzip.GzipFile(fileobj=data)
html = gzipper.read()
return html
def make_request(body):
URL = "https://%s/sync" % ICLOUD_SERVICE
zippedbody = dogzip(body)
request = urllib2.Request(URL, headers={
"Host": ICLOUD_SERVICE,
"User-Agent" : USER_AGENT,
"Accept": "*/*",
"Content-Encoding": "gzip",
"Accept-Language": "en-us",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "text/xml", #"application/x-www-form-urlencoded",
"Authorization": AUTHORIZATION,
"Connection": "keep-alive", #being turned to close
"Proxy-Connection": "keep-alive",
"X-MMe-Client-Info": X_MME_CLIENT_INFO,
"X-Apple-Request-UUID": X_APPLE_REQUEST_UUID,
"X-Apple-Scheduler-ID": X_APPLE_SCHEDULER_ID,
"Content-Length": len(zippedbody)
},
data=zippedbody)
try:
u = icl_urlopen.open(request)
except urllib2.HTTPError, error:
print 'E: POST %s => %s: %s' % (error.url, error.code, error.reason)
print error.headers
print error.read()
raise urllib2.HTTPError, error
data = u.read()
return ungzip(data)
def get_tabs():
# First make an empty (without tab data) request to get the latest registry string.
payload_plist = generate_plist(False, [], DEVICE_UUID, DEVICE_NAME)
response = make_request(payload_plist)
response_plist = plistlib.readPlistFromString(response)
registry_version = response_plist["apps"][0]["registry-version"]
#print plistlib.writePlistToString(response_plist)
print registry_version
# dump current tabs
for device in response_plist["apps"][0]["keys"]:
pprint.pprint(biplist.readPlistFromString(device["data"].data[12:]))
#print plistlib.loads(device["data"][12:], fmt=plistlib.FMT_BINARY) #py3.4
return (registry_version, response_plist)
def update_tabs(tabs):
registry_version, response_plist = get_tabs()
payload_plist = generate_plist(True, tabs, DEVICE_UUID, DEVICE_NAME, registry_version=registry_version)
upd_response = make_request(payload_plist)
print plistlib.writePlistToString(plistlib.readPlistFromString(upd_response)) #dump update confirmation
if __name__ == '__main__':
TABS = [
{
"Title": "XKCD",
"URL": "http://xkcd.com/"
},
{
"Title": "Stuff on Cats",
"URL": "http://stuffonmycat.com/"
}
]
#update_tabs([]) #purge tabs. ICLOUD_SERVICE auto-purges in 2 weeks if never updated again.
#update_tabs(TABS)
get_tabs()
# TODO: optparse flags for
# conffile path
# modes:
# get (device-name) > JSON
# update (device-name) (<) JSON