-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (82 loc) · 2.87 KB
/
index.js
File metadata and controls
96 lines (82 loc) · 2.87 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
"use strict";
var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/firefox');
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var app = express();
var fs = require('fs');
var port = 5000;
app.use(bodyParser.json());
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build();
var currentUrl = '';
var interceptorCode = fs.readFileSync('./interceptor.js', 'utf8');
function execJs(code) {
return driver.executeScript(code)
.then(function(res){
return res;
})
.catch(function(err){
console.dir('EXECJS ERR: ' + err);
return null;
});
}
function goToUrl(url) {
if (!url || url === currentUrl) return Promise.resolve(false);
return driver.get(url)
.then(function() {return; })
}
function getResource(url, res) {
console.dir('resource')
console.dir(url);
res.set('host', currentUrl)
res.set('referer', currentUrl)
return new Promise(function(resolve, reject){
request({url: url, encoding:null, headers:{referer:currentUrl}}, function(err, ress, body){
if (err) return reject(err);
body = body.toString().replace('</body>', interceptorCode.toString()+'</body>');
body = body.toString().replace(/((src|href)\s*=\s*("|'))(\/{1}\w+)/ig,"$1http://" + currentUrl + "$4")
body = body.toString().replace(/((action)\s*=\s*("|'))(\/{1}\w+)/ig, "$1/http://" + currentUrl + "$4")
return resolve(body);
})
});
}
app.post('/event', function(req, res, next){
execJs("return window.scrollMaxY")
.then(function(height){
return execJs("window.scrollTo(0, " + ((req.body.scroll*height)/100) + ")");
})
.then(function(){
return res.sendStatus(200)
})
.catch(function(err){
console.dir('ERR: ' + err);
})
});
app.get('/http://:url([A-z0-9]+\.[A-z0-9]+\.[A-z0-9]+)/:res(*)*', (req, res, next) => {
console.dir('hit!')
console.dir(req.params)
console.dir(req.originalUrl)
if (req.params.url !== currentUrl) {
currentUrl = req.params.url
}
return Promise.race([goToUrl(req.originalUrl.substr(1)), getResource(req.originalUrl.substr(1), res)])
.then(function(body){return res.status(200).send(body);})
.catch(function(err) { next(err); });
})
app.get('/*', function(req, res, next) {
console.dir('getResource')
console.dir(req.originalUrl)
return getResource('http://' + currentUrl + req.originalUrl, res)
.then(function(body){return res.status(200).send(body);})
.catch(function(err) { next(err); });
});
app.use(function(err, req, res, next) {
console.dir(err);
return res.status(400).send(err);
});
app.listen(port, function() {
console.log('Express running: ' + port);
});