fix(striker-ui-api): expose VNC error in cookie
This commit is contained in:
parent
dc4b068a8c
commit
88a656df6f
1
striker-ui-api/src/lib/consts/WS_GUID.ts
Normal file
1
striker-ui-api/src/lib/consts/WS_GUID.ts
Normal file
@ -0,0 +1 @@
|
||||
export const WS_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
@ -10,3 +10,4 @@ export * from './LOCAL';
|
||||
export * from './NODE_AND_DR_RESERVED_MEMORY_SIZE';
|
||||
export * from './OS_LIST';
|
||||
export * from './REG_EXP_PATTERNS';
|
||||
export * from './WS_GUID';
|
||||
|
@ -1,22 +1,24 @@
|
||||
import { createHash } from 'crypto';
|
||||
import { createProxyMiddleware } from 'http-proxy-middleware';
|
||||
|
||||
import { P_UUID } from '../lib/consts';
|
||||
import { P_UUID, WS_GUID } from '../lib/consts';
|
||||
|
||||
import { perr, pout } from '../lib/shell';
|
||||
import { getVncinfo } from '../lib/accessModule';
|
||||
import { cname } from '../lib/cname';
|
||||
import { perr, pout, poutvar } from '../lib/shell';
|
||||
|
||||
const WS_SVR_VNC_URL_PREFIX = '/ws/server/vnc';
|
||||
|
||||
const getServerUuid = (url = '') =>
|
||||
url.replace(new RegExp(`^${WS_SVR_VNC_URL_PREFIX}/(${P_UUID})`), '$1');
|
||||
|
||||
export const proxyServerVnc = createProxyMiddleware({
|
||||
changeOrigin: true,
|
||||
pathFilter: `${WS_SVR_VNC_URL_PREFIX}/*`,
|
||||
router: async (request) => {
|
||||
const { url = '' } = request;
|
||||
const { url } = request;
|
||||
|
||||
const serverUuid = url.replace(
|
||||
new RegExp(`^${WS_SVR_VNC_URL_PREFIX}/(${P_UUID})`),
|
||||
'$1',
|
||||
);
|
||||
const serverUuid = getServerUuid(url);
|
||||
|
||||
pout(`Got param [${serverUuid}] from [${url}]`);
|
||||
|
||||
@ -44,15 +46,49 @@ export const proxyServerVnc = createProxyMiddleware({
|
||||
return;
|
||||
}
|
||||
|
||||
if ('writeHead' in response) {
|
||||
pout('Got ServerResponse object');
|
||||
const serverUuid = getServerUuid(request.url);
|
||||
|
||||
return response.writeHead(500).end();
|
||||
const errapiName = cname(`vncerror.${serverUuid}`);
|
||||
const errapiObj = {
|
||||
code: '72c969b',
|
||||
message: error.message,
|
||||
name: error.name,
|
||||
};
|
||||
const errapiStr = JSON.stringify(errapiObj);
|
||||
const errapiValue = encodeURIComponent(errapiStr);
|
||||
const errapiCookie = `${errapiName}=j:${errapiValue}; Path=/server; SameSite=Lax; Max-Age=3`;
|
||||
|
||||
poutvar({ errapiCookie }, 'Error cookie: ');
|
||||
|
||||
if ('writeHead' in response) {
|
||||
pout('Found ServerResponse object');
|
||||
|
||||
return response
|
||||
.writeHead(500, {
|
||||
'Set-Cookie': `${errapiCookie}`,
|
||||
})
|
||||
.end();
|
||||
}
|
||||
|
||||
pout(`Got Socket object`);
|
||||
pout(`Found Socket object`);
|
||||
|
||||
response.end();
|
||||
const {
|
||||
headers: { 'sec-websocket-key': wskey },
|
||||
} = request;
|
||||
|
||||
const wsaccept = createHash('sha1')
|
||||
.update(wskey + WS_GUID, 'binary')
|
||||
.digest('base64');
|
||||
|
||||
const headers = [
|
||||
'HTTP/1.1 101 Switching Protocols',
|
||||
'Connection: upgrade',
|
||||
`Sec-WebSocket-Accept: ${wsaccept}`,
|
||||
`Set-Cookie: ${errapiCookie}`,
|
||||
'Upgrade: websocket',
|
||||
];
|
||||
|
||||
response.end(`${headers.join('\r\n')}\r\n`, 'utf-8');
|
||||
},
|
||||
},
|
||||
ws: true,
|
||||
|
Loading…
Reference in New Issue
Block a user