]>
git.proxmox.com Git - pve-xtermjs.git/blob - src/www/main.js
1 console
.log('xtermjs: starting');
20 starttime
= new Date();
22 var type
= getQueryParameter('console');
23 var vmid
= getQueryParameter('vmid');
24 var vmname
= getQueryParameter('vmname');
25 var nodename
= getQueryParameter('node');
26 var cmd
= getQueryParameter('cmd');
27 var cmdOpts
= getQueryParameter('cmd-opts');
29 function updateState(newState
, msg
, code
) {
30 var timeout
, severity
, message
;
32 case states
.connecting
:
33 message
= "Connecting...";
35 severity
= severities
.warning
;
37 case states
.connected
:
38 window
.onbeforeunload
= windowUnload
;
39 message
= "Connected";
41 case states
.disconnecting
:
42 window
.onbeforeunload
= undefined;
43 message
= "Disconnecting...";
45 severity
= severities
.warning
;
47 case states
.reconnecting
:
48 window
.onbeforeunload
= undefined;
49 message
= "Reconnecting...";
51 severity
= severities
.warning
;
53 case states
.disconnected
:
54 window
.onbeforeunload
= undefined;
57 case states
.connecting
:
58 case states
.reconnecting
:
59 message
= "Connection failed";
61 severity
= severities
.error
;
63 case states
.connected
:
64 case states
.disconnecting
:
65 var time_since_started
= new Date() - starttime
;
67 if (time_since_started
> 5*1000 || type
=== 'shell') {
68 message
= "Connection closed";
70 message
= "Connection failed";
71 severity
= severities
.error
;
74 case states
.disconnected
:
78 throw "unknown state";
82 throw "unknown state";
88 if (code
!== undefined) {
89 msgArr
.push(`Code: ${code}`);
91 if (msgArr
.length
> 0) {
92 message
+= ` (${msgArr.join(', ')})`;
95 showMsg(message
, timeout
, severity
);
98 var terminalContainer
= document
.getElementById('terminal-container');
99 document
.getElementById('status_bar').addEventListener('click', hideMsg
);
100 document
.getElementById('connect_btn').addEventListener('click', startGuest
);
101 const fitAddon
= new FitAddon
.FitAddon();
105 function startConnection(url
, params
, term
) {
109 url
: url
+ '/termproxy',
110 success: function(result
) {
111 var port
= encodeURIComponent(result
.data
.port
);
112 ticket
= result
.data
.ticket
;
113 socketURL
= protocol
+ location
.hostname
+ ((location
.port
) ? (':' + location
.port
) : '') + '/api2/json' + url
+ '/vncwebsocket?port=' + port
+ '&vncticket=' + encodeURIComponent(ticket
);
115 term
.open(terminalContainer
, true);
116 socket
= new WebSocket(socketURL
, 'binary');
117 socket
.binaryType
= 'arraybuffer';
118 socket
.onopen
= runTerminal
;
119 socket
.onclose
= tryReconnect
;
120 socket
.onerror
= tryReconnect
;
121 updateState(states
.connecting
);
123 failure: function(msg
) {
124 updateState(states
.disconnected
,msg
);
129 function startGuest() {
130 let api_type
= type
=== 'kvm' ? 'qemu' : 'lxc';
133 url
: `/nodes/${nodename}/${api_type}/${vmid}/status/start`,
134 success: function(result
) {
135 showMsg('Guest started successfully', 0);
136 setTimeout(function() {
140 failure: function(msg
) {
141 if (msg
.match(/already running/)) {
142 showMsg('Guest started successfully', 0);
143 setTimeout(function() {
147 updateState(states
.disconnected
,msg
);
153 function createTerminal() {
154 term
= new Terminal(getTerminalSettings());
155 term
.loadAddon(fitAddon
);
157 term
.onResize(function (size
) {
158 if (state
=== states
.connected
) {
159 socket
.send("1:" + size
.cols
+ ":" + size
.rows
+ ":");
163 protocol
= (location
.protocol
=== 'https:') ? 'wss://' : 'ws://';
166 var url
= '/nodes/' + nodename
;
169 url
+= '/qemu/' + vmid
;
172 url
+= '/lxc/' + vmid
;
175 params
.cmd
= 'upgrade';
178 params
.cmd
= decodeURI(cmd
);
179 if (cmdOpts
!== undefined && cmdOpts
!== null && cmdOpts
!== "") {
180 params
['cmd-opts'] = decodeURI(cmdOpts
);
184 if (type
=== 'kvm' || type
=== 'lxc') {
187 url
: `${url}/status/current`,
188 success: function(result
) {
189 if (result
.data
.status
=== 'running') {
190 startConnection(url
, params
, term
);
192 document
.getElementById('connect_dlg').classList
.add('pve_open');
195 failure: function(msg
) {
196 updateState(states
.disconnected
, msg
);
200 startConnection(url
, params
, term
);
204 function runTerminal() {
205 socket
.onmessage = function(event
) {
206 var answer
= new Uint8Array(event
.data
);
207 if (state
=== states
.connected
) {
209 } else if(state
=== states
.connecting
) {
210 if (answer
[0] === 79 && answer
[1] === 75) { // "OK"
211 updateState(states
.connected
);
212 term
.write(answer
.slice(2));
219 term
.onData(function(data
) {
220 if (state
=== states
.connected
) {
221 socket
.send("0:" + unescape(encodeURIComponent(data
)).length
.toString() + ":" + data
);
225 ping
= setInterval(function() {
229 window
.addEventListener('resize', function() {
230 clearTimeout(resize
);
231 resize
= setTimeout(function() {
237 socket
.send(PVE
.UserName
+ ':' + ticket
+ "\n");
239 // initial focus and resize
240 setTimeout(function() {
246 function getLxcStatus(callback
) {
249 url
: '/nodes/' + nodename
+ '/lxc/' + vmid
+ '/status/current',
250 success: function(result
) {
251 if (typeof callback
=== 'function') {
252 callback(true, result
);
255 failure: function(msg
) {
256 if (typeof callback
=== 'function') {
257 callback(false, msg
);
263 function checkMigration() {
265 if (apitype
=== 'kvm') {
273 url
: '/cluster/resources',
274 success: function(result
) {
275 // if not yet migrated , wait and try again
276 // if not migrating and stopped, cancel
277 // if started, connect
278 result
.data
.forEach(function(entity
) {
279 if (entity
.id
=== (apitype
+ '/' + vmid
)) {
280 var started
= entity
.status
=== 'running';
281 var migrated
= entity
.node
!== nodename
;
284 // goto different node
285 location
.href
= '?console=' + type
+
286 '&xtermjs=1&vmid=' + vmid
+ '&vmname=' +
287 vmname
+ '&node=' + entity
.node
;
290 updateState(states
.reconnecting
, 'waiting for migration to finish...');
291 setTimeout(checkMigration
, 5000);
294 if (type
=== 'lxc') {
295 // we have to check the status of the
296 // container to know if it has the
298 getLxcStatus(function(success
, result
) {
300 if (result
.data
.lock
=== 'migrate') {
302 updateState(states
.reconnecting
, 'waiting for migration to finish...');
303 setTimeout(checkMigration
, 5000);
304 } else if (started
) {
305 // container was rebooted
311 // probably the status call failed because
312 // the ct is already somewhere else, so retry
313 setTimeout(checkMigration
, 1000);
316 } else if (started
) {
317 // this happens if we have old data in
318 // /cluster/resources, or the connection
319 // disconnected, so simply try to reload here
321 } else if (type
=== 'kvm') {
322 // it seems the guest simply stopped
331 failure: function(msg
) {
332 errorTerminal({msg
: msg
});
337 function tryReconnect(event
) {
338 var time_since_started
= new Date() - starttime
;
339 var type
= getQueryParameter('console');
340 if (time_since_started
< 5*1000 || type
=== 'shell' || type
=== 'cmd') { // 5 seconds
345 updateState(states
.disconnecting
, 'Detecting migration...');
346 setTimeout(checkMigration
, 5000);
349 function clearEvents() {
350 term
.onResize(() => {});
351 term
.onData(() => {});
354 function windowUnload(e
) {
355 let message
= "Are you sure you want to leave this page?";
357 e
= e
|| window
.event
;
359 e
.returnValue
= message
;
365 function stopTerminal(event
) {
370 updateState(states
.disconnected
, event
.reason
, event
.code
);
373 function errorTerminal(event
) {
379 updateState(states
.disconnected
, event
.msg
, event
.code
);