chore: lint ./app/server/app.js #242
This commit is contained in:
parent
c3d63fa3c1
commit
8fb819d505
1 changed files with 29 additions and 27 deletions
|
|
@ -1,15 +1,16 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
/* jshint esversion: 6, asi: true, node: true */
|
/* jshint esversion: 6, asi: true, node: true */
|
||||||
|
/* eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }] */
|
||||||
// app.js
|
// app.js
|
||||||
|
|
||||||
var path = require('path')
|
const path = require('path')
|
||||||
var fs = require('fs')
|
const fs = require('fs')
|
||||||
var nodeRoot = path.dirname(require.main.filename)
|
const nodeRoot = path.dirname(require.main.filename)
|
||||||
var configPath = path.join(nodeRoot, 'config.json')
|
const configPath = path.join(nodeRoot, 'config.json')
|
||||||
var publicPath = path.join(nodeRoot, 'client', 'public')
|
const publicPath = path.join(nodeRoot, 'client', 'public')
|
||||||
console.log('WebSSH2 service reading config from: ' + configPath)
|
console.log('WebSSH2 service reading config from: ' + configPath)
|
||||||
var express = require('express')
|
const express = require('express')
|
||||||
var logger = require('morgan')
|
const logger = require('morgan')
|
||||||
|
|
||||||
// sane defaults if config.json or parts are missing
|
// sane defaults if config.json or parts are missing
|
||||||
let config = {
|
let config = {
|
||||||
|
|
@ -95,7 +96,7 @@ let config = {
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync(configPath)) {
|
if (fs.existsSync(configPath)) {
|
||||||
console.log('ephemeral_auth service reading config from: ' + configPath)
|
console.log('ephemeral_auth service reading config from: ' + configPath)
|
||||||
config = require('read-config-ng')(configPath)
|
config = require('read-config-ng')(configPath) // eslint-disable-line
|
||||||
} else {
|
} else {
|
||||||
console.error('\n\nERROR: Missing config.json for webssh. Current config: ' + JSON.stringify(config))
|
console.error('\n\nERROR: Missing config.json for webssh. Current config: ' + JSON.stringify(config))
|
||||||
console.error('\n See config.json.sample for details\n\n')
|
console.error('\n See config.json.sample for details\n\n')
|
||||||
|
|
@ -106,22 +107,22 @@ try {
|
||||||
console.error('ERROR:\n\n ' + err)
|
console.error('ERROR:\n\n ' + err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var session = require('express-session')({
|
const session = require('express-session')({
|
||||||
secret: config.session.secret,
|
secret: config.session.secret,
|
||||||
name: config.session.name,
|
name: config.session.name,
|
||||||
resave: true,
|
resave: true,
|
||||||
saveUninitialized: false,
|
saveUninitialized: false,
|
||||||
unset: 'destroy'
|
unset: 'destroy'
|
||||||
})
|
})
|
||||||
var app = express()
|
const app = express()
|
||||||
var server = require('http').Server(app)
|
const server = require('http').Server(app)
|
||||||
var myutil = require('./util')
|
const myutil = require('./util')
|
||||||
myutil.setDefaultCredentials(config.user.name, config.user.password, config.user.privatekey)
|
myutil.setDefaultCredentials(config.user.name, config.user.password, config.user.privatekey)
|
||||||
var validator = require('validator')
|
const validator = require('validator')
|
||||||
var io = require('socket.io')(server, { serveClient: false, path: '/ssh/socket.io', origins: config.http.origins })
|
const io = require('socket.io')(server, { serveClient: false, path: '/ssh/socket.io', origins: config.http.origins })
|
||||||
var socket = require('./socket')
|
const socket = require('./socket')
|
||||||
var expressOptions = require('./expressOptions')
|
const expressOptions = require('./expressOptions')
|
||||||
var favicon = require('serve-favicon')
|
const favicon = require('serve-favicon')
|
||||||
|
|
||||||
// express
|
// express
|
||||||
app.use(safeShutdownGuard)
|
app.use(safeShutdownGuard)
|
||||||
|
|
@ -137,7 +138,7 @@ app.use('/ssh', express.static(publicPath, expressOptions))
|
||||||
app.use(favicon(path.join(publicPath, 'favicon.ico')))
|
app.use(favicon(path.join(publicPath, 'favicon.ico')))
|
||||||
|
|
||||||
app.get('/ssh/reauth', function (req, res, next) {
|
app.get('/ssh/reauth', function (req, res, next) {
|
||||||
var r = req.headers.referer || '/'
|
const r = req.headers.referer || '/'
|
||||||
res.status(401).send('<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=' + r + '"></head><body bgcolor="#000"></body></html>')
|
res.status(401).send('<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=' + r + '"></head><body bgcolor="#000"></body></html>')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -197,17 +198,18 @@ app.use(function (err, req, res, next) {
|
||||||
// socket.io
|
// socket.io
|
||||||
// expose express session with socket.request.session
|
// expose express session with socket.request.session
|
||||||
io.use(function (socket, next) {
|
io.use(function (socket, next) {
|
||||||
(socket.request.res) ? session(socket.request, socket.request.res, next)
|
(socket.request.res)
|
||||||
: next(next)
|
? session(socket.request, socket.request.res, next)
|
||||||
|
: next(next) // eslint disable-line
|
||||||
})
|
})
|
||||||
|
|
||||||
// bring up socket
|
// bring up socket
|
||||||
io.on('connection', socket)
|
io.on('connection', socket)
|
||||||
|
|
||||||
// safe shutdown
|
// safe shutdown
|
||||||
var shutdownMode = false
|
let shutdownMode = false
|
||||||
var shutdownInterval = 0
|
let shutdownInterval = 0
|
||||||
var connectionCount = 0
|
let connectionCount = 0
|
||||||
|
|
||||||
function safeShutdownGuard (req, res, next) {
|
function safeShutdownGuard (req, res, next) {
|
||||||
if (shutdownMode) res.status(503).end('Service unavailable: Server shutting down')
|
if (shutdownMode) res.status(503).end('Service unavailable: Server shutting down')
|
||||||
|
|
@ -228,10 +230,10 @@ const signals = ['SIGTERM', 'SIGINT']
|
||||||
signals.forEach(signal => process.on(signal, function () {
|
signals.forEach(signal => process.on(signal, function () {
|
||||||
if (shutdownMode) stop('Safe shutdown aborted, force quitting')
|
if (shutdownMode) stop('Safe shutdown aborted, force quitting')
|
||||||
else if (connectionCount > 0) {
|
else if (connectionCount > 0) {
|
||||||
var remainingSeconds = config.safeShutdownDuration
|
let remainingSeconds = config.safeShutdownDuration
|
||||||
shutdownMode = true
|
shutdownMode = true
|
||||||
|
const message = (connectionCount === 1)
|
||||||
var message = (connectionCount === 1) ? ' client is still connected'
|
? ' client is still connected'
|
||||||
: ' clients are still connected'
|
: ' clients are still connected'
|
||||||
console.error(connectionCount + message)
|
console.error(connectionCount + message)
|
||||||
console.error('Starting a ' + remainingSeconds + ' seconds countdown')
|
console.error('Starting a ' + remainingSeconds + ' seconds countdown')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue