|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenVPN; |
|
4
|
|
|
|
|
5
|
|
|
use OpenVPN\Interfaces\ConfigInterface; |
|
6
|
|
|
use OpenVPN\Interfaces\GeneratorInterface; |
|
7
|
|
|
use function count; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Generator |
|
11
|
|
|
* |
|
12
|
|
|
* @since 1.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class Generator implements GeneratorInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \OpenVPN\Interfaces\ConfigInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Generator constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param \OpenVPN\Interfaces\ConfigInterface $config |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(ConfigInterface $config) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->config = $config; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Generate config in array format |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
private function generateArray(): array |
|
37
|
|
|
{ |
|
38
|
|
|
// Init the variable |
|
39
|
|
|
$config = []; |
|
40
|
|
|
|
|
41
|
|
|
// Basic parameters first |
|
42
|
|
|
foreach ($this->config->getParameters() as $key => $value) { |
|
43
|
|
|
$config[] = $key . ($value !== '' ? ' ' . $value : ''); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Get all what need for normal work |
|
47
|
|
|
$pushes = $this->config->getPushes(); |
|
48
|
|
|
$routes = $this->config->getRoutes(); |
|
49
|
|
|
$remotes = $this->config->getRemotes(); |
|
50
|
|
|
$certs = $this->config->getCerts(); |
|
51
|
|
|
|
|
52
|
|
|
// If we have routes or pushes in lists then generate it |
|
53
|
|
|
if (count($pushes) || count($routes) || count($remotes)) { |
|
54
|
|
|
foreach ($pushes as $push) { |
|
55
|
|
|
$config[] = 'push "' . $push . '"'; |
|
56
|
|
|
} |
|
57
|
|
|
foreach ($routes as $route) { |
|
58
|
|
|
$config[] = 'route ' . $route; |
|
59
|
|
|
} |
|
60
|
|
|
foreach ($remotes as $remote) { |
|
61
|
|
|
$config[] = 'remote ' . $remote; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Certs should be below everything, due embedded keys and certificates |
|
66
|
|
|
if (count($certs) > 0) { |
|
67
|
|
|
foreach ($this->config->getCerts() as $key => $value) { |
|
68
|
|
|
$config[] .= isset($value['content']) |
|
69
|
|
|
? "<$key>\n{$value['content']}\n</$key>" |
|
70
|
|
|
: "$key {$value['path']}"; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $config; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Generate config in JSON format |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
private function generateJson(): string |
|
83
|
|
|
{ |
|
84
|
|
|
$config = $this->generateArray(); |
|
85
|
|
|
|
|
86
|
|
|
return json_encode($config, JSON_PRETTY_PRINT); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Generate config in RAW format |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
private function generateRaw(): string |
|
95
|
|
|
{ |
|
96
|
|
|
$config = $this->generateArray(); |
|
97
|
|
|
|
|
98
|
|
|
return implode(PHP_EOL, $config); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Generate config by parameters in memory |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $type Type of generated config: raw (default), json, array |
|
105
|
|
|
* |
|
106
|
|
|
* @return array|string|null |
|
107
|
|
|
*/ |
|
108
|
|
|
public function generate(string $type = 'raw') |
|
109
|
|
|
{ |
|
110
|
|
|
if ($type === 'raw') { |
|
111
|
|
|
return $this->generateRaw(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ($type === 'json') { |
|
115
|
|
|
return $this->generateJson(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if ($type === 'array') { |
|
119
|
|
|
return $this->generateArray(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return null; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|