Spaces:
Sleeping
Sleeping
File size: 12,730 Bytes
2bf7a2b | 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 | const initSqlJs = require('sql.js');
const fs = require('fs');
const path = require('path');
const bcrypt = require('bcryptjs');
const { v4: uuidv4 } = require('uuid');
const KEY_PREFIX_ADMIN = 'mc_admin_';
const KEY_PREFIX_SERVER = 'mc_server_';
const KEY_PREFIX_REGULAR = 'mc_key_';
const KEY_TYPE_ADMIN = 'admin';
const KEY_TYPE_SERVER = 'server';
const KEY_TYPE_REGULAR = 'regular';
class Database {
constructor(dbPath = 'minecraft_ws.db') {
this.dbPath = dbPath;
this.db = null;
}
async init() {
const SQL = await initSqlJs();
if (fs.existsSync(this.dbPath)) {
const buffer = fs.readFileSync(this.dbPath);
this.db = new SQL.Database(buffer);
} else {
this.db = new SQL.Database();
}
this.initDbStructure();
this.save();
}
initDbStructure() {
this.db.run(`
CREATE TABLE IF NOT EXISTS api_keys (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
key_hash TEXT NOT NULL UNIQUE,
key_prefix TEXT NOT NULL,
key_type TEXT NOT NULL,
server_id TEXT,
regular_key_id TEXT,
created_at TEXT NOT NULL,
last_used TEXT,
is_active INTEGER DEFAULT 1
)
`);
this.db.run(`
CREATE TABLE IF NOT EXISTS event_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
event_type TEXT NOT NULL,
server_name TEXT NOT NULL,
timestamp TEXT NOT NULL,
data TEXT NOT NULL,
api_key_id TEXT,
FOREIGN KEY (api_key_id) REFERENCES api_keys (id)
)
`);
}
save() {
const data = this.db.export();
const buffer = Buffer.from(data);
fs.writeFileSync(this.dbPath, buffer);
}
generateRandomRawKeyString(prefix) {
return `${prefix}${uuidv4().replace(/-/g, '')}`;
}
async ensureInitialAdminKey() {
const stmt = this.db.prepare('SELECT 1 FROM api_keys WHERE key_type = ? LIMIT 1');
stmt.bind([KEY_TYPE_ADMIN]);
const adminKeyExists = stmt.step();
stmt.free();
if (!adminKeyExists) {
const generatedRawAdminKey = this.generateRandomRawKeyString(KEY_PREFIX_ADMIN);
const adminKeyName = 'Auto-Generated Admin Key';
const adminKeyDescription = `Auto-generated on ${new Date().toISOString()}`;
const keyId = uuidv4();
const keyHash = await bcrypt.hash(generatedRawAdminKey, 10);
const createdAt = new Date().toISOString();
try {
this.db.run(
'INSERT INTO api_keys (id, name, description, key_hash, key_prefix, key_type, created_at, is_active) VALUES (?, ?, ?, ?, ?, ?, ?, 1)',
[keyId, adminKeyName, adminKeyDescription, keyHash, KEY_PREFIX_ADMIN, KEY_TYPE_ADMIN, createdAt]
);
this.save();
return { name: adminKeyName, key: generatedRawAdminKey };
} catch (error) {
console.error('Error creating initial Admin Key:', error);
return null;
}
}
return null;
}
async createRegularKeyWithServerKey(name, description = '', serverId = null) {
const regularKeyId = uuidv4();
const serverKeyId = uuidv4();
// 创建Regular Key
const regularRawKey = this.generateRandomRawKeyString(KEY_PREFIX_REGULAR);
const regularKeyHash = await bcrypt.hash(regularRawKey, 10);
// 创建Server Key
const serverRawKey = this.generateRandomRawKeyString(KEY_PREFIX_SERVER);
const serverKeyHash = await bcrypt.hash(serverRawKey, 10);
const createdAt = new Date().toISOString();
try {
// 开始事务
this.db.exec('BEGIN TRANSACTION');
// 插入Regular Key
this.db.run(
'INSERT INTO api_keys (id, name, description, key_hash, key_prefix, key_type, server_id, created_at, is_active) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)',
[regularKeyId, name, description, regularKeyHash, KEY_PREFIX_REGULAR, KEY_TYPE_REGULAR, serverId, createdAt]
);
// 插入关联的Server Key
this.db.run(
'INSERT INTO api_keys (id, name, description, key_hash, key_prefix, key_type, server_id, regular_key_id, created_at, is_active) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1)',
[serverKeyId, `${name} - Server Key`, `Server Key for ${name}`, serverKeyHash, KEY_PREFIX_SERVER, KEY_TYPE_SERVER, serverId, regularKeyId, createdAt]
);
// 提交事务
this.db.exec('COMMIT');
this.save();
return {
regularKey: {
id: regularKeyId,
rawKey: regularRawKey,
keyPrefix: KEY_PREFIX_REGULAR,
keyType: KEY_TYPE_REGULAR,
serverId
},
serverKey: {
id: serverKeyId,
rawKey: serverRawKey,
keyPrefix: KEY_PREFIX_SERVER,
keyType: KEY_TYPE_SERVER,
serverId,
regularKeyId
}
};
} catch (error) {
this.db.exec('ROLLBACK');
throw new Error('Could not generate unique API key hash');
}
}
async createApiKey(name, description = '', keyType = KEY_TYPE_REGULAR, serverId = null, regularKeyId = null) {
const keyId = uuidv4();
let prefix;
switch(keyType) {
case KEY_TYPE_ADMIN:
prefix = KEY_PREFIX_ADMIN;
break;
case KEY_TYPE_SERVER:
prefix = KEY_PREFIX_SERVER;
break;
default:
prefix = KEY_PREFIX_REGULAR;
}
const rawKey = this.generateRandomRawKeyString(prefix);
const keyHash = await bcrypt.hash(rawKey, 10);
const createdAt = new Date().toISOString();
try {
this.db.run(
'INSERT INTO api_keys (id, name, description, key_hash, key_prefix, key_type, server_id, regular_key_id, created_at, is_active) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1)',
[keyId, name, description, keyHash, prefix, keyType, serverId, regularKeyId, createdAt]
);
this.save();
return { keyId, rawKey, keyPrefix: prefix, keyType, serverId, regularKeyId };
} catch (error) {
throw new Error('Could not generate unique API key hash');
}
}
async verifyApiKey(keyToCheck) {
const stmt = this.db.prepare('SELECT id, key_hash, key_type, server_id, regular_key_id FROM api_keys WHERE is_active = 1');
while (stmt.step()) {
const row = stmt.getAsObject();
const isValid = await bcrypt.compare(keyToCheck, row.key_hash);
if (isValid) {
stmt.free();
this.db.run(
'UPDATE api_keys SET last_used = ? WHERE id = ?',
[new Date().toISOString(), row.id]
);
this.save();
return {
id: row.id,
keyType: row.key_type,
serverId: row.server_id,
regularKeyId: row.regular_key_id
};
}
}
stmt.free();
return null;
}
getApiKeyDetailsById(keyId) {
const stmt = this.db.prepare(
'SELECT id, name, description, key_prefix, key_type, server_id, regular_key_id, created_at, last_used, is_active FROM api_keys WHERE id = ?'
);
stmt.bind([keyId]);
if (stmt.step()) {
const row = stmt.getAsObject();
stmt.free();
return {
id: row.id,
name: row.name,
description: row.description,
keyPrefix: row.key_prefix,
keyType: row.key_type,
serverId: row.server_id,
regularKeyId: row.regular_key_id,
createdAt: row.created_at,
lastUsed: row.last_used,
isActive: Boolean(row.is_active)
};
}
stmt.free();
return null;
}
getAllApiKeysInfo() {
const stmt = this.db.prepare(
'SELECT id, name, description, key_prefix, key_type, server_id, regular_key_id, created_at, last_used, is_active FROM api_keys ORDER BY created_at DESC'
);
const keys = [];
while (stmt.step()) {
const row = stmt.getAsObject();
keys.push({
id: row.id,
name: row.name,
description: row.description,
keyPrefix: row.key_prefix,
keyType: row.key_type,
serverId: row.server_id,
regularKeyId: row.regular_key_id,
createdAt: row.created_at,
lastUsed: row.last_used,
isActive: Boolean(row.is_active)
});
}
stmt.free();
return keys;
}
getServerKeysByRegularKeyId(regularKeyId) {
const stmt = this.db.prepare(
'SELECT id, name, description, key_prefix, key_type, server_id, created_at, last_used, is_active FROM api_keys WHERE regular_key_id = ? AND key_type = ? ORDER BY created_at DESC'
);
stmt.bind([regularKeyId, KEY_TYPE_SERVER]);
const keys = [];
while (stmt.step()) {
const row = stmt.getAsObject();
keys.push({
id: row.id,
name: row.name,
description: row.description,
keyPrefix: row.key_prefix,
keyType: row.key_type,
serverId: row.server_id,
createdAt: row.created_at,
lastUsed: row.last_used,
isActive: Boolean(row.is_active)
});
}
stmt.free();
return keys;
}
toggleApiKeyActivation(keyId, activate) {
const result = this.db.run(
'UPDATE api_keys SET is_active = ? WHERE id = ?',
[activate ? 1 : 0, keyId]
);
this.save();
return result.changes > 0;
}
deleteApiKeyById(keyId) {
const keyInfo = this.getApiKeyDetailsById(keyId);
if (keyInfo && keyInfo.keyType === KEY_TYPE_ADMIN) {
const stmt = this.db.prepare(
'SELECT COUNT(*) as count FROM api_keys WHERE key_type = ? AND id != ?'
);
stmt.bind([KEY_TYPE_ADMIN, keyId]);
stmt.step();
const row = stmt.getAsObject();
stmt.free();
if (row.count === 0) {
throw new Error('Cannot delete the last Admin Key');
}
}
const result = this.db.run('DELETE FROM api_keys WHERE id = ?', [keyId]);
this.save();
return result.changes > 0;
}
logEvent(event, apiKeyId) {
this.db.run(
'INSERT INTO event_logs (event_type, server_name, timestamp, data, api_key_id) VALUES (?, ?, ?, ?, ?)',
[event.event_type, event.server_name, event.timestamp, JSON.stringify(event.data), apiKeyId]
);
this.save();
}
getRecentEvents(limit = 100) {
const stmt = this.db.prepare(
'SELECT * FROM event_logs ORDER BY timestamp DESC LIMIT ?'
);
stmt.bind([limit]);
const events = [];
while (stmt.step()) {
const row = stmt.getAsObject();
events.push({
id: row.id,
eventType: row.event_type,
serverName: row.server_name,
timestamp: row.timestamp,
data: JSON.parse(row.data),
apiKeyId: row.api_key_id
});
}
stmt.free();
return events;
}
}
module.exports = Database;
module.exports.KEY_TYPE_ADMIN = KEY_TYPE_ADMIN;
module.exports.KEY_TYPE_SERVER = KEY_TYPE_SERVER;
module.exports.KEY_TYPE_REGULAR = KEY_TYPE_REGULAR;
|