|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
namespace fkooman\VPN\Server; |
|
18
|
|
|
|
|
19
|
|
|
use fkooman\VPN\Server\Exception\ServerSocketException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Higher level abstraction of ServerSocket providing a cleaner API that |
|
23
|
|
|
* performs some post processing making it easier for applications to use. |
|
24
|
|
|
*/ |
|
25
|
|
|
class ServerApi |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $id; |
|
29
|
|
|
|
|
30
|
|
|
/** @var SocketInterface */ |
|
31
|
|
|
private $serverSocket; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct($id, ServerSocketInterface $serverSocket) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->id = $id; |
|
36
|
|
|
$this->serverSocket = $serverSocket; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getId() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->id; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Obtain information about connected clients. |
|
46
|
|
|
* |
|
47
|
|
|
* @return array information about the connected clients |
|
48
|
|
|
*/ |
|
49
|
|
|
public function status() |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
|
|
$this->serverSocket->open(); |
|
53
|
|
|
$response = $this->serverSocket->command('status 2'); |
|
54
|
|
|
$this->serverSocket->close(); |
|
55
|
|
|
|
|
56
|
|
|
return $this->ok('status', StatusParser::parse($response)); |
|
57
|
|
|
} catch (ServerSocketException $e) { |
|
58
|
|
|
return $this->error(); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Obtain OpenVPN version information. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string the OpenVPN version string |
|
66
|
|
|
*/ |
|
67
|
|
View Code Duplication |
public function version() |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
try { |
|
70
|
|
|
$this->serverSocket->open(); |
|
71
|
|
|
$response = $this->serverSocket->command('version'); |
|
72
|
|
|
$this->serverSocket->close(); |
|
73
|
|
|
|
|
74
|
|
|
return $this->ok( |
|
75
|
|
|
'version', |
|
76
|
|
|
substr($response[0], strlen('OpenVPN Version: ')) |
|
77
|
|
|
); |
|
78
|
|
|
} catch (ServerSocketException $e) { |
|
79
|
|
|
return $this->error(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Obtain the current load statistics from OpenVPN. |
|
85
|
|
|
* |
|
86
|
|
|
* @return array load statistics |
|
87
|
|
|
*/ |
|
88
|
|
|
public function loadStats() |
|
89
|
|
|
{ |
|
90
|
|
|
try { |
|
91
|
|
|
$keyMapping = array( |
|
92
|
|
|
'nclients' => 'number_of_clients', |
|
93
|
|
|
'bytesin' => 'bytes_in', |
|
94
|
|
|
'bytesout' => 'bytes_out', |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$this->serverSocket->open(); |
|
98
|
|
|
$response = $this->serverSocket->command('load-stats'); |
|
99
|
|
|
$this->serverSocket->close(); |
|
100
|
|
|
|
|
101
|
|
|
$statArray = explode(',', substr($response[0], strlen('SUCCESS: '))); |
|
102
|
|
|
$loadStats = array(); |
|
103
|
|
|
foreach ($statArray as $statItem) { |
|
104
|
|
|
list($key, $value) = explode('=', $statItem); |
|
105
|
|
|
if (array_key_exists($key, $keyMapping)) { |
|
106
|
|
|
$loadStats[$keyMapping[$key]] = intval($value); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->ok('load-stats', $loadStats); |
|
111
|
|
|
} catch (ServerSocketException $e) { |
|
112
|
|
|
return $this->error(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Disconnect a client. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $commonName the common name of the connection to kill |
|
120
|
|
|
*/ |
|
121
|
|
View Code Duplication |
public function kill($commonName) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
try { |
|
124
|
|
|
$this->serverSocket->open(); |
|
125
|
|
|
$response = $this->serverSocket->command(sprintf('kill %s', $commonName)); |
|
126
|
|
|
$this->serverSocket->close(); |
|
127
|
|
|
|
|
128
|
|
|
return $this->ok('kill', 0 === strpos($response[0], 'SUCCESS: ')); |
|
129
|
|
|
} catch (ServerSocketException $e) { |
|
130
|
|
|
return $this->error(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
private function ok($command, $response) |
|
135
|
|
|
{ |
|
136
|
|
|
return array( |
|
137
|
|
|
'id' => $this->getId(), |
|
138
|
|
|
'ok' => true, |
|
139
|
|
|
$command => $response, |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function error() |
|
144
|
|
|
{ |
|
145
|
|
|
return array( |
|
146
|
|
|
'id' => $this->getId(), |
|
147
|
|
|
'ok' => false, |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..