forked from victronenergy/javascript-vnc-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
365 lines (322 loc) · 12.7 KB
/
index.php
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Remote Console on LAN</title>
<!--
Modified version of noVNC example, source is here.
https://github.com/kanaka/noVNC/blob/master/vnc_auto.html
Copyright and license of original file:
noVNC example: simple example using default UI
Copyright (C) 2012 Joel Martin
Copyright (C) 2013 Samuel Mannehed for Cendio AB
noVNC is licensed under the MPL 2.0 (see LICENSE.txt)
This file is licensed under the 2-Clause BSD license (see LICENSE.txt).
-->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link href="styling/styles.css" media="screen" rel="stylesheet" type="text/css" />
<script src="include/util.js"></script>
<script src="scripts/jquery-2.1.3.js"></script>
<script src="scripts/bCrypt.js"></script>
</head>
<body>
<div id="modal-popup-container-remote-console" class="modal-popup-container remote-console-popup-container mfp-with-anim">
<div class="remote-console-status">
<div class="remote-console-status-content">
<div class="remote-console-login-icon alarm">
<img src="/styling/img/svg-icons/icon_12_alarm-32px.svg" class="remote-console-control-icon svg" />
</div>
<div class="remote-console-login-icon notification">
<img src="/styling/img/svg-icons/icon_11_notification-32px.svg" class="remote-console-control-icon svg" />
</div>
<h2 class="remote-console-login-title">Remote console</h2>
<div class="remote-console-status-text"></div>
</div>
</div>
<div class="remote-console-login">
<div class="remote-console-display-container">
<div class="remote-console-login-contents">
<div class="remote-console-login-icon"><img src="/styling/img/svg-icons/password.svg" class="remote-console-control-icon svg" /></div>
<h2 class="remote-console-login-title">Remote console</h2>
<div class="remote-console-login-fields-container">
<div class="remote-console-login-field-title">Password <span class="remote-console-login-field-title-asterisk">*</span></div>
<div class="remote-console-login-fields">
<input type="password" id="remote-console-password"><button id="remote-console-login" class="clickable btn green"><img src="/styling/img/svg-icons/enter.svg" class="remote-console-control-icon svg" /></button>
</div>
</div>
</div>
</div>
<div class="remote-console-controls-container">
<div class="remote-console-controls-text">
<h3 class="remote-console-controls-title">Forgot the password?</h3>
<h4 class="remote-console-controls-explanation">Enter a new one on the Color Control.</h4>
</div>
</div>
</div>
<div class="remote-console-logged-in">
<h2 class="remote-console-title"></h2>
<div class="remote-console-display-container">
<div class="remote-console-display">
<div class="remote-console-display-inner">
<canvas id="remote-console-canvas">
Your browser does not support canvas, which is required for using the remote console.
</canvas>
</div>
</div>
</div>
<div class="remote-console-controls-container">
<div class="remote-console-controls-text">
<h2 class="remote-console-controls-title"></h2>
<h2 class="remote-console-controls-subtitle">hotkeys</h2>
</div>
<div class="remote-console-controls">
<div class="remote-console-controls-row">
<div class="clickable btn light remote-console-control-button left-button wide" data-button="left-button"><span class="remote-console-control-icon text">esc</span><span class="remote-console-control-icon"> </span></div>
<div class="clickable btn light remote-console-control-button right-button wide" data-button="right-button"><img src="/styling/img/svg-icons/enter.svg" class="remote-console-control-icon svg" /></div>
</div>
<div class="remote-console-controls-row">
<div class="clickable btn light remote-console-control-button up" data-button="up"><img src="/styling/img/svg-icons/arrow-up.svg" class="remote-console-control-icon svg" /></div>
</div>
<div class="remote-console-controls-row">
<div class="clickable btn light remote-console-control-button left" data-button="left"><img src="/styling/img/svg-icons/arrow-left.svg" class="remote-console-control-icon svg" /></div>
<div class="clickable btn light remote-console-control-button down" data-button="down"><img src="/styling/img/svg-icons/arrow-down.svg" class="remote-console-control-icon svg" /></div>
<div class="clickable btn light remote-console-control-button right" data-button="right"><img src="/styling/img/svg-icons/arrow-right.svg" class="remote-console-control-icon svg" /></div>
</div>
<div class="remote-console-controls-row">
<div class="clickable btn light remote-console-control-button center extra-wide" data-button="center"><img src="/styling/img/svg-icons/spacebar.svg" class="remote-console-control-icon svg" /></div>
</div>
</div>
</div>
</div>
</div>
<div class="remote-console-rotate-message-container">
<div class="rotate-message-contents">
<div class="rotate-message-icon"><img src="/styling/img/svg-icons/rotate-72px.svg" class="remote-console-control-icon svg" /></div>
<h2 class="rotate-message-title">Rotate your screen</h2>
</div>
</div>
<script type="text/javascript">
"use strict";
// Load supporting scripts
Util.load_scripts(["webutil.js", "base64.js", "websock.js", "des.js", "keysymdef.js", "keyboard.js", "input.js", "display.js", "jsunzip.js", "rfb.js", "keysym.js"]);
var attempt = 0;
var rfb = null;
var disconnected = false;
var remoteConsole, identifier;
window.onscriptsload = function() {
// Connection settings
var host = WebUtil.getQueryVar('host', window.location.hostname);
var port = WebUtil.getQueryVar('port', 81);
var password = WebUtil.getQueryVar('password', '');
var path = WebUtil.getQueryVar('path', 'websockify');
<?php
$identifier = @file_get_contents('/sys/class/net/eth0/address');
?>
var identifier = '<?php echo $identifier !== false ? trim(str_replace(':', '', strtolower($identifier))) : ''?>';
remoteConsole = $("#modal-popup-container-remote-console");
window.addEventListener('beforeunload', function() {
disconnect();
});
// Resize display on window resize
window.addEventListener('resize', function() {
resize();
});
// Password form
var sendPassword = function() {
if(disconnected) {
initRfb();
rfb.connect(host, port, getHashFromPasswordField(identifier), path);
} else {
rfb.sendPassword(getHashFromPasswordField(identifier));
}
};
remoteConsole.find('#remote-console-login').click(sendPassword);
remoteConsole.find('#remote-console-password').bind('keypress', function(e) {
if((e.keyCode ? e.keyCode : e.which) == 13) sendPassword();
});
//Attach click listeners for the buttons
remoteConsole.find('[data-button]').click(function() {
sendButton($(this).data('button'));
});
// Set up logging to console
WebUtil.init_logging('warn');
showStatus('Connecting.', 'notification');
disconnected = false;
// Set up connection
initRfb();
// Initial resize
resize();
// Connect
rfb.connect(host, port, password, path);
attempt = 0;
};
function initRfb() {
rfb = new RFB({
'target': $D('remote-console-canvas'),
'encrypt': WebUtil.getQueryVar('encrypt', false),
'repeaterID': '',
'true_color': true,
'local_cursor': false,
'shared': true,
'disconnectTimeout': 3,
'view_only': false,
onUpdateState: function(rfb, state, oldstate, message) {
switch(state) {
case 'normal':
showRemoteConsoleScreen();
break;
case 'failed':
switch(oldstate) {
case 'normal':
showStatus('Disconnected.', 'alarm');
break;
case 'connect':
showStatus('Failed to connect. Make sure to enable Remote<br />Console for LAN, and reboot the Color Control.', 'alarm');
break;
case 'SecurityResult':
(rfb.get_onPasswordRequired())(rfb);
break;
default:
showStatus('Failed to connect to the CCGX.', 'alarm');
break;
}
break;
case 'disconnected':
disconnected = true;
switch(oldstate) {
case 'failed':
// Already handled.
break;
case 'normal':
// Normal disconnect
showStatus('Disconnected.', 'notification');
break;
}
break;
}
},
onPasswordRequired: function(rfb) {
showPasswordScreen();
if (attempt > 1) {
// Password was entered incorrectly
alert('Incorrect password');
}
attempt++;
}
});
}
function getHashFromPasswordField(identifier) {
var password = remoteConsole.find('#remote-console-password');
if (password.val().length == 0) {
return '';
}
var setting = genSaltSyncFromString(8, identifier + identifier)
var hash = hashSync(password.val(), setting);
if (hash.length > 8) {
return hash.substr(hash.length - 8);
} else {
alert('Something went wrong while connecting to your CCGX');
}
}
function showPasswordScreen() {
remoteConsole.removeClass('logged-in').addClass("password-needed");;
hideStatus();
}
function showRemoteConsoleScreen() {
remoteConsole.removeClass("password-needed").addClass('logged-in');
hideStatus();
}
function showStatus(status, type) {
var statusContainer = remoteConsole.find('.remote-console-status');
remoteConsole.removeClass("password-needed").removeClass('logged-in');
remoteConsole.find('.remote-console-status-text').html(status).show();
statusContainer.removeClass('notification');
statusContainer.removeClass('alarm');
statusContainer.addClass(type);
}
function hideStatus() {
remoteConsole.find('.remote-console-status-text').empty().hide();
}
function disconnect() {
// Check if we're connected
if(rfb) {
// Disconnect
rfb.disconnect();
rfb = null;
}
// Hide any status messages
hideStatus();
}
function resize() {
var $display = remoteConsole.find('.remote-console-display-inner');
var windowHeight = $(window).height();
var windowWidth = $(window).width();
var isPortrait = windowHeight > windowWidth;
if (windowWidth <= 767) {
$display.height((windowWidth - 240) * 0.56);
} else {
$display.height('');
}
if (windowWidth < 686) {
$display.width(480 - (686 - windowWidth));
}
else {
$display.width('');
}
if (windowWidth < 768 && isPortrait && $('#modal-popup-container-remote-console').length > 0) {
$('.remote-console-rotate-message-container').show();
}
else {
$('.remote-console-rotate-message-container').hide();
}
}
function sendButton(button) {
// Check if we're connected
if(rfb) {
// Mapping buttons to key codes
// See https://www.cl.cam.ac.uk/~mgk25/ucs/keysymdef.h
var keymap = {
'primary': 65307, // Esc
'left-button': 65307, // Esc
'secondary': 65293, // Enter
'right-button': 65293, // Enter
'up': 65362, // Arrow up
'left': 65361, // Arrow left
'center': 32, // Space
'right': 65363, // Arrow right
'down': 65364 // Arrow down
};
// Send key
rfb.sendKey(keymap[button]);
}
}
$("img.svg").each(function() {
var $img = jQuery(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
jQuery.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = jQuery(data).find('svg');
// Add replaced image's ID to the new SVG
if(typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if(typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass+' replaced-svg');
}
// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Check if the viewport is set, if the viewport is not set the SVG wont't scale.
if(!$svg.attr('viewBox') && $svg.attr('height') && $svg.attr('width')) {
$svg.attr('viewBox', '0 0 ' + $svg.attr('height') + ' ' + $svg.attr('width'))
}
// Replace image with new SVG
$img.replaceWith($svg);
}, 'xml');
});
</script>
</body>
</html>