Project

General

Profile

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

    
25
##|+PRIV
26
##|*IDENT=page-status-openvpn
27
##|*NAME=Status: OpenVPN
28
##|*DESCR=Allow access to the 'Status: OpenVPN' page.
29
##|*MATCH=status_openvpn.php*
30
##|-PRIV
31

    
32
$pgtitle = array(gettext("Status"), gettext("OpenVPN"));
33
$shortcut_section = "openvpn";
34

    
35
require_once("guiconfig.inc");
36
require_once("openvpn.inc");
37
require_once("shortcuts.inc");
38
require_once("service-utils.inc");
39

    
40
/* Handle AJAX */
41
if ($_REQUEST['action']) {
42
	if ($_REQUEST['action'] == "kill") {
43
		$port  = $_REQUEST['port'];
44
		$remipp  = $_REQUEST['remipp'];
45
		$client_id  = $_REQUEST['client_id'];
46
		if (!empty($port) and !empty($remipp)) {
47
			$retval = openvpn_kill_client($port, $remipp, $client_id);
48
			echo htmlentities("|{$port}|{$remipp}|{$retval}|");
49
		} else {
50
			echo gettext("invalid input");
51
		}
52
		exit;
53
	}
54
}
55
if ($_POST['action']) {
56
	if (($_POST['action'] == "showrule") && is_numeric($_POST['vpnid']) &&
57
	    !preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['username']) && is_port($_POST['port'])) {
58
		$rulesfile = "{$g['tmp_path']}/ovpn_ovpns{$_POST['vpnid']}_{$_POST['username']}_{$_POST['port']}.rules";
59
		if (file_exists($rulesfile)) {
60
			$rule_text = base64_encode(file_get_contents($rulesfile));
61
			echo $rule_text;
62
		}
63
		exit;
64
	}
65
}
66

    
67
$servers = openvpn_get_active_servers();
68
$sk_servers = openvpn_get_active_servers("p2p");
69
$clients = openvpn_get_active_clients();
70

    
71
include("head.inc"); ?>
72

    
73
<form action="status_openvpn.php" method="get" name="iform">
74
<script type="text/javascript">
75
//<![CDATA[
76
	function killClient(mport, remipp, client_id) {
77
		var busy = function(index,icon) {
78
			$(icon).bind("onclick","");
79
			$(icon).attr('src',$(icon).attr('src').replace("\.gif", "_d.gif"));
80
			$(icon).css("cursor","wait");
81
		}
82

    
83
		$('img[name="i:' + mport + ":" + remipp + '"]').each(busy);
84

    
85
		$.ajax(
86
			"<?=$_SERVER['SCRIPT_NAME'];?>" +
87
				"?action=kill&port=" + mport + "&remipp=" + remipp + "&client_id=" + client_id,
88
			{ type: "get", complete: killComplete }
89
		);
90
	}
91

    
92
	function killComplete(req) {
93
		var values = req.responseText.split("|");
94
		if (values[3] != "0") {
95
	//		alert('<?=gettext("An error occurred.");?>' + ' (' + values[3] + ')');
96
			return;
97
		}
98

    
99
		$('tr[id="r:' + values[1] + ":" + values[2] + '"]').each(
100
			function(index,row) { $(row).fadeOut(1000); }
101
		);
102
	}
103

    
104
	function showRuleContents(vpnid, username, port) {
105
			$('#rulesviewer_text').text("...Loading...");
106
			$('#rulesviewer').modal('show');
107

    
108
			$.ajax(
109
				"<?=$_SERVER['SCRIPT_NAME'];?>",
110
				{
111
					type: 'post',
112
					data: {
113
						vpnid:           vpnid,
114
						username:     username,
115
						port:             port,
116
						action:      'showrule'
117
					},
118
					complete: ruleComplete
119
				}
120
			);
121
	}
122

    
123
	function ruleComplete(req) {
124
			$('#rulesviewer_text').text(atob(req.responseText));
125
			$('#rulesviewer_text').attr('readonly', true);
126
	}
127

    
128
//]]>
129
</script>
130

    
131
<?php
132
	$i = 0;
133
	foreach ($servers as $server):
134
?>
135

    
136
<div class="panel panel-default">
137
		<div class="panel-heading"><h2 class="panel-title"><?=htmlspecialchars($server['name']);?> <?=gettext('Client Connections') . ": " . ($server['conns'][0]['common_name'] != '[error]' ? sizeof($server['conns']) : '0');?></h2></div>
138
		<div class="panel-body table-responsive">
139
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
140
				<thead>
