-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthon.js
More file actions
461 lines (411 loc) · 16 KB
/
Copy pathauthon.js
File metadata and controls
461 lines (411 loc) · 16 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
/**
* Authon JavaScript/Node.js SDK v1.0.0
* Official SDK for Authon — Software Licensing & Authentication Platform
*
* Website: https://authon.pro
* API: https://api.authon.pro/v1
* Docs: https://authon.pro/docs
* Discord: https://discord.gg/jMZCTKPsmE
* Status: https://authon.pro/status
*
* Requirements: Node.js 18+ (built-in fetch)
* Works with: Node.js, Electron, Bun, Deno
*
* Usage:
* const { Authon } = require('./authon');
* const auth = new Authon('your-app-id', 'your-api-key');
* await auth.init();
* const result = await auth.login('username', 'password');
*
* License: MIT
*/
'use strict';
const { createHash } = require('crypto');
const { hostname, platform, arch, cpus } = require('os');
const fs = require('fs');
const path = require('path');
const SDK_VERSION = '1.0.0';
const DEFAULT_API_URL = 'https://api.authon.pro/v1';
const REQUEST_TIMEOUT = 15000; // 15 seconds
const FILE_TIMEOUT = 60000; // 60 seconds for downloads
class AuthonError extends Error {
constructor(message, code = null) {
super(message);
this.name = 'AuthonError';
this.code = code;
}
}
class Authon {
/**
* Create an Authon SDK instance.
*
* @param {string} appId - Your Application ID (Dashboard → Apps → Your App)
* @param {string} apiKey - Your API Key (Dashboard → Apps → Settings)
* @param {string} [apiUrl] - Custom API URL (default: https://api.authon.pro/v1)
*
* @example
* const auth = new Authon('your-app-id', 'your-api-key');
*/
constructor(appId, apiKey, apiUrl = DEFAULT_API_URL) {
if (!appId || !apiKey) {
throw new AuthonError('appId and apiKey are required');
}
this.appId = appId;
this.apiKey = apiKey;
this.apiUrl = apiUrl;
// Session state (populated after authentication)
this.sessionToken = null;
this.username = null;
this.level = 0;
this.subscription = null;
this.expiresAt = null;
// App info (populated after init())
this.appName = null;
this.appVersion = null;
this.hwidLock = false;
this._initialized = false;
}
/** @returns {boolean} Whether user has an active session */
get isAuthenticated() {
return this.sessionToken !== null;
}
/**
* Generate a unique Hardware ID from the current machine.
* - Windows: disk serial + hostname
* - macOS: hardware UUID
* - Linux: /etc/machine-id
*
* @returns {string} MD5 hash of hardware identifiers
*/
static getHWID() {
try {
let raw;
if (process.platform === 'win32') {
const { execSync } = require('child_process');
const serial = execSync('wmic diskdrive get serialnumber', { encoding: 'utf8', timeout: 5000 })
.split('\n')[1]?.trim() || '';
raw = serial + hostname();
} else if (process.platform === 'darwin') {
const { execSync } = require('child_process');
const output = execSync('ioreg -rd1 -c IOPlatformExpertDevice', { encoding: 'utf8', timeout: 5000 });
const match = output.match(/"IOPlatformUUID"\s*=\s*"([^"]+)"/);
raw = match ? match[1] : hostname() + arch();
} else {
// Linux
if (fs.existsSync('/etc/machine-id')) {
raw = fs.readFileSync('/etc/machine-id', 'utf8').trim();
} else {
raw = hostname() + platform() + arch() + (cpus()[0]?.model || '');
}
}
return createHash('md5').update(raw).digest('hex');
} catch {
return createHash('md5').update(hostname() + platform()).digest('hex');
}
}
/**
* Internal: Send POST request to Authon API
* @private
*/
async _request(data, timeout = REQUEST_TIMEOUT) {
data.appId = this.appId;
data.apiKey = this.apiKey;
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
const response = await fetch(this.apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': `Authon-JS-SDK/${SDK_VERSION}`,
},
body: JSON.stringify(data),
signal: controller.signal,
});
clearTimeout(timeoutId);
// Handle binary response (file downloads)
const contentType = response.headers.get('content-type') || '';
if (contentType.includes('octet-stream')) {
const buffer = Buffer.from(await response.arrayBuffer());
return { success: true, binary: buffer };
}
const json = await response.json();
return json;
} catch (err) {
if (err.name === 'AbortError') {
return { success: false, message: 'Request timed out. Check API status: https://authon.pro/status' };
}
return { success: false, message: err.message || 'Connection failed' };
}
}
// ═══════════════════════════════════════════════════════════
// INITIALIZATION
// ═══════════════════════════════════════════════════════════
/**
* Initialize connection to Authon API.
* Must be called before any other method.
*
* @returns {Promise<boolean>} True if connection successful
*
* @example
* const auth = new Authon('app-id', 'api-key');
* if (!await auth.init()) {
* console.error('Connection failed');
* process.exit(1);
* }
* console.log(`Connected to ${auth.appName} v${auth.appVersion}`);
*/
async init() {
const result = await this._request({ type: 'init' });
if (result.success && result.data) {
this.appName = result.data.name;
this.appVersion = result.data.version;
this.hwidLock = result.data.hwidLock || false;
this._initialized = true;
return true;
}
return false;
}
// ═══════════════════════════════════════════════════════════
// AUTHENTICATION
// ═══════════════════════════════════════════════════════════
/**
* Login with username and password.
*
* @param {string} username - User's username
* @param {string} password - User's password
* @param {string} [hwid] - Hardware ID (auto-generated if not provided)
* @returns {Promise<Object>} {success, message, data: {sessionToken, username, level, subscription, expiresAt}}
*
* @example
* const result = await auth.login('john', 'mypassword');
* if (result.success) {
* console.log(`Welcome ${auth.username}! Level: ${auth.level}`);
* } else {
* console.log(`Error: ${result.message}`);
* // Possible: "Invalid credentials", "Account banned", "HWID mismatch",
* // "Subscription expired", "VPN/Proxy not allowed"
* }
*/
async login(username, password, hwid = null) {
if (!username || !password) {
return { success: false, message: 'Username and password are required' };
}
const result = await this._request({
type: 'login',
username,
password,
hwid: hwid || Authon.getHWID(),
});
if (result.success && result.data) {
this.sessionToken = result.data.sessionToken;
this.username = result.data.username;
this.level = result.data.level || 0;
this.subscription = result.data.subscription;
this.expiresAt = result.data.expiresAt;
}
return result;
}
/**
* Authenticate with license key only (no username/password needed).
*
* @param {string} key - License key (e.g., "XXXXX-XXXXX-XXXXX-XXXXX")
* @param {string} [hwid] - Hardware ID (auto-generated if not provided)
* @returns {Promise<Object>} {success, message, data}
*
* @example
* const result = await auth.license('A1B2C-D3E4F-G5H6I-J7K8L');
* if (result.success) {
* console.log(`Valid! Level: ${auth.level}, Expires: ${auth.expiresAt}`);
* }
*/
async license(key, hwid = null) {
if (!key) return { success: false, message: 'License key is required' };
const result = await this._request({
type: 'license',
licenseKey: key,
hwid: hwid || Authon.getHWID(),
});
if (result.success && result.data) {
this.sessionToken = result.data.sessionToken;
this.level = result.data.level || 0;
this.subscription = result.data.subscription;
this.expiresAt = result.data.expiresAt;
}
return result;
}
/**
* Register a new account with a license key.
*
* @param {string} username - Desired username
* @param {string} password - Desired password
* @param {string} licenseKey - Valid unused license key
* @param {string} [hwid] - Hardware ID
* @returns {Promise<Object>} {success, message}
*/
async register(username, password, licenseKey, hwid = null) {
if (!username || !password || !licenseKey) {
return { success: false, message: 'username, password, and licenseKey are required' };
}
return this._request({
type: 'register',
username,
password,
licenseKey,
hwid: hwid || Authon.getHWID(),
});
}
// ═══════════════════════════════════════════════════════════
// SESSION MANAGEMENT
// ═══════════════════════════════════════════════════════════
/**
* Validate current session (heartbeat).
* @returns {Promise<boolean>} True if session is still valid
*/
async check() {
if (!this.sessionToken) return false;
const result = await this._request({ type: 'check', sessionToken: this.sessionToken });
return result.success === true;
}
/**
* End current session.
* @returns {Promise<boolean>} True if logout successful
*/
async logout() {
if (!this.sessionToken) return false;
const result = await this._request({ type: 'logout', sessionToken: this.sessionToken });
if (result.success) {
this.sessionToken = null;
this.username = null;
this.level = 0;
}
return result.success === true;
}
// ═══════════════════════════════════════════════════════════
// VARIABLES
// ═══════════════════════════════════════════════════════════
/**
* Get an application-level variable (set by seller in dashboard).
* @param {string} key - Variable name
* @returns {Promise<string|null>} Value or null
*/
async getVar(key) {
const result = await this._request({ type: 'var', key, sessionToken: this.sessionToken });
return result.success ? result.data?.value : null;
}
/**
* Set a user-level variable (stored per-user).
* @param {string} key - Variable name
* @param {string} value - Variable value
* @returns {Promise<boolean>}
*/
async setVar(key, value) {
const result = await this._request({ type: 'setvar', key, value: String(value), sessionToken: this.sessionToken });
return result.success === true;
}
/**
* Get a user-level variable.
* @param {string} key - Variable name
* @returns {Promise<string|null>} Value or null
*/
async getUserVar(key) {
const result = await this._request({ type: 'getvar', key, sessionToken: this.sessionToken });
return result.success ? result.data?.value : null;
}
// ═══════════════════════════════════════════════════════════
// FILES
// ═══════════════════════════════════════════════════════════
/**
* List files available for the current user's level.
* @returns {Promise<Array<{id: string, name: string, size: number, minLevel: number}>>}
*/
async listFiles() {
const result = await this._request({ type: 'list_files', sessionToken: this.sessionToken });
return result.success ? (result.data || []) : [];
}
/**
* Download a file by ID (returns Buffer).
* @param {string} fileId - File ID from listFiles()
* @returns {Promise<Buffer|null>} File content or null
*
* @example
* const files = await auth.listFiles();
* const buffer = await auth.downloadFile(files[0].id);
* fs.writeFileSync('output.exe', buffer);
*/
async downloadFile(fileId) {
if (!this.sessionToken || !fileId) return null;
const result = await this._request({
type: 'file',
fileId,
sessionToken: this.sessionToken,
}, FILE_TIMEOUT);
if (result.binary) return result.binary;
// Fallback: GET endpoint
try {
const baseUrl = this.apiUrl.replace('/v1', '');
const url = `${baseUrl}/v1/files/download/${fileId}?token=${this.sessionToken}`;
const response = await fetch(url, {
headers: { 'X-Session-Token': this.sessionToken },
signal: AbortSignal.timeout(FILE_TIMEOUT),
});
if (response.headers.get('content-type')?.includes('octet-stream')) {
return Buffer.from(await response.arrayBuffer());
}
} catch { /* ignore fallback failure */ }
return null;
}
// ═══════════════════════════════════════════════════════════
// LOGGING & STATS
// ═══════════════════════════════════════════════════════════
/**
* Send an activity log (visible in seller dashboard).
* @param {string} message - Log message (max 500 chars)
* @returns {Promise<boolean>}
*/
async log(message) {
const result = await this._request({ type: 'log', message: String(message).slice(0, 500), sessionToken: this.sessionToken });
return result.success === true;
}
/**
* Get online user count and list.
* @returns {Promise<{count: number, users: Array}>}
*/
async fetchOnline() {
const result = await this._request({ type: 'fetch_online', sessionToken: this.sessionToken });
return result.success ? (result.data || { count: 0, users: [] }) : { count: 0, users: [] };
}
/**
* Get application statistics.
* @returns {Promise<{totalUsers: number, onlineUsers: number, totalKeys: number, appVersion: string}>}
*/
async fetchStats() {
const result = await this._request({ type: 'fetch_stats', sessionToken: this.sessionToken });
return result.success ? (result.data || {}) : {};
}
// ═══════════════════════════════════════════════════════════
// UTILITY
// ═══════════════════════════════════════════════════════════
/**
* Check if IP or HWID is blacklisted.
* @param {string} [ip] - IP to check
* @param {string} [hwid] - HWID to check
* @returns {Promise<{blacklisted: boolean, reason: string|null}>}
*/
async checkBlacklist(ip = null, hwid = null) {
const data = { type: 'check_blacklist' };
if (ip) data.ip = ip;
if (hwid) data.hwid = hwid;
const result = await this._request(data);
return result.success ? (result.data || { blacklisted: false }) : { blacklisted: false };
}
/**
* Redeem a referral code for bonus days.
* @param {string} code - Referral code
* @returns {Promise<Object>} {success, message, data: {expiresAt, rewardDays}}
*/
async redeemReferral(code) {
return this._request({ type: 'redeem_referral', code, sessionToken: this.sessionToken });
}
}
module.exports = { Authon, AuthonError };