1
|
<?php
|
2
|
/*
|
3
|
* syslog.inc
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2013 BSD Perimeter
|
7
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
8
|
* Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
|
9
|
* All rights reserved.
|
10
|
*
|
11
|
* originally part of m0n0wall (http://m0n0.ch/wall)
|
12
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
13
|
* All rights reserved.
|
14
|
*
|
15
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
16
|
* you may not use this file except in compliance with the License.
|
17
|
* You may obtain a copy of the License at
|
18
|
*
|
19
|
* http://www.apache.org/licenses/LICENSE-2.0
|
20
|
*
|
21
|
* Unless required by applicable law or agreed to in writing, software
|
22
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
23
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
24
|
* See the License for the specific language governing permissions and
|
25
|
* limitations under the License.
|
26
|
*/
|
27
|
|
28
|
require_once("globals.inc");
|
29
|
require_once('config.inc');
|
30
|
|
31
|
// Read the Wireguard configuration from $config.xml and create a number of *.conf files for wg-quick to read
|
32
|
function wg_create_config_files() {
|
33
|
global $config, $g;
|
34
|
|
35
|
if ($config["wireguard"]["tunnel"]) {
|
36
|
$wg_tunnels = $config["wireguard"]["tunnel"];
|
37
|
$cfgpath = $g["wg_conf_path"];
|
38
|
|
39
|
if (!file_exists($cfgpath)) {
|
40
|
mkdir($cfgpath, 0644);
|
41
|
}
|
42
|
}
|
43
|
|
44
|
$idx = 0;
|
45
|
foreach ($wg_tunnels as $tunnel) {
|
46
|
make_wg_conf($tunnel, $idx);
|
47
|
$idx++;
|
48
|
}
|
49
|
}
|
50
|
|
51
|
function make_wg_conf($tunnel, $idx) {
|
52
|
$txt = "# This Wireguard config file has been created automatically. Do not edit!\n";
|
53
|
|
54
|
$txt .= "# Description: {$tunnel['descr']}\n\n";
|
55
|
|
56
|
// Process Interfaces section
|
57
|
$txt .= "[Interface]\n";
|
58
|
|
59
|
if (isset($tunnel["interface"]["privatekey"]) && strlen($tunnel["interface"]["privatekey"]) > 0 ) {
|
60
|
$txt .= "PrivateKey = {$tunnel["interface"]["privatekey"]}\n";
|
61
|
}
|
62
|
|
63
|
if (isset($tunnel["interface"]["address"]) && strlen($tunnel["interface"]["address"]) > 0 ) {
|
64
|
$txt .= "Address = {$tunnel["interface"]["address"]}\n";
|
65
|
}
|
66
|
|
67
|
if (isset($tunnel["interface"]["listenport"]) && strlen($tunnel["interface"]["listenport"]) > 0 ) {
|
68
|
$txt .= "ListenPort = {$tunnel["interface"]["listenport"]}\n";
|
69
|
}
|
70
|
|
71
|
$txt .= "\n";
|
72
|
|
73
|
// Process peers section
|
74
|
if (isset($tunnel["peer"]) && count($tunnel["peer"]) > 0) {
|
75
|
global $g;
|
76
|
|
77
|
foreach ($tunnel["peer"] as $peer) {
|
78
|
$txt .= "# Peer: {$peer['descr']}\n";
|
79
|
$txt .= "[Peer]\n";
|
80
|
|
81
|
if (isset($peer["publickey"]) && strlen($peer["publickey"]) > 0 ) {
|
82
|
$txt .= "PublicKey = {$peer['publickey']}\n";
|
83
|
}
|
84
|
|
85
|
if (isset($peer["address"]) && strlen($peer["address"]) > 0 ) {
|
86
|
$txt .= "Address = {$peer['address']}\n";
|
87
|
}
|
88
|
|
89
|
if (isset($peer["listenport"]) && strlen($peer["listenport"]) > 0 ) {
|
90
|
$txt .= "ListenPort = {$peer['listenport']}\n";
|
91
|
}
|
92
|
|
93
|
if (isset($peer["endpoint"]) && strlen($peer["endpoint"]) > 0 ) {
|
94
|
$txt .= "EndPoint = {$peer['endpoint']}\n";
|
95
|
}
|
96
|
|
97
|
if (isset($peer["allowedips"]) && strlen($peer["allowedips"]) > 0 ) {
|
98
|
$txt .= "AllowedIPs = {$peer['allowedips']}\n";
|
99
|
}
|
100
|
|
101
|
$txt .= "\n";
|
102
|
}
|
103
|
}
|
104
|
|
105
|
file_put_contents($g["wg_conf_path"] . "/wg_" . $idx . ".conf", $txt);
|
106
|
}
|
107
|
|