1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-05 09:17:41 +03:00

M #-: Fix some errors with VMRC (#3108)

* Fix websockets logger error
* Fix websockets logger error
* Add header for CORS
This commit is contained in:
Jorge Miguel Lobo Escalona 2024-06-12 18:47:19 +02:00 committed by GitHub
parent 14ef631a7e
commit 41e9902b2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 29 deletions

View File

@ -37,14 +37,14 @@ const formatError = 'Error: %s'
const configError = (error) => ({
color: 'red',
message: formatError,
error: error && error.message,
error: error?.message,
})
// guacamole client options
const clientOptions = {
crypt: {
cypher: 'AES-256-CBC',
key: (global && global.paths && global.paths.FIREEDGE_KEY) || '',
key: global?.paths?.FIREEDGE_KEY || '',
},
allowedUnencryptedConnectionSettings: {
rdp: ['width', 'height', 'dpi'],
@ -66,7 +66,7 @@ const clientOptions = {
const clientCallbacks = {
processConnectionSettings: (settings, callback) => {
if (settings.expiration && settings.expiration < Date.now()) {
if (settings?.expiration < Date.now()) {
return callback(new Error('Token expired'))
}
@ -85,19 +85,14 @@ const guacdHost = guacd.host || 'localhost'
* @param {object} appServer - express app
*/
const guacamole = (appServer) => {
if (
appServer &&
appServer.constructor &&
appServer.constructor.name &&
appServer.constructor.name === 'Server'
) {
if (appServer?.constructor?.name === 'Server') {
const guacamoleServer = new GuacamoleOpennebula(
{ server: appServer, path: endpointGuacamole },
{ host: guacdHost, port: guacdPort },
clientOptions,
clientCallbacks
)
guacamoleServer.on('error', (clientConnection, error) => {
guacamoleServer.on('error', (_, error) => {
writeInLogger(error, {
format: formatError,
})

View File

@ -18,7 +18,6 @@
const { parse } = require('url')
const { createProxyMiddleware } = require('http-proxy-middleware')
const { getFireedgeConfig } = require('server/utils/yml')
const { messageTerminal } = require('server/utils/general')
const {
genPathResources,
validateServerIsSecure,
@ -32,9 +31,6 @@ const appConfig = getFireedgeConfig()
const port = appConfig.port || defaultPort
const protocol = validateServerIsSecure() ? 'https' : 'http'
const url = `${protocol}://localhost:${port}`
const config = {
color: 'red',
}
const vmrcProxy = createProxyMiddleware(endpointVmrc, {
target: url,
changeOrigin: false,
@ -42,22 +38,36 @@ const vmrcProxy = createProxyMiddleware(endpointVmrc, {
secure: /^(https):\/\/[^ "]+$/.test(url),
logLevel: 'debug',
pathRewrite: (path) => path.replace(endpointVmrc, '/ticket'),
onError: (err) => {
config.error = err.message
config.message = 'Error connection : %s'
messageTerminal(config)
onProxyReqWs: (proxyReq, __, socket) => {
proxyReq.setHeader('Access-Control-Allow-Origin', '*');
socket.on('error', (err) => {
writeInLogger(err?.message || '', {
format: 'WebSocket Error connection : %s',
level: 2,
})
socket.end()
})
},
onError: (err, _, res) => {
writeInLogger(err?.message || '', {
format: 'Error connection : %s',
level: 2,
})
res.status(500).send('VMRC proxy error')
},
// eslint-disable-next-line consistent-return
router: (req) => {
if (req && req.url) {
if (req?.url) {
const parseURL = parse(req.url)
if (parseURL && parseURL.pathname) {
if (parseURL?.pathname) {
const ticket = parseURL.pathname.split('/')[3]
writeInLogger(ticket, {
format: 'path to vmrc token: %s',
format: 'Path to VMRC token: %s',
level: 2,
})
if (global && global.vcenterToken && global.vcenterToken[ticket]) {
if (global?.vcenterToken?.[ticket]) {
return global.vcenterToken[ticket]
} else {
writeInLogger(ticket, {
@ -76,13 +86,7 @@ const vmrcProxy = createProxyMiddleware(endpointVmrc, {
* @param {object} appServer - express app
*/
const vmrc = (appServer) => {
if (
appServer &&
appServer.on &&
appServer.constructor &&
appServer.constructor.name &&
appServer.constructor.name === 'Server'
) {
if (appServer && appServer?.on && appServer?.constructor?.name === 'Server') {
appServer.on('upgrade', vmrcProxy.upgrade)
}
}