import express from 'express' import morgan from 'morgan' import * as pup from 'puppeteer-real-browser' import serveIndex from 'serve-index' import { exec } from 'node:child_process' import { tmpdir } from 'node:os' import { setTimeout } from 'node:timers/promises' import { format, promisify } from 'node:util' const app = express() const execPromise = promisify(exec) app.set('json spaces', 2) app.use(express.json({ limit: '200mb' })) app.use(express.urlencoded({ extended: true, limit: '200mb' })) app.use(morgan('dev')) const tmpDir = tmpdir() app.use( tmpDir, express.static(tmpDir), serveIndex( tmpDir, { hidden: true, icons: true } ) ) app.all( '/', (_, res) => res.json({ msg: 'Hello World' }) ) app.get('/shell', async (req, res) => { const { q = 'w' } = req.query let o, output = '' try { o = await execPromise(q) } catch (e) { o = e } finally { const { stderr, stdout } = o if (stderr) output += `STDERR:\n${stderr}\n` if (stdout) output += `\nSTDOUT:\n${stdout}` res.send(output.trim()) } }) app.get('/ss', async (req, res) => { const { delay = 0, full = false, url = 'https://hf.co' } = req.query const conn = await pup.connect({ customConfig: { executablePath: process.env.CHROME_BIN }, // disableXvfb: true, headless: 'new', turnstile: true }) try { const page = conn.page await page.goto( url, { waitUntil: 'networkidle0'} ) const name = format( '%s/%s.png', tmpDir, Math.random().toString(36).slice(2) ) if (/^\d$/.test(delay)) await setTimeout(+delay) await page.screenshot({ fullPage: full, path: name, omitBackground: true }) res.redirect(name) } catch (e) { console.error(e) res .status(500) .json({ err: true, msg: format(e?.message || e) }) } finally { await conn.browser.close() } }) const PORT = process.env.PORT || 7860 app.listen( PORT, () => console.log('App running on port', PORT) )