|
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 RuntimeException; |
|
20
|
|
|
|
|
21
|
|
|
class Utils |
|
22
|
|
|
{ |
|
23
|
|
|
public static function exec($cmd) |
|
24
|
|
|
{ |
|
25
|
|
|
exec($cmd, $output, $returnValue); |
|
26
|
|
|
|
|
27
|
|
|
if (0 !== $returnValue) { |
|
28
|
|
|
throw new RuntimeException( |
|
29
|
|
|
sprintf('command "%s" did not complete successfully (%d)', $cmd, $returnValue) |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function writeTempConfig($tmpConfig, array $configFileData) |
|
35
|
|
|
{ |
|
36
|
|
|
if (false === @file_put_contents($tmpConfig, implode(PHP_EOL, $configFileData))) { |
|
37
|
|
|
throw new RuntimeException('unable to write temporary config file'); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function getUsedIpList($ipPoolDir) |
|
42
|
|
|
{ |
|
43
|
|
|
$usedIpList = []; |
|
44
|
|
|
foreach (glob(sprintf('%s/*', $ipPoolDir)) as $ipFile) { |
|
45
|
|
|
$usedIpList[] = basename($ipFile); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $usedIpList; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public static function addRoute4($v4, $dev) |
|
52
|
|
|
{ |
|
53
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -4 ro add %s/32 dev %s', $v4, $dev); |
|
54
|
|
|
self::exec($cmd); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function addRoute6($v6, $dev) |
|
58
|
|
|
{ |
|
59
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -6 ro add %s/128 dev %s', $v6, $dev); |
|
60
|
|
|
self::exec($cmd); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function delRoute4($v4) |
|
64
|
|
|
{ |
|
65
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -4 ro del %s/32', $v4); |
|
66
|
|
|
self::exec($cmd); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function delRoute6($v6) |
|
70
|
|
|
{ |
|
71
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -6 ro del %s/128', $v6); |
|
72
|
|
|
self::exec($cmd); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|