Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	status_ovpenvpn.php
4

    
5
    Copyright (C) 2010 Jim Pingle
6
    Copyright (C) 2008 Shrew Soft Inc.
7

    
8
    AJAX bits borrowed from diag_dump_states.php
9
    Copyright (C) 2005 Scott Ullrich, Colin Smith
10

    
11
    All rights reserved.
12

    
13
	Redistribution and use in source and binary forms, with or without
14
	modification, are permitted provided that the following conditions are met:
15

    
16
	1. Redistributions of source code must retain the above copyright notice,
17
	   this list of conditions and the following disclaimer.
18

    
19
	2. Redistributions in binary form must reproduce the above copyright
20
	   notice, this list of conditions and the following disclaimer in the
21
	   documentation and/or other materials provided with the distribution.
22

    
23
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
	POSSIBILITY OF SUCH DAMAGE.
33
*/
34
/* DISABLE_PHP_LINT_CHECKING */
35
/*
36
	pfSense_MODULE:	openvpn
37
*/
38

    
39
##|+PRIV
40
##|*IDENT=page-status-openvpn
41
##|*NAME=Status: OpenVPN page
42
##|*DESCR=Allow access to the 'Status: OpenVPN' page.
43
##|*MATCH=status_openvpn.php*
44
##|-PRIV
45

    
46
$pgtitle = array("Status", "OpenVPN");
47
require("guiconfig.inc");
48
require_once("vpn.inc");
49

    
50
/* Handle AJAX */
51
if($_GET['action']) {
52
	if($_GET['action'] == "kill") {
53
		$port  = $_GET['port'];
54
		$remipp  = $_GET['remipp'];
55
		if (!empty($port) and !empty($remipp)) {
56
			$retval = kill_client($port, $remipp);
57
			echo htmlentities("|{$port}|{$remipp}|{$retval}|");
58
		} else {
59
			echo "invalid input";
60
		}
61
		exit;
62
	}
63
}
64

    
65

    
66
function kill_client($port, $remipp) {
67
	$tcpsrv = "tcp://127.0.0.1:{$port}";
68
	$errval;
69
	$errstr;
70

    
71
	/* open a tcp connection to the management port of each server */
72
	$fp = @stream_socket_client($tcpsrv, $errval, $errstr, 1);
73
	$killed = -1;
74
	if ($fp) {
75
		fputs($fp, "kill {$remipp}\n");
76
		while (!feof($fp)) {
77
			$line = fgets($fp, 1024);
78
			/* parse header list line */
79
			if (strpos($line, "INFO:"))
80
				continue;
81
			if (strpos($line, "UCCESS")) {
82
				$killed = 0;
83
			}
84
			break;
85
		}
86
		fclose($fp);
87
	}
88
	return $killed;
89
}
90

    
91
$servers = array();
92
$clients = array();
93

    
94
if (is_array($config['openvpn']['openvpn-server'])) {
95
	foreach ($config['openvpn']['openvpn-server'] as & $settings) {
96

    
97
		$prot = $settings['protocol'];
98
		$port = $settings['local_port'];
99

    
100
		$server = array();
101
		$server['port'] = $settings['local_port'];
102
		if ($settings['description'])
103
			$server['name'] = "{$settings['description']} {$prot}:{$port}";
104
		else
105
			$server['name'] = "Server {$prot}:{$port}";
106
		$server['conns'] = array();
107

    
108
		$tcpsrv = "tcp://127.0.0.1:{$port}";
109
		$errval;
110
		$errstr;
111

    
112
		/* open a tcp connection to the management port of each server */
113
		$fp = @stream_socket_client($tcpsrv, $errval, $errstr, 1);
114
		if ($fp) {
115

    
116
			/* send our status request */
117
			fputs($fp, "status 2\n");
118

    
119
			/* recv all response lines */
120
			while (!feof($fp)) {
121

    
122
				/* read the next line */
123
				$line = fgets($fp, 1024);
124

    
125
				/* parse header list line */
126
				if (strstr($line, "HEADER"))
127
					continue;
128

    
129
				/* parse end of output line */
130
				if (strstr($line, "END"))
131
					break;
132

    
133
				/* parse client list line */
134
				if (strstr($line, "CLIENT_LIST")) {
135
					$list = explode(",", $line);
136
					$conn = array();
137
					$conn['common_name'] = $list[1];
138
					$conn['remote_host'] = $list[2];
139
					$conn['virtual_addr'] = $list[3];
140
					$conn['bytes_recv'] = $list[4];
141
					$conn['bytes_sent'] = $list[5];
142
					$conn['connect_time'] = $list[6];
143
					$server['conns'][] = $conn;
144
				}
145
			}
146

    
147
			/* cleanup */
148
			fclose($fp);
149
		} else {
150
			$conn = array();
151
			$conn['common_name'] = "[error]";
152
			$conn['remote_host'] = "Management Daemon Unreachable";
153
			$conn['virtual_addr'] = "";
154
			$conn['bytes_recv'] = 0;
155
			$conn['bytes_sent'] = 0;
156
			$conn['connect_time'] = 0;
157
			$server['conns'][] = $conn;
158
		}
159

    
160
		$servers[] = $server;
161
	}
162
}
163

    
164

    
165
if (is_array($config['openvpn']['openvpn-client'])) {
166
	foreach ($config['openvpn']['openvpn-client'] as & $settings) {
167

    
168
		$prot = $settings['protocol'];
169
		$port = $settings['local_port'];
170

    
171
		$client = array();
172
		$client['port'] = $settings['local_port'];
173
		if ($settings['description'])
174
			$client['name'] = "{$settings['description']} {$prot}:{$port}";
175
		else
176
			$client['name'] = "Client {$prot}:{$port}";
177

    
178
		$tcpcli = "tcp://127.0.0.1:{$port}";
179
		$errval;
180
		$errstr;
181

    
182
		$client['status']="down";
183

    
184
		/* open a tcp connection to the management port of each cli */
185
		$fp = @stream_socket_client($tcpcli, $errval, $errstr, 1);
186
		if ($fp) {
187

    
188
			/* send our status request */
189
			fputs($fp, "state 1\n");
190

    
191
			/* recv all response lines */
192
			while (!feof($fp)) {
193
				/* read the next line */
194
				$line = fgets($fp, 1024);
195

    
196
				/* Get the client state */
197
				if (strstr($line,"CONNECTED")) {
198
					$client['status']="up";
199
					$list = explode(",", $line);
200

    
201
					$client['connect_time']  = date("D M j G:i:s Y", $list[0]);
202
					$client['virtual_addr']  = $list[3];
203
					$client['remote_host'] = $list[4];
204
				}
205
				/* parse end of output line */
206
				if (strstr($line, "END"))
207
					break;
208
			}
209

    
210
			/* If up, get read/write stats */
211
			if (strcmp($client['status'], "up") == 0) {
212
				fputs($fp, "status 2\n");
213
				/* recv all response lines */
214
				while (!feof($fp)) {
215
					/* read the next line */
216
					$line = fgets($fp, 1024);
217

    
218
					if (strstr($line,"TCP/UDP read bytes")) {
219
						$list = explode(",", $line);
220
						$client['bytes_recv'] = $list[1];
221
					}
222

    
223
					if (strstr($line,"TCP/UDP write bytes")) {
224
						$list = explode(",", $line);
225
						$client['bytes_sent'] = $list[1];
226
					}
227

    
228
					/* parse end of output line */
229
					if (strstr($line, "END"))
230
						break;
231
				}
232
			}
233

    
234
			fclose($fp);
235

    
236
		} else {
237
			$DisplayNote=true;
238
			$client['remote_host'] = "No Management Daemon";
239
			$client['virtual_addr'] = "See Note Below";
240
			$client['bytes_recv'] = 0;
241
			$client['bytes_sent'] = 0;
242
			$client['connect_time'] = 0;
243
		}
244

    
245
		$clients[] = $client;
246
	}
247
}
248
include("head.inc"); ?>
249

    
250
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onload="<?=$jsevents["body"]["onload"];?>">
251
<script src="/javascript/sorttable.js" type="text/javascript"></script>
252
<?php include("fbegin.inc"); ?>
253
<form action="status_openvpn.php" method="get" name="iform">
254
<script type="text/javascript">
255
	function killClient(mport, remipp) {
256
		var busy = function(icon) {
257
			icon.onclick      = "";
258
			icon.src          = icon.src.replace("\.gif", "_d.gif");
259
			icon.style.cursor = "wait";
260
		}
261

    
262
		$A(document.getElementsByName("i:" + mport + ":" + remipp)).each(busy);
263

    
264
		new Ajax.Request(
265
			"<?=$_SERVER['SCRIPT_NAME'];?>" +
266
				"?action=kill&port=" + mport + "&remipp=" + remipp,
267
			{ method: "get", onComplete: killComplete }
268
		);
269
	}
