-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
229 lines (190 loc) · 6.7 KB
/
Copy pathdatabase.js
File metadata and controls
229 lines (190 loc) · 6.7 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
const initSqlJs = require('sql.js');
const bcrypt = require('bcrypt');
const crypto = require('crypto');
const fs = require('fs');
const config = require('./config');
class KeyDatabase {
constructor(dbPath) {
this.dbPath = dbPath;
this.db = null;
this.SQL = null;
}
async init() {
// Initialize sql.js
this.SQL = await initSqlJs();
// Load existing database or create new one
if (fs.existsSync(this.dbPath)) {
const buffer = fs.readFileSync(this.dbPath);
this.db = new this.SQL.Database(buffer);
} else {
this.db = new this.SQL.Database();
}
// Create keys table
this.db.run(`
CREATE TABLE IF NOT EXISTS keys (
key TEXT PRIMARY KEY,
type TEXT NOT NULL,
created_at INTEGER NOT NULL,
expires_at INTEGER,
activated_at INTEGER,
hwid TEXT,
is_active INTEGER DEFAULT 1
)
`);
// Create admin table
this.db.run(`
CREATE TABLE IF NOT EXISTS admins (
username TEXT PRIMARY KEY,
password_hash TEXT NOT NULL
)
`);
this.save();
// Initialize admin user if not exists
await this.initAdmin();
}
save() {
const data = this.db.export();
const buffer = Buffer.from(data);
fs.writeFileSync(this.dbPath, buffer);
}
async initAdmin() {
const stmt = this.db.prepare('SELECT * FROM admins WHERE username = ?');
stmt.bind([config.admin.username]);
const hasAdmin = stmt.step();
stmt.free();
if (!hasAdmin) {
const hash = await bcrypt.hash(config.admin.password, 10);
this.db.run('INSERT INTO admins (username, password_hash) VALUES (?, ?)',
[config.admin.username, hash]);
this.save();
console.log('Admin user initialized with default credentials');
}
}
async verifyAdmin(username, password) {
const stmt = this.db.prepare('SELECT password_hash FROM admins WHERE username = ?');
stmt.bind([username]);
if (stmt.step()) {
const row = stmt.getAsObject();
stmt.free();
return await bcrypt.compare(password, row.password_hash);
}
stmt.free();
return false;
}
generateKey(type) {
const prefix = `CS2-${type}`;
const part1 = crypto.randomBytes(3).toString('hex').toUpperCase().substring(0, 3);
const part2 = crypto.randomBytes(3).toString('hex').toUpperCase().substring(0, 3);
return `${prefix}-${part1}-${part2}`;
}
createKey(type) {
const key = this.generateKey(type);
const createdAt = Date.now();
// Don't set expires_at yet - it will be set when the key is first activated
const duration = config.keyDurations[type] || 1;
try {
this.db.run(`
INSERT INTO keys (key, type, created_at, expires_at, is_active)
VALUES (?, ?, ?, ?, 1)
`, [key, type, createdAt, null]); // expires_at is NULL until activated
this.save();
return { success: true, key, duration };
} catch (error) {
return { success: false, error: error.message };
}
}
validateKey(key, hwid) {
const stmt = this.db.prepare('SELECT * FROM keys WHERE key = ?');
stmt.bind([key]);
if (!stmt.step()) {
stmt.free();
return { valid: false, message: 'Invalid key' };
}
const keyData = stmt.getAsObject();
stmt.free();
if (!keyData.is_active) {
return { valid: false, message: 'Key has been deactivated' };
}
const now = Date.now();
// Check if key has been activated
if (keyData.activated_at) {
// Key already activated, check HWID match
if (keyData.hwid !== hwid) {
return { valid: false, message: 'Key is bound to another machine' };
}
// Check expiration
if (now > keyData.expires_at) {
return { valid: false, message: 'Key has expired' };
}
return {
valid: true,
message: 'Key is valid',
expiresAt: keyData.expires_at,
type: keyData.type
};
} else {
// First time activation - set expiration based on key type
const duration = config.keyDurations[keyData.type] || 1;
const expiresAt = now + (duration * 24 * 60 * 60 * 1000);
// Activate the key
this.db.run(`
UPDATE keys
SET activated_at = ?, hwid = ?, expires_at = ?
WHERE key = ?
`, [now, hwid, expiresAt, key]);
this.save();
return {
valid: true,
message: 'Key activated successfully',
expiresAt: expiresAt,
type: keyData.type
};
}
}
getAllKeys() {
const stmt = this.db.prepare('SELECT * FROM keys ORDER BY created_at DESC');
const keys = [];
while (stmt.step()) {
keys.push(stmt.getAsObject());
}
stmt.free();
return keys;
}
deleteKey(key) {
const stmt = this.db.prepare('SELECT COUNT(*) as count FROM keys WHERE key = ?');
stmt.bind([key]);
stmt.step();
const result = stmt.getAsObject();
stmt.free();
if (result.count > 0) {
this.db.run('DELETE FROM keys WHERE key = ?', [key]);
this.save();
return true;
}
return false;
}
getStats() {
let stmt, result;
stmt = this.db.prepare('SELECT COUNT(*) as count FROM keys');
stmt.step();
const total = stmt.getAsObject().count;
stmt.free();
stmt = this.db.prepare('SELECT COUNT(*) as count FROM keys WHERE activated_at IS NOT NULL');
stmt.step();
const active = stmt.getAsObject().count;
stmt.free();
stmt = this.db.prepare('SELECT COUNT(*) as count FROM keys WHERE expires_at IS NOT NULL AND ? > expires_at');
stmt.bind([Date.now()]);
stmt.step();
const expired = stmt.getAsObject().count;
stmt.free();
stmt = this.db.prepare('SELECT type, COUNT(*) as count FROM keys GROUP BY type');
const byType = [];
while (stmt.step()) {
byType.push(stmt.getAsObject());
}
stmt.free();
return { total, active, expired, byType };
}
}
module.exports = KeyDatabase;