Repository navigation
Expand file tree
/
Copy pathsendkey.php
More file actions
54 lines (52 loc) · 2.06 KB
/
Copy pathsendkey.php
File metadata and controls
54 lines (52 loc) · 2.06 KB
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
<?php
// sends a key combination (DOMAIN_KEY_COMBOS) to a running machine; called with
// fetch() from the machine page and the console, answers JSON
require_once 'function.php';
header('Content-Type: application/json');
$fail = function ($code, $message) {
http_response_code($code);
echo json_encode(['ok' => false, 'error' => $message]);
exit;
};
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !hash_equals(csrf_token(), (string)($_POST['csrf'] ?? ''))) {
$fail(400, 'Invalid request (CSRF token mismatch).');
}
if ($readonly) {
$fail(403, t('Connection is read-only.'));
}
$node = (string)($_POST['node'] ?? '');
$key = (string)($_POST['key'] ?? '');
if (!machine_visible($con, $node)) {
$fail(400, t('Unknown machine: %s', $node));
}
if (!machine_allowed('operate', $node)) {
$fail(403, t('You do not have permission for this action.'));
}
$text = (string)($_POST['text'] ?? '');
if ($text === '' && !isset(DOMAIN_KEY_COMBOS[$key])) {
$fail(400, t('Unknown key combination.'));
}
$res = libvirt_domain_lookup_by_name($con, $node);
if (!libvirt_domain_is_active($res)) {
$fail(409, t('The machine is not running.'));
}
// "type text": every character as a key press (US layout), e.g. a password into a login prompt
if ($text !== '') {
$codes = mb_strlen($text) <= 1000 ? domain_text_keycodes($text) : null;
if ($codes === null) {
$fail(400, t('Only US keyboard characters can be typed (up to 1000), no letters with diacritics.'));
}
foreach ($codes as $combo) {
if (!libvirt_domain_send_key_api($res, 0, 20, $combo)) {
$fail(500, (string)libvirt_get_last_error());
}
usleep(15000);
}
action_log('type_text', $node, true, mb_strlen($text).' characters');
echo json_encode(['ok' => true]);
exit;
}
// codeset 0 = Linux key codes, hold 100 ms
$ok = (bool)libvirt_domain_send_key_api($res, 0, 100, DOMAIN_KEY_COMBOS[$key][1]);
action_log('send_key', $node, $ok, DOMAIN_KEY_COMBOS[$key][0]);
echo json_encode($ok ? ['ok' => true] : ['ok' => false, 'error' => (string)libvirt_get_last_error()]);