forked from KeyAuth/KeyAuth-Python-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
567 lines (484 loc) · 16.3 KB
/
api.py
File metadata and controls
567 lines (484 loc) · 16.3 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
from os import _exit, system, getenv
from json import loads
from binascii import unhexlify
from .hwid import WINDOWS_HWID
from .req import post as _req_post
class mainapi:
def __init__(self, name=None, ownerid=None, version=None, hash_to_check=None):
"""
Initializes the API instance with given parameters.
:param name: Name of the app.
:param ownerid: Account ID.
:param version: Application version.
:param hash_to_check: Value of MD5 checksum (hash) of a file.
"""
self.name = name
self.ownerid = ownerid
self.version = version
self.hash_to_check = hash_to_check
self.initialized = False
self.init()
def CheckInit_HWID(self):
if not self.initialized:
print("Initialize first, in order to use the functions")
_exit(1)
return WINDOWS_HWID().main()
def init(self):
if len(self.ownerid) != 10:
print(
"Invalid owner ID length. Visit https://keyauth.cc/app/, copy the Python code, "
"and replace the code in main.py with that."
)
_exit(1)
post_data = {
"type": "init",
"ver": self.version,
"hash": self.hash_to_check,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data) # response & statusCode in txt from req.py `sever`
if response == "KeyAuth_Invalid":
print(f"The application doesn't exist ")
_exit(1)
response_json = loads(response)
if response_json["message"] == "invalidver":
if response_json["download"] != "":
print("New Version Available")
download_link = response_json["download"]
system(f"start {download_link}")
_exit(1)
else:
print("Invalid Version, Contact owner to add download link to latest app version")
_exit(1)
if not response_json["success"]:
print('Something went wrong here !')
print(f'Response From Server : \n\n{response_json}')
self.initialized = True
self.sessionid = response_json["sessionid"]
def fetchStats(self):
"""
fetch stats return dict of data that include stats
example total keys, seller panel link, etc
"""
params = {
"type": "fetchStats",
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid}
response = _req_post(params)
return loads(response)
def fetchOnline(self):
"""
fetch number of users online
"""
self.CheckInit_HWID()
post_data = {
"type": "fetchOnline",
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json.get("success"):
return json.get("users") if json.get("users") else None
else:
return json
def log(self, message):
'''
Log message to the server and then to your webhook what is set on app settings
keyauthapp.log("Message")
'''
self.CheckInit_HWID()
post_data = {
"type": "log",
"pcuser": getenv('username'),
"message": message,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
resp = loads(_req_post(post_data))
resp['message'] = message
return resp
def register(self, user, password, license, hwid=None):
"""
Registering new username & password
:param user: username to set.
:param password: password to set.
:param license: LicenseKey.
:param hwid: user HWID || You can also use this paramter with SSID.
"""
sys_hwid = self.CheckInit_HWID()
post_data = {
"type": "register",
"username": user,
"pass": password,
"key": license,
"hwid": sys_hwid,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
print(json["message"])
return load_user_data(json["info"])
else:
print(json["message"])
return json
def license(self, key, hwid=None):
"""
Checking License Key Validity
:param key: license key using as login.
:param hwid: user hwid to verify device login.
"""
sys_hwid = self.CheckInit_HWID()
post_data = {
"type": "license",
"key": key,
"hwid": sys_hwid,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
print(json["message"])
return load_user_data(json["info"])
else:
print(json["message"])
return json
def upgrade(self, user, license):
"""
Upgrading duration of user
:param user: username.
:param license: license key.
"""
self.CheckInit_HWID()
post_data = {
"type": "upgrade",
"username": user,
"key": license,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
print(json["message"])
print("Please restart program and login")
return json["message"]
else:
print(json["message"])
return json
def login(self, user, password, hwid=None):
"""
login user with username & password
:param user: username for login.
:param password: password for login.
:param hwid: user hwid to verify device login.
"""
sys_hwid = self.CheckInit_HWID()
post_data = {
"type": "login",
"username": user,
"pass": password,
"hwid": sys_hwid,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
print(json["message"])
return load_user_data(json['info'])
else:
print(json["message"])
return json
def var(self, name):
'''
Get normal variable and print it
data = keyauthapp.var("varName")
'''
self.CheckInit_HWID()
post_data = {
"type": "var",
"varid": name,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
print(json['message'])
return json["message"]
else:
print(json["message"])
return json
def getvar(self, var_name):
'''
Get user variable and print it
keyauthapp.getvar("varName")
'''
self.CheckInit_HWID()
post_data = {
"type": "getvar",
"var": var_name,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
print(json['response'])
return json["response"]
else:
print(f"NOTE: This is commonly misunderstood. This is for user variables, not the normal variables.\nUse keyauthapp.var(\"{var_name}\") for normal variables");
print(json["message"])
return json
def setvar(self, var_name, var_data):
'''
:param var_name: Name Of Variable.
:param var_data: data to set on variable data.
Set up user variable
keyauthapp.setvar("varName", "varValue")
'''
self.CheckInit_HWID()
post_data = {
"type": "setvar",
"var": var_name,
"data": var_data,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
return True
else:
print(json["message"])
return False
def ban(self):
'''
Ban the user and blacklist their HWID and IP Address.
Good function to call upon if you use anti-debug and have detected an intrusion attempt.
:Note: Function only works after login.
keyauthapp.ban()
'''
self.CheckInit_HWID()
post_data = {
"type": "ban",
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
return True
else:
print(json["message"])
return False
def file(self, fileid):
'''
Download Files form the server to your computer using the download function in the api class
bytes = keyauthapp.file("385624")
f = open("example.exe", "wb")
f.write(bytes)
f.close()
'''
self.CheckInit_HWID()
post_data = {
"type": "file",
"fileid": fileid,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
print(json)
if json["success"]:
print(json)
else:
print(json["message"])
return unhexlify(json["contents"])
def webhook(self, webid, param, body = "", conttype = ""):
'''
Log message to the server and then to your webhook what is set on app settings
keyauthapp.log("Message")
'''
self.CheckInit_HWID()
post_data = {
"type": "webhook",
"webid": webid,
"params": param,
"body": body,
"conttype": conttype,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
return json["message"]
else:
print(json["message"])
return json
def check(self):
'''
Use this to see if the user is logged in or not.
print(f"Current Session Validation Status: {keyauthapp.check()}")
'''
self.CheckInit_HWID()
post_data = {
"type": "check",
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
return True
else:
return False
def check_blacklist(self):
"""
Verifies whether the HWID or IP address is blacklisted.
This optional check can enhance security by preventing blacklisted users from accessing the program, even for a brief moment.
However, for faster performance, you may opt to skip this check and rely on the KeyAuth server to automatically
block blacklisted users at login or registration attempts. This function serves as an auxiliary, preemptive security measure.
"""
hwid = self.CheckInit_HWID()
post_data = {
"type": "checkblacklist",
"hwid": hwid,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
return json['success']
def chatGet(self, channel):
'''
Example from the form example on how to fetch the chat messages.
Get chat messages
messages = keyauthapp.chatGet("CHANNEL")
'''
self.CheckInit_HWID()
post_data = {
"type": "chatget",
"channel": channel,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
return json["messages"]
else:
return None
def chatSend(self, message, channel):
'''
* Send chat message
keyauthapp.chatSend("MESSAGE", "CHANNEL")
'''
self.CheckInit_HWID()
post_data = {
"type": "chatsend",
"message": message,
"channel": channel,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
return True
else:
return False
def changeUsername(self, username):
'''
use this to change username from old -> new, username
'''
self.CheckInit_HWID()
post_data = {
"type": "changeUsername",
"newUsername": username,
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
print("Successfully changed username")
else:
print(json["message"])
return json
def logout(self):
'''
Logout the users session and close the application.
This only works if the user is authenticated (logged in)
:call func: keyauthapp.logout()
'''
self.CheckInit_HWID()
post_data = {
"type": "logout",
"sessionid": self.sessionid,
"name": self.name,
"ownerid": self.ownerid
}
response = _req_post(post_data)
json = loads(response)
if json["success"]:
print("Successfully logged out")
return json
else:
print(json["message"])
return json
def load_app_data(data) -> dict:
'''
:param data: contain app data.
this will return a dictonary containing `app data`
'''
results: dict = {}
results.update({
'users_len': data["numUsers"],
'keys_len': data["numKeys"],
'app_version': data["version"],
'customer_panel': data["customerPanelLink"],
'active_users': data["numOnlineUsers"],
})
return results
def load_user_data(data: dict) -> dict:
'''
:param data: contain user data.
this will also return dictonary which include `user data`
'''
results: dict = {}
results.update({
'username': data["username"],
'ip': data["ip"],
'hwid': data["hwid"] or "N/A",
'expire': data["subscriptions"][0]["expiry"],
'create_date': data["createdate"],
'last_login': data["lastlogin"],
'subscription': data["subscriptions"][0]["subscription"],
'subscriptions': data["subscriptions"],
})
return results