Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
101 lines (85 loc) · 2.91 KB
/
Copy pathserver.js
File metadata and controls
101 lines (85 loc) · 2.91 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
/**
* GhostMeta - Zero-Trace Metadata & AI Provenance Eraser
* Local Web Server (Node.js built-in HTTP)
*/
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PORT = parseInt(process.env.PORT, 10) || 8780;
const HOST = process.env.HOST || 'localhost';
const MIME_TYPES = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'application/javascript; charset=utf-8',
'.mjs': 'application/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.webp': 'image/webp',
'.gif': 'image/gif',
'.svg': 'image/svg+xml; charset=utf-8',
'.ico': 'image/x-icon',
'.pdf': 'application/pdf',
'.mp4': 'video/mp4',
'.mp3': 'audio/mpeg',
'.wav': 'audio/wav',
'.flac': 'audio/flac',
'.ogg': 'audio/ogg',
'.txt': 'text/plain; charset=utf-8'
};
const server = http.createServer((req, res) => {
// CORS & Security headers
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('Access-Control-Allow-Origin', '*');
if (req.method !== 'GET' && req.method !== 'HEAD') {
res.writeHead(405, { 'Content-Type': 'text/plain' });
return res.end('Method Not Allowed');
}
let reqPath = decodeURIComponent(req.url.split('?')[0]);
if (reqPath === '/' || reqPath === '') {
reqPath = '/index.html';
}
const safePath = path.normalize(reqPath).replace(/^(\.\.[/\\])+/, '');
let filePath = path.join(__dirname, safePath);
// If path is a directory, serve index.html
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
filePath = path.join(filePath, 'index.html');
}
fs.stat(filePath, (err, stats) => {
if (err || !stats.isFile()) {
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
return res.end('404 Not Found: ' + reqPath);
}
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
res.writeHead(200, {
'Content-Type': contentType,
'Content-Length': stats.size,
'Cache-Control': 'no-cache'
});
if (req.method === 'HEAD') {
return res.end();
}
const stream = fs.createReadStream(filePath);
stream.pipe(res);
stream.on('error', (streamErr) => {
if (!res.headersSent) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
}
res.end('Internal Server Error');
});
});
});
server.listen(PORT, HOST, () => {
console.log('='.repeat(56));
console.log(' GhostMeta — Zero-Trace Metadata & Provenance Eraser');
console.log(` Web app running at: http://${HOST}:${PORT}`);
console.log(' Press Ctrl+C to stop');
console.log('='.repeat(56));
});
export default server;