Projet

Général

Profil

Télécharger (6,88 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / www / widgets / widgets / openvpn.widget.php @ f334f8bf

1
<?php
2

    
3
$nocsrf = true;
4

    
5
require_once("guiconfig.inc");
6
require_once("openvpn.inc");
7

    
8
/* Handle AJAX */
9
if($_GET['action']) {
10
	if($_GET['action'] == "kill") {
11
		$port = $_GET['port'];
12
		$remipp = $_GET['remipp'];
13
		if (!empty($port) and !empty($remipp)) {
14
			$retval = kill_client($port, $remipp);
15
			echo htmlentities("|{$port}|{$remipp}|{$retval}|");
16
		} else {
17
			echo gettext("invalid input");
18
		}
19
		exit;
20
	}
21
}
22

    
23

    
24
function kill_client($port, $remipp) {
25
	global $g;
26

    
27
	//$tcpsrv = "tcp://127.0.0.1:{$port}";
28
	$tcpsrv = "unix://{$g['varetc_path']}/openvpn/{$port}.sock";
29
	$errval;
30
	$errstr;
31

    
32
	/* open a tcp connection to the management port of each server */
33
	$fp = @stream_socket_client($tcpsrv, $errval, $errstr, 1);
34
	$killed = -1;
35
	if ($fp) {
36
		stream_set_timeout($fp, 1);
37
		fputs($fp, "kill {$remipp}\n");
38
		while (!feof($fp)) {
39
			$line = fgets($fp, 1024);
40

    
41
			$info = stream_get_meta_data($fp);
42
			if ($info['timed_out'])
43
				break;
44

    
45
			/* parse header list line */
46
			if (strpos($line, "INFO:") !== false)
47
				continue;
48
			if (strpos($line, "SUCCESS") !== false) {
49
				$killed = 0;
50
			}
51
			break;
52
		}
53
		fclose($fp);
54
	}
55
	return $killed;
56
}
57

    
58
$servers = openvpn_get_active_servers();
59
$sk_servers = openvpn_get_active_servers("p2p");
60
$clients = openvpn_get_active_clients();
61
?>
62

    
63
<br />
64
<script type="text/javascript">
65
	function killClient(mport, remipp) {
66
		var busy = function(index,icon) {
67
			jQuery(icon).bind("onclick","");
68
			jQuery(icon).attr('src',jQuery(icon).attr('src').replace("\.gif", "_d.gif"));
69
			jQuery(icon).css("cursor","wait");
70
		}
71

    
72
		jQuery('img[name="i:' + mport + ":" + remipp + '"]').each(busy);
73

    
74
		jQuery.ajax(
75
			"<?=$_SERVER['SCRIPT_NAME'];?>" +
76
				"?action=kill&port=" + mport + "&remipp=" + remipp,
77
			{ type: "get", complete: killComplete }
78
		);
79
	}
80

    
81
	function killComplete(req) {
82
		var values = req.responseText.split("|");
83
		if(values[3] != "0") {
84
			alert('<?=gettext("An error occurred.");?>' + ' (' + values[3] + ')');
85
			return;
86
		}
87

    
88
		jQuery('tr[name="r:' + values[1] + ":" + values[2] + '"]').each(
89
			function(index,row) { jQuery(row).fadeOut(1000); }
90
		);
91
	}
92
</script>
93

    
94
<?php foreach ($servers as $server): ?>
95

    
96
<table style="padding-top:0px; padding-bottom:0px; padding-left:0px; padding-right:0px" width="100%" border="0" cellpadding="0" cellspacing="0">
97
	<tr>
98
		<td colspan="6" class="listtopic">
99
			<?=$server['name'];?> Client connections
100
		</td>
101
	</tr>
102
	<tr>
103
		<td>
104
			<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" sortableMultirow="2">
105
			<tr>
106
				<td class="listhdrr">Name/Time</td>
107
				<td class="listhdrr">Real/Virtual IP</td>
108
			</tr>
109
			<?php $rowIndex = 0;
110
			foreach ($server['conns'] as $conn):
111
			$evenRowClass = $rowIndex % 2 ? " listMReven" : " listMRodd";
112
			$rowIndex++;
113
			?>
114
			<tr name='<?php echo "r:{$server['mgmt']}:{$conn['remote_host']}"; ?>' class="<?=$evenRowClass?>">
115
				<td class="listMRlr">
116
					<?=$conn['common_name'];?>
117
				</td>
118
				<td class="listMRr">
119
					<?=$conn['remote_host'];?>
120
				</td>
121
				<td class='listMR' rowspan="2">
122
					<img src='/themes/<?php echo $g['theme']; ?>/images/icons/icon_x.gif' height='17' width='17' border='0'
123
					   onclick="killClient('<?php echo $server['mgmt']; ?>', '<?php echo $conn['remote_host']; ?>');" style='cursor:pointer;'
124
					   name='<?php echo "i:{$server['mgmt']}:{$conn['remote_host']}"; ?>'
125
					   title='Kill client connection from <?php echo $conn['remote_host']; ?>' alt='' />
126
				</td>
127
			</tr>
128
			<tr name='<?php echo "r:{$server['mgmt']}:{$conn['remote_host']}"; ?>' class="<?=$evenRowClass?>">
129
				<td class="listMRlr">
130
					<?=$conn['connect_time'];?>
131
				</td>
132
				<td class="listMRr">
133
					<?=$conn['virtual_addr'];?>
134
				</td>
135
			</tr>
136

    
137
			<?php endforeach; ?>
138
			<tfoot>
139
			<tr>
140
				<td colspan="6" class="list" height="12"></td>
141
			</tr>
142
			</tfoot>
143
		</table>
144
		</td>
145
	</tr>
146
</table>
147

    
148
<?php endforeach; ?>
149
<?php if (!empty($sk_servers)) { ?>
150
<table style="padding-top:0px; padding-bottom:0px; padding-left:0px; padding-right:0px" width="100%" border="0" cellpadding="0" cellspacing="0">
151
	<tr>
152
		<td colspan="6" class="listtopic">
153
			Peer to Peer Server Instance Statistics
154
		</td>
155
	</tr>
156
	<tr>
157
		<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">
158
		<tr>
159
			<td class="listhdrr">Name/Time</td>
160
			<td class="listhdrr">Remote/Virtual IP</td>
161
		</tr>
162

    
163
<?php foreach ($sk_servers as $sk_server): ?>
164
		<tr name='<?php echo "r:{$sk_server['port']}:{$sk_server['remote_host']}"; ?>'>
165
			<td class="listlr">
166
				<?=$sk_server['name'];?>
167
			</td>
168
			<td class="listr">
169
				<?=$sk_server['remote_host'];?>
170
			</td>
171
			<td rowspan="2" align="center">
172
			<?php
173
			if ($sk_server['status'] == "up") {
174
				/* tunnel is up */
175
				$iconfn = "interface_up";
176
			} else {
177
				/* tunnel is down */
178
				$iconfn = "interface_down";
179
			}
180
			echo "<img src ='/themes/{$g['theme']}/images/icons/icon_{$iconfn}.gif'>";
181
			?>
182
			</td>
183
		</tr>
184
		<tr name='<?php echo "r:{$sk_server['port']}:{$sk_server['remote_host']}"; ?>'>
185
			<td class="listlr">
186
				<?=$sk_server['connect_time'];?>
187
			</td>
188
			<td class="listr">
189
				<?=$sk_server['virtual_addr'];?>
190
			</td>
191
		</tr>
192
<?php endforeach; ?>
193
		</table>
194
	</tr>
195
</table>
196

    
197
<?php
198
} ?>
199
<?php if (!empty($clients)) { ?>
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 Instance Statistics
204
		</td>
205
	</tr>
206
	<tr>
207
		<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">
208
		<tr>
209
			<td class="listhdrr">Name/Time</td>
210
			<td class="listhdrr">Remote/Virtual IP</td>
211
		</tr>
212

    
213
<?php foreach ($clients as $client): ?>
214
		<tr name='<?php echo "r:{$client['port']}:{$client['remote_host']}"; ?>'>
215
			<td class="listlr">
216
				<?=$client['name'];?>
217
			</td>
218
			<td class="listr">
219
				<?=$client['remote_host'];?>
220
			</td>
221
			<td rowspan="2" align="center">
222
			<?php
223
			if ($client['status'] == "up") {
224
				/* tunnel is up */
225
				$iconfn = "interface_up";
226
			} else {
227
				/* tunnel is down */
228
				$iconfn = "interface_down";
229
			}
230
			echo "<img src ='/themes/{$g['theme']}/images/icons/icon_{$iconfn}.gif'>";
231
			?>
232
			</td>
233
		</tr>
234
		<tr name='<?php echo "r:{$client['port']}:{$client['remote_host']}"; ?>'>
235
			<td class="listlr">
236
				<?=$client['connect_time'];?>
237
			</td>
238
			<td class="listr">
239
				<?=$client['virtual_addr'];?>
240
			</td>
241
		</tr>
242
<?php endforeach; ?>
243
		</table>
244
	</tr>
245
</table>
246

    
247
<?php
248
}
249

    
250
if ($DisplayNote) {
251
	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";
252
}
253

    
254
if ((empty($clients)) && (empty($servers)) && (empty($sk_servers))) {
255
	echo "No OpenVPN instance defined";
256
}
257
?>
(13-13/21)