270

    
271
	function killComplete(req) {
272
		var values = req.responseText.split("|");
273
		if(values[3] != "0") {
274
			alert('<?=gettext("An error occurred.");?>' + ' (' + values[3] + ')');
275
			return;
276
		}
277

    
278
		$A(document.getElementsByName("r:" + values[1] + ":" + values[2])).each(
279
			function(row) { Effect.Fade(row, { duration: 1.0 }); }
280
		);
281
	}
282
</script>
283

    
284
<?php foreach ($servers as $server): ?>
285

    
286
<table style="padding-top:0px; padding-bottom:0px; padding-left:0px; padding-right:0px" width="100%" border="0" cellpadding="0" cellspacing="0">
287
	<tr>
288
		<td colspan="6" class="listtopic">
289
			Client connections for <?=$server['name'];?>
290
		</td>
291
	</tr>
292
	<tr>
293
		<td>
294
			<table style="padding-top:0px; padding-bottom:0px; padding-left:0px; padding-right:0px" class="tabcont sortable" width="100%" border="0" cellpadding="0" cellspacing="0">
295
			<tr>
296
				<td class="listhdrr">Common Name</td>
297
				<td class="listhdrr">Real Address</td>
298
				<td class="listhdrr">Virtual Address</td>
299
				<td class="listhdrr">Connected Since</td>
