-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
iksm.py
371 lines (311 loc) · 12.2 KB
/
iksm.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# eli fessler
# clovervidia
from __future__ import print_function
from builtins import input
import requests, json, re, sys
import os, base64, hashlib, urllib
import uuid, time, random, string
from bs4 import BeautifulSoup
USE_OLD_NSOAPP_VER = False
version = "unknown"
nsoapp_version = "unknown"
nsoapp_ver_fallback = "2.10.1"
session = requests.Session()
# structure:
# log_in() -> get_session_token()
# get_cookie() -> call_imink_api() -> f
# enter_cookie()
# get_nsoapp_version()
# place config.txt in same directory as script (bundled or not)
if getattr(sys, 'frozen', False):
app_path = os.path.dirname(sys.executable)
elif __file__:
app_path = os.path.dirname(__file__)
config_path = os.path.join(app_path, "config.txt")
def get_nsoapp_version():
'''Fetches the current Nintendo Switch Online app version from f API or the Apple App Store and sets it globally.'''
if USE_OLD_NSOAPP_VER:
return nsoapp_ver_fallback
global nsoapp_version
if nsoapp_version != "unknown": # already set
return nsoapp_version
else:
try: # try to get NSO version from f API
f_conf_url = "https://api.imink.app/config"
f_conf_header = {'User-Agent': 'splatnet2statink/{}'.format(version)}
f_conf_rsp = requests.get(f_conf_url, headers=f_conf_header)
f_conf_json = json.loads(f_conf_rsp.text)
ver = f_conf_json["nso_version"]
nsoapp_version = ver
return nsoapp_version
except: # fallback to apple app store
try:
page = requests.get("https://apps.apple.com/us/app/nintendo-switch-online/id1234806557")
soup = BeautifulSoup(page.text, 'html.parser')
elt = soup.find("p", {"class": "whats-new__latest__version"})
ver = elt.get_text().replace("Version ", "").strip()
nsoapp_version = ver
return nsoapp_version
except: # error with web request
pass
return nsoapp_ver_fallback
def log_in(ver):
'''Logs in to a Nintendo Account and returns a session_token.'''
global version
version = ver
auth_state = base64.urlsafe_b64encode(os.urandom(36))
auth_code_verifier = base64.urlsafe_b64encode(os.urandom(32))
auth_cv_hash = hashlib.sha256()
auth_cv_hash.update(auth_code_verifier.replace(b'=', b''))
auth_code_challenge = base64.urlsafe_b64encode(auth_cv_hash.digest())
app_head = {
'Host': 'accounts.nintendo.com',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Linux; Android 14; Pixel 7a) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.230 Mobile Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8n',
'DNT': '1',
'Accept-Encoding': 'gzip,deflate,br',
}
body = {
'state': auth_state,
'redirect_uri': 'npf71b963c1b7b6d119://auth',
'client_id': '71b963c1b7b6d119',
'scope': 'openid user user.birthday user.mii user.screenName',
'response_type': 'session_token_code',
'session_token_code_challenge': auth_code_challenge.replace(b'=', b''),
'session_token_code_challenge_method': 'S256',
'theme': 'login_form'
}
print("\nMake sure you have fully read the \"Cookie generation\" section of the readme before proceeding. To manually input a cookie instead, enter \"skip\" at the prompt below.")
print("\nNavigate to this URL in your browser:")
print('https://accounts.nintendo.com/connect/1.0.0/authorize?{}'.format(urllib.parse.urlencode(body)))
print("Log in, right click the \"Select this account\" button, copy the link address, and paste it below:")
while True:
try:
use_account_url = input("")
if use_account_url == "skip":
return "skip"
session_token_code = re.search('de=(.*)&st', use_account_url).group(1)
return get_session_token(session_token_code, auth_code_verifier)
except KeyboardInterrupt:
print("\nBye!")
sys.exit(1)
except AttributeError:
print("Malformed URL. Please try again, or press Ctrl+C to exit.")
print("URL:", end=' ')
def get_session_token(session_token_code, auth_code_verifier):
'''Helper function for log_in().'''
nsoapp_version = get_nsoapp_version()
app_head = {
'User-Agent': 'OnlineLounge/' + nsoapp_version + ' NASDKAPI Android',
'Accept-Language': 'en-US',
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': '540',
'Host': 'accounts.nintendo.com',
'Connection': 'Keep-Alive',
'Accept-Encoding': 'gzip'
}
body = {
'client_id': '71b963c1b7b6d119',
'session_token_code': session_token_code,
'session_token_code_verifier': auth_code_verifier.replace(b'=', b'')
}
url = 'https://accounts.nintendo.com/connect/1.0.0/api/session_token'
r = session.post(url, headers=app_head, data=body)
try:
container = json.loads(r.text)
s_t = container["session_token"]
except json.decoder.JSONDecodeError:
print("Got non-JSON response from Nintendo (in api/session_token step). Please try again.")
sys.exit(1)
except KeyError:
print("\nThe URL has expired. Logging out & back in to your Nintendo Account and retrying may fix this.")
print("Error from Nintendo (in api/session_token step):")
print(json.dumps(container, indent=2))
sys.exit(1)
return s_t
def get_cookie(session_token, userLang, ver):
'''Returns a new cookie provided the session_token.'''
nsoapp_version = get_nsoapp_version()
global version
version = ver
# timestamp = time.time_ns() // 1000000
# guid = str(uuid.uuid4())
app_head = {
'Host': 'accounts.nintendo.com',
'Accept-Encoding': 'gzip',
'Content-Type': 'application/json; charset=utf-8',
'Accept-Language': userLang,
'Content-Length': '439',
'Accept': 'application/json',
'Connection': 'Keep-Alive',
'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 14; Pixel 7a Build/UQ1A.240105.004)'
}
body = {
'client_id': '71b963c1b7b6d119', # Splatoon 2 service
'session_token': session_token,
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer-session-token'
}
url = "https://accounts.nintendo.com/connect/1.0.0/api/token"
r = requests.post(url, headers=app_head, json=body)
id_response = json.loads(r.text)
# get user info
try:
app_head = {
'User-Agent': 'NASDKAPI; Android',
'Accept-Language': userLang,
'Accept': 'application/json',
'Authorization': 'Bearer {}'.format(id_response["access_token"]),
'Host': 'api.accounts.nintendo.com',
'Connection': 'Keep-Alive',
'Accept-Encoding': 'gzip'
}
except:
print("Not a valid authorization request. Please delete config.txt and try again.")
print("Error from Nintendo (in api/token step):")
print(json.dumps(id_response, indent=2))
sys.exit(1)
url = "https://api.accounts.nintendo.com/2.0.0/users/me"
r = requests.get(url, headers=app_head)
user_info = json.loads(r.text)
nickname = user_info["nickname"]
na_id = user_info["id"]
# get access token
app_head = {
'Host': 'api-lp1.znc.srv.nintendo.net',
'Accept-Language': userLang,
'User-Agent': 'com.nintendo.znca/' + nsoapp_version + ' (Android/14)',
'Accept': 'application/json',
'X-ProductVersion': nsoapp_version,
'Content-Type': 'application/json; charset=utf-8',
'Connection': 'Keep-Alive',
'Authorization': 'Bearer',
# 'Content-Length': '1036',
'X-Platform': 'Android',
'Accept-Encoding': 'gzip'
}
body = {}
try:
idToken = id_response["id_token"]
f, uuid, timestamp = call_imink_api(idToken, 1, na_id)
parameter = {
'f': f,
'naIdToken': idToken,
'timestamp': timestamp,
'requestId': uuid,
'naCountry': user_info["country"],
'naBirthday': user_info["birthday"],
'language': user_info["language"]
}
except SystemExit:
sys.exit(1)
except:
print("Error(s) from Nintendo:")
print(json.dumps(id_response, indent=2))
print(json.dumps(user_info, indent=2))
sys.exit(1)
body["parameter"] = parameter
url = "https://api-lp1.znc.srv.nintendo.net/v3/Account/Login"
r = requests.post(url, headers=app_head, json=body)
splatoon_token = json.loads(r.text)
try:
idToken = splatoon_token["result"]["webApiServerCredential"]["accessToken"]
coral_user_id = splatoon_token["result"]["user"]["id"]
f, uuid, timestamp = call_imink_api(idToken, 2, na_id, coral_user_id)
except:
print("Error from Nintendo (in Account/Login step):")
print(json.dumps(splatoon_token, indent=2))
sys.exit(1)
# get splatoon access token
try:
app_head = {
'Host': 'api-lp1.znc.srv.nintendo.net',
'User-Agent': 'com.nintendo.znca/' + nsoapp_version + ' (Android/14)',
'Accept': 'application/json',
'X-ProductVersion': nsoapp_version,
'Content-Type': 'application/json; charset=utf-8',
'Connection': 'Keep-Alive',
'Authorization': 'Bearer {}'.format(splatoon_token["result"]["webApiServerCredential"]["accessToken"]),
'Content-Length': '37',
'X-Platform': 'Android',
'Accept-Encoding': 'gzip'
}
except:
print("Error from Nintendo (in Account/Login step):")
print(json.dumps(splatoon_token, indent=2))
sys.exit(1)
body = {}
parameter = {
'id': 5741031244955648,
'f': f,
'registrationToken': idToken,
'timestamp': timestamp,
'requestId': uuid,
}
body["parameter"] = parameter
url = "https://api-lp1.znc.srv.nintendo.net/v2/Game/GetWebServiceToken"
r = requests.post(url, headers=app_head, json=body)
splatoon_access_token = json.loads(r.text)
# get cookie
try:
app_head = {
'Host': 'app.splatoon2.nintendo.net',
'X-IsAppAnalyticsOptedIn': 'false',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip,deflate',
'X-GameWebToken': splatoon_access_token["result"]["accessToken"],
'Accept-Language': userLang,
'X-IsAnalyticsOptedIn': 'false',
'Connection': 'keep-alive',
'DNT': '1',
'User-Agent': 'Mozilla/5.0 (Linux; Android 14; Pixel 7a Build/UQ1A.240105.004; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/120.0.6099.230 Mobile Safari/537.36',
'X-Requested-With': 'com.nintendo.znca'
}
except:
print("Error from Nintendo (in Game/GetWebServiceToken step):")
print(json.dumps(splatoon_access_token, indent=2))
sys.exit(1)
url = "https://app.splatoon2.nintendo.net/?lang={}".format(userLang)
r = requests.get(url, headers=app_head)
return nickname, r.cookies["iksm_session"]
def call_imink_api(id_token, step, na_id, coral_user_id=None):
'''Passes naIdToken & user ID to the imink API and fetches the response (f token, UUID, timestamp).'''
try:
nsoapp_version = get_nsoapp_version()
api_head = {
'User-Agent': 'splatnet2statink/{}'.format(version),
'Content-Type': 'application/json; charset=utf-8',
'X-znca-Platform': 'Android',
'X-znca-Version': nsoapp_version
}
api_body = {
'token': id_token,
'hash_method': step,
'na_id': na_id
}
if step == 2 and coral_user_id is not None:
api_body["coral_user_id"] = coral_user_id
api_response = requests.post("https://api.imink.app/f", data=json.dumps(api_body), headers=api_head)
resp = json.loads(api_response.text)
f = resp["f"]
uuid = resp["request_id"]
timestamp = resp["timestamp"]
return f, uuid, timestamp
except:
try: # if api_response never gets set
if api_response.text:
print(u"Error from the imink API:\n{}".format(json.dumps(json.loads(api_response.text), indent=2, ensure_ascii=False)))
else:
print("Error from the imink API: Error {}.".format(api_response.status_code))
except:
pass
sys.exit(1)
def enter_cookie():
'''Prompts the user to enter their iksm_session cookie'''
new_cookie = input("Go to the page below to find instructions to obtain your iksm_session cookie:\nhttps://github.com/frozenpandaman/splatnet2statink/wiki/mitmproxy-instructions\nEnter it here: ")
while len(new_cookie) != 40:
new_cookie = input("Cookie is invalid. Please enter it again.\nCookie: ")
return new_cookie