141
					<tr>
142
						<th><?=gettext("Common Name")?></th>
143
						<th><?=gettext("Real Address")?></th>
144
						<th><?=gettext("Virtual Address"); ?></th>
145
						<th><?=gettext("Connected Since"); ?></th>
146
						<th><?=gettext("Bytes Sent")?></th>
147
						<th><?=gettext("Bytes Received")?></th>
148
						<th><?=gettext("Cipher")?></th>
149
						<th><?=gettext("Actions")?></th>
150
					</tr>
151
				</thead>
152
				<tbody>
153

    
154
					<?php
155
							foreach ($server['conns'] as $conn):
156
								$remote_port = substr($conn['remote_host'], strpos($conn['remote_host'], ':') + 1);
157
								$rulesfile = "{$g['tmp_path']}/ovpn_ovpns{$server['vpnid']}_{$conn['user_name']}_{$remote_port}.rules";
158
					?>
159
					<tr id="<?php echo "r:{$server['mgmt']}:{$conn['remote_host']}"; ?>">
160
						<td>
161
							<?=$conn['common_name'];?>
162
					<?php if (!empty($conn['common_name']) && !empty($conn['user_name']) && ($conn['user_name'] != "UNDEF")): ?>
163
							<br />
164
					<?php endif; ?>
165
					<?php if (!empty($conn['user_name']) && ($conn['user_name'] != "UNDEF")): ?>
166
							<?=$conn['user_name'];?>
167
					<?php endif; ?>
168
						</td>
169
						<td><?=$conn['remote_host'];?></td>
170
						<td>
171
							<?=$conn['virtual_addr'];?>
172
					<?php if (!empty($conn['virtual_addr']) && !empty($conn['virtual_addr6'])): ?>
173
							<br />
174
					<?php endif; ?>
175
							<?=$conn['virtual_addr6'];?>
176
						</td>
177
						<td><?=$conn['connect_time'];?></td>
178
						<td data-value="<?=trim($conn['bytes_sent'])?>"><?=format_bytes($conn['bytes_sent']);?></td>
179
						<td data-value="<?=trim($conn['bytes_recv'])?>"><?=format_bytes($conn['bytes_recv']);?></td>
180
						<td data-value="<?=trim($conn['cipher'])?>"><?=$conn['cipher'];?></td>
181
						<td>
182

    
183
					<?php if (file_exists($rulesfile)): ?>
184
							<a
185
							onclick="showRuleContents('<?=$server['vpnid'];?>', '<?=$conn['user_name'];?>', '<?=$remote_port;?>');" style="cursor:pointer;"
186
							   title="<?php echo gettext("Show RADIUS ACL generated ruleset"); ?>">
187
							<i class="fa fa-info"></i>
188
							</a>&nbsp;
189
					<?php endif; ?>
190
							<a
191
							   onclick="killClient('<?=$server['mgmt'];?>', '<?=$conn['remote_host'];?>', '');" style="cursor:pointer;"
192
							   id="<?php echo "i:{$server['mgmt']}:{$conn['remote_host']}"; ?>"
193
							   title="<?php echo sprintf(gettext("Kill client connection from %s"), $conn['remote_host']); ?>">
194
							<i class="fa fa-times"></i>
195
							</a>&nbsp;
196
							<a
197
							   onclick="killClient('<?=$server['mgmt'];?>', '<?=$conn['remote_host'];?>', '<?=$conn['client_id'];?>');" style="cursor:pointer;"
198
							   id="<?php echo "i:{$server['mgmt']}:{$conn['remote_host']}"; ?>"
199
							   title="<?php echo sprintf(gettext("Halt client connection from %s"), $conn['remote_host']); ?>">
200
							<i class="fa fa-times-circle text-danger"></i>
201
							</a>
202
						</td>
203
					</tr>
204
					<?php
205
							endforeach;
206
					?>
207
				</tbody>
208
				<tfoot>
209
					<tr>
210
						<td colspan="2">
211
							<table>
212
								<tr>
213
										<?php $ssvc = find_service_by_openvpn_vpnid($server['vpnid']); ?>
214
									<td>
215
										<?= gettext("Status") . ": " . get_service_status_icon($ssvc, false, true, false, "service_state"); ?>
216
									</td>
217
									<td>
218
										<?= gettext("Actions") . ": " . get_service_control_links($ssvc); ?>
219
									</td>
220
								</tr>
221
							</table>
222
						</td>
223
						<td colspan="5">
224
						</td>
225
					</tr>
226
				</tfoot>
227
			</table>
