-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotmate.js
40 lines (38 loc) · 1.65 KB
/
botmate.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
"use strict";
var phantom = require('phantom'),
http = require('http'),
url = require('url'),
moment = require('moment'),
logger = require('./lib/logger');
phantom.create(function (ph) {
http.createServer(function (req, res) {
var query, targetUrl,
reqMoment = moment();
query = url.parse(req.url, true).query;
if (query.host === undefined && query.fragment === undefined) {
res.writeHead(400, {'Content-Type': 'text/plain'});
res.end("Something is wrong with request arguments. Please read documentation of Botmate.\n", 'utf-8');
return logger.perform('wrong-args', undefined, req);
}
targetUrl = "http://" + query.host + "#!" + query.fragment;
logger.perform('processing', targetUrl, req);
ph.createPage(function (page) {
page.set('onLoadFinished', function (status) {
if (status !== 'success') {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end("Couldn't open desired page\n", 'utf-8');
logger.perform('request-error', targetUrl, req);
return page.close();
}
page.get('content', function (content) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(content, 'utf-8');
logger.perform('success', targetUrl, req, reqMoment);
return page.close();
});
});
page.open(targetUrl);
});
}).listen(3001, '127.0.0.1');
});
console.log("Proxy listening at http://127.0.0.1:3001");