Project

General

Profile

Download (5.48 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * vpn_wg.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
##|+PRIV
23
##|*IDENT=page-vpn-wg
24
##|*NAME=VPN: WireGuard
25
##|*DESCR=Allow access to the 'VPN: WireGuard' page.
26
##|*MATCH=vpn_wg.php*
27
##|-PRIV
28

    
29
require_once("guiconfig.inc");
30
require_once("functions.inc");
31
require_once("wg.inc");
32

    
33
init_config_arr(array('wireguard', 'tunnel'));
34
$tunnels = &$config['wireguard']['tunnel'];
35

    
36
$pgtitle = array(gettext("VPN"), gettext("WireGuard"), gettext("Tunnels"));
37
$pglinks = array("", "@self", "@self");
38
$shortcut_section = "wireguard";
39

    
40
include("head.inc");
41

    
42
// Delete a tunnel?
43
if (array_key_exists('delidx', $_POST)) {
44
	deleteTunnel($_POST['delidx']);
45
	header("Location: vpn_wg.php");
46
}
47
?>
48

    
49
<form name="mainform" method="post">
50
<?php
51
	if (count($tunnels) == 0):
52
		print_info_box(gettext('No WireGuard tunnels have been configured. Click the "Add tunnel" button below to create one.'), 'warning', false);
53
	else:
54
?>
55
	<div class="panel panel-default">
56
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('WireGuard Tunnels')?></h2></div>
57
		<div class="panel-body table-responsive">
58
			<table class="table table-striped table-hover">
59
				<thead>
60
					<tr>
61
						<th class="peer-entries"></th>
62
						<th><?=gettext("Name")?></th>
63
						<th><?=gettext("Description")?></th>
64
						<th><?=gettext("Address")?></th>
65
						<th><?=gettext("Port")?></th>
66
						<th><?=gettext("# Peers")?></th>
67
						<th><?=gettext("Actions")?></th>
68
					</tr>
69
				</thead>
70
				<tbody>
71
<?php
72

    
73
		$i = 0;
74
		foreach ($tunnels as $tunnel):
75
			$entryStatus = ($tunnel['enabled'] == 'yes') ? 'enabled':'disabled';
76
			if (!$tunnel['peers'] || !is_array($tunnel['peers'])) {
77
				$tunnel['peers'] = array();
78
			}
79
			if (!$tunnel['peers']['wgpeer'] || !is_array($tunnel['peers']['wgpeer'])) {
80
				$tunnel['peers']['wgpeer'] = array();
81
			}
82
?>
83
					<tr id="fr<?=$i?>" id="frd<?=$i?>" class="<?= $entryStatus ?>">
84
						<td class="peer-entries"><?=gettext('Interface')?></td>
85
						<td><?=$tunnel['name']?></td>
86
						<td><?=$tunnel['descr']?></td>
87
						<td><?=$tunnel['interface']['address']?></td>
88
						<td><?=$tunnel['interface']['listenport']?></td>
89
						<td><?=count($tunnel['peers']['wgpeer'])?></td>
90

    
91
						<td style="cursor: pointer;">
92
							<a class="fa fa-pencil" href="vpn_wg_edit.php?index=<?=$i?>" title="<?=gettext("Edit tunnel"); ?>"></a>
93
							<a class="fa fa-trash text-danger" id="Xdel_<?=$i?>" title="<?=gettext('Delete tunnel'); ?>"></a>
94
						</td>
95
					</tr>
96

    
97
					<tr class="peer-entries" style="background-color:#ccf2ff;"> <!-- Move to pfSense.css -->
98
						<td>Peers</td>
99
<?php
100
			if (count($tunnel['peers']['wgpeer']) > 0):
101
?>
102
						<td colspan="6">
103
							<table class="table table-hover" style="background-color:#ccf2ff;"> <!-- Move to pfSense.css -->
104
								<thead>
105
									<tr>
106
										<th>Description</th>
107
										<th>Endpoint</th>
108
										<th>Allowed IPs</th>
109
										<th>Public key</th>
110
									</tr>
111
								</thead>
112
								<tbody>
113

    
114
<?php
115
				foreach ($tunnel['peers']['wgpeer'] as $peer):
116
?>
117
									<tr>
118
										<td><?=$peer['descr']?></td>
119
										<td><?=$peer['endpoint']?>:<?=$peer['port']?></td>
120
										<td><?=$peer['allowedips']?></td>
121
										<td><?=$peer['publickey']?></td>
122
									</tr>
123
<?php
124
				endforeach;
125
?>
126
								</tbody>
127
							</table>
128
						</td>
129
<?php
130
			else:
131
				print('<td colspan="6">' . gettext("No peers have been configured") . '</td>');
132
			endif;
133
?>
134
					</tr>
135
<?php
136
			$i++;
137
		endforeach;	 // $tunnelsa
138
?>
139
				</tbody>
140
			</table>
141
		</div>
142
	</div>
143

    
144

    
145
<?php
146
		$section = new Form_Section('Firewall keys');
147
		$section->addClass('fwkeys');
148

    
149
		$section->addInput(new Form_Input(
150
			'pubkey',
151
			'Public key',
152
			'',
153
			''
154
		))->setReadonly();
155

    
156
		$section->addInput(new Form_Input(
157
			'privkey',
158
			'Private key',
159
			'',
160
			''
161
		))->setReadonly();
162

    
163
		print($section);
164
	endif;
165
?>
166

    
167
	<nav class="action-buttons">
168
		<a href="#" class="btn btn-info btn-sm" id="showpeers">
169
			<i class="fa fa-info icon-embed-btn"></i>
170
			<?=gettext("Show peers")?>
171
		</a>
172

    
173
		<a href="vpn_wg_edit.php?index=<?=nextFreeWGInterfaceName();?>" class="btn btn-success btn-sm">
174
			<i class="fa fa-plus icon-embed-btn"></i>
175
			<?=gettext("Add Tunnel")?>
176
		</a>
177
	</nav>
178
</form>
179

    
180
<!-- Simple form that is submitted on tunnel delete -->
181
<form name="delform" id="delform" method="post">
182
	<input id="delidx" name="delidx" type="hidden" />
183
</form>
184

    
185
<script type="text/javascript">
186
//<![CDATA[
187

    
188
events.push(function() {
189
	var peershidden = true;
190
	var keyshidden = true;
191

    
192
	hideClass('peer-entries', peershidden);
193
	hideClass('fwkeys', keyshidden);
194

    
195
	// Toggle peer visibility
196
	$('#showpeers').click(function () {
197
		peershidden = !peershidden;
198
		hideClass('peer-entries', peershidden);
199
	})
200

    
201
	// Delete tunnel
202
	$('[id^=Xdel_]').click(function (event) {
203
		var idx = event.target.id.split('_')[1];
204
		$('#delidx').val(idx);  // Set the id of the tunnel
205
		$('#delform').submit(); // Submit the form
206
	});
207
});
208
//]]>
209
</script>
210

    
211
<?php
212
include("foot.inc");
(228-228/231)