This repository was archived by the owner on Oct 15, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
297 lines (263 loc) · 8.89 KB
/
Copy pathserver.js
File metadata and controls
297 lines (263 loc) · 8.89 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const express = require("express");
const bodyParser = require("body-parser");
const fileUpload = require("express-fileupload");
const fs = require("fs-extra");
const makeDir = require("make-dir");
const path = require("path");
const tempy = require("tempy");
const CDP = require("chrome-remote-interface");
const URL = require("url");
const IPCIDR = require("ip-cidr");
const Address4 = require("ip-address").Address4;
const dns = require("dns");
const PRIVATE_IP_RANGES = [
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
"127.0.0.1/32", // loopback
"169.254.169.254/32", // aws metadata server
];
const cdpHost = process.env.CHROME_HEADLESS_PORT_9222_TCP_ADDR || "localhost";
const cdpPort = process.env.CHROME_HEADLESS_PORT_9222_TCP_PORT || "9222";
function print({
url,
format = "png",
width = 8.5,
height = 11,
delay = 300,
marginBottom = 0,
marginLeft = 0,
marginRight = 0,
marginTop = 0,
userAgent = null,
full = false
}) {
return new Promise((resolve, reject) => {
// Start the Chrome Debugging Protocol
CDP.New({ host: cdpHost, port: cdpPort })
.then(target => CDP({ target, host: cdpHost, port: cdpPort }))
.then(client => {
// Extract used DevTools domains.
const { DOM, Emulation, Network, Page, Runtime } = client;
// Set up viewport resolution, etc.
const deviceMetrics = {
width,
height,
deviceScaleFactor: 0,
mobile: false,
fitWindow: false,
};
// Enable events on domains we are interested in.
Promise.all([
Page.enable(),
DOM.enable(),
Network.enable(),
]).then(() => {
Network.setRequestInterceptionEnabled({ enabled: true })
}).then(() => {
Network.requestIntercepted(data => {
const interceptionId = data.interceptionId;
const request = data.request;
const parsedUrl = URL.parse(request.url);
// set to true if a ip requested is an internal IP.
let internalIp = false;
internalIp = isInternalIp(parsedUrl.hostname);
console.log(`IsInternalIP ${parsedUrl.hostname} = ${internalIp}`)
// set to true if a host being requested is a cluster local host.
let internalHost = false;
// if the requested url is file it is blocked outside this method.
// but we might generate a local url so allow that.
const fileUrl = request.url.indexOf("file://") >= 0 && request.url !== url;
function continueRequestMaybe() {
if (fileUrl || internalIp || internalHost) {
console.log("Blocking URL: " + request.url + " as it is not safe!");
console.log("Reason for blocking: " + request.url + " " + fileUrl + " " + internalIp + " " + internalHost);
Network.continueInterceptedRequest({
interceptionId,
errorReason: "Aborted"
});
} else {
Network.continueInterceptedRequest({
interceptionId,
});
}
}
if (parsedUrl.protocol === "file:") {
continueRequestMaybe();
return;
}
// cluster local domains might not have a "."
if (
parsedUrl.hostname.indexOf(".") <= 0 ||
parsedUrl.hostname.endsWith(".internal") ||
parsedUrl.hostname.endsWith("cluster.local")
) {
internalHost = true;
continueRequestMaybe();
} else {
// try to resolve hostname and check if IP is internal
dns.resolve4(parsedUrl.hostname, (err, addresses) => {
if (err) {
continueRequestMaybe();
return;
}
if (isInternalIp(addresses[0])) {
internalHost = true;
}
continueRequestMaybe();
});
}
})
}).then(() => Emulation.setDeviceMetricsOverride(deviceMetrics)
).then(() => Emulation.setVisibleSize({ width, height })
).then(() => Page.navigate({ url })).catch((e) => reject(e));
// Wait for page load, then print. loadEventFired returns an unsubscribe
// function (not a Promise), so do not chain .catch onto it.
Page.loadEventFired(() => {
setTimeout(() => {
Page.printToPDF({
paperWidth: width,
paperHeight: height,
scale: 1,
displayHeaderFooter: false,
printBackground: true,
marginTop,
marginBottom,
marginLeft,
marginRight,
})
.then(screenshot => {
const buffer = Buffer.from(screenshot.data, "base64");
client.close();
CDP.Close({ id: client.target.id, host: cdpHost, port: cdpPort })
.then(() => resolve(buffer))
.catch(e => reject(e));
})
.catch(e => reject(e));
}, delay);
});
});
});
}
const app = express();
// bodyParser = {
// json: { limit: "50mb", extended: true },
// urlencoded: { limit: "50mb", extended: true }
// };
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use(bodyParser.json({ limit: "50mb", extended: true }));
app.use(fileUpload());
app.get("/", (req, res) => {
res.type("text/plain").send(`Here's a nice curl example of the api:
curl -F "htmlFile=@test.html" -F "width=8.5" -F "height=11" -X POST -H "Content-Type: multipart/form-data" -o result.pdf http://thisurl/
OR
curl -F "url=http://www.google.com" -F "width=8.5" -F "height=11" -X POST -H "Content-Type: multipart/form-data" -o result.pdf http://thisurl/
`);
});
app.post("/", (req, res) => {
const file = req.files && req.files.htmlFile;
const bodyFile = req.body && req.body.htmlFile;
const getIntOrUndefined = name =>
req.body[name] ? parseInt(req.body[name], 10) : undefined;
const width = getIntOrUndefined("width");
const height = getIntOrUndefined("height");
const delay = getIntOrUndefined("delay");
const marginBottom = getIntOrUndefined("marginBottom");
const marginLeft = getIntOrUndefined("marginLeft");
const marginRight = getIntOrUndefined("marginRight");
const marginTop = getIntOrUndefined("marginTop");
let url = req.body.url;
let newPath;
function runPrint() {
console.log(`Printing ${url} with args:
w=${width} h=${height} delay=${delay}
margins: t=${marginTop} r=${marginRight} b=${marginBottom} l=${marginLeft}`);
print({
width,
height,
delay,
url,
marginBottom,
marginLeft,
marginRight,
marginTop
})
.then(data => {
console.log(`SUCCESS Printing ${url}`);
res
.status(200)
.type("application/pdf")
.send(data);
if (newPath) {
fs.remove(newPath);
}
})
.catch(e => {
console.log(`ERROR Printing ${url}`);
console.log(e);
res.status(500).send("some kind of failure");
});
}
if (!file && !url && !bodyFile) {
console.log(`URL / FILE not specified`);
return res
.status(422)
.send("No htmlFile or url sent. One of them is required!");
}
if (file) {
console.log(`FILE specified`);
const tmp = tempy.file({ extension: "html" });
file.mv(tmp, err => {
if (err) {
res.status(500).send("There was an error.");
throw err;
}
newPath = `/printfiles/${tmp.replace(/^.*\/(.*)$/, "$1")}`;
fs.move(tmp, newPath, { overwrite: true }, err => {
if (err) {
console.log(err);
res.status(500).send("There was an error.");
return;
}
url = "file://" + newPath;
runPrint();
});
});
} else if (bodyFile) {
console.log(`file specified in JSON Body`);
const newFile = tempy.file({ extension: "html" });
const newPath = `/printfiles/${newFile.replace(/^.*\/(.*)$/, "$1")}`;
makeDir.sync(path.dirname(newPath));
fs.writeFileSync(newPath, bodyFile);
url = "file://" + newPath;
runPrint();
} else {
console.log(`URL specified ${url}`);
if (url.indexOf("file://") >= 0) {
res.status(422).send("File URL detected");
return;
}
runPrint();
}
});
app.listen(process.env.NODE_PORT || 8888, function () {
console.log("listening to +++", 8888);
});
/** Checks if the IP falls in an Internal CIDR range. */
function isInternalIp(hostname) {
let parsedIp;
try {
parsedIp = new Address4(hostname);
} catch (e) {
return false;
}
if (parsedIp.isValid()) {
for (let i = 0; i < PRIVATE_IP_RANGES.length; i++) {
const cird = new IPCIDR(PRIVATE_IP_RANGES[i]);
if (cird.contains(parsedIp)) {
return true;
}
};
}
return false;
}