2017-03-08 10:45:51 +00:00
|
|
|
// app
|
|
|
|
// external modules
|
|
|
|
var express = require('express')
|
|
|
|
var toobusy = require('toobusy-js')
|
|
|
|
var ejs = require('ejs')
|
|
|
|
var passport = require('passport')
|
|
|
|
var methodOverride = require('method-override')
|
|
|
|
var cookieParser = require('cookie-parser')
|
|
|
|
var bodyParser = require('body-parser')
|
2015-05-04 07:53:29 +00:00
|
|
|
var compression = require('compression')
|
2017-03-08 10:45:51 +00:00
|
|
|
var session = require('express-session')
|
|
|
|
var SequelizeStore = require('connect-session-sequelize')(session.Store)
|
|
|
|
var fs = require('fs')
|
|
|
|
var url = require('url')
|
|
|
|
var path = require('path')
|
|
|
|
var imgur = require('imgur')
|
|
|
|
var formidable = require('formidable')
|
|
|
|
var morgan = require('morgan')
|
|
|
|
var passportSocketIo = require('passport.socketio')
|
|
|
|
var helmet = require('helmet')
|
|
|
|
var i18n = require('i18n')
|
|
|
|
var flash = require('connect-flash')
|
|
|
|
var validator = require('validator')
|
|
|
|
|
|
|
|
// core
|
|
|
|
var config = require('./lib/config.js')
|
|
|
|
var logger = require('./lib/logger.js')
|
|
|
|
var auth = require('./lib/auth.js')
|
|
|
|
var response = require('./lib/response.js')
|
|
|
|
var models = require('./lib/models')
|
|
|
|
|
|
|
|
// server setup
|
|
|
|
var app = express()
|
|
|
|
var server = null
|
2015-05-15 04:58:13 +00:00
|
|
|
if (config.usessl) {
|
2017-03-08 10:45:51 +00:00
|
|
|
var ca = (function () {
|
|
|
|
var i, len, results
|
|
|
|
results = []
|
|
|
|
for (i = 0, len = config.sslcapath.length; i < len; i++) {
|
|
|
|
results.push(fs.readFileSync(config.sslcapath[i], 'utf8'))
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
})()
|
|
|
|
var options = {
|
|
|
|
key: fs.readFileSync(config.sslkeypath, 'utf8'),
|
|
|
|
cert: fs.readFileSync(config.sslcertpath, 'utf8'),
|
|
|
|
ca: ca,
|
|
|
|
dhparam: fs.readFileSync(config.dhparampath, 'utf8'),
|
|
|
|
requestCert: false,
|
|
|
|
rejectUnauthorized: false
|
|
|
|
}
|
|
|
|
server = require('https').createServer(options, app)
|
2015-05-15 04:58:13 +00:00
|
|
|
} else {
|
2017-03-08 10:45:51 +00:00
|
|
|
server = require('http').createServer(app)
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// logger
|
2015-06-01 10:04:25 +00:00
|
|
|
app.use(morgan('combined', {
|
2017-03-08 10:45:51 +00:00
|
|
|
'stream': logger.stream
|
|
|
|
}))
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// socket io
|
|
|
|
var io = require('socket.io')(server)
|
2016-11-18 04:18:29 +00:00
|
|
|
io.engine.ws = new (require('uws').Server)({
|
2017-03-08 10:45:51 +00:00
|
|
|
noServer: true,
|
|
|
|
perMessageDeflate: false
|
|
|
|
})
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// others
|
|
|
|
var realtime = require('./lib/realtime.js')
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// assign socket io to realtime
|
|
|
|
realtime.io = io
|
2015-09-24 03:36:41 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// methodOverride
|
|
|
|
app.use(methodOverride('_method'))
|
2015-05-04 07:53:29 +00:00
|
|
|
|
|
|
|
// create application/x-www-form-urlencoded parser
|
|
|
|
var urlencodedParser = bodyParser.urlencoded({
|
2017-03-08 10:45:51 +00:00
|
|
|
extended: false,
|
|
|
|
limit: 1024 * 1024 * 10 // 10 mb
|
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// session store
|
2016-04-20 10:03:55 +00:00
|
|
|
var sessionStore = new SequelizeStore({
|
2017-03-08 10:45:51 +00:00
|
|
|
db: models.sequelize
|
|
|
|
})
|
2015-06-01 10:04:25 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// compression
|
|
|
|
app.use(compression())
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2016-03-15 02:41:49 +00:00
|
|
|
// use hsts to tell https users stick to this
|
|
|
|
app.use(helmet.hsts({
|
2017-03-08 10:45:51 +00:00
|
|
|
maxAge: 31536000 * 1000, // 365 days
|
|
|
|
includeSubdomains: true,
|
|
|
|
preload: true
|
|
|
|
}))
|
2016-03-15 02:41:49 +00:00
|
|
|
|
2016-08-19 03:49:24 +00:00
|
|
|
i18n.configure({
|
2017-03-08 10:45:51 +00:00
|
|
|
locales: ['en', 'zh', 'fr', 'de', 'ja', 'es', 'el', 'pt', 'it', 'tr', 'ru', 'nl', 'hr', 'pl', 'uk', 'hi', 'sv', 'eo'],
|
|
|
|
cookie: 'locale',
|
|
|
|
directory: path.join(__dirname, '/locales')
|
|
|
|
})
|
2016-08-19 03:49:24 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
app.use(cookieParser())
|
2016-08-19 03:49:24 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
app.use(i18n.init)
|
2016-08-19 03:49:24 +00:00
|
|
|
|
2016-04-20 10:14:28 +00:00
|
|
|
// routes without sessions
|
|
|
|
// static files
|
2017-03-08 10:45:51 +00:00
|
|
|
app.use('/', express.static(path.join(__dirname, '/public'), { maxAge: config.staticcachetime }))
|
2016-04-20 10:14:28 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// session
|
2015-05-04 07:53:29 +00:00
|
|
|
app.use(session({
|
2017-03-08 10:45:51 +00:00
|
|
|
name: config.sessionname,
|
|
|
|
secret: config.sessionsecret,
|
|
|
|
resave: false, // don't save session if unmodified
|
|
|
|
saveUninitialized: true, // always create session to ensure the origin
|
|
|
|
rolling: true, // reset maxAge on every response
|
|
|
|
cookie: {
|
|
|
|
maxAge: config.sessionlife
|
|
|
|
},
|
|
|
|
store: sessionStore
|
|
|
|
}))
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2016-03-15 02:42:07 +00:00
|
|
|
// session resumption
|
2017-03-08 10:45:51 +00:00
|
|
|
var tlsSessionStore = {}
|
2016-03-15 02:42:07 +00:00
|
|
|
server.on('newSession', function (id, data, cb) {
|
2017-03-08 10:45:51 +00:00
|
|
|
tlsSessionStore[id.toString('hex')] = data
|
|
|
|
cb()
|
|
|
|
})
|
2016-03-15 02:42:07 +00:00
|
|
|
server.on('resumeSession', function (id, cb) {
|
2017-03-08 10:45:51 +00:00
|
|
|
cb(null, tlsSessionStore[id.toString('hex')] || null)
|
|
|
|
})
|
2016-03-15 02:42:07 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// middleware which blocks requests when we're too busy
|
2015-05-04 07:53:29 +00:00
|
|
|
app.use(function (req, res, next) {
|
2017-03-08 10:45:51 +00:00
|
|
|
if (toobusy()) {
|
|
|
|
response.errorServiceUnavailable(res)
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
app.use(flash())
|
2016-12-01 17:58:14 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// passport
|
|
|
|
app.use(passport.initialize())
|
|
|
|
app.use(passport.session())
|
|
|
|
auth.registerAuthMethod()
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// serialize and deserialize
|
2015-05-04 07:53:29 +00:00
|
|
|
passport.serializeUser(function (user, done) {
|
2017-03-08 10:45:51 +00:00
|
|
|
logger.info('serializeUser: ' + user.id)
|
|
|
|
return done(null, user.id)
|
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
passport.deserializeUser(function (id, done) {
|
2017-03-08 10:45:51 +00:00
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
logger.info('deserializeUser: ' + user.id)
|
|
|
|
return done(null, user)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return done(err, null)
|
|
|
|
})
|
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2016-12-03 06:37:24 +00:00
|
|
|
// check uri is valid before going further
|
2017-03-08 10:45:51 +00:00
|
|
|
app.use(function (req, res, next) {
|
|
|
|
try {
|
|
|
|
decodeURIComponent(req.path)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorBadRequest(res)
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
})
|
2016-12-03 06:37:24 +00:00
|
|
|
|
2016-12-12 02:50:43 +00:00
|
|
|
// redirect url without trailing slashes
|
2017-03-08 10:45:51 +00:00
|
|
|
app.use(function (req, res, next) {
|
|
|
|
if (req.method === 'GET' && req.path.substr(-1) === '/' && req.path.length > 1) {
|
|
|
|
var query = req.url.slice(req.path.length)
|
|
|
|
var urlpath = req.path.slice(0, -1)
|
|
|
|
var serverurl = config.serverurl
|
|
|
|
if (config.urlpath) serverurl = serverurl.slice(0, -(config.urlpath.length + 1))
|
|
|
|
res.redirect(301, serverurl + urlpath + query)
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
})
|
2016-04-20 10:19:11 +00:00
|
|
|
|
2016-04-20 10:14:28 +00:00
|
|
|
// routes need sessions
|
2017-03-08 10:45:51 +00:00
|
|
|
// template files
|
|
|
|
app.set('views', path.join(__dirname, '/public/views'))
|
|
|
|
// set render engine
|
|
|
|
app.engine('ejs', ejs.renderFile)
|
|
|
|
// set view engine
|
|
|
|
app.set('view engine', 'ejs')
|
|
|
|
// get index
|
|
|
|
app.get('/', response.showIndex)
|
|
|
|
// get 403 forbidden
|
|
|
|
app.get('/403', function (req, res) {
|
|
|
|
response.errorForbidden(res)
|
|
|
|
})
|
|
|
|
// get 404 not found
|
|
|
|
app.get('/404', function (req, res) {
|
|
|
|
response.errorNotFound(res)
|
|
|
|
})
|
|
|
|
// get 500 internal error
|
|
|
|
app.get('/500', function (req, res) {
|
|
|
|
response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
// get status
|
|
|
|
app.get('/status', function (req, res, next) {
|
|
|
|
realtime.getStatus(function (data) {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
|
|
|
})
|
|
|
|
res.send(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// get status
|
|
|
|
app.get('/temp', function (req, res) {
|
|
|
|
var host = req.get('host')
|
|
|
|
if (config.alloworigin.indexOf(host) === -1) {
|
|
|
|
response.errorForbidden(res)
|
|
|
|
} else {
|
|
|
|
var tempid = req.query.tempid
|
|
|
|
if (!tempid) {
|
|
|
|
response.errorForbidden(res)
|
|
|
|
} else {
|
|
|
|
models.Temp.findOne({
|
|
|
|
where: {
|
|
|
|
id: tempid
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
}).then(function (temp) {
|
|
|
|
if (!temp) {
|
|
|
|
response.errorNotFound(res)
|
|
|
|
} else {
|
|
|
|
res.header('Access-Control-Allow-Origin', '*')
|
|
|
|
res.send({
|
|
|
|
temp: temp.data
|
|
|
|
})
|
|
|
|
temp.destroy().catch(function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('remove temp failed: ' + err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// post status
|
|
|
|
app.post('/temp', urlencodedParser, function (req, res) {
|
|
|
|
var host = req.get('host')
|
|
|
|
if (config.alloworigin.indexOf(host) === -1) {
|
|
|
|
response.errorForbidden(res)
|
|
|
|
} else {
|
|
|
|
var data = req.body.data
|
|
|
|
if (!data) {
|
|
|
|
response.errorForbidden(res)
|
|
|
|
} else {
|
|
|
|
if (config.debug) {
|
|
|
|
logger.info('SERVER received temp from [' + host + ']: ' + req.body.data)
|
|
|
|
}
|
|
|
|
models.Temp.create({
|
|
|
|
data: data
|
|
|
|
}).then(function (temp) {
|
|
|
|
if (temp) {
|
|
|
|
res.header('Access-Control-Allow-Origin', '*')
|
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
id: temp.id
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
response.errorInternalError(res)
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|
|
|
|
})
|
2016-07-31 16:06:07 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function setReturnToFromReferer (req) {
|
|
|
|
var referer = req.get('referer')
|
|
|
|
if (!req.session) req.session = {}
|
|
|
|
req.session.returnTo = referer
|
2016-07-31 16:06:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// facebook auth
|
2016-04-20 10:03:55 +00:00
|
|
|
if (config.facebook) {
|
2017-03-08 10:45:51 +00:00
|
|
|
app.get('/auth/facebook', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('facebook')(req, res, next)
|
|
|
|
})
|
|
|
|
// facebook auth callback
|
|
|
|
app.get('/auth/facebook/callback',
|
2016-04-20 10:03:55 +00:00
|
|
|
passport.authenticate('facebook', {
|
2017-03-08 10:45:51 +00:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
|
|
|
failureRedirect: config.serverurl + '/'
|
|
|
|
}))
|
2016-04-20 10:03:55 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
// twitter auth
|
2016-04-20 10:03:55 +00:00
|
|
|
if (config.twitter) {
|
2017-03-08 10:45:51 +00:00
|
|
|
app.get('/auth/twitter', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('twitter')(req, res, next)
|
|
|
|
})
|
|
|
|
// twitter auth callback
|
|
|
|
app.get('/auth/twitter/callback',
|
2016-04-20 10:03:55 +00:00
|
|
|
passport.authenticate('twitter', {
|
2017-03-08 10:45:51 +00:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
|
|
|
failureRedirect: config.serverurl + '/'
|
|
|
|
}))
|
2016-04-20 10:03:55 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
// github auth
|
2016-04-20 10:03:55 +00:00
|
|
|
if (config.github) {
|
2017-03-08 10:45:51 +00:00
|
|
|
app.get('/auth/github', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('github')(req, res, next)
|
|
|
|
})
|
|
|
|
// github auth callback
|
|
|
|
app.get('/auth/github/callback',
|
2016-04-20 10:03:55 +00:00
|
|
|
passport.authenticate('github', {
|
2017-03-08 10:45:51 +00:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
|
|
|
failureRedirect: config.serverurl + '/'
|
|
|
|
}))
|
|
|
|
// github callback actions
|
|
|
|
app.get('/auth/github/callback/:noteId/:action', response.githubActions)
|
2016-04-20 10:03:55 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
// gitlab auth
|
2016-05-09 20:27:35 +00:00
|
|
|
if (config.gitlab) {
|
2017-03-08 10:45:51 +00:00
|
|
|
app.get('/auth/gitlab', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('gitlab')(req, res, next)
|
|
|
|
})
|
|
|
|
// gitlab auth callback
|
|
|
|
app.get('/auth/gitlab/callback',
|
2016-05-09 20:27:35 +00:00
|
|
|
passport.authenticate('gitlab', {
|
2017-03-08 10:45:51 +00:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
|
|
|
failureRedirect: config.serverurl + '/'
|
|
|
|
}))
|
|
|
|
// gitlab callback actions
|
|
|
|
app.get('/auth/gitlab/callback/:noteId/:action', response.gitlabActions)
|
2016-05-09 20:27:35 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
// dropbox auth
|
2016-04-20 10:03:55 +00:00
|
|
|
if (config.dropbox) {
|
2017-03-08 10:45:51 +00:00
|
|
|
app.get('/auth/dropbox', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('dropbox-oauth2')(req, res, next)
|
|
|
|
})
|
|
|
|
// dropbox auth callback
|
|
|
|
app.get('/auth/dropbox/callback',
|
2016-04-20 10:03:55 +00:00
|
|
|
passport.authenticate('dropbox-oauth2', {
|
2017-03-08 10:45:51 +00:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
|
|
|
failureRedirect: config.serverurl + '/'
|
|
|
|
}))
|
2016-04-20 10:03:55 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
// google auth
|
2016-05-21 14:48:00 +00:00
|
|
|
if (config.google) {
|
2017-03-08 10:45:51 +00:00
|
|
|
app.get('/auth/google', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('google', { scope: ['profile'] })(req, res, next)
|
|
|
|
})
|
|
|
|
// google auth callback
|
|
|
|
app.get('/auth/google/callback',
|
2016-05-21 14:48:00 +00:00
|
|
|
passport.authenticate('google', {
|
2017-03-08 10:45:51 +00:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
|
|
|
failureRedirect: config.serverurl + '/'
|
|
|
|
}))
|
2016-05-21 14:48:00 +00:00
|
|
|
}
|
2016-12-13 21:31:35 +00:00
|
|
|
// ldap auth
|
|
|
|
if (config.ldap) {
|
2017-03-08 10:45:51 +00:00
|
|
|
app.post('/auth/ldap', urlencodedParser, function (req, res, next) {
|
|
|
|
if (!req.body.username || !req.body.password) return response.errorBadRequest(res)
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('ldapauth', {
|
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
|
|
|
failureRedirect: config.serverurl + '/',
|
|
|
|
failureFlash: true
|
|
|
|
})(req, res, next)
|
|
|
|
})
|
2016-12-13 21:31:35 +00:00
|
|
|
}
|
2016-12-01 17:58:14 +00:00
|
|
|
// email auth
|
|
|
|
if (config.email) {
|
2017-03-08 10:45:51 +00:00
|
|
|
if (config.allowemailregister) {
|
|
|
|
app.post('/register', urlencodedParser, function (req, res, next) {
|
|
|
|
if (!req.body.email || !req.body.password) return response.errorBadRequest(res)
|
|
|
|
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res)
|
|
|
|
models.User.findOrCreate({
|
|
|
|
where: {
|
|
|
|
email: req.body.email
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
password: req.body.password
|
|
|
|
}
|
|
|
|
}).spread(function (user, created) {
|
|
|
|
if (user) {
|
|
|
|
if (created) {
|
|
|
|
if (config.debug) {
|
|
|
|
logger.info('user registered: ' + user.id)
|
|
|
|
}
|
|
|
|
req.flash('info', "You've successfully registered, please signin.")
|
|
|
|
} else {
|
|
|
|
if (config.debug) {
|
|
|
|
logger.info('user found: ' + user.id)
|
|
|
|
}
|
|
|
|
req.flash('error', 'This email has been used, please try another one.')
|
|
|
|
}
|
|
|
|
return res.redirect(config.serverurl + '/')
|
|
|
|
}
|
|
|
|
req.flash('error', 'Failed to register your account, please try again.')
|
|
|
|
return res.redirect(config.serverurl + '/')
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('auth callback failed: ' + err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
app.post('/login', urlencodedParser, function (req, res, next) {
|
|
|
|
if (!req.body.email || !req.body.password) return response.errorBadRequest(res)
|
|
|
|
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res)
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('local', {
|
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
|
|
|
failureRedirect: config.serverurl + '/',
|
|
|
|
failureFlash: 'Invalid email or password.'
|
|
|
|
})(req, res, next)
|
|
|
|
})
|
2016-12-01 17:58:14 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
// logout
|
2015-05-04 07:53:29 +00:00
|
|
|
app.get('/logout', function (req, res) {
|
2017-03-08 10:45:51 +00:00
|
|
|
if (config.debug && req.isAuthenticated()) { logger.info('user logout: ' + req.user.id) }
|
|
|
|
req.logout()
|
|
|
|
res.redirect(config.serverurl + '/')
|
|
|
|
})
|
|
|
|
var history = require('./lib/history.js')
|
|
|
|
// get history
|
|
|
|
app.get('/history', history.historyGet)
|
|
|
|
// post history
|
|
|
|
app.post('/history', urlencodedParser, history.historyPost)
|
|
|
|
// post history by note id
|
|
|
|
app.post('/history/:noteId', urlencodedParser, history.historyPost)
|
|
|
|
// delete history
|
|
|
|
app.delete('/history', history.historyDelete)
|
|
|
|
// delete history by note id
|
|
|
|
app.delete('/history/:noteId', history.historyDelete)
|
|
|
|
// get me info
|
2015-05-04 07:53:29 +00:00
|
|
|
app.get('/me', function (req, res) {
|
2017-03-08 10:45:51 +00:00
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.user.id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
if (!user) { return response.errorNotFound(res) }
|
|
|
|
var profile = models.User.getProfile(user)
|
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
id: req.user.id,
|
|
|
|
name: profile.name,
|
|
|
|
photo: profile.photo
|
|
|
|
})
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('read me failed: ' + err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res.send({
|
|
|
|
status: 'forbidden'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// upload image
|
2015-05-15 04:58:13 +00:00
|
|
|
app.post('/uploadimage', function (req, res) {
|
2017-03-08 10:45:51 +00:00
|
|
|
var form = new formidable.IncomingForm()
|
2016-11-14 09:07:07 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
form.keepExtensions = true
|
2016-11-16 04:05:24 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
if (config.imageUploadType === 'filesystem') {
|
|
|
|
form.uploadDir = 'public/uploads'
|
|
|
|
}
|
2016-11-14 08:45:57 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
form.parse(req, function (err, fields, files) {
|
|
|
|
if (err || !files.image || !files.image.path) {
|
|
|
|
response.errorForbidden(res)
|
|
|
|
} else {
|
|
|
|
if (config.debug) { logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image)) }
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (config.imageUploadType) {
|
|
|
|
case 'filesystem':
|
|
|
|
res.send({
|
|
|
|
link: url.resolve(config.serverurl + '/', files.image.path.match(/^public\/(.+$)/)[1])
|
|
|
|
})
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
case 's3':
|
|
|
|
var AWS = require('aws-sdk')
|
|
|
|
var awsConfig = new AWS.Config(config.s3)
|
|
|
|
var s3 = new AWS.S3(awsConfig)
|
|
|
|
|
|
|
|
fs.readFile(files.image.path, function (err, buffer) {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err)
|
|
|
|
res.status(500).end('upload image error')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var params = {
|
|
|
|
Bucket: config.s3bucket,
|
|
|
|
Key: path.join('uploads', path.basename(files.image.path)),
|
|
|
|
Body: buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
s3.putObject(params, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err)
|
|
|
|
res.status(500).end('upload image error')
|
|
|
|
return
|
2016-11-14 09:07:07 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
res.send({
|
|
|
|
link: `https://s3-${config.s3.region}.amazonaws.com/${config.s3bucket}/${params.Key}`
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
break
|
|
|
|
case 'imgur':
|
|
|
|
default:
|
|
|
|
imgur.setClientId(config.imgur.clientID)
|
|
|
|
imgur.uploadFile(files.image.path)
|
|
|
|
.then(function (json) {
|
|
|
|
if (config.debug) { logger.info('SERVER uploadimage success: ' + JSON.stringify(json)) }
|
|
|
|
res.send({
|
|
|
|
link: json.data.link.replace(/^http:\/\//i, 'https://')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return res.status(500).end('upload image error')
|
|
|
|
})
|
|
|
|
break
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
} catch (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return res.status(500).end('upload image error')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// get new note
|
|
|
|
app.get('/new', response.newNote)
|
|
|
|
// get publish note
|
|
|
|
app.get('/s/:shortid', response.showPublishNote)
|
|
|
|
// publish note actions
|
|
|
|
app.get('/s/:shortid/:action', response.publishNoteActions)
|
|
|
|
// get publish slide
|
|
|
|
app.get('/p/:shortid', response.showPublishSlide)
|
|
|
|
// publish slide actions
|
|
|
|
app.get('/p/:shortid/:action', response.publishSlideActions)
|
|
|
|
// get note by id
|
|
|
|
app.get('/:noteId', response.showNote)
|
|
|
|
// note actions
|
|
|
|
app.get('/:noteId/:action', response.noteActions)
|
|
|
|
// note actions with action id
|
|
|
|
app.get('/:noteId/:action/:actionId', response.noteActions)
|
2016-04-20 10:19:29 +00:00
|
|
|
// response not found if no any route matches
|
|
|
|
app.get('*', function (req, res) {
|
2017-03-08 10:45:51 +00:00
|
|
|
response.errorNotFound(res)
|
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// socket.io secure
|
|
|
|
io.use(realtime.secure)
|
|
|
|
// socket.io auth
|
2015-06-01 10:04:25 +00:00
|
|
|
io.use(passportSocketIo.authorize({
|
2017-03-08 10:45:51 +00:00
|
|
|
cookieParser: cookieParser,
|
|
|
|
key: config.sessionname,
|
|
|
|
secret: config.sessionsecret,
|
|
|
|
store: sessionStore,
|
|
|
|
success: realtime.onAuthorizeSuccess,
|
|
|
|
fail: realtime.onAuthorizeFail
|
|
|
|
}))
|
|
|
|
// socket.io heartbeat
|
|
|
|
io.set('heartbeat interval', config.heartbeatinterval)
|
|
|
|
io.set('heartbeat timeout', config.heartbeattimeout)
|
|
|
|
// socket.io connection
|
|
|
|
io.sockets.on('connection', realtime.connection)
|
|
|
|
|
|
|
|
// listen
|
|
|
|
function startListen () {
|
|
|
|
server.listen(config.port, function () {
|
|
|
|
var schema = config.usessl ? 'HTTPS' : 'HTTP'
|
|
|
|
logger.info('%s Server listening at port %d', schema, config.port)
|
|
|
|
config.maintenance = false
|
|
|
|
})
|
2015-07-11 04:44:16 +00:00
|
|
|
}
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// sync db then start listen
|
2016-06-17 08:09:33 +00:00
|
|
|
models.sequelize.sync().then(function () {
|
2017-03-08 10:45:51 +00:00
|
|
|
// check if realtime is ready
|
|
|
|
if (realtime.isReady()) {
|
|
|
|
models.Revision.checkAllNotesRevision(function (err, notes) {
|
|
|
|
if (err) throw new Error(err)
|
|
|
|
if (!notes || notes.length <= 0) return startListen()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
throw new Error('server still not ready after db synced')
|
|
|
|
}
|
|
|
|
})
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// log uncaught exception
|
2015-07-11 04:44:16 +00:00
|
|
|
process.on('uncaughtException', function (err) {
|
2017-03-08 10:45:51 +00:00
|
|
|
logger.error('An uncaught exception has occured.')
|
|
|
|
logger.error(err)
|
|
|
|
logger.error('Process will exit now.')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2016-06-01 06:18:54 +00:00
|
|
|
|
2017-01-20 01:13:09 +00:00
|
|
|
// install exit handler
|
2017-03-08 10:45:51 +00:00
|
|
|
function handleTermSignals () {
|
|
|
|
config.maintenance = true
|
|
|
|
// disconnect all socket.io clients
|
|
|
|
Object.keys(io.sockets.sockets).forEach(function (key) {
|
|
|
|
var socket = io.sockets.sockets[key]
|
|
|
|
// notify client server going into maintenance status
|
|
|
|
socket.emit('maintenance')
|
|
|
|
setTimeout(function () {
|
|
|
|
socket.disconnect(true)
|
|
|
|
}, 0)
|
|
|
|
})
|
|
|
|
var checkCleanTimer = setInterval(function () {
|
|
|
|
if (realtime.isReady()) {
|
|
|
|
models.Revision.checkAllNotesRevision(function (err, notes) {
|
|
|
|
if (err) return logger.error(err)
|
|
|
|
if (!notes || notes.length <= 0) {
|
|
|
|
clearInterval(checkCleanTimer)
|
|
|
|
return process.exit(0)
|
2016-06-01 06:18:54 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}, 100)
|
2017-01-20 01:13:09 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
process.on('SIGINT', handleTermSignals)
|
|
|
|
process.on('SIGTERM', handleTermSignals)
|