Spaces:
Sleeping
Sleeping
Create server/server.js
Browse files- server/server.js +56 -0
server/server.js
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
require('dotenv').config();
|
2 |
+
const express = require('express');
|
3 |
+
const multer = require('multer');
|
4 |
+
const cors = require('cors');
|
5 |
+
const { decodeAuthFromImage } = require('./decoder');
|
6 |
+
|
7 |
+
const app = express();
|
8 |
+
const port = process.env.PORT || 3000;
|
9 |
+
|
10 |
+
// --- CORS Configuration ---
|
11 |
+
const allowedOrigins = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
|
12 |
+
const corsOptions = {
|
13 |
+
origin: (origin, callback) => {
|
14 |
+
// Allow requests with no origin (like mobile apps or curl) and from allowed origins
|
15 |
+
if (!origin || allowedOrigins.indexOf(origin) !== -1 || allowedOrigins.includes('*')) {
|
16 |
+
callback(null, true);
|
17 |
+
} else {
|
18 |
+
callback(new Error('Not allowed by CORS'));
|
19 |
+
}
|
20 |
+
},
|
21 |
+
};
|
22 |
+
app.use(cors(corsOptions));
|
23 |
+
|
24 |
+
// --- Multer for file uploads ---
|
25 |
+
const upload = multer({
|
26 |
+
storage: multer.memoryStorage(),
|
27 |
+
limits: { fileSize: 5 * 1024 * 1024 }, // 5MB limit
|
28 |
+
});
|
29 |
+
|
30 |
+
// --- API Endpoint ---
|
31 |
+
app.post('/api/decode', upload.single('authImage'), async (req, res) => {
|
32 |
+
const privateKey = process.env.PLUGIN_PRIVATE_KEY;
|
33 |
+
|
34 |
+
if (!privateKey) {
|
35 |
+
console.error("FATAL: PLUGIN_PRIVATE_KEY environment variable is not set.");
|
36 |
+
return res.status(500).json({ success: false, error: 'Server configuration error.' });
|
37 |
+
}
|
38 |
+
|
39 |
+
if (!req.file) {
|
40 |
+
return res.status(400).json({ success: false, error: 'No image file provided. Please upload a file with the key "authImage".' });
|
41 |
+
}
|
42 |
+
|
43 |
+
try {
|
44 |
+
const credentials = await decodeAuthFromImage(req.file.buffer, privateKey);
|
45 |
+
res.json({ success: true, data: credentials });
|
46 |
+
} catch (error) {
|
47 |
+
res.status(400).json({ success: false, error: error.message });
|
48 |
+
}
|
49 |
+
});
|
50 |
+
|
51 |
+
app.listen(port, () => {
|
52 |
+
console.log(`KeyLock Decoder Plugin listening at http://localhost:${port}`);
|
53 |
+
if (allowedOrigins.length > 0) {
|
54 |
+
console.log(`CORS enabled for: ${allowedOrigins.join(', ')}`);
|
55 |
+
}
|
56 |
+
});
|