Reorganized files.

This commit is contained in:
Dave Greene 2021-09-11 16:09:53 -04:00
parent 66b36587e4
commit c33db21185
8 changed files with 124 additions and 98 deletions

88
app.js
View File

@ -1,30 +1,12 @@
// LinuxServer Guacamole Client
const path = require('path');
const express = require('express');
const http = require('http');
const cloudcmd = require('cloudcmd');
const { pamAuthenticate } = require('node-linux-pam');
const GuacamoleLite = require('guacamole-lite');
const { clientOptions, connectionOptions } = require('./options');
const {
encrypt,
trimTrailingSlash,
deepMerge,
loadConfig,
} = require('./utils');
// Application Variables
const baseurl = process.env.SUBFOLDER || '/';
const CUSTOM_PORT = process.env.CUSTOM_PORT || 3000;
const app = express();
app.set('view engine', 'ejs');
app.set('x-powered-by', false);
const { app } = require('./lib/server');
const { clientOptions } = require('./lib/options');
const server = http.Server(app);
const baserouter = express.Router();
const credentials = loadConfig(__dirname);
const CUSTOM_PORT = process.env.CUSTOM_PORT || 3000;
// Spinup the Guac websocket proxy on port 3000 if guacd is running
// eslint-disable-next-line no-unused-vars
@ -36,69 +18,7 @@ const guacServer = new GuacamoleLite({
port: 4822,
}, clientOptions);
// Public JS and CSS
baserouter.use('/public', express.static(path.join(__dirname, '/public')));
// Embedded guac
baserouter.get('/', (req, res) => {
const { crypt: { key, cypher } } = clientOptions;
const token = req.query.login
? encrypt(connectionOptions, cypher, key)
: encrypt(deepMerge(connectionOptions, {
connection: {
settings: credentials,
},
}), cypher, key);
res.render(path.join(__dirname, '/rdp.ejs'), {
token,
baseurl,
});
});
// Web File Browser
baserouter.use(express.json());
baserouter.use(express.urlencoded());
baserouter.get('/files', (req, res) => {
res.send('Unauthorized');
res.end();
});
baserouter.post('/files', (req, res, next) => {
pamAuthenticate({
username: credentials.username,
password: req.body.password,
}, (err) => {
if (err) {
res.send('Unauthorized');
res.end();
return;
}
next();
});
});
baserouter.use('/files', cloudcmd({
config: {
root: '/',
prefix: `${trimTrailingSlash(baseurl)}/files`,
terminal: false,
console: false,
configDialog: false,
contact: false,
auth: false,
name: 'Files',
log: false,
keysPanel: false,
oneFilePanel: true,
},
}));
// Spin up application on CUSTOM_PORT with fallback to port 3000
app.use('/', baserouter);
server.listen(CUSTOM_PORT, () => {
console.log(`listening on *: ${CUSTOM_PORT}`);
});

93
lib/server.js Normal file
View File

@ -0,0 +1,93 @@
// LinuxServer Guacamole Client
const path = require('path');
const express = require('express');
const cloudcmd = require('cloudcmd');
const { pamAuthenticate } = require('node-linux-pam');
const { clientOptions, connectionOptions } = require('./options');
const {
encrypt,
trimTrailingSlash,
deepMerge,
loadConfig,
} = require('./utils');
// Application Variables
const baseurl = process.env.SUBFOLDER || '/';
const app = express();
app.set('view engine', 'ejs');
app.set('x-powered-by', false);
const baserouter = express.Router();
const credentials = loadConfig(path.resolve(__dirname, '..'));
// Public JS and CSS
baserouter.use('/public', express.static(path.join(__dirname, '..', '/public')));
// Embedded guac
baserouter.get('/', (req, res) => {
const { crypt: { key, cypher } } = clientOptions;
const token = req.query.login
? encrypt(connectionOptions, cypher, key)
: encrypt(deepMerge(connectionOptions, {
connection: {
settings: credentials,
},
}), cypher, key);
res.render(path.join(__dirname, '/rdp.ejs'), {
token,
baseurl,
});
});
// Web File Browser
baserouter.use(express.json());
baserouter.use(express.urlencoded({
extended: true,
}));
baserouter.get('/files', (req, res) => {
res.status(401).send('Unauthorized');
});
baserouter.post('/files', (req, res, next) => {
if (!req.body.password) {
res.status(401).send('Unauthorized');
return;
}
pamAuthenticate({
username: credentials.username,
password: req.body.password,
}, (err) => {
if (!err) {
next();
return;
}
res.status(401).send('Unauthorized');
});
});
baserouter.use('/files', cloudcmd({
config: {
root: '/',
prefix: `${trimTrailingSlash(baseurl)}/files`,
terminal: false,
console: false,
configDialog: false,
contact: false,
auth: false,
name: 'Files',
log: false,
keysPanel: false,
oneFilePanel: true,
},
}));
app.use('/', baserouter);
exports.app = app;

23
lib/token.js Normal file
View File

@ -0,0 +1,23 @@
const { clientOptions, connectionOptions } = require('./options');
const { encrypt, deepMerge } = require('./utils');
const makeToken = (credentials) => {
if (!('username' in credentials)) {
throw new Error('credential is missing `username`');
}
if (!('password' in credentials)) {
throw new Error('credential is missing `password`');
}
return encrypt(deepMerge(connectionOptions, {
connection: {
settings: {
username: credentials.username,
password: credentials.password,
},
},
}), clientOptions.crypt.cypher, clientOptions.crypt.key);
};
exports.makeToken = makeToken;

View File

@ -1,14 +1,4 @@
const { clientOptions, connectionOptions } = require('./options');
const { encrypt, deepMerge, loadConfig } = require('./utils');
const { makeToken } = require('./lib/token');
const { loadConfig } = require('./lib/utils');
const credentials = loadConfig(__dirname);
const options = deepMerge(connectionOptions, {
connection: {
settings: {
username: 'abc',
password: credentials.password,
},
},
});
console.log(encrypt(options, clientOptions.crypt.cypher, clientOptions.crypt.key));
console.log(makeToken(loadConfig(__dirname)));

View File

@ -5,7 +5,7 @@
"main": "app.js",
"scripts": {
"test": "jest --coverage --detectOpenHandles",
"lint": "eslint *.js"
"lint": "eslint *.js lib/*.js"
},
"repository": {
"type": "git",