-
Notifications
You must be signed in to change notification settings - Fork 9
/
servercore.js
129 lines (92 loc) · 3.47 KB
/
servercore.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
var http = require("http");
var url = require("url");
var fs = require("fs");
var path = require('path');
var ana = require("./analyzer");
var S = require('string');
var twitter = require("./twitter");
var moment = require("moment");
var config = "test";
var mimeTypes = {
"html": "text/html",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"js": "text/javascript",
"css": "text/css"};
/**
* retrive the correct path for the stored html data
* @param config
* @param openShiftDataDir
* @returns {string}
*/
function getHtmlPath(config, openShiftDataDir) {
var openShiftDataDir = process.env.OPENSHIFT_DATA_DIR;
var configuredHtmlPath = config.html;
if (openShiftDataDir != undefined)
{
configuredHtmlPath = openShiftDataDir + '/html/';
}
else
{
configuredHtmlPath = config.html;
}
return configuredHtmlPath;
}
function start(configName) {
config = require(configName);
function onRequest(request, response) {
/* set the correct content type */
response.writeHead(200, {"Content-Type": "text/html"});
var query = request.url;
var ip = request.connection.remoteAddress;
// get data directory for OpenShift (easy check, if we run in Openshift)
var openShiftDataDir = process.env.OPENSHIFT_DATA_DIR;
var logPath = "/var/log/nodepot.log";
var configuredHtmlPath = getHtmlPath(config, openShiftDataDir);
if (openShiftDataDir != undefined)
{
logPath = openShiftDataDir + "/log/nodepot.log"
}
// admin check (query and IP range)
if ((query == "/admin" || query == "admin") && (S(ip).contains("127.0.0.1") || S(ip).contains(config.home_ip)))
{
// show UI
var defaultTemplateStart = fs.readFileSync(configuredHtmlPath + '/adminstart.html', 'utf8');
var learnedStuff = fs.readFileSync(logPath, 'utf8');
var defaultTemplateEnd = fs.readFileSync(configuredHtmlPath + '/adminend.html', 'utf8');
response.write(defaultTemplateStart);
response.write(learnedStuff);
response.write(defaultTemplateEnd);
}
else
{ /* default analyze case */
/* check if the request contains an .. code */
statusAnalyze = ana.analyze(request, response, config);
var defaultTemplate = fs.readFileSync(configuredHtmlPath + '/demo.html', 'utf8');
var learnedStuff = fs.readFileSync(configuredHtmlPath + '/dork.html', 'utf8');
response.write(defaultTemplate);
response.write(learnedStuff);
if (config.twitter.verbose == "yes" && statusAnalyze != null)
{
twitter.tweet(moment().format('MMMM Do YYYY, h:mm:ss a') + "(Nodepot): Found request from ip " + ip, config);
}
}
response.end();
}
var openShiftIP = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT;
if (port != undefined) {
console.log(moment().format('MMMM Do YYYY, h:mm:ss a') + " Server tries to start on port " + port);
}
if (openShiftIP != undefined)
{
http.createServer(onRequest).listen(process.env.OPENSHIFT_NODEJS_PORT, openShiftIP);
}
else
{
http.createServer(onRequest).listen(config.port);
}
console.log(moment().format('MMMM Do YYYY, h:mm:ss a') + " Server has started.");
}
exports.start = start;