228
		</div>
229
</div>
230
<?php
231
		if (is_array($server['routes']) && count($server['routes'])):
232
?>
233
<div id="shroutebut-<?= $i ?>">
234
	<button type="button" class="btn btn-info" onClick="show_routes('tabroute-<?= $i ?>','shroutebut-<?= $i ?>')" value="<?php echo gettext("Show Routing Table"); ?>">
235
		<i class="fa fa-plus-circle icon-embed-btn"></i>
236
		<?php echo gettext("Show Routing Table"); ?>
237
	</button>
238
	- <?= gettext("Display OpenVPN's internal routing table for this server.") ?>
239
	<br /><br />
240
</div>
241
<div class="panel panel-default" id="tabroute-<?=$i?>" style="display: none;">
242
		<div class="panel-heading"><h2 class="panel-title"><?=htmlspecialchars($server['name']);?> <?=gettext("Routing Table"); ?></h2></div>
243
		<div class="panel-body table-responsive">
244
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
245
				<thead>
246
					<tr>
247
						<th><?=gettext("Common Name"); ?></th>
248
						<th><?=gettext("Real Address"); ?></th>
249
						<th><?=gettext("Target Network"); ?></th>
250
						<th><?=gettext("Last Used"); ?></th>
251
					</tr>
252
				</thead>
253
				<tbody>
254

    
255
<?php
256
			foreach ($server['routes'] as $conn):
257
?>
258
					<tr id="<?php echo "r:{$server['mgmt']}:{$conn['remote_host']}"; ?>">
259
						<td><?=$conn['common_name'];?></td>
260
						<td><?=$conn['remote_host'];?></td>
261
						<td><?=$conn['virtual_addr'];?></td>
262
						<td><?=$conn['last_time'];?></td>
263
					</tr>
264
<?php
265
			endforeach;
266
?>
267
				</tbody>
268
				<tfoot>
269
					<tr>
270
						<td colspan="4"><?= gettext("An IP address followed by C indicates a host currently connected through the VPN.") ?></td>
271
					</tr>
272
				</tfoot>
273
			</table>
274
		</div>
275
</div>
276
<?php
277
		endif;
278
?>
279
<br />
280
<?php
281
		$i++;
282
	endforeach;
283
?>
284
<br />
285

    
286
<?php
287
	if (!empty($sk_servers)) {
288
?>
289
<div class="panel panel-default">
290
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Peer to Peer Server Instance Statistics"); ?></h2></div>
291
		<div class="panel-body table-responsive">
292
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
293
				<thead>
294
					<tr>
295
						<th><?=gettext("Name"); ?></th>
296
						<th><?=gettext("Status"); ?></th>
297
						<th><?=gettext("Connected Since"); ?></th>
298
						<th><?=gettext("Virtual Address"); ?></th>
299
						<th><?=gettext("Remote Host"); ?></th>
300
						<th><?=gettext("Bytes Sent"); ?></th>
301
						<th><?=gettext("Bytes Received"); ?></th>
302
						<th><?=gettext("Service"); ?></th>
303
					</tr>
304
				</thead>
305
				<tbody>
306

    
307
<?php
308
		foreach ($sk_servers as $sk_server):
309
?>
310
					<tr id="<?php echo "r:{$sk_server['port']}:{$sk_server['vpnid']}"; ?>">
311
						<td><?=htmlspecialchars($sk_server['name']);?></td>
312
						<td><?=$sk_server['status'];?></td>
313
						<td><?=$sk_server['connect_time'];?></td>
314
						<td>
315
							<?=$sk_server['virtual_addr'];?>
316
					<?php if (!empty($sk_server['virtual_addr']) && !empty($sk_server['virtual_addr6'])): ?>
317
							<br />
318
					<?php endif; ?>
319
							<?=$sk_server['virtual_addr6'];?>
320
						</td>
321
						<td><?=$sk_server['remote_host'];?></td>
322
						<td data-value="<?=trim($sk_server['bytes_sent'])?>"><?=format_bytes($sk_server['bytes_sent']);?></td>
323
						<td data-value="<?=trim($sk_server['bytes_recv'])?>"><?=format_bytes($sk_server['bytes_recv']);?></td>
324
						<td>
325
							<table>
326
								<tr>
327
									<td>
328
										<?php $ssvc = find_service_by_openvpn_vpnid($sk_server['vpnid']); ?>
329
										<?= get_service_status_icon($ssvc, false, true); ?>
330
										<?= get_service_control_links($ssvc, true); ?>
331
									</td>
332
								</tr>
333
							</table>
334
						</td>
335
					</tr>
336
<?php
337
		endforeach;
338
?>
339
				</tbody>
340
			</table>
341
		</div>
342
</div>
343

    
344
<?php
345
	}
