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
|
2 |
|
public function __construct(ConfigInterface $config) |
27
|
|
|
{ |
28
|
2 |
|
$this->config = $config; |
29
|
2 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Generate config in array format |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
2 |
|
private function generateArray(): array |
37
|
|
|
{ |
38
|
|
|
// Init the variable |
39
|
2 |
|
$config = []; |
40
|
|
|
|
41
|
|
|
// Basic parameters first |
42
|
2 |
|
foreach ($this->config->getParameters() as $key => $value) { |
43
|
1 |
|
$config[] = $key . ($value !== '' ? ' ' . $value : ''); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Get all what need for normal work |
47
|
2 |
|
$pushes = $this->config->getPushes(); |
48
|
2 |
|
$routes = $this->config->getRoutes(); |
49
|
2 |
|
$remotes = $this->config->getRemotes(); |
50
|
2 |
|
$certs = $this->config->getCerts(); |
51
|
|
|
|
52
|
|
|
// If we have routes or pushes in lists then generate it |
53
|
2 |
|
if (count($pushes) || count($routes) || count($remotes)) { |
54
|
2 |
|
foreach ($pushes as $push) { |
55
|
2 |
|
$config[] = 'push "' . $push . '"'; |
56
|
|
|
} |
57
|
2 |
|
foreach ($routes as $route) { |
58
|
1 |
|
$config[] = 'route ' . $route; |
59
|
|
|
} |
60
|
2 |
|
foreach ($remotes as $remote) { |
61
|
1 |
|
$config[] = 'remote ' . $remote; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Certs should be below everything, due embedded keys and certificates |
66
|
2 |
|
if (count($certs) > 0) { |
67
|
1 |
|
foreach ($this->config->getCerts() as $key => $value) { |
68
|
1 |
|
$config[] .= isset($value['content']) |
69
|
|
|
? "<$key>\n{$value['content']}\n</$key>" |
70
|
1 |
|
: "$key {$value['path']}"; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
return $config; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Generate config in JSON format |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
private function generateJson(): string |
83
|
|
|
{ |
84
|
1 |
|
$config = $this->generateArray(); |
85
|
|
|
|
86
|
1 |
|
return json_encode($config, JSON_PRETTY_PRINT); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Generate config in RAW format |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
2 |
|
private function generateRaw(): string |
95
|
|
|
{ |
96
|
2 |
|
$config = $this->generateArray(); |
97
|
|
|
|
98
|
2 |
|
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
|
2 |
|
public function generate(string $type = 'raw') |
109
|
|
|
{ |
110
|
2 |
|
if ($type === 'raw') { |
111
|
2 |
|
return $this->generateRaw(); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
if ($type === 'json') { |
115
|
1 |
|
return $this->generateJson(); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
if ($type === 'array') { |
119
|
1 |
|
return $this->generateArray(); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
return null; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|