-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (67 loc) · 2.21 KB
/
Copy pathindex.js
File metadata and controls
77 lines (67 loc) · 2.21 KB
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
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const cors = require('cors');
const bodyParser = require('body-parser');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const instaService = require('./instaService');
const app = express();
const server = http.createServer(app);
const io = new Server(server, { cors: { origin: '*' } });
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
const upload = multer({ dest: path.join(__dirname, 'uploads') });
io.on('connection', (socket) => {
console.log('socket connected', socket.id);
// forward logs from service to client socket
const listener = (line) => socket.emit('log', line);
instaService.onLog(listener);
socket.on('disconnect', () => {
instaService.offLog(listener);
});
});
app.post('/api/login', async (req, res) => {
const { username, password } = req.body;
try {
await instaService.login(username, password, io);
res.json({ ok: true });
} catch (e) {
console.error(e);
res.status(500).json({ ok: false, error: e.message });
}
});
app.get('/api/threads', async (req, res) => {
try {
const threads = await instaService.getThreads();
res.json({ ok: true, threads });
} catch (e) {
res.status(500).json({ ok: false, error: e.message });
}
});
app.post('/api/start', upload.single('spamfile'), async (req, res) => {
const { threads, delay, mode } = req.body;
const spamFilePath = req.file ? req.file.path : null;
try {
await instaService.startSpam({ threads, delay: Number(delay||1000), spamFilePath, mode });
res.json({ ok: true });
} catch (e) {
res.status(500).json({ ok: false, error: e.message });
}
});
app.post('/api/stop', async (req, res) => {
try {
await instaService.stopSpam();
res.json({ ok: true });
} catch (e) {
res.status(500).json({ ok: false, error: e.message });
}
});
app.get('/api/status', (req,res) => {
res.json({ ok: true, status: instaService.getStatus() });
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log('Server listening on', PORT));