-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpastebin.js
84 lines (79 loc) · 1.96 KB
/
pastebin.js
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
const https = require('https')
const httpsReq = (options, data = '') =>
new Promise((resolve, reject) => {
var req = https.request(options, res => {
var data = ''
res.on('error', err => {
reject(err)
})
res.on('data', d => {
data += d
})
res.on('end', () => {
resolve({
data: data,
status: res.statusCode,
headers: res.headers
})
})
})
req.write(data)
req.on('error', err => {
reject(err)
})
req.on('timeout', () => {
reject(new Error('Timeout'))
})
req.end()
})
let cookie = null
let csrf = null
const getCsrf = async () => {
if (cookie) return
const res = await httpsReq('https://pastebin.com')
cookie = res.headers['set-cookie'][0]
csrf = res.data.match(/<meta name="csrf-token" content="(?<csrf>.+)">/).groups.csrf
}
const makeFormData = (dict, boundary) => {
let out = ''
for (let [key, value] of Object.entries(dict)) {
out += `--${boundary}\n`
out += `Content-Disposition: form-data; name="${key}"\n\n`
out += value
out += '\n'
}
out += '--' + boundary + '--\n'
return out
}
const post = async (title, content) => {
await getCsrf()
const boundary = '-'.repeat(20) + Math.floor(Math.random() * 10 ** 15)
formData = makeFormData(
{
'_csrf-frontend': csrf,
'PostForm[text]': content,
'PostForm[format]': '1',
'PostForm[expiration]': 'N',
'PostForm[status]': '0',
'PostForm[is_password_enabled]': '0',
'PostForm[is_burn]': '0',
'PostForm[name]': title
},
boundary
)
const res = await httpsReq(
{
method: 'POST',
host: 'pastebin.com',
path: '/',
headers: {
cookie: cookie,
'content-type': `multipart/form-data; boundary=${boundary}`
}
},
formData
)
if (res.status !== 302) throw new Error('Unexpected status ' + res.status)
return res.headers.location
}
module.exports = { post }