|
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
|
|
|
* @package OpenVPN |
|
13
|
|
|
* @since 1.0.0 |
|
14
|
|
|
*/ |
|
15
|
|
|
class Generator implements GeneratorInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \OpenVPN\Interfaces\ConfigInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $config; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Generator constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param \OpenVPN\Interfaces\ConfigInterface $config |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(ConfigInterface $config) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->config = $config; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Generate config by parameters in memory |
|
34
|
|
|
* |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
public function generate(): string |
|
38
|
|
|
{ |
|
39
|
|
|
// Init the variable |
|
40
|
|
|
$config = ''; |
|
41
|
|
|
|
|
42
|
|
|
// Basic parameters first |
|
43
|
|
|
foreach ($this->config->getParameters() as $key => $value) { |
|
44
|
|
|
$config .= $key . ($value !== '' ? ' ' . $value : '') . "\n"; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Get all what need for normal work |
|
48
|
|
|
$pushes = $this->config->getPushes(); |
|
49
|
|
|
$routes = $this->config->getRoutes(); |
|
50
|
|
|
$certs = $this->config->getCerts(); |
|
51
|
|
|
|
|
52
|
|
|
// If we have routes or pushes in lists then generate it |
|
53
|
|
|
if (count($pushes) || count($routes)) { |
|
54
|
|
|
$config .= "\n### Networking\n"; |
|
55
|
|
|
foreach ($routes as $route) { |
|
56
|
|
|
$config .= 'route ' . $route . "\n"; |
|
57
|
|
|
} |
|
58
|
|
|
foreach ($pushes as $push) { |
|
59
|
|
|
$config .= 'push "' . $push . "\"\n"; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// Certs should be below everything, due embedded keys and certificates |
|
64
|
|
|
if (count($certs) > 0) { |
|
65
|
|
|
$config .= "\n### Certificates\n"; |
|
66
|
|
|
foreach ($this->config->getCerts() as $key => $value) { |
|
67
|
|
|
$config .= isset($value['content']) |
|
68
|
|
|
? "<$key>\n{$value['content']}\n</$key>\n" |
|
69
|
|
|
: "$key {$value['path']}\n"; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $config; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|