300
				<td class="listhdrr">Bytes Sent</td>
301
				<td class="listhdrr">Bytes Received</td>
302
			</tr>
303

    
304
			<?php foreach ($server['conns'] as $conn): ?>
305
			<tr name='<?php echo "r:{$server['port']}:{$conn['remote_host']}"; ?>'>
306
				<td class="listlr">
307
					<?=$conn['common_name'];?>
308
				</td>
309
				<td class="listr">
310
					<?=$conn['remote_host'];?>
311
				</td>
312
				<td class="listr">
313
					<?=$conn['virtual_addr'];?>
314
				</td>
315
				<td class="listr">
316
					<?=$conn['connect_time'];?>
317
				</td>
318
				<td class="listr">
319
					<?=$conn['bytes_sent'];?>
320
				</td>
321
				<td class="listr">
322
					<?=$conn['bytes_recv'];?>
323
				</td>
324
				<td class='list'>
325
					<img src='/themes/<?php echo $g['theme']; ?>/images/icons/icon_x.gif' height='17' width='17' border='0'
326
					   onclick="killClient('<?php echo $server['port']; ?>', '<?php echo $conn['remote_host']; ?>');" style='cursor:pointer;'
327
					   name='<?php echo "i:{$server['port']}:{$conn['remote_host']}"; ?>'
328
					   title='Kill client connection from <?php echo $conn['remote_host']; ?>' alt='' />
329
				</td>
330
			</tr>
331

    
332
			<?php endforeach; ?>
333
			<tr>
334
				<td colspan="6" class="list" height="12"></td>
335
			</tr>
336

    
337
		</table>
338
		</td>
339
	</tr>
340
</table>
341

    
342
<?php endforeach; ?>
343
<br>
344

    
345

    
346
<?php if (!empty($clients)) { ?>
347
<table style="padding-top:0px; padding-bottom:0px; padding-left:0px; padding-right:0px" width="100%" border="0" cellpadding="0" cellspacing="0">
348
	<tr>
349
		<td colspan="6" class="listtopic">
350
			OpenVPN client instances statistics
351
		</td>
352
	</tr>
353
	<tr>
354
		<table style="padding-top:0px; padding-bottom:0px; padding-left:0px; padding-right:0px" class="tabcont sortable" width="100%" border="0" cellpadding="0" cellspacing="0">
355
		<tr>
356
			<td class="listhdrr">Name</td>
357
			<td class="listhdrr">Status</td>
358
			<td class="listhdrr">Connected Since</td>
359
			<td class="listhdrr">Virtual Addr</td>
360
			<td class="listhdrr">Remote Host</td>
361
			<td class="listhdrr">Bytes Sent</td>
362
			<td class="listhdrr">Bytes Received</td>
363
		</tr>
364

    
365
<?php foreach ($clients as $client): ?>
366
		<tr name='<?php echo "r:{$client['port']}:{$conn['remote_host']}"; ?>'>
367
			<td class="listlr">
368
				<?=$client['name'];?>
369
			</td>
370
			<td class="listlr">
371
				<?=$client['status'];?>
372
			</td>
373
			<td class="listr">
374
				<?=$client['connect_time'];?>
375
			</td>
376
			<td class="listr">
377
				<?=$client['virtual_addr'];?>
378
			</td>
379
			<td class="listr">
380
				<?=$client['remote_host'];?>
381
			</td>
382
			<td class="listr">
383
				<?=$client['bytes_sent'];?>
384
			</td>
385
			<td class="listr">
386
				<?=$client['bytes_recv'];?>
387
			</td>
388
		</tr>
389
<?php endforeach; ?>
390
		</table>
391
	</tr>
392
</table>
393

    
394
<?php 
395
}
396

    
397
if ($DisplayNote) {
398
	echo "<br/><b>NOTE:</b> You need to bind each OpenVPN client to enable its management daemon: use 'Local port' setting in the OpenVPN client screen";
399
}
400

    
401
if ((empty($clients)) && (empty($servers))) {
402
	echo "No OpenVPN instance defined";
403
}
404
?>
405

    
406

    
407
<?php include("fend.inc"); ?>
(159-159/218)