Spaces:
Running
Running
File size: 9,139 Bytes
f6b8770 54271e1 b1376cc 54271e1 b1376cc 54271e1 b1376cc 54271e1 b1376cc 54271e1 b1376cc 54271e1 b1376cc 54271e1 b1376cc 54271e1 b1376cc 54271e1 f6b8770 b1376cc f6b8770 | 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | const express = require('express');
const path = require('path');
const fs = require('fs');
const { spawn } = require('child_process');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
const PORT = Number(process.env.PORT) || 4000;
const ARCHIVES_DIR = path.join(__dirname, '..', 'archives');
const gameProcs = [];
const tunnelProcs = [];
const tunnelUrls = {};
const gamePorts = {};
const useHosting = process.argv.includes('--host') || process.env.HOSTING === 'true' || process.env.HOST === 'true';
function loadEntries() {
const entries = [];
const dirs = fs.readdirSync(ARCHIVES_DIR, { withFileTypes: true });
for (const d of dirs) {
if (!d.isDirectory()) continue;
const metaPath = path.join(ARCHIVES_DIR, d.name, 'archive.json');
if (!fs.existsSync(metaPath)) continue;
try {
const entry = JSON.parse(fs.readFileSync(metaPath, 'utf8'));
entries.push(entry);
} catch (e) {
console.error(`Skipping ${d.name}: ${e.message}`);
}
}
entries.sort((a, b) => new Date(b.date) - new Date(a.date));
return entries;
}
function getEntry(id) {
return loadEntries().find(e => e.id === id);
}
function mapEntryUrl(entry) {
const port = gamePorts[entry.id] || entry.port;
if (port) {
if (useHosting && tunnelUrls[port]) {
return { ...entry, url: tunnelUrls[port] };
}
return { ...entry, url: `/play/${entry.id}` };
}
return entry;
}
function startTunnel(port) {
return new Promise((resolve, reject) => {
console.log(`[tunnel] Starting cloudflared tunnel for port ${port}...`);
const proc = spawn('cloudflared', ['tunnel', '--url', `http://localhost:${port}`]);
let resolved = false;
tunnelProcs.push(proc);
const timer = setTimeout(() => {
if (!resolved) {
resolved = true;
reject(new Error(`cloudflared tunnel for port ${port} timed out after 15 seconds`));
}
}, 15000);
const handleData = (data) => {
const text = data.toString();
const match = text.match(/https:\/\/[a-zA-Z0-9-]+\.trycloudflare\.com/);
if (match && !resolved) {
resolved = true;
clearTimeout(timer);
const url = match[0];
console.log(`[tunnel] Tunnel established for port ${port} -> ${url}`);
resolve(url);
}
};
proc.stdout.on('data', handleData);
proc.stderr.on('data', handleData);
proc.on('error', (err) => {
if (!resolved) {
resolved = true;
clearTimeout(timer);
reject(new Error(`Failed to start cloudflared: ${err.message}`));
}
});
proc.on('exit', (code) => {
if (!resolved) {
resolved = true;
clearTimeout(timer);
reject(new Error(`cloudflared exited with code ${code} before establishing tunnel`));
}
});
});
}
const refererProxies = {};
function getRefererProxy(port) {
if (!refererProxies[port]) {
refererProxies[port] = createProxyMiddleware({
target: `http://localhost:${port}`,
changeOrigin: true,
logLevel: 'silent',
});
}
return refererProxies[port];
}
// 1. Redirect /play/:id to /play/:id/ to ensure proper relative asset resolution and set active_game cookie
app.use((req, res, next) => {
const match = req.path.match(/^\/play\/([^\/]+)/);
if (match) {
const gameId = match[1];
const isSecure = req.secure || req.headers['x-forwarded-proto'] === 'https';
res.cookie('active_game', gameId, {
path: '/',
sameSite: isSecure ? 'none' : 'lax',
secure: isSecure
});
if (req.path === `/play/${gameId}`) {
return res.redirect(301, `${req.path}/`);
}
}
next();
});
// Helper to extract game ID from referer or cookies
function getGameId(req) {
const referer = req.headers.referer || '';
// 1. Try to extract game ID directly from Referer (e.g. /play/rblx/...)
const refMatch = referer.match(/\/play\/([^\/]+)/);
if (refMatch) {
return refMatch[1];
}
// 2. Determine if the referer is the main portal page (path is exactly "/")
let isPortal = false;
if (referer) {
try {
const refUrl = new URL(referer);
if (refUrl.pathname === '/') {
isPortal = true;
}
} catch (e) {
// Ignore URL parsing errors
}
}
// 3. Fallback to cookie if we are not on the portal page
if (!isPortal) {
const cookieHeader = req.headers.cookie || '';
const cookieMatch = cookieHeader.match(/(?:^|; )active_game=([^;]*)/);
if (cookieMatch) {
return cookieMatch[1];
}
}
return null;
}
// 2. Referer/Cookie-based HTTP proxy for absolute paths (e.g. /css/..., /js/..., /api/...)
app.use((req, res, next) => {
if (req.path === '/ws') return next();
if (req.path.startsWith('/api/entries') || req.path.startsWith('/archives') || req.path.startsWith('/play/')) {
return next();
}
const gameId = getGameId(req);
if (gameId) {
const port = gamePorts[gameId];
if (port) {
const proxy = getRefererProxy(port);
return proxy(req, res, next);
}
}
next();
});
// 3. WS Proxy specifically for WebSocket connections to /ws
const wsProxy = createProxyMiddleware({
target: 'http://localhost:3000', // dummy fallback, overridden by router
router: (req) => {
const gameId = getGameId(req);
if (gameId) {
const port = gamePorts[gameId];
if (port) {
return `http://localhost:${port}`;
}
}
return undefined;
},
changeOrigin: true,
ws: true,
logLevel: 'silent',
on: {
error: (err, req, res) => {
console.error('[ws-proxy] error:', err);
}
}
});
app.use('/ws', wsProxy);
app.use('/archives', express.static(ARCHIVES_DIR));
app.use(express.static(path.join(__dirname, '..', 'public')));
app.get('/api/entries', (req, res) => {
res.json(loadEntries().map(mapEntryUrl));
});
app.get('/api/entries/:id', (req, res) => {
const entry = getEntry(req.params.id);
if (!entry) return res.status(404).json({ error: 'not found' });
res.json(mapEntryUrl(entry));
});
function serveIndex(req, res) {
res.sendFile(path.join(__dirname, '..', 'public', 'index.html'));
}
async function startGameServers() {
const entries = loadEntries();
let nextPort = 3001;
const tunnelPromises = [];
for (const entry of entries) {
const gameDir = path.join(ARCHIVES_DIR, entry.id, 'app');
const serverEntry = path.join(gameDir, 'server', 'index.js');
if (!fs.existsSync(serverEntry)) continue;
const port = entry.port || nextPort;
nextPort = Math.max(nextPort, port) + 1;
gamePorts[entry.id] = port;
const proc = spawn('node', ['server/index.js'], {
cwd: gameDir,
stdio: 'pipe',
env: { ...process.env, PORT: String(port) },
});
proc.stdout.on('data', d => process.stdout.write(`[${entry.id}] ${d}`));
proc.stderr.on('data', d => process.stderr.write(`[${entry.id}] ${d}`));
proc.on('error', err => console.error(`[${entry.id}] failed:`, err.message));
proc.on('exit', (code) => {
if (code !== 0) console.error(`[${entry.id}] exited with code ${code}`);
});
gameProcs.push(proc);
app.use(`/play/${entry.id}`, createProxyMiddleware({
target: `http://localhost:${port}`,
changeOrigin: true,
}));
console.log(`[proxy] /play/${entry.id} -> http://localhost:${port}`);
if (useHosting) {
const tunnelPromise = startTunnel(port)
.then(url => {
tunnelUrls[port] = url;
})
.catch(err => {
console.error(`[${entry.id}] Tunnel error: ${err.message}`);
});
tunnelPromises.push(tunnelPromise);
}
}
if (useHosting && tunnelPromises.length > 0) {
console.log('[tunnel] Waiting for all game tunnels to be established...');
await Promise.all(tunnelPromises);
console.log('[tunnel] All game tunnels established!');
}
}
function cleanup() {
for (const proc of gameProcs) proc.kill();
for (const proc of tunnelProcs) proc.kill();
}
function listen(port) {
const srv = app.listen(port);
srv.on('upgrade', wsProxy.upgrade);
srv.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.log(`port ${port} busy, trying ${port + 1}`);
listen(port + 1);
} else {
throw err;
}
});
srv.on('listening', async () => {
const actualPort = srv.address().port;
console.log('');
console.log(` ✦ AI Archives`);
console.log(` ✦ running at http://localhost:${actualPort}`);
if (useHosting) {
try {
const portalUrl = await startTunnel(actualPort);
tunnelUrls[actualPort] = portalUrl;
console.log('');
console.log(` ✦ Public Portal URL: ${portalUrl}`);
console.log('');
} catch (err) {
console.error(` ✦ Failed to start portal tunnel: ${err.message}`);
console.log('');
}
} else {
console.log('');
}
await startGameServers();
app.use(serveIndex);
});
}
process.on('SIGINT', () => { cleanup(); process.exit(); });
process.on('SIGTERM', () => { cleanup(); process.exit(); });
process.on('exit', cleanup);
listen(PORT);
|