Repository navigation
Expand file tree
/
Copy pathdev-https.js
More file actions
214 lines (183 loc) · 5.58 KB
/
Copy pathdev-https.js
File metadata and controls
214 lines (183 loc) · 5.58 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
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
#!/usr/bin/env node
import https from 'https';
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PORT = 8443;
const HOST = 'localhost';
// Paths for certificate files
const CERT_DIR = path.join(__dirname, '.ssl');
const KEY_PATH = path.join(CERT_DIR, 'key.pem');
const CERT_PATH = path.join(CERT_DIR, 'cert.pem');
/**
* Generate self-signed certificate using OpenSSL
*/
function generateCertificate() {
if (!fs.existsSync(CERT_DIR)) {
fs.mkdirSync(CERT_DIR);
}
// Generate self-signed certificate valid for 365 days
try {
execSync(
`openssl req -x509 -newkey rsa:2048 -nodes ` +
`-keyout "${KEY_PATH}" -out "${CERT_PATH}" -days 365 ` +
`-subj "/C=US/ST=Dev/L=Dev/O=MatrixVTT/CN=localhost"`,
{ stdio: 'inherit' }
);
} catch {
console.error('[HTTPS] Failed to generate certificate. Make sure OpenSSL is installed.');
console.error('[HTTPS] Install with: sudo apt install openssl (Linux) or brew install openssl (Mac)');
process.exit(1);
}
}
/**
* Ensure certificate exists
*/
function ensureCertificate() {
if (!fs.existsSync(KEY_PATH) || !fs.existsSync(CERT_PATH)) {
generateCertificate();
}
}
/**
* Get MIME type for file
*/
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
const mimeTypes = {
'.html': 'text/html',
'.js': 'application/javascript',
'.mjs': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.woff': 'font/woff',
'.woff2': 'font/woff2'
};
return mimeTypes[ext] || 'application/octet-stream';
}
/**
* Serve static files
*/
function serveStaticFile(req, res) {
let filePath = req.url === '/' ? '/index-production.html' : req.url;
filePath = filePath.split('?')[0];
// Construct absolute path
const absolutePath = path.join(__dirname, filePath);
// Security check: ensure path is within project directory
if (!absolutePath.startsWith(__dirname)) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.end('403 Forbidden');
return;
}
fs.stat(absolutePath, (err, stats) => {
if (err || !stats.isFile()) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
return;
}
// Read and serve file
fs.readFile(absolutePath, (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('500 Internal Server Error');
console.error(`[500] ${req.url}:`, err);
return;
}
const mimeType = getMimeType(absolutePath);
// For HTML files, inject cache-busting timestamp
let responseData = data;
if (mimeType === 'text/html') {
const timestamp = Date.now();
responseData = data.toString()
.replace(/src="([^"]+\.js)"/g, `src="$1?v=${timestamp}"`)
.replace(/src='([^']+\.js)'/g, `src='$1?v=${timestamp}'`);
}
// Set CORS headers for widget embedding with aggressive cache-busting
res.writeHead(200, {
'Content-Type': mimeType,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
'Pragma': 'no-cache',
'Expires': '0'
});
res.end(responseData);
});
});
}
/**
* Handle log message from widget
*/
function handleLogMessage(logData) {
const { prefix, message, args } = logData;
const timestamp = new Date().toISOString().substr(11, 12);
// Also write to log file
const logFilePath = path.join(__dirname, 'widget.log');
const plainTextLog = `${timestamp} [${prefix}] ${message}${args && args.length > 0 ? ' ' + JSON.stringify(args) : ''}\n`;
fs.appendFileSync(logFilePath, plainTextLog);
}
/**
* Start HTTPS server
*/
function startServer() {
ensureCertificate();
const options = {
key: fs.readFileSync(KEY_PATH),
cert: fs.readFileSync(CERT_PATH)
};
const server = https.createServer(options, (req, res) => {
if (req.method === 'OPTIONS') {
res.writeHead(204, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
});
res.end();
return;
}
if (req.method === 'POST' && req.url === '/log') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
try {
const logData = JSON.parse(body);
handleLogMessage(logData);
} catch (error) {
console.error('[LOG] Failed to parse log message:', error);
}
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
});
res.end(JSON.stringify({ success: true }));
});
return;
}
serveStaticFile(req, res);
});
server.listen(PORT, HOST, () => {
});
server.on('error', (error) => {
if (error.code === 'EADDRINUSE') {
console.error(`[ERROR] Port ${PORT} is already in use`);
console.error(` Try: lsof -ti:${PORT} | xargs kill`);
} else {
console.error('[ERROR]', error);
}
process.exit(1);
});
}
process.on('SIGINT', () => {
process.exit(0);
});
startServer();