Yoshie Muranaka | 198ce1f | 2019-09-20 10:33:04 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Directive for KVM (Kernel-based Virtual Machine) |
| 3 | * |
| 4 | * @module app/serverControl |
| 5 | * @exports kvmConsole |
| 6 | * @name kvmConsole |
| 7 | */ |
| 8 | |
| 9 | import RFB from '@novnc/novnc/core/rfb.js'; |
| 10 | |
| 11 | window.angular && (function(angular) { |
| 12 | 'use strict'; |
| 13 | |
| 14 | angular.module('app.serverControl').directive('kvmConsole', [ |
James Feist | c3f5e31 | 2020-04-07 15:19:59 -0700 | [diff] [blame^] | 15 | '$log', '$cookies', '$location', |
Yoshie Muranaka | 198ce1f | 2019-09-20 10:33:04 -0700 | [diff] [blame] | 16 | function($log, $location) { |
| 17 | return { |
| 18 | restrict: 'E', template: require('./kvm-console.html'), |
Dixsie Wolmers | bea45d5 | 2019-09-27 15:49:01 -0500 | [diff] [blame] | 19 | scope: {newWindowBtn: '=?'}, link: function(scope, element) { |
Yoshie Muranaka | 198ce1f | 2019-09-20 10:33:04 -0700 | [diff] [blame] | 20 | var rfb; |
| 21 | |
| 22 | element.on('$destroy', function() { |
| 23 | if (rfb) { |
| 24 | rfb.disconnect(); |
| 25 | } |
| 26 | }); |
| 27 | |
| 28 | function sendCtrlAltDel() { |
| 29 | rfb.sendCtrlAltDel(); |
| 30 | return false; |
| 31 | }; |
| 32 | |
| 33 | function connected(e) { |
| 34 | $log.debug('RFB Connected'); |
| 35 | } |
Dixsie Wolmers | bea45d5 | 2019-09-27 15:49:01 -0500 | [diff] [blame] | 36 | |
Yoshie Muranaka | 198ce1f | 2019-09-20 10:33:04 -0700 | [diff] [blame] | 37 | function disconnected(e) { |
| 38 | $log.debug('RFB disconnected'); |
| 39 | } |
| 40 | |
| 41 | var host = $location.host(); |
| 42 | var port = $location.port(); |
| 43 | var target = element[0].firstElementChild; |
| 44 | try { |
James Feist | c3f5e31 | 2020-04-07 15:19:59 -0700 | [diff] [blame^] | 45 | var token = $cookies.get('XSRF-TOKEN'); |
Yoshie Muranaka | 198ce1f | 2019-09-20 10:33:04 -0700 | [diff] [blame] | 46 | rfb = new RFB( |
James Feist | c3f5e31 | 2020-04-07 15:19:59 -0700 | [diff] [blame^] | 47 | target, 'wss://' + host + '/kvm/0', |
| 48 | {'wsProtocols': [token]}); |
Yoshie Muranaka | 198ce1f | 2019-09-20 10:33:04 -0700 | [diff] [blame] | 49 | rfb.addEventListener('connect', connected); |
| 50 | rfb.addEventListener('disconnect', disconnected); |
| 51 | } catch (exc) { |
| 52 | $log.error(exc); |
| 53 | updateState( |
| 54 | null, 'fatal', null, |
| 55 | 'Unable to create RFB client -- ' + exc); |
| 56 | return; // don't continue trying to connect |
| 57 | }; |
Dixsie Wolmers | bea45d5 | 2019-09-27 15:49:01 -0500 | [diff] [blame] | 58 | |
| 59 | scope.openWindow = function() { |
| 60 | window.open( |
| 61 | '#/server-control/kvm-window', 'Kvm Window', |
| 62 | 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=1125,height=900'); |
| 63 | }; |
Yoshie Muranaka | 198ce1f | 2019-09-20 10:33:04 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | } |
| 67 | ]); |
| 68 | })(window.angular); |