-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
232 lines (202 loc) · 5.55 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
232
/*
Copyright 2014 Penn Manor School District, Andrew Lobos, Benjamin Thomas, Alex Lagunas
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var fs = require("fs"),
scheduler = require("node-schedule"),
urlParser = require("url"),
async = require("async"),
cheerio = require("cheerio"),
express = require("express"),
bodyParser = require('body-parser'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
app.set('view engine', 'ejs');
io.set('log level', 1); // Stop socket.io debug output
process.chdir(__dirname); // Change working directory to server.js's directory
config = JSON.parse(fs.readFileSync("config.json")); // Load config.json
var log = new Array();
// Configure node-schedule to reset links and files
var rule = new scheduler.RecurrenceRule();
rule.hour = 23;
rule.minute = 0;
var resetJob = scheduler.scheduleJob(rule, function()
{
log = new Array();
fs.readdir("uploads", function (err, files)
{
if ( err )
{
console.log("Failed to remove uploads: "+err);
return;
}
for ( var i = 0; i < files.length; i++ )
{
if ( files[i] != "index.html")
{
fs.unlink("uploads/"+files[i], function(err)
{
if ( err )
console.log("Removing upload \"uploads/"+files[i]+"\" failed: "+err);
});
}
}
});
console.log("Files and links reset");
});
// Configure express
app.use(express.static("frontend"));
app.use(bodyParser());
app.use("/uploads", express.static("uploads"));
// Handler used by file uploads
app.post("/uploadHandler", function(req,res)
{
if ( !req.param("room") || !req.param("username") )
{
res.send("NO");
return;
}
uploadedFile = req.files.file;
if ( !uploadedFile )
{
res.send("You didn't send a file!");
return;
}
if ( uploadedFile.type != "application/pdf" && uploadedFile.type != "image/jpeg" && uploadedFile.type != "image/png" && uploadedFile.type != "image/jpg" )
{
res.send("Bad file type");
return;
}
if ( uploadedFile.size > 4194304 )
{
res.send("Invalid Size");
return;
}
fs.readFile(uploadedFile.path, function(err, data)
{
var fsName = (Math.floor(new Date().getTime()/1000))+uploadedFile.name;
var fTitle = req.param("title");
if ( !fTitle )
fTitle = uploadedFile.name;
fs.writeFile("uploads/"+fsName, data, function(err)
{
f = {type:"file", username: req.param("username"), fileName: uploadedFile.name, fsFileName: fsName, title: fTitle, room: req.param("room")};
log.push(f);
console.log(req.connection.remoteAddress+"("+req.param("username")+") uploaded "+uploadedFile.name+ " to "+req.param("room"));
io.sockets.emit("file", f);
});
});
res.send("OK");
});
app.get("/iplist/:room", function(req,res)
{
clients = io.sockets.clients();
ipAddrs = new Array();
async.each(clients, function(c, cb)
{
c.get("room", function(err, room)
{
if ( room == req.param("room") )
{
ipAddrs.push(c.handshake.address.address);
cb();
}
else
cb();
});
},
function(err)
{
if ( err )
res.send("Error "+err);
else
{
res.render("iplist", {room: req.param("room"), list: ipAddrs});
}
});
});
io.on("connection", function(socket)
{
// Event for "resyncing" a client with the room's history
socket.on("resync", function(data)
{
socket.room = data.room;
for( var i = 0; i < log.length; i++ )
{
if ( data.room == log[i].room )
socket.emit(log[i].type, log[i]);
}
});
// Event for submitting a link
socket.on("link", function(data)
{
if ( !data.room || !data.username )
return;
data.type = "link";
data.linkTitle = data.url;
console.log(socket.handshake.address.address+"("+data.username+") posted "+data.url+" in "+data.room);
// If the URL is http ( not SSL ), attempt to connect and pull the data from the page's <title>
if ( config.autoTitle && data.url.split(":")[0] == "http" )
{
url = urlParser.parse(data.url);
var options = {
host: url.hostname,
path: url.pathname,
port: 80,
method: "GET",
headers: {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
}
};
http.get(options, function(res) {
var html = "";
res.on("data", function(chunk)
{
html += chunk;
});
res.on("end", function()
{
contentType = res.headers['content-type'];
title = false;
if ( contentType == "text/html" )
{
try
{
$ = cheerio.load(html);
title = $("title").html();
} catch(e) {}
}
if ( title )
data.linkTitle = title;
log.push(data);
io.sockets.emit("link", data);
});
}).on('error', function(e) {
log.push(data);
io.sockets.emit("link", data);
});
}
else // Otherwise, send it out with the URL as the title
{
log.push(data);
io.sockets.emit("link", data);
}
});
});
// Bind HTTP server, and switch to unprivileged user
server.listen(config.HTTPport);
if ( config.changeUser )
{
process.setgid(config.group);
process.setuid(config.user);
}