-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (154 loc) · 4.09 KB
/
index.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
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
const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const fs = require('fs');
const express = require('express');
// @ts-ignore
const allowedIPs = require('./allowedIPs.json');
const port = 8090;
const timeout = 600000;
const { until, By, logging } = webdriver;
const app = express();
app.use(express.json());
let driver;
setTimeout(() => {
driver = buildDriver();
console.info('Driver ready');
}, 5000);
app.post('/generate', async (req, res) => {
const requestor =
req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log(requestor);
console.log(req.body);
if (
!allowedIPs.some(ip => {
return requestor.endsWith(ip);
})
) {
console.log("Denied by IP filter (" + requestor + ")");
res.status(450).send("You're not supposed to be here.");
return;
}
const data = await takeScreenshot(req.body.url, req.body);
const img = new Buffer(data, 'base64');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
res.end(img);
});
app.listen(port, () => {
console.log('Screenshot server open on port ' + port);
});
async function takeScreenshot(
url,
{ width = undefined, height = undefined, delay = 0 } = {}
) {
const driver = await buildDriver();
if (width > 0 && height > 0) {
await driver
.manage()
.window()
.setRect({
x: +width,
y: +height + 126,
height: +height + 126,
width: +width
});
}
await driver.get(url);
await sleep(delay);
const body = await driver.findElement(By.tagName('body'));
const data = await body.takeScreenshot();
driver.close();
return data;
//const base64Data = data.replace(/^data:image\/png;base64,/, "");
fs.writeFile('out.png', base64Data, 'base64');
}
app.post('/blindtask', async (req, res) => {
const requestor =
req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log(requestor);
console.log(req.body);
if (
!allowedIPs.some(ip => {
return requestor.endsWith(ip);
})
) {
console.log("Denied by IP filter (" + requestor + ")");
res.status(450).send("You're not supposed to be here.");
return;
}
const result = await blindtask(req.body);
res.send(result);
});
async function blindtask({ url = '', delay = 0 } = {}) {
const driver = await buildDriver()
await driver.get(url);
await sleep(delay);
let logs = await driver
.manage()
.logs()
.get(logging.Type.PERFORMANCE);
let response = '';
const log = logs
.filter(entry => {
if (entry.message.search('Network.requestWillBeSent') === -1) {
return false;
}
return true;
})
.map(entry => {
if (entry.message) {
entry.message = JSON.parse(entry.message);
}
return entry;
})
.find(entry => {
// return true
if (
entry.message &&
entry.message.message &&
entry.message.message.params &&
entry.message.message.params.request
) {
const url = entry.message.message.params.request.url;
// console.log(entry.message)
if (
url &&
(url.indexOf('clientsavecountriesmaxminforbeam') > -1 ||
url.indexOf('clientsavecitiesgainsforbeam') > -1)
) {
response = '{"savedAt":"' + url + '"}';
return true;
}
return false;
}
});
driver.close();
return response;
}
function sleep(time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, +time);
});
}
function buildDriver() {
const options = new chrome.Options();
const logging_prefs = new webdriver.logging.Preferences();
logging_prefs.setLevel(
webdriver.logging.Type.PERFORMANCE,
webdriver.logging.Level.ALL
);
options.setLoggingPrefs(logging_prefs);
const driver = new webdriver.Builder()
.forBrowser('chrome')
.usingServer('http://standalone-chrome:4444/wd/hub')
.withCapabilities(options)
.build();
driver
.manage()
.setTimeouts({ implicit: timeout, pageLoad: timeout, script: timeout });
return driver;
}