|
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
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server; |
|
19
|
|
|
|
|
20
|
|
|
use RuntimeException; |
|
21
|
|
|
|
|
22
|
|
|
class Utils |
|
23
|
|
|
{ |
|
24
|
|
|
public static function exec($cmd, $throwExceptionOnFailure = true) |
|
25
|
|
|
{ |
|
26
|
|
|
exec($cmd, $output, $returnValue); |
|
27
|
|
|
|
|
28
|
|
|
if (0 !== $returnValue) { |
|
29
|
|
|
if ($throwExceptionOnFailure) { |
|
30
|
|
|
throw new RuntimeException( |
|
31
|
|
|
sprintf('command "%s" did not complete successfully (%d)', $cmd, $returnValue) |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function writeTempConfig($tmpConfig, array $configFileData) |
|
38
|
|
|
{ |
|
39
|
|
|
if (false === @file_put_contents($tmpConfig, implode(PHP_EOL, $configFileData))) { |
|
40
|
|
|
throw new RuntimeException('unable to write temporary config file'); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function normalizeIP($ipAddress) |
|
45
|
|
|
{ |
|
46
|
|
|
return inet_ntop(inet_pton($ipAddress)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function getActiveLeases($leaseDir) |
|
50
|
|
|
{ |
|
51
|
|
|
$activeLeases = []; |
|
52
|
|
|
foreach (glob(sprintf('%s/*', $leaseDir)) as $leaseFile) { |
|
53
|
|
|
$activeLeases[] = basename($leaseFile); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $activeLeases; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function addRoute4($v4, $dev) |
|
60
|
|
|
{ |
|
61
|
|
|
self::delRoute4($v4, false); |
|
62
|
|
|
self::flushRouteCache4(); |
|
63
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -4 ro add %s/32 dev %s', $v4, $dev); |
|
64
|
|
|
self::exec($cmd); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function addRoute6($v6, $dev) |
|
68
|
|
|
{ |
|
69
|
|
|
self::delRoute6($v6, false); |
|
70
|
|
|
self::flushRouteCache6(); |
|
71
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -6 ro add %s/128 dev %s', $v6, $dev); |
|
72
|
|
|
self::exec($cmd); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public static function delRoute4($v4, $throwExceptionOnFailure = true) |
|
76
|
|
|
{ |
|
77
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -4 ro del %s/32', $v4); |
|
78
|
|
|
self::exec($cmd, $throwExceptionOnFailure); |
|
79
|
|
|
self::flushRouteCache4(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public static function delRoute6($v6, $throwExceptionOnFailure = true) |
|
83
|
|
|
{ |
|
84
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -6 ro del %s/128', $v6); |
|
85
|
|
|
self::exec($cmd, $throwExceptionOnFailure); |
|
86
|
|
|
self::flushRouteCache6(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private static function flushRouteCache4() |
|
90
|
|
|
{ |
|
91
|
|
|
$cmd = '/usr/bin/sudo /sbin/ip -4 ro flush cache'; |
|
92
|
|
|
self::exec($cmd, false); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
private static function flushRouteCache6() |
|
96
|
|
|
{ |
|
97
|
|
|
$cmd = '/usr/bin/sudo /sbin/ip -6 ro flush cache'; |
|
98
|
|
|
self::exec($cmd, false); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|