Project

General

Profile

Download (7.17 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

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

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

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

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

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

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

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

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

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

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

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

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

    
159
		$servers[] = $server;
160
	}
161
}
162
include("head.inc"); ?>
163

    
164
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onload="<?=$jsevents["body"]["onload"];?>">
165
<script src="/javascript/sorttable.js" type="text/javascript"></script>
166
<?php include("fbegin.inc"); ?>
167
<form action="status_openvpn.php" method="get" name="iform">
168
<script type="text/javascript">
169
	function killClient(mport, remipp) {
170
		var busy = function(icon) {
171
			icon.onclick      = "";
172
			icon.src          = icon.src.replace("\.gif", "_d.gif");
173
			icon.style.cursor = "wait";
174
		}
175

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

    
178
		new Ajax.Request(
179
			"<?=$_SERVER['SCRIPT_NAME'];?>" +
180
				"?action=kill&port=" + mport + "&remipp=" + remipp,
181
			{ method: "get", onComplete: killComplete }
182
		);
183
	}
184

    
185
	function killComplete(req) {
186
		var values = req.responseText.split("|");
187
		if(values[3] != "0") {
188
			alert('<?=gettext("An error occurred.");?>' + ' (' + values[3] + ')');
189
			return;
190
		}
191

    
192
		$A(document.getElementsByName("r:" + values[1] + ":" + values[2])).each(
193
			function(row) { Effect.Fade(row, { duration: 1.0 }); }
194
		);
195
	}
196
</script>
197

    
198
<?php foreach ($servers as $server): ?>
199

    
200
<table style="padding-top:0px; padding-bottom:0px; padding-left:0px; padding-right:0px" width="100%" border="0" cellpadding="0" cellspacing="0">
201
	<tr>
202
		<td colspan="6" class="listtopic">
203
			Client connections for <?=$server['name'];?>
204
		</td>
205
	</tr>
206
	<tr>
207
		<td>
208
			<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">
209
			<tr>
210
				<td class="listhdrr">Common Name</td>
211
				<td class="listhdrr">Real Address</td>
212
				<td class="listhdrr">Virtual Address</td>
213
				<td class="listhdrr">Connected Since</td>
214
				<td class="listhdrr">Bytes Sent</td>
215
				<td class="listhdrr">Bytes Received</td>
216
			</tr>
217

    
218
			<?php foreach ($server['conns'] as $conn): ?>
219
			<tr name='<?php echo "r:{$server['port']}:{$conn['remote_host']}"; ?>'>
220
				<td class="listlr">
221
					<?=$conn['common_name'];?>
222
				</td>
223
				<td class="listr">
224
					<?=$conn['remote_host'];?>
225
				</td>
226
				<td class="listr">
227
					<?=$conn['virtual_addr'];?>
228
				</td>
229
				<td class="listr">
230
					<?=$conn['connect_time'];?>
231
				</td>
232
				<td class="listr">
233
					<?=$conn['bytes_sent'];?>
234
				</td>
235
				<td class="listr">
236
					<?=$conn['bytes_recv'];?>
237
				</td>
238
				<td class='list'>
239
					<img src='/themes/<?php echo $g['theme']; ?>/images/icons/icon_x.gif' height='17' width='17' border='0'
240
					   onclick="killClient('<?php echo $server['port']; ?>', '<?php echo $conn['remote_host']; ?>');" style='cursor:pointer;'
241
					   name='<?php echo "i:{$server['port']}:{$conn['remote_host']}"; ?>'
242
					   title='Kill client connection from <?php echo $conn['remote_host']; ?>' alt='' />
243
				</td>
244
			</tr>
245

    
246
			<?php endforeach; ?>
247
			<tr>
248
				<td colspan="6" class="list" height="12"></td>
249
			</tr>
250

    
251
		</table>
252
		</td>
253
	</tr>
254
</table>
255

    
256
<?php endforeach; ?>
257

    
258
<?php include("fend.inc"); ?>
(156-156/215)