-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
138 lines (116 loc) · 4.15 KB
/
Copy pathserver.js
File metadata and controls
138 lines (116 loc) · 4.15 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
const express = require('express');
const cors = require('cors');
const path = require('path');
const config = require('./config');
const KeyDatabase = require('./database');
const app = express();
const db = new KeyDatabase(config.dbPath);
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// Admin authentication middleware
function requireAuth(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Basic ')) {
return res.status(401).json({ error: 'Unauthorized' });
}
const base64Credentials = authHeader.split(' ')[1];
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii');
const [username, password] = credentials.split(':');
db.verifyAdmin(username, password).then(isValid => {
if (isValid) {
next();
} else {
res.status(401).json({ error: 'Invalid credentials' });
}
}).catch(err => {
res.status(500).json({ error: 'Authentication error' });
});
}
// Routes
// Validate key (public endpoint for C++ client)
app.post('/api/validate', (req, res) => {
const { key, hwid } = req.body;
if (!key || !hwid) {
return res.status(400).json({
valid: false,
message: 'Missing key or hwid'
});
}
const result = db.validateKey(key, hwid);
res.json(result);
});
// Admin login check
app.post('/api/admin/login', requireAuth, (req, res) => {
res.json({ success: true, message: 'Authenticated' });
});
// Generate keys
app.post('/api/keys/generate', requireAuth, (req, res) => {
const { type, count = 1 } = req.body;
const validTypes = ['DAY', 'WEEK', 'MONTH', 'LIFE'];
if (!validTypes.includes(type)) {
return res.status(400).json({
success: false,
error: 'Invalid key type. Must be DAY, WEEK, MONTH, or LIFE'
});
}
const keys = [];
for (let i = 0; i < count; i++) {
const result = db.createKey(type);
if (result.success) {
keys.push(result.key);
}
}
res.json({
success: true,
keys,
count: keys.length
});
});
// Get all keys
app.get('/api/keys', requireAuth, (req, res) => {
const keys = db.getAllKeys();
res.json(keys);
});
// Delete key
app.delete('/api/keys/:key', requireAuth, (req, res) => {
const { key } = req.params;
const success = db.deleteKey(key);
if (success) {
res.json({ success: true, message: 'Key deleted' });
} else {
res.status(404).json({ success: false, message: 'Key not found' });
}
});
// Get statistics
app.get('/api/stats', requireAuth, (req, res) => {
const stats = db.getStats();
res.json(stats);
});
// Start server
async function startServer() {
try {
// Initialize database
await db.init();
// Start listening
app.listen(config.port, () => {
console.log(`\x1b[36m╔════════════════════════════════════════╗\x1b[0m`);
console.log(`\x1b[36m║ CS2 Key Management Server Started ║\x1b[0m`);
console.log(`\x1b[36m╚════════════════════════════════════════╝\x1b[0m`);
console.log(`\x1b[32m✓\x1b[0m Server running on port \x1b[33m${config.port}\x1b[0m`);
console.log(`\x1b[32m✓\x1b[0m Admin panel: \x1b[34mhttp://localhost:${config.port}\x1b[0m`);
console.log(`\x1b[32m✓\x1b[0m Database: \x1b[35m${config.dbPath}\x1b[0m`);
console.log('');
console.log(`\x1b[33m⚠\x1b[0m Default admin credentials:`);
console.log(` Username: \x1b[36m${config.admin.username}\x1b[0m`);
console.log(` Password: \x1b[36m${config.admin.password}\x1b[0m`);
console.log(` \x1b[31mPlease change these in config.js!\x1b[0m`);
console.log('');
});
} catch (error) {
console.error('\x1b[31mFailed to start server:\x1b[0m', error);
process.exit(1);
}
}
startServer();