-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
52 lines (44 loc) · 1.47 KB
/
server.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
const http = require('http')
const { readdir } = require('fs')
const { promisify } = require('util')
const { send, json } = require('micro')
const {
router,
get,
post
} = require('microrouter')
const readdirPromise = promisify(readdir)
const Spammer = require('./lib/spammer')
const spammer = new Spammer()
const spam = async (req, res) => {
const { strategy = 'Facebook', user, pass, data } = await json(req)
const strategies = await readdirPromise('./strategies/')
if (!strategies.includes(strategy)) {
return send(res, 400, { status: 400, body: `${strategy} strategy not found :(` })
}
console.log('* Request received to', user)
await spammer.apply(require(`./strategies/${strategy}/index`), { user, pass })
spammer.run(data)
send(res, 200, { status: 200, body: 'All messages have been received and are being processed :)' })
}
const root = (req, res) => send(res, 200, { status: 200, body: 'ok' })
const notfound = (req, res) => send(res, 404, 'Not found route')
const Routes = router(
post('/spam', spam),
get('/', root),
get('/*', notfound)
)
http
.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
if (req.method === 'OPTIONS') {
res.writeHead(200)
res.end()
return
}
Routes(req, res)
})
.listen(process.env.PORT || 3000)
console.log('* Server running..')