|
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\Http\Exception\BadRequestException; |
|
20
|
|
|
use RuntimeException; |
|
21
|
|
|
|
|
22
|
|
|
class Utils |
|
23
|
|
|
{ |
|
24
|
|
|
public static function validateCommonName($commonName) |
|
25
|
|
|
{ |
|
26
|
|
|
if (0 === preg_match('/^[a-zA-Z0-9-_.@]+$/', $commonName)) { |
|
27
|
|
|
throw new BadRequestException('invalid characters in common name'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// MUST NOT be '..' |
|
31
|
|
|
if ('..' === $commonName) { |
|
32
|
|
|
throw new BadRequestException('common name cannot be ".."'); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function validateUserId($userId) |
|
37
|
|
|
{ |
|
38
|
|
|
if (0 === preg_match('/^[a-zA-Z0-9-_.@]+$/', $userId)) { |
|
39
|
|
|
throw new BadRequestException('invalid characters in userId'); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function validateAddress($ipAddress) |
|
44
|
|
|
{ |
|
45
|
|
|
if (false === filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
46
|
|
|
throw new BadRequestException('invalid v4 address'); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Validate that the date is in YYYY-MM-DD format. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $dateString the date in YYYY-MM-DD format |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function validateDate($dateString) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $dateString)) { |
|
58
|
|
|
throw new BadRequestException('invalid date format'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function exec($cmd) |
|
63
|
|
|
{ |
|
64
|
|
|
exec($cmd, $output, $returnValue); |
|
65
|
|
|
|
|
66
|
|
|
if (0 !== $returnValue) { |
|
67
|
|
|
throw new RuntimeException( |
|
68
|
|
|
sprintf('command "%s" did not complete successfully (%d)', $cmd, $returnValue) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function writeTempConfig($tmpConfig, array $configFileData) |
|
74
|
|
|
{ |
|
75
|
|
|
if (false === @file_put_contents($tmpConfig, implode(PHP_EOL, $configFileData))) { |
|
76
|
|
|
throw new RuntimeException('unable to write temporary config file'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public static function getUsedIpList($ipPoolDir) |
|
81
|
|
|
{ |
|
82
|
|
|
$usedIpList = []; |
|
83
|
|
|
foreach (glob(sprintf('%s/*', $ipPoolDir)) as $ipFile) { |
|
84
|
|
|
$usedIpList[] = basename($ipFile); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $usedIpList; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public static function addRoute4($v4, $dev) |
|
91
|
|
|
{ |
|
92
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -4 ro add %s/32 dev %s', $v4, $dev); |
|
93
|
|
|
self::exec($cmd); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public static function addRoute6($v6, $dev) |
|
97
|
|
|
{ |
|
98
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -6 ro add %s/128 dev %s', $v6, $dev); |
|
99
|
|
|
self::exec($cmd); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public static function delRoute4($v4, $dev) |
|
103
|
|
|
{ |
|
104
|
|
|
try { |
|
105
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -4 ro del %s/32 dev %s', $v4, $dev); |
|
106
|
|
|
self::exec($cmd); |
|
107
|
|
|
} catch (RuntimeException $e) { |
|
108
|
|
|
// not the end of the world if the route cannot be deleted |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public static function delRoute6($v6, $dev) |
|
113
|
|
|
{ |
|
114
|
|
|
try { |
|
115
|
|
|
$cmd = sprintf('/usr/bin/sudo /sbin/ip -6 ro del %s/128 dev %s', $v6, $dev); |
|
116
|
|
|
self::exec($cmd); |
|
117
|
|
|
} catch (RuntimeException $e) { |
|
118
|
|
|
// not the end of the world if the route cannot be deleted |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|