forked from MechanicalDragon0687/friendbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfriend_functions.py
239 lines (221 loc) · 8.75 KB
/
friend_functions.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
import sys
import hashlib
import base64
import struct
import requests
import urllib3
import time
import logging
from datetime import datetime, timedelta
import threading
import queue
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from const import Const
sys.path.append("./NintendoClients")
from nintendo.nex import backend, authentication, friends, nintendo_notification
from nintendo import account
class NINTENDO_SERVER_ERROR(Const):
SUCCESS = 0
NO_ERROR = 0
PRUDP_DISCONNECTED = 1
class process_friend:
def __init__(self, fc, resync_interval=90):
self.fc = fc
self.pid = int(fc) & 0xffffffff
self.added_time = datetime.utcnow()
self.resync_time = datetime.utcnow()+timedelta(seconds=resync_interval)
self.lfcs = None
@classmethod
def from_pid(cls, pid, resync_interval=90):
return cls(PID2FC(pid),resync_interval)
class FLists(object):
def __init__(self):
self.notadded = list()
self.added = list()
self.lfcs = list()
self.remove = list()
self.newlfcs = queue.Queue()
## FC2PID(pid)
## convert a pid to the friend code
def PID2FC(principal_id):
checksum = hashlib.sha1(struct.pack('<L', principal_id)).digest()[0] >> 1
return '{:012d}'.format(principal_id | checksum << 32)
## is_valid_fc(fc)
## generates checksum from pid and compares checksum byte to generated checksum
def is_valid_fc(fc):
cur_cs = int(fc)>>32
principal_id = int(fc) & 0xffffffff
checksum = hashlib.sha1(struct.pack('<L', principal_id)).digest()[0] >> 1
return cur_cs == checksum
## FC2PID(fc)
## convert a friend code to the pid, just removes the checksum
def FC2PID(fc):
return int(fc) & 0xffffffff
def FormattedFriendCode(fc):
return fc[0:4]+"-"+fc[4:8]+"-"+fc[8:12]
class NASCInteractor(object):
@classmethod
def nintendo_base64_encode(cls, data):
return base64.b64encode(data).decode('ascii').replace('+', '.').replace('/', '-').replace('=', '*')
@classmethod
def nintendo_base64_decode(cls, s):
return base64.b64decode(s.replace('.', '+').replace('-', '/').replace('*', '='))
def __init__(self, identity):
self.ErrorCount = 0
self.client = None
self.backend = None
self.blob = {
'gameid': b'00003200',
'sdkver': b'000000',
'titleid': b'0004013000003202',
'gamecd': b'----',
'gamever': b'0011',
'mediatype': b'0',
'makercd': b'00',
'servertype': b'L1',
'fpdver': b'000C',
'unitcd': b'2', # ?
'macadr': identity['mac_address'].encode('ascii'), # 3DS' wifi MAC
'bssid': identity['bssid'].encode('ascii'), # current AP's wifi MAC
'apinfo': identity['apinfo'].encode('ascii'),
'fcdcert': open(identity['cert_filename'], 'rb').read(),
'devname': identity['name'].encode('utf16'),
'devtime': b'340605055519',
'lang': b'01',
'region': b'02',
'csnum': identity['serial'].encode('ascii'),
'uidhmac': identity['uid_hmac'].encode('ascii'), # TODO: figure out how this is calculated. b'213dc099',
'userid': str(identity['user_id']).encode('ascii'),
'action': b'LOGIN',
'ingamesn': b''
}
self.blob_enc = {}
for k in self.blob:
self.blob_enc[k] = NASCInteractor.nintendo_base64_encode(self.blob[k])
self.port = 0
self.host = ""
self.pid = str(identity['user_id'])
self.password = identity['password']
self.token = None
self.lfcs = identity['lfcs']
self.connected=False
def getNASCBits(self):
print(f"Getting a NASC token for {self.blob['csnum'].decode('ascii')}..")
resp = requests.post("https://nasc.nintendowifi.net/ac", headers={'User-Agent': 'CTR FPD/000B', 'X-GameID': '00003200'}, data=self.blob_enc, cert='ClCertA.pem', verify=False)
print (resp.text)
bits = dict(map(lambda a: a.split("="), resp.text.split("&")))
self.token = bits['token']
bits_dec = {}
for k in bits:
bits_dec[k] = NASCInteractor.nintendo_base64_decode(bits[k])
self.host, port = bits_dec['locator'].decode().split(':')
self.port = int(port)
def connect(self):
self.ErrorCount=0
if not self.client is None:
self.disconnect()
time.sleep(3)
self.getNASCBits()
self.backend = backend.BackEndClient(
friends.FriendsTitle.ACCESS_KEY,
friends.FriendsTitle.NEX_VERSION,
backend.Settings("friends.cfg")
)
self.backend.connect(self.host, self.port)
self.backend.login(
self.pid, self.password,
auth_info = None,
login_data = authentication.AccountExtraInfo(168823937, 2134704128, 0, self.token),
)
self.client = friends.Friends3DSClient(self.backend)
self.connected=True
def disconnect(self):
if not self.backend is None:
self.ErrorCount=0
self.backend.close()
self.backend = None
self.client = None
self.connected=False
def reconnect(self):
self.disconnect()
self.connect()
def IsConnected(self):
self.connected = self.PRUDUP_isConnected()
return self.connected
def PRUDUP_isConnected(self):
## client is Friends3dsClient
if not self.client is None:
## client.client is backend.secure_client (service client)
if not self.client.client is None:
## client.client.client is prudp client (which is what i see failing)
if not self.client.client.client is None:
return self.client.client.client.is_connected()
return False
def SetNotificationHandler(self,handler_function):
if self.connected == True:
self.backend.nintendo_notification_server.handler = handler_function()
return True
return False
def _ConnectionError(self):
self.ErrorCount += 1
def _ConnectionSuccess(self):
self.ErrorCount = 0
def Error(self):
return self.ErrorCount
##AddFriendPID(pid)
## Adds a friend based on pid but only if prdudp is connected
def AddFriendPID(self,pid):
if not self.PRUDUP_isConnected():
self._ConnectionError()
print("[",datetime.now(),"] Unable to add friend:",FormattedFriendCode(PID2FC(pid)))
return None
rel = self.client.add_friend_by_principal_id(self.lfcs, pid)
#TODO: HANDLE ERRORS RETURNED
print("[",datetime.now(),"] Added friend:",FormattedFriendCode(PID2FC(pid)))
return rel
## AddFriendFC(fc)
## adds a friend by friend code, converts fc to pid and then calls AddFriendPID
def AddFriendFC(self,fc):
return self.AddFriendPID(FC2PID(fc))
def RemoveFriendPID(self,pid):
if not self.PRUDUP_isConnected():
self._ConnectionError()
print("[",datetime.now(),"] Unable to remove friend:",FormattedFriendCode(PID2FC(pid)))
return False
##TODO: MORE HANDLING ERRORS
rel = self.client.remove_friend(pid)
print("[",datetime.now(),"] Removed friend:",FormattedFriendCode(PID2FC(pid)))
return True
def RemoveFriendFC(self,fc):
return self.RemoveFriendPID(FC2PID(fc))
def RefreshFriendData(self,pid):
if not self.PRUDUP_isConnected():
self._ConnectionError()
return None
else:
self._ConnectionSuccess()
return self.client.sync_friend(self.lfcs, [pid], [])[0]
def RefreshAllFriendData(self,pids):
try:
self._ConnectionSuccess()
return self.client.sync_friend(self.lfcs, pids, [])[0]
except:
self._ConnectionError()
return []
def UpdatePresence(self,gameid,msg,Unk = True):
if not self.PRUDUP_isConnected():
self._ConnectionError()
return None
else:
self._ConnectionSuccess()
presence = friends.NintendoPresenceV1(0xffffffff, friends.GameKey(gameid, 0), msg, 0, 0, 0, 0, 0, 0, b"")
self.client.update_presence(presence, Unk)
def GetAllFriends(self):
try:
x = self.client.get_all_friends()
logging.info("Got all friends: %s",len(x))
self._ConnectionSuccess()
return x
except:
self._ConnectionError()
return []