-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (72 loc) · 2.9 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
const express = require('express');
const axios = require('axios');
const mime = require('mime');
const morgan = require('morgan');
const { URL } = require('url');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 3000;
let lastProtoHost;
app.use(cors());
app.use(morgan('tiny'));
const regex = /\s+(href|src)=['"](.*?)['"]/g;
const getMimeType = url => {
if (url.indexOf('?') !== -1) { // remove url query so we can have a clean extension
url = url.split("?")[0];
}
if (mime.getType(url) === 'application/x-msdownload') return 'text/html';
return mime.getType(url) || 'text/html'; // if there is no extension return as html
};
app.get('/', (req, res) => {
const { url } = req.query; // get url parameter
if (!url) {
res.type('text/html');
return res.end("You need to specify <code>url</code> query parameter");
}
axios.get(url, { responseType: 'arraybuffer' }) // set response type array buffer to access raw data
.then(({ data }) => {
const urlMime = getMimeType(url); // get mime type of the requested url
if (urlMime === 'text/html') { // replace links only in html
data = data.toString().replace(regex, (match, p1, p2) => {
let newUrl = '';
if (p2.indexOf('http') !== -1) {
newUrl = p2;
} else if (p2.substr(0, 2) === '//') {
newUrl = 'http:' + p2;
} else {
const searchURL = new URL(url);
let protoHost = searchURL.protocol + '//' + searchURL.host;
newUrl = protoHost + p2;
if (lastProtoHost != protoHost) {
lastProtoHost = protoHost;
console.log(`Using '${protoHost}' as base for new requests.`);
}
}
return ` ${p1}="https://${req.hostname}?url=${newUrl}"`;
});
}
res.type(urlMime);
res.send(data);
}).catch(error => {
console.log(error);
res.status(500);
res.end("Error")
});
});
app.get('/*', (req, res) => {
if (!lastProtoHost) {
res.type('text/html');
return res.end("You need to specify <code>url</code> query parameter first");
}
const url = lastProtoHost + req.originalUrl;
axios.get(url, { responseType: 'arraybuffer' }) // set response type array buffer to access raw data
.then(({ data }) => {
const urlMime = getMimeType(url); // get mime type of the requested url
res.type(urlMime);
res.send(data);
}).catch(error => {
res.status(501);
res.end("Not Implemented")
});
});
app.listen(port, () => console.log(`Listening on port ${port}!`));