-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
150 lines (132 loc) · 4.36 KB
/
server.js
File metadata and controls
150 lines (132 loc) · 4.36 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
#!/usr/bin/env node
/**
* Simple HTTP server for Risk.fun Hash Verifier
* Serves static files with proper MIME types and CORS headers
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
// Configuration
const PORT = process.env.PORT || 8000;
const HOST = process.env.HOST || 'localhost';
// MIME types for different file extensions
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.ico': 'image/x-icon',
'.svg': 'image/svg+xml'
};
/**
* Get MIME type for file extension
*/
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
return mimeTypes[ext] || 'application/octet-stream';
}
/**
* Serve static file
*/
function serveFile(filePath, res) {
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
// File not found
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write(`
<!DOCTYPE html>
<html>
<head>
<title>404 - Not Found</title>
<style>
body { font-family: monospace; background: #0d110e; color: white; padding: 40px; text-align: center; }
.error { background: #2a1a1a; border-left: 4px solid #fd5050; padding: 20px; border-radius: 8px; margin: 20px auto; max-width: 600px; }
</style>
</head>
<body>
<div class="error">
<h1>404 - File Not Found</h1>
<p>The requested file <code>${filePath}</code> was not found.</p>
<p><a href="/" style="color: #58ef50;">← Back to Hash Verifier</a></p>
</div>
</body>
</html>
`);
res.end();
} else {
// Server error
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.write('500 - Internal Server Error');
res.end();
}
return;
}
// Set headers
const mimeType = getMimeType(filePath);
res.writeHead(200, {
'Content-Type': mimeType,
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
res.write(content);
res.end();
});
}
/**
* Main server handler
*/
const server = http.createServer((req, res) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
// Parse URL
let filePath = req.url;
// Handle root path
if (filePath === '/') {
filePath = '/index.html';
}
// Remove query parameters
filePath = filePath.split('?')[0];
// Security: prevent directory traversal
filePath = path.normalize(filePath);
if (filePath.includes('..')) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.write('400 - Bad Request');
res.end();
return;
}
// Build full file path
const fullPath = path.join(__dirname, filePath);
// Serve the file
serveFile(fullPath, res);
});
// Start server
server.listen(PORT, HOST, () => {
console.log(`🚀 Risk.fun Hash Verifier Server running at http://${HOST}:${PORT}/`);
console.log('');
console.log('Available endpoints:');
console.log(` • Main Verifier: http://${HOST}:${PORT}/`);
console.log(` • Debug Tool: http://${HOST}:${PORT}/debug-advanced.html`);
console.log(` • SHA3 Test: http://${HOST}:${PORT}/test-sha3.html`);
console.log('');
console.log('Press Ctrl+C to stop the server');
});
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('\n👋 Shutting down server...');
server.close(() => {
console.log('✅ Server stopped');
process.exit(0);
});
});
process.on('SIGTERM', () => {
console.log('\n👋 Shutting down server...');
server.close(() => {
console.log('✅ Server stopped');
process.exit(0);
});
});