-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
VncScreen.tsx
375 lines (323 loc) · 10.1 KB
/
VncScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import React, {
forwardRef,
useEffect,
useImperativeHandle,
useRef,
useState,
} from 'react';
import RFB from '../noVNC/core/rfb';
export interface RFBOptions {
shared: boolean;
credentials: {
username?: string;
password?: string;
target?: string;
};
repeaterID: string;
wsProtocols: string | string[];
}
export interface Props {
url: string;
style?: object;
className?: string;
viewOnly?: boolean;
rfbOptions?: Partial<RFBOptions>;
focusOnClick?: boolean;
clipViewport?: boolean;
dragViewport?: boolean;
scaleViewport?: boolean;
resizeSession?: boolean;
showDotCursor?: boolean;
background?: string;
qualityLevel?: number;
compressionLevel?: number;
autoConnect?: boolean;
retryDuration?: number;
debug?: boolean;
loadingUI?: React.ReactNode;
onConnect?: (rfb?: RFB) => void;
onDisconnect?: (rfb?: RFB) => void;
onCredentialsRequired?: (rfb?: RFB) => void;
onSecurityFailure?: (e?: { detail: { status: number, reason: string } }) => void;
onClipboard?: (e?: { detail: { text: string } }) => void;
onBell?: () => void;
onDesktopName?: (e?: { detail: { name: string } }) => void;
onCapabilities?: (e?: { detail: { capabilities: RFB["capabilities"] } }) => void;
}
export enum Events {
connect,
disconnect,
credentialsrequired,
securityfailure,
clipboard,
bell,
desktopname,
capabilities,
}
export type EventListeners = { -readonly [key in keyof typeof Events]?: (e?: any) => void };
export type VncScreenHandle = {
connect: () => void;
disconnect: () => void;
connected: boolean;
sendCredentials: (credentials: RFBOptions["credentials"]) => void;
sendKey: (keysym: number, code: string, down?: boolean) => void;
sendCtrlAltDel: () => void;
focus: () => void;
blur: () => void;
machineShutdown: () => void;
machineReboot: () => void;
machineReset: () => void;
clipboardPaste: (text: string) => void;
rfb: RFB | null;
eventListeners: EventListeners;
};
const VncScreen: React.ForwardRefRenderFunction<VncScreenHandle, Props> = (props, ref) => {
const rfb = useRef<RFB | null>(null);
const connected = useRef<boolean>(props.autoConnect ?? true);
const timeouts = useRef<Array<NodeJS.Timeout>>([]);
const eventListeners = useRef<EventListeners>({});
const screen = useRef<HTMLDivElement>(null);
const [loading, setLoading] = useState<boolean>(true);
const {
url,
style,
className,
viewOnly,
rfbOptions,
focusOnClick,
clipViewport,
dragViewport,
scaleViewport,
resizeSession,
showDotCursor,
background,
qualityLevel,
compressionLevel,
autoConnect = true,
retryDuration = 3000,
debug = false,
loadingUI,
onConnect,
onDisconnect,
onCredentialsRequired,
onSecurityFailure,
onClipboard,
onBell,
onDesktopName,
onCapabilities,
} = props;
const logger = {
log: (...args: any[]) => { if (debug) console.log(...args); },
info: (...args: any[]) => { if (debug) console.info(...args); },
error: (...args: any[]) => { if (debug) console.error(...args); },
};
const getRfb = () => {
return rfb.current;
};
const setRfb = (_rfb: RFB | null) => {
rfb.current = _rfb;
};
const getConnected = () => {
return connected.current;
};
const setConnected = (state: boolean) => {
connected.current = state;
};
const _onConnect = () => {
const rfb = getRfb();
if (onConnect) {
onConnect(rfb ?? undefined);
setLoading(false);
return;
}
logger.info('Connected to remote VNC.');
setLoading(false);
};
const _onDisconnect = () => {
const rfb = getRfb();
if (onDisconnect) {
onDisconnect(rfb ?? undefined);
setLoading(true);
return;
}
const connected = getConnected();
if (connected) {
logger.info(`Unexpectedly disconnected from remote VNC, retrying in ${retryDuration / 1000} seconds.`);
timeouts.current.push(setTimeout(connect, retryDuration));
} else {
logger.info(`Disconnected from remote VNC.`);
}
setLoading(true);
};
const _onCredentialsRequired = () => {
const rfb = getRfb();
if (onCredentialsRequired) {
onCredentialsRequired(rfb ?? undefined);
return;
}
const password = rfbOptions?.credentials?.password ?? prompt("Password Required:");
rfb?.sendCredentials({ password: password });
};
const _onDesktopName = (e: { detail: { name: string } }) => {
if (onDesktopName) {
onDesktopName(e);
return;
}
logger.info(`Desktop name is ${e.detail.name}`);
};
const disconnect = () => {
const rfb = getRfb();
try {
if (!rfb) {
return;
}
timeouts.current.forEach(clearTimeout);
(Object.keys(eventListeners.current) as (keyof typeof Events)[]).forEach((event) => {
if (eventListeners.current[event]) {
rfb.removeEventListener(event, eventListeners.current[event]);
eventListeners.current[event] = undefined;
}
});
rfb.disconnect();
setRfb(null);
setConnected(false);
// NOTE(roerohan): This needs to be called since the event listener is removed.
// Even if the event listener is removed after rfb.disconnect(), the disconnect
// event is not fired.
_onDisconnect();
} catch (err) {
logger.error(err);
setRfb(null);
setConnected(false);
}
};
const connect = () => {
try {
if (connected && !!rfb) {
disconnect();
}
if (!screen.current) {
return;
}
screen.current.innerHTML = '';
const _rfb = new RFB(screen.current, url, rfbOptions);
_rfb.viewOnly = viewOnly ?? false;
_rfb.focusOnClick = focusOnClick ?? false;
_rfb.clipViewport = clipViewport ?? false;
_rfb.dragViewport = dragViewport ?? false;
_rfb.resizeSession = resizeSession ?? false;
_rfb.scaleViewport = scaleViewport ?? false;
_rfb.showDotCursor = showDotCursor ?? false;
_rfb.background = background ?? '';
_rfb.qualityLevel = qualityLevel ?? 6;
_rfb.compressionLevel = compressionLevel ?? 2;
setRfb(_rfb);
eventListeners.current.connect = _onConnect;
eventListeners.current.disconnect = _onDisconnect;
eventListeners.current.credentialsrequired = _onCredentialsRequired;
eventListeners.current.securityfailure = onSecurityFailure;
eventListeners.current.clipboard = onClipboard;
eventListeners.current.bell = onBell;
eventListeners.current.desktopname = _onDesktopName;
eventListeners.current.capabilities = onCapabilities;
(Object.keys(eventListeners.current) as (keyof typeof Events)[]).forEach((event) => {
if (eventListeners.current[event]) {
_rfb.addEventListener(event, eventListeners.current[event]);
}
});
setConnected(true);
} catch (err) {
logger.error(err);
}
};
const sendCredentials = (credentials: RFBOptions["credentials"]) => {
const rfb = getRfb();
rfb?.sendCredentials(credentials);
};
const sendKey = (keysym: number, code: string, down?: boolean) => {
const rfb = getRfb();
rfb?.sendKey(keysym, code, down);
};
const sendCtrlAltDel = () => {
const rfb = getRfb();
rfb?.sendCtrlAltDel();
};
const focus = () => {
const rfb = getRfb();
rfb?.focus();
};
const blur = () => {
const rfb = getRfb();
rfb?.blur();
};
const machineShutdown = () => {
const rfb = getRfb();
rfb?.machineShutdown();
};
const machineReboot = () => {
const rfb = getRfb();
rfb?.machineReboot();
};
const machineReset = () => {
const rfb = getRfb();
rfb?.machineReset();
};
const clipboardPaste = (text: string) => {
const rfb = getRfb();
rfb?.clipboardPasteFrom(text);
};
useImperativeHandle(ref, () => ({
connect,
disconnect,
connected: connected.current,
sendCredentials,
sendKey,
sendCtrlAltDel,
focus,
blur,
machineShutdown,
machineReboot,
machineReset,
clipboardPaste,
rfb: rfb.current,
eventListeners: eventListeners.current,
}));
useEffect(() => {
if (autoConnect) {
connect();
}
return disconnect;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const handleClick = () => {
const rfb = getRfb();
if (!rfb) return;
rfb.focus();
};
const handleMouseEnter = () => {
if (document.activeElement && document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
handleClick();
};
const handleMouseLeave = () => {
const rfb = getRfb();
if (!rfb) {
return;
}
rfb.blur();
};
return (
<>
<div
style={style}
className={className}
ref={screen}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
/>
{loading && (loadingUI ?? <div className="text-white loading">Loading...</div>)}
</>
);
}
export default forwardRef(VncScreen);