File size: 5,023 Bytes
bc93ab9
092eef4
bc93ab9
092eef4
a4ca21f
75e21d4
092eef4
 
c79f84a
87b2f6b
092eef4
 
 
c79f84a
092eef4
 
bc93ab9
092eef4
bc93ab9
092eef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc93ab9
 
 
 
 
 
 
 
 
4896609
 
 
 
bc93ab9
4896609
 
 
 
 
 
 
bc93ab9
4896609
87b2f6b
 
 
 
bc93ab9
4896609
 
 
87b2f6b
4896609
87b2f6b
4896609
87b2f6b
bc93ab9
4896609
 
 
 
bc93ab9
4896609
 
 
 
 
87b2f6b
4896609
 
 
e58c3e0
bc93ab9
4896609
 
 
 
 
 
 
 
 
 
085af66
092eef4
ce0afca
e0106dc
 
 
 
 
 
 
 
 
ce0afca
 
 
 
 
 
 
 
 
 
17da645
ce0afca
17da645
ce0afca
 
 
 
e0106dc
ce0afca
17da645
e0106dc
 
 
 
 
 
 
 
 
 
 
267b874
 
87b2f6b
8a56370
 
87b2f6b
e088b2c
8a56370
543ab97
c79f84a
0e33816
8a56370
c79f84a
ce0afca
 
 
 
 
87b2f6b
c79f84a
 
 
 
 
8a56370
 
 
 
0e33816
1e29a3b
c79f84a
 
 
 
267b874
 
 
b5616c6
092eef4
 
 
 
a4ca21f
75e21d4
092eef4
 
 
 
a4ca21f
 
 
 
b5616c6
a4ca21f
 
 
b5616c6
a4ca21f
 
 
 
 
092eef4
a4ca21f
 
 
092eef4
 
 
 
a4ca21f
092eef4
 
 
 
 
a4ca21f
092eef4
 
 
 
 
75e21d4
092eef4
a4ca21f
092eef4
 
 
 
 
 
 
 
 
 
b5616c6
092eef4
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { format as formatBytes } from 'bytes'
import express from 'express'
import { fileTypeFromBuffer } from 'file-type'
import morgan from 'morgan'
import pg from 'puppeteer-ghost'
import * as prb from 'puppeteer-real-browser'
import serveIndex from 'serve-index'

import { spawn } from 'node:child_process'
import { promises as fsp } from 'node:fs'
import { tmpdir } from 'node:os'
import { env } from 'node:process'
import { setTimeout } from 'node:timers/promises'
import { format } from 'node:util'

const app = express()
app.enable('trust proxy')

const limitSize = '1000mb'
app.set('json spaces', 2)
app.use(express.json({ limit: limitSize }))
app.use(express.urlencoded({
  extended: true, 
  limit: limitSize
}))
app.use(morgan('dev'))

const tmpDir = tmpdir()
app.use(
  tmpDir,
  express.static(tmpDir),
  serveIndex(
    tmpDir,
    { hidden: true, icons: true }
  )
)

const isBase64 = (str) => {
  try {
    return btoa(atob(str)) === str
  } catch {
    return false
  }
}

app.all('/', async (req, res) => {
  try {
    if (req.method !== 'POST') return res
      .status(405)
      .json({ msg: 'Hello World' })

    const { file } = req.body
    if (!isBase64(file)) return res
      .status(400)
      .json({
        err: true,
        msg: 'Bad Request'
      })

    const buffer = Buffer.from(file, 'base64')
    const {
      mime = 'application/octet-stream',
      ext = 'bin'
    } = await fileTypeFromBuffer(buffer) || {}

    const name = format(
      '%s/%s-%s.%s',
      tmpDir,
      mime.split('/')[0],
      Math.random().toString(36).slice(2),
      ext
    )
    await fsp.writeFile(name, buffer)

    const bytes = buffer.length
    res.json({
      name: name.split('/').pop(),
      size: {
        bytes,
        readable: formatBytes(
          bytes,
          { unitSeparator: ' ' }
        ),
      },
      type: { mime, ext },
      url: format(
        '%s://%s',
        req.protocol,
        req.host + name
      )
    })
  } catch (e) {
    console.error(e)
    res
      .status(500)
      .json({
        err: true,
        msg: format(e?.message || e)
      })
  }  
})

app.get('/dl', async (req, res) => {
  const { url } = req.query
  if (!url) return res.redirect('/')
  const api = format(
    'https://ochinpo-helper.hf.space/mediafire?url=%s',
     url 
  )
  try {
    const json = await (await fetch(api)).json()
    const { result } = json
    const target = format(
      '%s/%s',
      tmpDir,
      result.filename
    )
    const cmds = [
      '/shell?cmd=wget', '%s', '-O', '"%s"',
      '--header="cookie: %s"','&&',
      'unzip', '"%s"', '-d', '%s'
    ]
    const route = format(
      cmds.join(' '),
      result.download,
      target,
      result.cookie,
      target,
      target.replace('.zip', '')
    )
    res.set('user-agent', env.UA)
    res.redirect(route)
  } catch (e) {
    console.error(e)
    res
      .status(500)
      .json({
        err: true,
        msg: format(e?.message || e)
      })
  }
})

app.get(
  '/shell',
  (req, res, next) =>
    req.get('user-agent') != env.UA ?
    res.redirect('/') :
    next(),
  async (req, res) => {
    req.query.cmd ||= 'w'
    const [cmd, ...args] = req
      .query
      .cmd
      .trim()
      .split(' ')
    const file = format(
      '%s/%s.log',
      tmpDir,
      Date.now()
    )
    const log = await fsp.open(file, 'w+')
    const child = spawn(
      cmd,
      args,
      {
        detached: true,
        env: Object.assign(
          env,
          { FORCE_COLOR: '0' }
        ),
        shell: true,
        stdio: ['ignore', log, log]
      }
    )
    child.unref()
    res.redirect(file)
  }
)

const executablePath = env.CHROME_BIN
app.get('/ss', async (req, res) => {
  const {
    delay = 0,
    full = false,
    mode = 'ghost',
    transparent = false,
    url = 'https://example.com'
  } = req.query
  if (String(url).includes('whatsapp.com'))
    return res.redirect('/')

  const isGhost = /^ghost$/i.test(mode)
  const ctx = isGhost ?
    await pg.launch({
      executablePath,
      headless: 'new'
    }) :
    await prb.connect({
      customConfig: { executablePath },
      disableXvfb: true,
      headless: 'new',
      turnstile: true
    })

  try {
    const page = isGhost ?
      await ctx.newPage() :
      ctx.page
    await page.goto(
      url,
      { waitUntil: 'networkidle0'}
    )

    const name = format(
      '%s/%s.png',
      tmpDir,
      Math.random().toString(36).slice(2)
    )

    if (/^\d$/.test(delay) && delay > 0)
      await setTimeout(+delay)
    await page.screenshot({
      fullPage: full,
      path: name,
      omitBackground: transparent
    })

    res.redirect(name)
  } catch (e) {
    console.error(e)
    res
      .status(500)
      .json({
        err: true,
        msg: format(e?.message || e)
      })
  } finally {
    await (isGhost ? ctx : ctx.browser).close()
  }
})

const PORT = env.SERVER_PORT || env.PORT || 7860
app.listen(
  PORT,
  () => console.log('App running on port', PORT)
)