-
Notifications
You must be signed in to change notification settings - Fork 2
/
KeyAuth.js
694 lines (574 loc) · 17.3 KB
/
KeyAuth.js
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//* Importing Axios *//
import axios from 'axios';
//* Importing OS *//
import os from 'os';
//* Import FS *//
// import fs from 'fs';
import Constants from 'expo-constants';
import * as Crypto from 'expo-crypto';
//* KeyAuth Class *//
class KeyAuth {
/**
* @param {string} [name] - The name of the application
* @param {string} [ownerId] - The ownerId of the application
* @param {string} [secret] - The secret of the application
* @param {string} [version] - The version of the application
**/
constructor(name, ownerId, secret, version) {
if (!(name && ownerId && secret && version)) {
Misc.error('Application not setup correctly.')
}
this.name = name
this.ownerId = ownerId
this.secret = secret
this.version = version
this.responseTime = null;
};
/**
* Initializes the connection with KeyAuth in order to use any of the functions
**/
Initialize = () => new Promise(async (resolve, reject) => {
const post_data = {
type: 'init',
ver: this.version,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
if (Json === 'KeyAuth_Invalid') {
Misc.error('Invalid Application, please check your application details.', reject)
}
if (!Json.success || Json.success == false) {
return resolve(false)
}
this.app_data = Json.appinfo
this.sessionid = Json.sessionid
this.initialized = true
resolve(true)
})
/**
* Registers the user using a license and gives the user a subscription that matches their license level
* @param {string} [username] - The username for the user
* @param {string} [password] - The password for the user
* @param {string} [license] - The License Key for the sub
**/
register = (user, password, license, email = "") => new Promise(async (resolve, reject) => {
this.check_initialize()
let hwId
if (!hwId) {
hwId = await Misc.GetCurrentHardwareId()
}
const post_data = {
type: 'register',
username: user,
pass: password,
email,
key: license,
hwid: hwId,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success) {
this.Load_User_Data(Json.info)
return resolve(Json.message)
} else {
Misc.error(Json.message, reject)
}
})
forgot = (username, email) => new Promise(async (resolve, reject) => {
this.check_initialize()
const post_data = {
type: 'forgot',
username,
email,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success === true) {
resolve(Json);
} else {
Misc.error(Json.message, reject);
}
});
/**
* Authenticates the user using their username and password
* @param {string} [username] - The username for the user
* @param {string} [password] - The password for the user
**/
login = (username, password) => new Promise(async (resolve, reject) => {
this.check_initialize()
let hwId;
if (!hwId) {
hwId = await Misc.GetCurrentHardwareId();
}
const post_data = {
type: 'login',
username,
pass: password,
hwid: hwId,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
};
const Json = await this.make_request(post_data);
this.Load_Response_Struct(Json);
if (Json.success && Json.success === true) {
this.Load_User_Data(Json.info);
resolve(Json);
} else {
Misc.error(Json.message, reject);
}
});
/**
* Authenticate without using usernames and passwords
* @param {string} [key] - Licence used to login with
**/
license = (key) => new Promise(async (resolve, reject) => {
this.check_initialize()
let hwId
if (!hwId) {
hwId = await Misc.GetCurrentHardwareId()
}
const post_data = {
type: 'license',
key,
hwid: hwId,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
this.Load_User_Data(Json.info)
return resolve(Json)
} else {
Misc.error(Json.message, reject)
}
})
/**
* Gives the user a subscription that has the same level as the key
* @param {string} [username] - Username of the user thats going to get upgraded
* @param {string} [license] - License with the same level as the subscription you want to give the user
**/
upgrade = (username, license) => new Promise(async (resolve, reject) => {
this.check_initialize()
const post_data = {
type: 'upgrade',
username,
key: license,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (!Json.success || Json.success == false) {
return resolve(Json.message)
} else {
// Don't let them yet for dashboard.
Misc.error(Json.message, reject)
}
})
/**
* Gets an existing global variable
* @param {string} [VarId] - Name of the variable / Variable ID
* returns {string} - The value of the variable / The content of the variable
**/
var = (VarId) => new Promise(async (resolve) => {
this.check_initialize()
const post_data = {
type: 'var',
varid: VarId,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
return resolve(Json)
}
resolve(Json.message)
})
/**
* Gets the an existing user variable
* @Param {string} [VarId] - User Variable Name
* returns {string} - The value of the variable / The content of the user variable
**/
GetVar = (VarId) => new Promise(async (resolve) => {
this.check_initialize()
const post_data = {
type: 'getvar',
var: VarId,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
return resolve(Json)
}
resolve(Json.message)
})
/**
* Change the data of an existing user variable, *User must be logged in*
* @Param {string} [VarId] - User variable name
* @Param {string} [VarData] - The content of the variable
**/
SetVar = (VarId, VarData) => new Promise(async (resolve) => {
this.check_initialize()
const post_data = {
type: 'setvar',
var: VarId,
data: VarData,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
return resolve(Json)
}
resolve(Json.message)
})
/**
* Bans the current logged in user
**/
ban = () => new Promise(async (resolve) => {
this.check_initialize()
const post_data = {
type: 'ban',
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
return resolve(true)
}
resolve(Json.message)
})
// /**
// * KeyAuth acts as proxy and downlods the file in a secure way
// * @Param {string} [fileId] - File ID
// * @Param {string} [path] - Path to save the file
// * @Param {boolean} [execute] - Execute the file after download - Windows Only Requires path for file!
// * returns {byte} - Returns The bytes of the download file
// **/
// file = (fileId, path = null, execute = false) => new Promise(async (resolve) => {
// this.check_initialize()
// const post_data = {
// type: 'file',
// fileid: fileId.toString(),
// sessionid: this.sessionid,
// name: this.name,
// ownerid: this.ownerId
// }
// const Json = await this.make_request(post_data)
// this.Load_Response_Struct(Json)
// if (Json.success && Json.success == true) {
// if (path != null) {
// var bytes = await this.strToByteArray(Json.contents);
// fs.writeFile(path, bytes, async (err) => {
// if (err) throw err;
// if (execute) {
// var exec = require('child_process').exec;
// await exec(path, function (error, stdout, stderr) {
// if (error) {
// console.error(error);
// return;
// }
// });
// return resolve(true);
// } else {
// return resolve(true);
// }
// });
// } else {
// return resolve(this.strToByteArray(Json.contents))
// }
// }
// resolve(Json.message)
// })
/**
* Sends a request to a webhook that you've added in the dashboard in a safe way without it being showed for example a http debugger
* @Param {string} [webId] - Webhook ID
* @Param {string} [Params] - Webhook Parameters
* @Param {string} [message] - Body of the request, empty by default
* @Param {string} [username] - Content type, empty by default
* Returns {string} - Returns the response of the webhook
**/
webhook = (webId, Params, body = '', contType = '') => new Promise(async (resolve) => { // havent tested
this.check_initialize()
const post_data = {
type: 'webhook',
webid: webId,
params: Params,
body,
conttype: contType,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
return resolve(Json.response)
}
resolve(Json.message)
})
/**
* Check if the current session is validated or not
* Returns {string} - Returns if the session is valid or not
**/
check = () => new Promise(async (resolve) => {
this.check_initialize()
const post_data = {
type: 'check',
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
return resolve(Json)
};
resolve(Json.message)
})
/**
* Checks if the current IP Address/HardwareId is blacklisted
* returns {boolean} - Returns true if the IP Address/HardwareId is blacklisted, otherwise false
**/
checkBlack = () => new Promise(async (resolve) => {
this.check_initialize()
const hwId = await Misc.GetCurrentHardwareId()
const post_data = {
type: 'checkblacklist',
hwid: hwId,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
return resolve(true)
}
resolve(false)
})
/**
* Fetch usernames of online users
* Returns {array} - Returns an array of usernames
**/
fetchOnline = () => new Promise(async (resolve) => {
this.check_initialize()
const post_data = {
type: 'fetchOnline',
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
return resolve(Json.users)
} else {
return resolve(Json.message)
}
})
/**
* Gets the last 20 sent messages of that channel
* @param {string} [ChannelName] - The name of the channel, where you want the messages
* Returns {array} the last 20 sent messages of that channel
**/
ChatGet = (ChannelName) => new Promise(async (resolve) => {
this.check_initialize()
const post_data = {
type: 'chatget',
channel: ChannelName,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
if (Json.messages[0].message == 'not_found') {
return resolve([])
} else {
return resolve(Json.messages)
}
} else {
return resolve([])
}
})
/**
* Sends a message to the given channel name
* @param {string} [ChannelName] - Channel Name where the message will be sent to
* @param {string} [Message] - Message what will be sent to [ChannelName]
* Returns {bool} - Returns true if the message was sent, otherwise false
**/
ChatSend = (ChannelName, Message) => new Promise(async (resolve) => {
this.check_initialize()
const post_data = {
type: 'chatsend',
message: Message,
channel: ChannelName,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
const Json = await this.make_request(post_data)
this.Load_Response_Struct(Json)
if (Json.success && Json.success == true) {
return resolve(true)
} else {
return resolve(false)
}
})
/**
* Logs the IP address,PC Name with a message, if a discord webhook is set up in the app settings, the log will get sent there and the dashboard if not set up it will only be in the dashboard
* @param {string} [message] - Message / Discord Embed Title Message
* Returns None
**/
log = (message) => new Promise(async (resolve) => {
this.check_initialize()
const post_data = {
type: 'log',
pcuser: os.userInfo().username,
message,
sessionid: this.sessionid,
name: this.name,
ownerid: this.ownerId
}
await this.make_request(post_data)
resolve(true)
})
strToByteArray = (hex) => new Promise(async (resolve, reject) => {
try {
const numberChars = hex.length;
const bytes = new Uint8Array(numberChars / 2);
for (let i = 0; i < numberChars; i += 2) {
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
}
resolve(bytes)
} catch (err) {
Misc.error('The session has ended, open program again.', reject)
}
})
/**
* Check if the current session is initialized
* @returns [true] if client is Initialized.
**/
check_initialize() {
if (!this.initialized) {
Misc.error('You must initialize the API before using it!')
}
return true
};
/**
* Load the response struct for Response of Request
**/
Load_Response_Struct(data) {
this.response = {
success: data.success,
message: data.message
}
};
/**
* Load the response struct for User Data
**/
Load_User_Data(data) {
this.user_data = {
username: data.username,
ip: data.ip,
hwid: data.hwid,
createdate: data.createdate,
lastlogin: data.lastlogin,
subscriptions: data.subscriptions
}
};
/**
* Change Console Application Title
* @param {string} [title] - Your new Title for the App
* Returns Promise Timeout
**/
setTitle(title) {
process.stdout.write(
String.fromCharCode(27) + ']0;' + title + String.fromCharCode(7)
)
};
/**
* Sleeping / Timeout Function
* @param {number} [ms] - Time in milliseconds
* Returns Promise Timeout
**/
sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
};
/**
* Request the API with the POST Data
* @param {string} [data] - Post Data Array
* Returns {array} - Returns the API Response [NON-ENCRYPTED]
**/
make_request(data) {
const startTime = Date.now(); // Start the stopwatch
return new Promise(async (resolve, reject) => {
const request = await axios({
method: 'POST',
url: 'https://keyauth.win/api/1.2/',
data: new URLSearchParams(data).toString()
}).catch((err) => {
Misc.error(err, reject)
})
const endTime = Date.now(); // Stop the stopwatch
this.responseTime = `${endTime - startTime} ms`;
if (request && request.data) {
resolve(request.data)
} else {
resolve(null)
};
})
}
}
class Misc {
/**
* Get the current user HardwareId
* @returns {string} - Returns user HardwareID
**/
static async GetCurrentHardwareId() {
let system_id = null;
const input = Constants.installationId;
const generateHash = async (data) => {
return await Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA256, data);
};
const result = await generateHash(input);
system_id = result;
return system_id;
};
/**
* Error Print Function
* @param {string} [message] - Message to Show and then exit app.
**/
static error(message, reject = null) {
console.log(message);
reject ? reject(message) : process.exit(1);
}
}
/**
* Export KeyAuth Class to be used in other files
**/
module.exports = KeyAuth