|
|
|
@ -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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const serverUuid = getServerUuid(request.url); |
|
|
|
|
|
|
|
|
|
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('Got ServerResponse object'); |
|
|
|
|
pout('Found ServerResponse object'); |
|
|
|
|
|
|
|
|
|
return response.writeHead(500).end(); |
|
|
|
|
return response |
|
|
|
|
.writeHead(500, { |
|
|
|
|
'Set-Cookie': `${errapiCookie}`, |
|
|
|
|
}) |
|
|
|
|
.end(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pout(`Got Socket object`); |
|
|
|
|
pout(`Found Socket object`); |
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
|
response.end(`${headers.join('\r\n')}\r\n`, 'utf-8'); |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
ws: true, |
|
|
|
|