-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connection.php
92 lines (76 loc) · 2.2 KB
/
Connection.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
<?php
/**
* Daniel Ziegler
* MIT License
*
* Connection handling taken from
* https://github.com/OrangeTux/einder/blob/master/einder/client.py
* many thanks!
*/
namespace nook24\Horizon;
class Connection {
/**
* @var string
*/
private $address;
/**
* @var int
*/
private $port;
/**
* @var resource
*/
private $socket;
/**
* Connection constructor.
* @param string $address
* @param int $port
*/
public function __construct($address = '192.168.1.200', $port = 5900) {
$this->address = $address;
$this->port = $port;
}
public function connect() {
//Many thanks to https://github.com/OrangeTux/einder/blob/master/einder/client.py#L23-L87
//Open the connection. If we do this, we get a version number or so
$this->socket = fsockopen($this->address, $this->port);
$version = \fgets($this->socket);
//To "authorize" with the horizon, we send the version back? :D
\fwrite($this->socket, $version);
//Horizon will return with 2 bytes
$response = $this->toHex(fgets($this->socket, 2)); //01
\fwrite($this->socket, $this->toBin("01"));
//Again we get 4 bytes of what so ever
$response = $this->toHex(fgets($this->socket, 4)); //010000
//To make the client work, we need to send this data
\fwrite($this->socket, $this->toBin("01"));
}
/**
* @param int $key
*/
function sendKey($key) {
if (!\is_resource($this->socket)) {
$this->connect();
}
\fwrite($this->socket, \pack('CCCCCCn', 4, 1, 0, 0, 0, 0, $key)); //Press key
usleep(200);
\fwrite($this->socket, \pack('CCCCCCn', 4, 0, 0, 0, 0, 0, $key)); //Release key
}
public function disconnect() {
\fclose($this->socket);
}
/**
* @param $data
* @return string
*/
public function toHex($data) {
return \bin2hex($data);
}
/**
* @param $data
* @return bool|string
*/
public function toBin($data) {
return \hex2bin($data);
}
}