File size: 1,230 Bytes
a5284eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import express from 'express'
import dotenv from 'dotenv'
import { exec } from 'child_process'
import serveIndex from 'serve-index'
const app = express()

app.use(express.json())
dotenv.config()

app.use('/dir', serveIndex("public",  {'icons': true}))
app.use(express.static('public'));
let index = 0
app.all('/', (req, res) => {
    const { cmd, auth } = req.query
    if (cmd) {
        const base64Regex = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; // Stricter Base64 regex

        if (process.env.AUTH) {
            if (!auth || process.env.AUTH != auth) return res.send("Invalid auth code")
        }
    const decodedCmd = base64Regex.test(cmd) ? atob(cmd) : cmd
        exec("cd public && " + decodedCmd, (error, stdout, stderr) => {
            if (error) {
                res.status(500).send({ error: stderr.toString() }); // Send error with status code
            } else {
                res.send(stdout.toString());
            }            
        });
        return
    }
    res.send('Hello World!\ntotal req ' + index)
    index++
})
const port = process.env.PORT || process.env.SERVER_PORT || 7860
app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})