-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
231 lines (183 loc) · 5.61 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var http = require('http');
var url = require('url');
var fs = require('fs');
var moment = require('moment');
var qs = require('querystring');
var config = require('./config');
// FrontDoor by JeromSar (@JeromSar)
var server = http.createServer();
var blockedIps = [];
var serverIps = [];
var logs = [];
var log = function (message, level) {
level = level || "INFO";
var time = moment().format("DD MMM HH:mm:ss");
var entry = time + " " + level + " - " + message;
console.log(entry);
logs[logs.length] = entry;
};
var contains = function(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
};
var getResponse = function(req) {
if (req.headers['x-forwarded-for']) {
var ip = req.headers['x-forwarded-for'].split(", ")[0] || req.connection.remoteAddress;
} else {
var ip = req.connection.remoteAddress;
}
for (var i in blockedIps) {
if (blockedIps[i] == ip) {
return "false";
}
};
return "true";
};
var redirect = function(res, url) {
res.writeHead(301, {'Content-Type': 'text/html', "Location": url});
res.end();
};
server.on('request', function (req, res) {
var uri = url.parse(req.url, true);
if (uri.pathname == '/favicon.ico') { // Eww favicon!
res.writeHead(404, {'Content-Type': 'image/x-icon'} );
res.end();
return;
}
if (req.headers['x-forwarded-for']) {
var ip = req.headers['x-forwarded-for'].split(", ")[0] || req.connection.remoteAddress;
} else {
var ip = req.connection.remoteAddress;
}
if (uri.pathname == '/admin') {
if (req.method != "POST") { // Plain admin page
html(req, res, "admin.html");
return;
}
// Below this line: admin page with status update
var buffer = "";
req.on('data', function (chunk) {
buffer += chunk;
});
req.on('end', function () {
var params = qs.parse(buffer);
if (!params.pass || params.pass != config.admin_pass) {
params.msg = "Incorrect password";
return html(req, res, "admin.html", params);
}
if (params.logs) {
return html(req, res, "logs.html", params);
}
if (params.clear) {
log(ip + " Cleared the logs");
serverIps = [];
var newLogs = [];
newLogs[0] = logs[0]; // Preserve port info
newLogs[1] = logs[logs.length - 1]; // Preserve log clearer
logs = newLogs;
params.msg = "Logs cleared";
return html(req, res, "logs.html", params);
}
if (!params.add && !params.remove) {
return html(req, res, "admin.html", params);
}
if (!params.ip || params.ip == "") {
params.msg = "Invalid IP";
return html(req, res, "admin.html", params);
}
if (params.add) {
if (params.ip == "64.34.165.5") {
log(ip + " Attempted to add forbidden IP!", "WARN");
params.msg = "Forbidden IP!";
return html(req, res, "admin.html", params);
}
for(var i = blockedIps.length - 1; i >= 0; i--) {
if(blockedIps[i] == params.ip) {
params.msg = "IP already added";
return html(req, res, "admin.html", params);
}
}
blockedIps[blockedIps.length] = params.ip;
log(ip + " Added IP: " + params.ip);
params.msg = "IP added";
return html(req, res, "admin.html", params);
} else if (params.remove) {
var original = blockedIps.length;
for(var i = blockedIps.length - 1; i >= 0; i--) {
if(blockedIps[i] == params.ip) {
blockedIps.splice(i, 1);
}
}
if (original != blockedIps.length) {
log(ip + " Removed IP: " + params.ip);
params.msg = "IP Removed";
} else {
params.msg = "IP not found";
}
}
html(req, res, "admin.html", params);
});
return;
}
if (!contains(serverIps, ip)) {
var query = url.parse(req.url, true).query;
if (query.port) {
query.version = query.version || "ersion unknown";
if (query.name) {
query.name = " - (" + query.name + ")";
} else {
query.name = "";
}
log("New TFM Server: (V" + query.version + ") " + ip + ":" + query.port + query.name);
serverIps[serverIps.length] = ip;
}
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(getResponse(req));
});
var html = function(req, res, name, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile("./" + name, "binary", function(err, content) {
if (err || !content) {
res.end("Oh noes! Something went wrong!");
return;
}
data = data || {};
var formattedBlockedIps = formatList(blockedIps);
var formattedLogs = formatList(logs);
var pass = "";
var msg = "<br />";
if (req.method == "POST" && data.pass) {
pass = data.pass;
}
if (data.msg) {
msg = "<b>" + data.msg + "</b>";
}
res.end(content.toString()
.replace("{ips}", formattedBlockedIps)
.replace("{logs}", formattedLogs)
.replace("{pass}", pass)
.replace("{msg}", msg)
);
});
};
var formatList = function(list) {
var result = "<ul>";
for (var i in list) {
result += "<li>" + list[i] + "</li>";
}
if (result == "<ul>") {
result += "<li>None</li>";
}
return result + "</ul>";
};
var main = function() {
server.listen(config.http_port, function () {
log("Server listening on port " + config.http_port);
});
};
main();