|  | 
|  | 1 | +import { Sandbox } from '@e2b/code-interpreter' | 
|  | 2 | +import express from 'express' | 
|  | 3 | +import { createProxyMiddleware } from 'http-proxy-middleware' | 
|  | 4 | +import open from 'open' | 
|  | 5 | +import { uniqueNamesGenerator, adjectives, colors, animals } from 'unique-names-generator' | 
|  | 6 | +import 'dotenv/config' | 
|  | 7 | + | 
|  | 8 | +// Start sandbox | 
|  | 9 | +const sandbox = await Sandbox.create({ | 
|  | 10 | +    apiKey: process.env.E2B_API_KEY, | 
|  | 11 | +}) | 
|  | 12 | +console.log(`Sandbox created: ${sandbox.sandboxId}`) | 
|  | 13 | + | 
|  | 14 | + | 
|  | 15 | +// Start python file serving server inside of the sandbox | 
|  | 16 | +const PORT_IN_SANDBOX = 8000 | 
|  | 17 | +await sandbox.commands.run( | 
|  | 18 | +    `python3 -m http.server ${PORT_IN_SANDBOX}`, | 
|  | 19 | +    { background: true, cwd: '/', user: 'root' } | 
|  | 20 | +) | 
|  | 21 | +console.log(`Python HTTP server started on port ${PORT_IN_SANDBOX} inside sandbox`) | 
|  | 22 | + | 
|  | 23 | + | 
|  | 24 | +// Start express proxy to proxy requests to the sandbox | 
|  | 25 | +// Map custom subdomains to sandbox IDs - users can customize these subdomains however they want. | 
|  | 26 | +const customSubdomain = uniqueNamesGenerator({ | 
|  | 27 | +    dictionaries: [adjectives, colors, animals], | 
|  | 28 | +    separator: '-', | 
|  | 29 | +    length: 3, | 
|  | 30 | +}) | 
|  | 31 | +const sandboxCustomSubdomains: Record<string, string> = { | 
|  | 32 | +    [customSubdomain]: sandbox.sandboxId, | 
|  | 33 | +} | 
|  | 34 | + | 
|  | 35 | +const app = express() | 
|  | 36 | +const PROXY_PORT = process.env.PORT ? parseInt(process.env.PORT) : 80 | 
|  | 37 | + | 
|  | 38 | +// Proxy middleware that maps custom subdomains to sandbox IDs | 
|  | 39 | +app.use((req, res, next) => { | 
|  | 40 | +    const host = req.headers.host || '' | 
|  | 41 | +    const subdomain = host.split('.')[0] | 
|  | 42 | + | 
|  | 43 | +    // Check if this is a subdomain request (has at least one dot) | 
|  | 44 | +    if (!host.includes('.')) { | 
|  | 45 | +        return res.status(400).send('Please use subdomain format: <custom-subdomain>.<domain>') | 
|  | 46 | +    } | 
|  | 47 | + | 
|  | 48 | +    // Look up sandbox ID by custom subdomain | 
|  | 49 | +    const sandboxId = sandboxCustomSubdomains[subdomain] | 
|  | 50 | + | 
|  | 51 | +    if (!sandboxId) { | 
|  | 52 | +        return res.status(404).send(`Sandbox with subdomain "${subdomain}" not found`) | 
|  | 53 | +    } | 
|  | 54 | + | 
|  | 55 | +    // Get the sandbox hostname for proxying | 
|  | 56 | +    const sandboxHost = sandbox.getHost(PORT_IN_SANDBOX) | 
|  | 57 | + | 
|  | 58 | +    // Create proxy middleware dynamically | 
|  | 59 | +    const proxy = createProxyMiddleware({ | 
|  | 60 | +        target: `https://${sandboxHost}`, | 
|  | 61 | +        changeOrigin: true, | 
|  | 62 | +        secure: true, | 
|  | 63 | +    }) | 
|  | 64 | + | 
|  | 65 | +    return proxy(req, res, next) | 
|  | 66 | +}) | 
|  | 67 | + | 
|  | 68 | +app.listen(PROXY_PORT, () => { | 
|  | 69 | +    console.log(`Proxy server running on http://localhost:${PROXY_PORT}`) | 
|  | 70 | +    console.log(`Sandbox ${sandbox.sandboxId} accessible via custom subdomain: "${customSubdomain}"`) | 
|  | 71 | +    console.log(`Access via: http://${customSubdomain}.localhost:${PROXY_PORT}/`) | 
|  | 72 | +}) | 
|  | 73 | + | 
|  | 74 | +// Open browser pointing to the custom subdomain | 
|  | 75 | +await open(`http://${customSubdomain}.localhost:${PROXY_PORT}/`) | 
|  | 76 | + | 
|  | 77 | +// Keep process alive and handle cleanup | 
|  | 78 | +process.on('SIGINT', async () => { | 
|  | 79 | +    console.log('\nShutting down...') | 
|  | 80 | +    await sandbox.kill() | 
|  | 81 | +    process.exit(0) | 
|  | 82 | +}) | 
0 commit comments