forked from john-doherty/node-iframe-replacement
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·151 lines (121 loc) · 4.17 KB
/
server.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
var path = require('path'),
express = require('express'),
exphbs = require('express-handlebars'),
iframeReplacement = require('../index.js');
var http = require('http'),
https = require('https');
var fs = require('fs');
var bodyParser = require('body-parser');
var request = require('request');
function Server() {
if (process.argv.length != 6) {
console.log('Not enough argumets (require 5): node example/server.js <SERVER_NAME:localhost> <SERVER_PORT:12345> <TARGET_SERVER_URL:http://localhost:8001> <ENABLE_HTTPS:TRUE:FALSE>');
process.exit();
}
process.env.SERVER_NAME = process.argv[2];
process.env.SERVER_PORT = process.argv[3];
process.env.TARGET_SERVER_URL = process.argv[4];
process.env.ENABLE_HTTPS = process.argv[5];
var enableHttps = process.env.ENABLE_HTTPS == 'TRUE' ? true : false;
// create an instance of express
var app = express();
// add iframe replacement to express as middleware (adds res.merge method)
app.use(iframeReplacement);
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({
extended: true
})); // for parsing application/x-www-form-urlencoded
// add handlebars view engine (you can use any)
app.engine('hbs', exphbs());
// let express know how to locate the views/templates
app.set('views', path.resolve(__dirname, 'views'));
app.set('view engine', 'hbs');
// create simple route to test our fake news
app.get('/proxy', function(req, res) {
// respond to this request with our fake-new content embedded within the BBC News home page
res.merge('fake-drag-drop-bar', {
sourceUrl: req.query.url, // external url to fetch
sourcePlaceholder: 'div[id="fake-collection-bar"]' // css selector to inject our content into
});
});
app.post('/proxy', function(req, res) {
var headers = {
'X-Requested-With': 'XMLHttpRequest'
};
console.log(req.body);
var options = {
url: req.query.url,
method: 'POST',
headers: headers,
body: JSON.stringify(req.body)
};
request(options, function(err, response, body) {
res.end(body);
});
});
app.post('/redirect', function(req, res) {
res.redirect(301, req.query.url);
});
// create simple route to test our fake news
app.get('/load', function(req, res) {
var filePath = '../config/' + req.query.file;
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
contentType = 'image/jpg';
break;
case '.wav':
contentType = 'audio/wav';
break;
}
fs.readFile(path.join(__dirname, filePath), function(error, content) {
if (error) {
if (error.code == 'ENOENT') {
fs.readFile('./404.html', function(error, content) {
res.writeHead(200, {
'Content-Type': contentType
});
res.end(content, 'utf-8');
});
} else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
res.end();
}
} else {
res.writeHead(200, {
'Content-Type': contentType
});
res.end(content, 'utf-8');
}
});
});
// start the server
if (enableHttps) {
var options = {
key: fs.readFileSync('/home/node/ssl/server-self.key'),
cert: fs.readFileSync('/home/node/ssl/server-self.crt')
};
https.createServer(options, app).listen(process.env.SERVER_PORT, function() {
console.log('Server running... Visit https://' + process.env.SERVER_NAME + ':' + process.env.SERVER_PORT + ' in your browser');
});
} else {
http.createServer(app).listen(process.env.SERVER_PORT, function() {
console.log('Server running... Visit http://' + process.env.SERVER_NAME + ':' + process.env.SERVER_PORT + ' in your browser');
});
}
}
module.exports = new Server();