aripbae commited on
Commit
c79f84a
·
verified ·
1 Parent(s): 720a7b9

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +37 -26
index.js CHANGED
@@ -6,12 +6,13 @@ import pg from 'puppeteer-ghost'
6
  import * as prb from 'puppeteer-real-browser'
7
  import serveIndex from 'serve-index'
8
 
9
- import { exec } from 'node:child_process'
 
10
  import { writeFile } from 'node:fs/promises'
11
  import { tmpdir } from 'node:os'
12
  import { env } from 'node:process'
13
  import { setTimeout } from 'node:timers/promises'
14
- import { format, promisify } from 'node:util'
15
 
16
  const app = express()
17
  app.enable('trust proxy')
@@ -101,33 +102,43 @@ app.all('/', async (req, res) => {
101
  }
102
  })
103
 
104
- const execPromise = promisify(exec)
105
  app.get(
106
  '/shell',
107
- //(req, res, next) =>
108
- // req.get('user-agent') != env.UA ?
109
- // res.redirect('/') :
110
- // next(),
 
 
 
 
 
111
  async (req, res) => {
112
- const { prompt = 'w' } = req.query
113
- let o
114
- try {
115
- o = await execPromise(prompt)
116
- } catch (e) {
117
- console.error(e)
118
- o = e
119
- } finally {
120
- const parse = (str) => str
121
- .replace(/\n/g, '<br>')
122
- .trim()
123
- res.send(
124
- format(
125
- 'StdErr:<br>%s<br>StdOut:<br>%s',
126
- parse(o.stderr),
127
- parse(o.stdout)
128
- )
129
- )
130
- }
 
 
 
 
 
131
  }
132
  )
133
 
 
6
  import * as prb from 'puppeteer-real-browser'
7
  import serveIndex from 'serve-index'
8
 
9
+ import { spawn } from 'node:child_process'
10
+ import { createWriteStream } from 'node:fs'
11
  import { writeFile } from 'node:fs/promises'
12
  import { tmpdir } from 'node:os'
13
  import { env } from 'node:process'
14
  import { setTimeout } from 'node:timers/promises'
15
+ import { format } from 'node:util'
16
 
17
  const app = express()
18
  app.enable('trust proxy')
 
102
  }
103
  })
104
 
105
+ //const execPromise = promisify(exec)
106
  app.get(
107
  '/shell',
108
+ (req, res, next) => {
109
+ console.log(
110
+ req.headers,
111
+ req.get('user-agent')
112
+ )
113
+ //req.get('user-agent') != env.UA ?
114
+ //res.redirect('/') :
115
+ next()
116
+ },
117
  async (req, res) => {
118
+ const [cmd = 'w', ...args] = req
119
+ .query
120
+ .cmd
121
+ .trim()
122
+ .split(' ')
123
+ const file = format(
124
+ '%s/%s.log'
125
+ tmpDir,
126
+ Date.now()
127
+ )
128
+ const log = createWriteStream(
129
+ file,
130
+ { flags: 'a' }
131
+ )
132
+ const child = spawn(
133
+ cmd,
134
+ args,
135
+ {
136
+ detached: true,
137
+ stdio: ['ignore', log, log]
138
+ }
139
+ )
140
+ child.unref()
141
+ res.redirect(file)
142
  }
143
  )
144