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