346
?>
347
<br />
348
<?php
349
	if (!empty($clients)) {
350
?>
351
<div class="panel panel-default">
352
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Client Instance Statistics"); ?></h2></div>
353
		<div class="panel-body table-responsive">
354
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
355
				<thead>
356
					<tr>
357
						<th><?=gettext("Name"); ?></th>
358
						<th><?=gettext("Status"); ?></th>
359
						<th><?=gettext("Connected Since"); ?></th>
360
						<th><?=gettext("Local Address"); ?></th>
361
						<th><?=gettext("Virtual Address"); ?></th>
362
						<th><?=gettext("Remote Host"); ?></th>
363
						<th><?=gettext("Bytes Sent"); ?></th>
364
						<th><?=gettext("Bytes Received"); ?></th>
365
						<th><?=gettext("Service"); ?></th>
366
					</tr>
367
				</thead>
368
				<tbody>
369

    
370
<?php
371
		foreach ($clients as $client):
372
?>
373
					<tr id="<?php echo "r:{$client['port']}:{$client['vpnid']}"; ?>">
374
						<td><?=htmlspecialchars($client['name']);?></td>
375
						<td><?=$client['status'];?></td>
376
						<td><?=$client['connect_time'];?></td>
377
						<td>
378
					<?php if (empty($client['local_host']) && empty($client['local_port'])): ?>
379
							(pending)
380
					<?php else: ?>
381
							<?=$client['local_host'];?>:<?=$client['local_port'];?>
382
					<?php endif; ?>
383
						</td>
384
						<td>
385
							<?=$client['virtual_addr'];?>
386
					<?php if (!empty($client['virtual_addr']) && !empty($client['virtual_addr6'])): ?>
387
							<br />
388
					<?php endif; ?>
389
							<?=$client['virtual_addr6'];?>
390
						</td>
391
						<td>
392
					<?php if (empty($client['remote_host']) && empty($client['remote_port'])): ?>
393
							(pending)
394
					<?php else: ?>
395
							<?=$client['remote_host'];?>:<?=$client['remote_port'];?>
396
					<?php endif; ?>
397
						</td>
398
						<td data-value="<?=trim($client['bytes_sent'])?>"><?=format_bytes($client['bytes_sent']);?></td>
399
						<td data-value="<?=trim($client['bytes_recv'])?>"><?=format_bytes($client['bytes_recv']);?></td>
400
						<td>
401
							<table>
402
								<tr>
403
									<td>
404
										<?php $ssvc = find_service_by_openvpn_vpnid($client['vpnid']); ?>
405
										<?= get_service_status_icon($ssvc, false, true); ?>
406
										<?= get_service_control_links($ssvc, true); ?>
407
									</td>
408
								</tr>
409
							</table>
410
						</td>
411
					</tr>
412
<?php
413
		endforeach;
414
?>
415
				</tbody>
416
			</table>
417
		</div>
418
	</div>
419

    
420
<?php
421
}
422

    
423
if ($DisplayNote) {
424
 	print_info_box(gettext("If there are custom options that override the management features of OpenVPN on a client or server, they will cause that OpenVPN instance to not work correctly with this status page."));
425
}
426

    
427
if ((empty($clients)) && (empty($servers)) && (empty($sk_servers))) {
428
	print_info_box(gettext("No OpenVPN instances defined."));
429
}
430

    
431
// Create a Modal object to display RADIUS ACL generated ruleset
432
$form = new Form(FALSE);
433
$modal = new Modal('RADIUS ACL Generated Ruleset', 'rulesviewer', 'large', 'Close');
434
$modal->addInput(new Form_Textarea (
435
	'rulesviewer_text',
436
	null,
437
	'...Loading...'
438
))->removeClass('form-control')->addClass('row-fluid col-sm-11')->setAttribute('rows', '10')->setAttribute('wrap', 'soft');
439
$form->add($modal);
440
print($form);
441
?>
442
</form>
443

    
444
<script type="text/javascript">
445
//<![CDATA[
446

    
447
function show_routes(id, buttonid) {
448
	document.getElementById(buttonid).innerHTML='';
449
	aodiv = document.getElementById(id);
450
	aodiv.style.display = "block";
451
}
452

    
453
//]]>
454
</script>
455

    
456
<?php include("foot.inc"); ?>
(178-178/228)