2011-03-26 14:52:56 +00:00
|
|
|
/**
|
2011-05-30 14:53:11 +00:00
|
|
|
* This module is started with bin/run.sh. It sets up a Express HTTP and a Socket.IO Server.
|
|
|
|
* Static file Requests are answered directly from this module, Socket.IO messages are passed
|
|
|
|
* to MessageHandler and minfied requests are passed to minified.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 14:26:41 +00:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-03-26 14:52:56 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2011-05-16 14:30:21 +00:00
|
|
|
require('joose');
|
|
|
|
|
2011-08-03 18:31:25 +00:00
|
|
|
var log4js = require('log4js');
|
2011-05-28 17:09:17 +00:00
|
|
|
var socketio = require('socket.io');
|
2011-06-30 19:03:09 +00:00
|
|
|
var fs = require('fs');
|
2011-07-27 17:52:23 +00:00
|
|
|
var settings = require('./utils/Settings');
|
|
|
|
var db = require('./db/DB');
|
2011-05-19 16:36:26 +00:00
|
|
|
var async = require('async');
|
|
|
|
var express = require('express');
|
|
|
|
var path = require('path');
|
2011-07-27 17:52:23 +00:00
|
|
|
var minify = require('./utils/Minify');
|
2011-07-25 15:31:41 +00:00
|
|
|
var formidable = require('formidable');
|
2011-08-03 18:31:25 +00:00
|
|
|
var apiHandler;
|
2011-07-19 18:48:11 +00:00
|
|
|
var exportHandler;
|
2011-07-21 19:13:58 +00:00
|
|
|
var importHandler;
|
2011-07-08 17:33:01 +00:00
|
|
|
var exporthtml;
|
|
|
|
var readOnlyManager;
|
2011-08-04 15:07:58 +00:00
|
|
|
var padManager;
|
2011-08-15 21:20:37 +00:00
|
|
|
var securityManager;
|
2011-08-16 14:53:09 +00:00
|
|
|
var socketIORouter;
|
2011-05-19 16:36:26 +00:00
|
|
|
|
2011-06-30 19:03:09 +00:00
|
|
|
//try to get the git version
|
|
|
|
var version = "";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var ref = fs.readFileSync("../.git/HEAD", "utf-8");
|
|
|
|
var refPath = "../.git/" + ref.substring(5, ref.indexOf("\n"));
|
|
|
|
version = fs.readFileSync(refPath, "utf-8");
|
|
|
|
version = version.substring(0, 8);
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
2011-07-31 17:25:51 +00:00
|
|
|
console.warn("Can't get git version for server header\n" + e.message)
|
2011-06-30 19:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var serverName = "Etherpad-Lite " + version + " (http://j.mp/ep-lite)";
|
|
|
|
|
2011-07-21 19:13:58 +00:00
|
|
|
//cache 6 hours
|
2011-06-30 11:43:11 +00:00
|
|
|
exports.maxAge = 1000*60*60*6;
|
2011-05-19 16:36:26 +00:00
|
|
|
|
2011-05-14 17:57:07 +00:00
|
|
|
async.waterfall([
|
2011-05-19 16:36:26 +00:00
|
|
|
//initalize the database
|
2011-05-14 17:57:07 +00:00
|
|
|
function (callback)
|
|
|
|
{
|
|
|
|
db.init(callback);
|
|
|
|
},
|
2011-05-19 16:36:26 +00:00
|
|
|
//initalize the http server
|
2011-05-14 17:57:07 +00:00
|
|
|
function (callback)
|
2011-03-26 13:10:41 +00:00
|
|
|
{
|
2011-05-19 16:36:26 +00:00
|
|
|
//create server
|
|
|
|
var app = express.createServer();
|
|
|
|
|
2011-07-08 17:33:01 +00:00
|
|
|
//load modules that needs a initalized db
|
2011-07-27 17:52:23 +00:00
|
|
|
readOnlyManager = require("./db/ReadOnlyManager");
|
|
|
|
exporthtml = require("./utils/ExportHtml");
|
|
|
|
exportHandler = require('./handler/ExportHandler');
|
|
|
|
importHandler = require('./handler/ImportHandler');
|
2011-08-03 18:31:25 +00:00
|
|
|
apiHandler = require('./handler/APIHandler');
|
2011-08-04 15:07:58 +00:00
|
|
|
padManager = require('./db/PadManager');
|
2011-08-15 21:20:37 +00:00
|
|
|
securityManager = require('./db/SecurityManager');
|
2011-08-16 14:53:09 +00:00
|
|
|
socketIORouter = require("./handler/SocketIORouter");
|
2011-07-08 17:33:01 +00:00
|
|
|
|
2011-07-31 17:25:51 +00:00
|
|
|
//install logging
|
|
|
|
var httpLogger = log4js.getLogger("http");
|
|
|
|
app.configure(function()
|
|
|
|
{
|
|
|
|
app.use(log4js.connectLogger(httpLogger, { level: log4js.levels.INFO, format: ':status, :method :url'}));
|
2011-08-15 21:20:37 +00:00
|
|
|
app.use(express.cookieParser());
|
2011-07-31 17:25:51 +00:00
|
|
|
});
|
2011-05-19 16:36:26 +00:00
|
|
|
|
|
|
|
//serve static files
|
|
|
|
app.get('/static/*', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
2011-05-28 17:09:17 +00:00
|
|
|
var filePath = path.normalize(__dirname + "/.." + req.url.split("?")[0]);
|
2011-06-30 11:40:37 +00:00
|
|
|
res.sendfile(filePath, { maxAge: exports.maxAge });
|
2011-05-14 17:57:07 +00:00
|
|
|
});
|
2011-05-19 16:36:26 +00:00
|
|
|
|
2011-05-28 17:09:17 +00:00
|
|
|
//serve minified files
|
2011-07-26 16:39:25 +00:00
|
|
|
app.get('/minified/:id', function(req, res, next)
|
2011-05-28 17:09:17 +00:00
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
|
|
|
|
var id = req.params.id;
|
|
|
|
|
2011-07-26 16:39:25 +00:00
|
|
|
if(id == "pad.js" || id == "timeslider.js")
|
2011-05-28 17:09:17 +00:00
|
|
|
{
|
2011-07-26 16:39:25 +00:00
|
|
|
minify.minifyJS(req,res,id);
|
2011-05-28 17:09:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-26 16:39:25 +00:00
|
|
|
next();
|
2011-05-28 17:09:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-07-08 17:33:01 +00:00
|
|
|
//serve read only pad
|
|
|
|
app.get('/ro/:id', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
|
|
|
|
var html;
|
|
|
|
var padId;
|
|
|
|
var pad;
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
//translate the read only pad to a padId
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
readOnlyManager.getPadId(req.params.id, function(err, _padId)
|
|
|
|
{
|
|
|
|
padId = _padId;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//render the html document
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
//return if the there is no padId
|
|
|
|
if(padId == null)
|
|
|
|
{
|
|
|
|
callback("notfound");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//render the html document
|
|
|
|
exporthtml.getPadHTMLDocument(padId, null, false, function(err, _html)
|
|
|
|
{
|
|
|
|
html = _html;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], function(err)
|
|
|
|
{
|
|
|
|
//throw any unexpected error
|
|
|
|
if(err && err != "notfound")
|
|
|
|
throw err;
|
|
|
|
|
|
|
|
if(err == "notfound")
|
|
|
|
res.send('404 - Not Found', 404);
|
|
|
|
else
|
|
|
|
res.send(html);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-08-15 21:20:37 +00:00
|
|
|
//checks for padAccess
|
|
|
|
function hasPadAccess(req, res, callback)
|
|
|
|
{
|
|
|
|
securityManager.checkAccess(req.params.pad, req.cookies.sessionid, req.cookies.token, req.cookies.password, function(err, accessObj)
|
|
|
|
{
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
//there is access, continue
|
|
|
|
if(accessObj.accessStatus == "grant")
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
//no access
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.send("403 - Can't touch this", 403);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-05-19 16:36:26 +00:00
|
|
|
//serve pad.html under /p
|
2011-06-30 11:40:31 +00:00
|
|
|
app.get('/p/:pad', function(req, res, next)
|
2011-07-14 19:11:03 +00:00
|
|
|
{
|
2011-06-30 11:40:31 +00:00
|
|
|
//ensure the padname is valid and the url doesn't end with a /
|
2011-08-04 15:07:58 +00:00
|
|
|
if(!padManager.isValidPadId(req.params.pad) || /\/$/.test(req.url))
|
2011-06-30 11:40:31 +00:00
|
|
|
{
|
2011-08-13 19:20:30 +00:00
|
|
|
res.send('Such a padname is forbidden', 404);
|
2011-06-30 11:40:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-19 16:36:26 +00:00
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/pad.html");
|
2011-06-30 11:40:37 +00:00
|
|
|
res.sendfile(filePath, { maxAge: exports.maxAge });
|
2011-05-19 16:36:26 +00:00
|
|
|
});
|
|
|
|
|
2011-06-20 10:44:04 +00:00
|
|
|
//serve timeslider.html under /p/$padname/timeslider
|
2011-06-30 11:40:31 +00:00
|
|
|
app.get('/p/:pad/timeslider', function(req, res, next)
|
2011-06-20 10:44:04 +00:00
|
|
|
{
|
2011-06-30 11:40:31 +00:00
|
|
|
//ensure the padname is valid and the url doesn't end with a /
|
2011-08-04 15:07:58 +00:00
|
|
|
if(!padManager.isValidPadId(req.params.pad) || /\/$/.test(req.url))
|
2011-06-30 11:40:31 +00:00
|
|
|
{
|
2011-08-13 19:20:30 +00:00
|
|
|
res.send('Such a padname is forbidden', 404);
|
2011-06-30 11:40:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-20 10:44:04 +00:00
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/timeslider.html");
|
2011-06-30 11:40:37 +00:00
|
|
|
res.sendfile(filePath, { maxAge: exports.maxAge });
|
2011-06-20 10:44:04 +00:00
|
|
|
});
|
|
|
|
|
2011-07-19 18:48:11 +00:00
|
|
|
//serve timeslider.html under /p/$padname/timeslider
|
|
|
|
app.get('/p/:pad/export/:type', function(req, res, next)
|
|
|
|
{
|
2011-08-04 15:07:58 +00:00
|
|
|
//ensure the padname is valid and the url doesn't end with a /
|
|
|
|
if(!padManager.isValidPadId(req.params.pad) || /\/$/.test(req.url))
|
|
|
|
{
|
2011-08-13 19:20:30 +00:00
|
|
|
res.send('Such a padname is forbidden', 404);
|
2011-08-04 15:07:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-19 18:48:11 +00:00
|
|
|
var types = ["pdf", "doc", "txt", "html", "odt"];
|
|
|
|
//send a 404 if we don't support this filetype
|
|
|
|
if(types.indexOf(req.params.type) == -1)
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//if abiword is disabled, and this is a format we only support with abiword, output a message
|
|
|
|
if(settings.abiword == null && req.params.type != "html" && req.params.type != "txt" )
|
|
|
|
{
|
|
|
|
res.send("Abiword is not enabled at this Etherpad Lite instance. Set the path to Abiword in settings.json to enable this feature");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-14 13:47:41 +00:00
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
2011-07-19 18:48:11 +00:00
|
|
|
res.header("Server", serverName);
|
2011-08-15 21:20:37 +00:00
|
|
|
|
|
|
|
hasPadAccess(req, res, function()
|
|
|
|
{
|
|
|
|
exportHandler.doExport(req, res, req.params.pad, req.params.type);
|
|
|
|
});
|
2011-07-19 18:48:11 +00:00
|
|
|
});
|
|
|
|
|
2011-07-21 19:13:58 +00:00
|
|
|
//handle import requests
|
|
|
|
app.post('/p/:pad/import', function(req, res, next)
|
|
|
|
{
|
2011-08-04 15:07:58 +00:00
|
|
|
//ensure the padname is valid and the url doesn't end with a /
|
|
|
|
if(!padManager.isValidPadId(req.params.pad) || /\/$/.test(req.url))
|
|
|
|
{
|
2011-08-13 19:20:30 +00:00
|
|
|
res.send('Such a padname is forbidden', 404);
|
2011-08-04 15:07:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-08-13 19:20:30 +00:00
|
|
|
|
2011-07-21 19:13:58 +00:00
|
|
|
//if abiword is disabled, skip handling this request
|
|
|
|
if(settings.abiword == null)
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.header("Server", serverName);
|
2011-08-15 21:20:37 +00:00
|
|
|
|
|
|
|
hasPadAccess(req, res, function()
|
|
|
|
{
|
|
|
|
importHandler.doImport(req, res, req.params.pad);
|
|
|
|
});
|
2011-07-21 19:13:58 +00:00
|
|
|
});
|
|
|
|
|
2011-08-08 16:45:44 +00:00
|
|
|
var apiLogger = log4js.getLogger("API");
|
|
|
|
|
2011-08-03 18:31:25 +00:00
|
|
|
//This is a api call, collect all post informations and pass it to the apiHandler
|
2011-08-08 19:14:01 +00:00
|
|
|
app.get('/api/1/:func', function(req, res)
|
2011-08-03 18:31:25 +00:00
|
|
|
{
|
2011-08-08 16:34:51 +00:00
|
|
|
res.header("Server", serverName);
|
2011-08-16 19:06:59 +00:00
|
|
|
res.header("Content-Type", "application/json");
|
2011-08-08 16:34:51 +00:00
|
|
|
|
2011-08-08 19:14:01 +00:00
|
|
|
apiLogger.info("REQUEST, " + req.params.func + ", " + JSON.stringify(req.query));
|
|
|
|
|
|
|
|
//wrap the send function so we can log the response
|
|
|
|
res._send = res.send;
|
|
|
|
res.send = function(response)
|
2011-08-03 18:31:25 +00:00
|
|
|
{
|
2011-08-08 19:14:01 +00:00
|
|
|
response = JSON.stringify(response);
|
|
|
|
apiLogger.info("RESPONSE, " + req.params.func + ", " + response);
|
|
|
|
res._send(response);
|
2011-08-03 18:31:25 +00:00
|
|
|
}
|
2011-08-08 19:14:01 +00:00
|
|
|
|
|
|
|
//call the api handler
|
|
|
|
apiHandler.handle(req.params.func, req.query, req, res);
|
2011-08-03 18:31:25 +00:00
|
|
|
});
|
|
|
|
|
2011-07-25 15:31:41 +00:00
|
|
|
//The Etherpad client side sends information about how a disconnect happen
|
|
|
|
app.post('/ep/pad/connection-diagnostic-info', function(req, res)
|
|
|
|
{
|
|
|
|
new formidable.IncomingForm().parse(req, function(err, fields, files)
|
|
|
|
{
|
2011-07-31 17:25:51 +00:00
|
|
|
console.log("DIAGNOSTIC-INFO: " + fields.diagnosticInfo);
|
2011-07-25 15:31:41 +00:00
|
|
|
res.end("OK");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-05-19 16:36:26 +00:00
|
|
|
//serve index.html under /
|
|
|
|
app.get('/', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/index.html");
|
2011-06-30 11:40:37 +00:00
|
|
|
res.sendfile(filePath, { maxAge: exports.maxAge });
|
2011-05-19 16:36:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//serve robots.txt
|
|
|
|
app.get('/robots.txt', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/robots.txt");
|
2011-06-30 11:40:37 +00:00
|
|
|
res.sendfile(filePath, { maxAge: exports.maxAge });
|
2011-05-19 16:36:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//serve favicon.ico
|
|
|
|
app.get('/favicon.ico', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/favicon.ico");
|
2011-06-30 11:40:37 +00:00
|
|
|
res.sendfile(filePath, { maxAge: exports.maxAge });
|
2011-05-19 16:36:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//let the server listen
|
2011-07-30 15:39:53 +00:00
|
|
|
app.listen(settings.port, settings.ip);
|
|
|
|
console.log("Server is listening at " + settings.ip + ":" + settings.port);
|
2011-05-14 17:57:07 +00:00
|
|
|
|
2011-05-19 16:36:26 +00:00
|
|
|
//init socket.io and redirect all requests to the MessageHandler
|
|
|
|
var io = socketio.listen(app);
|
2011-06-20 10:44:04 +00:00
|
|
|
|
2011-07-05 17:26:31 +00:00
|
|
|
//this is only a workaround to ensure it works with all browers behind a proxy
|
|
|
|
//we should remove this when the new socket.io version is more stable
|
|
|
|
io.set('transports', ['xhr-polling']);
|
|
|
|
|
2011-07-31 17:25:51 +00:00
|
|
|
var socketIOLogger = log4js.getLogger("socket.io");
|
|
|
|
io.set('logger', {
|
|
|
|
debug: function (str)
|
|
|
|
{
|
|
|
|
//supress debug messages
|
|
|
|
//socketIOLogger.debug(str);
|
|
|
|
},
|
|
|
|
info: function (str)
|
|
|
|
{
|
|
|
|
socketIOLogger.info(str);
|
|
|
|
},
|
|
|
|
warn: function (str)
|
|
|
|
{
|
|
|
|
socketIOLogger.warn(str);
|
|
|
|
},
|
|
|
|
error: function (str)
|
|
|
|
{
|
|
|
|
socketIOLogger.error(str);
|
|
|
|
},
|
|
|
|
});
|
2011-07-07 17:15:39 +00:00
|
|
|
|
2011-07-27 13:46:45 +00:00
|
|
|
//minify socket.io javascript
|
|
|
|
if(settings.minify)
|
|
|
|
io.enable('browser client minification');
|
|
|
|
|
2011-07-27 17:52:23 +00:00
|
|
|
var padMessageHandler = require("./handler/PadMessageHandler");
|
|
|
|
var timesliderMessageHandler = require("./handler/TimesliderMessageHandler");
|
2011-06-20 10:44:04 +00:00
|
|
|
|
|
|
|
//Initalize the Socket.IO Router
|
|
|
|
socketIORouter.setSocketIO(io);
|
|
|
|
socketIORouter.addComponent("pad", padMessageHandler);
|
|
|
|
socketIORouter.addComponent("timeslider", timesliderMessageHandler);
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2011-05-14 17:57:07 +00:00
|
|
|
callback(null);
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
2011-05-14 17:57:07 +00:00
|
|
|
]);
|