Project

General

Profile

« Previous | Next » 

Revision 3d0a7703

Added by Stephen Beaver almost 9 years ago

Completed #6723\
by making the OpenVPN widget update dynamically

(cherry picked from commit f30e6bd49c773de28b5ec143add30fc900e7874d)

View differences:

src/usr/local/www/widgets/widgets/openvpn.widget.php
71 71
	}
72 72
}
73 73

  
74
$servers = openvpn_get_active_servers();
75
$sk_servers = openvpn_get_active_servers("p2p");
76
$clients = openvpn_get_active_clients();
74
// Compose the table contents and pass it back to the ajax caller
75
if ($_REQUEST && $_REQUEST['ajax']) {
76
	printPanel();
77
	exit;
78
}
79

  
80
// Output the widget panel from this function so that it can be called from the AJAX handler as well as
81
// when first rendering the page
82
function printPanel() {
83

  
84
	$servers = openvpn_get_active_servers();
85
	$sk_servers = openvpn_get_active_servers("p2p");
86
	$clients = openvpn_get_active_clients();
87

  
88
	$opstring = "";
89

  
90
	foreach ($servers as $server):
91

  
92
	$opstring .= "<div class=\"widget panel panel-default\">";
93
	$opstring .=	"<div class=\"panel-heading\"><h2 class=\"panel-title\">" . htmlspecialchars($server['name']) . "</h2></div>";
94
	$opstring .=	"<div class=\"table-responsive\">";
95
	$opstring .=		"<table class=\"table table-striped table-hover table-condensed sortable-theme-bootstrap\" data-sortable>";
96
	$opstring .=			"<thead>";
97
	$opstring .=				"<tr>";
98
	$opstring .=					"<th>" . gettext('Name/Time') . "</th>";
99
	$opstring .=					"<th>" . gettext('Real/Virtual IP') . "</th>";
100
	$opstring .=					"<th></th>";
101
	$opstring .=				"</tr>";
102
	$opstring .=			"</thead>";
103
	$opstring .=			"<tbody>";
104

  
105
				$rowIndex = 0;
106
				foreach ($server['conns'] as $conn):
107
					$evenRowClass = $rowIndex % 2 ? " listMReven" : " listMRodd";
108
					$rowIndex++;
109

  
110
	$opstring .=				"<tr name=\"" . "r:" . $server['mgmt'] . ":" . $conn['remote_host'] . "\" class=\"" . $evenRowClass . "\">";
111
	$opstring .=					"<td>";
112
	$opstring .=						$conn['common_name'];
113
	$opstring .=					"</td>";
114
	$opstring .=					"<td>";
115
	$opstring .=						$conn['remote_host'];
116
	$opstring .=					"</td>";
117
	$opstring .=					"<td>";
118
	$opstring .=						"<i class=\"fa fa-times-circle\" ";
119
	$opstring .=							"onclick=\"killClient('" . $server['mgmt'] . "', '" . $conn['remote_host'] . "');\" ";
120
	$opstring .=							"style=\"cursor:pointer;\" ";
121
	$opstring .=							"name=\"" . "i:" . $server['mgmt'] . ":" . $conn['remote_host'] . "\" ";
122
	$opstring .=							"title=\"" . sprintf(gettext('Kill client connection from %s'), $conn['remote_host']) . "\">";
123
	$opstring .=						"</i>";
124
	$opstring .=					"</td>";
125
	$opstring .=				"</tr>";
126
	$opstring .=				"<tr name=\"" . "r:" . $server['mgmt'] . ":" . $conn['remote_host'] . "\" class=\"" . $evenRowClass . "\">";
127
	$opstring .=					"<td>";
128
	$opstring .=						$conn['connect_time'];
129
	$opstring .=					"</td>";
130
	$opstring .=					"<td>";
131
	$opstring .=						$conn['virtual_addr'];
132
	$opstring .=					"</td>";
133
	$opstring .=					"<td></td>";
134
	$opstring .=				"</tr>";
135

  
136
				endforeach;
137

  
138
	$opstring .=			"</tbody>";
139
	$opstring .=		"</table>";
140
	$opstring .=	"</div>";
141
	$opstring .= "</div>";
142

  
143
	endforeach;
144

  
145
	print($opstring);
146

  
147
	if (!empty($sk_servers)):
148

  
149
	$opstring = "";
150
	$opstring .= "<div class=\"widget panel panel-default\">";
151
	$opstring .=	"<div class=\"panel-heading\"><h2 class=\"panel-title\">" . gettext("Peer to Peer Server Instance Statistics") . "</h2></div>";
152
	$opstring .=	"<div class=\"table-responsive\">";
153
	$opstring .=		"<table class=\"table table-striped table-hover table-condensed sortable-theme-bootstrap\" data-sortable>";
154
	$opstring .=			"<thead>";
155
	$opstring .=				"<tr>";
156
	$opstring .=					"<th>" . gettext('Nome/Time') . "</th>";
157
	$opstring .=					"<th>" . gettext('Remote/Virtual IP') . "</th>";
158
	$opstring .=					"<th></th>";
159
	$opstring .=				"</tr>";
160
	$opstring .=			"</thead>";
161
	$opstring .=			"<tbody>";
162

  
163
				foreach ($sk_servers as $sk_server):
164

  
165
	$opstring .=				"<tr name=\"r:" . $sk_server['port'] . ":" . $sk_server['remote_host'] . "\">";
166
	$opstring .=					"<td>";
167
	$opstring .=						$sk_server['name'];
168
	$opstring .=					"</td>";
169
	$opstring .=					"<td>";
170
	$opstring .=						$sk_server['remote_host'];
171
	$opstring .=					"</td>";
172
	$opstring .=					"<td>";
173

  
174
					if ($sk_server['status'] == "up") {
175
						/* tunnel is up */
176
	$opstring .=						"<i class=\"fa fa-arrow-up text-success\"></i>";
177
					} else {
178
						/* tunnel is down */
179
	$opstring .=						"<i class=\"fa fa-arrow-down text-danger\"></i>";
180
					}
181

  
182
	$opstring .=					"</td>";
183
	$opstring .=				"</tr>";
184
	$opstring .=				"<tr name=\"r:" . $sk_server['port'] . ":" . $sk_server['remote_host'] . "\">";
185
	$opstring .=					"<td>";
186
	$opstring .=						$sk_server['connect_time'];
187
	$opstring .=					"</td>";
188
	$opstring .=					"<td>";
189
	$opstring .=						$sk_server['virtual_addr'];
190
	$opstring .=					"</td>";
191
	$opstring .=					"<td></td>";
192
	$opstring .=				"</tr>";
193

  
194
				endforeach;
195

  
196
	$opstring .=			"</tbody>";
197
	$opstring .=		"</table>";
198
	$opstring .=	"</div>";
199
	$opstring .= "</div>";
200

  
201
	print($opstring);
202

  
203
	endif;
204

  
205
	if (!empty($clients)):
206
		$opstring = "";
207

  
208
	$opstring .= "<div class=\"widget panel panel-default\">";
209
	$opstring .=	"<div class=\"panel-heading\"><h2 class=\"panel-title\">" . gettext("Client Instance Statistics") . "</h2></div>";
210
	$opstring .=	"<div class=\"table-responsive\">";
211
	$opstring .=		"<table class=\"table table-striped table-hover table-condensed sortable-theme-bootstrap\" data-sortable>";
212
	$opstring .=			"<thead>";
213
	$opstring .=				"<tr>";
214
	$opstring .=					"<th>" . gettext('Name/Time') . "</th>";
215
	$opstring .=					"<th>" . gettext('Remote/Virtual IP') . "</th>";
216
	$opstring .=					"<th></th>";
217
	$opstring .=				"</tr>";
218
	$opstring .=			"</thead>";
219
	$opstring .=			"<tbody>";
220

  
221
				foreach ($clients as $client):
222

  
223
	$opstring .=				"<tr name=\"r:" . client['port'] . ":" . $client['remote_host'] . "\">";
224
	$opstring .=					"<td>";
225
	$opstring .=						$client['name'];
226
	$opstring .=					"</td>";
227
	$opstring .=					"<td>";
228
	$opstring .=						$client['remote_host'];
229
	$opstring .=					"</td>";
230
	$opstring .=					"<td>";
231

  
232
					if ($client['status'] == "up") {
233
						/* tunnel is up */
234
	$opstring .=						"<i class=\"fa fa-arrow-up text-success\"></i>";
235
					} else {
236
						/* tunnel is down */
237
	$opstring .=						"<i class=\"fa fa-arrow-down text-danger\"></i>";
238
					}
239

  
240
	$opstring .=					"</td>";
241
	$opstring .=				"</tr>";
242
	$opstring .=				"<tr name=\"r:" . $client['port'] . ":" . $client['remote_host'] . "\">";
243
	$opstring .=					"<td>";
244
	$opstring .=						$client['connect_time'];
245
	$opstring .=					"</td>";
246
	$opstring .=					"<td>";
247
	$opstring .=						$client['virtual_addr'];
248
	$opstring .=					"</td>";
249
	$opstring .=					"<td></td>";
250
	$opstring .=				"</tr>";
251

  
252
				endforeach;
253

  
254
	$opstring .=			"</tbody>";
255
	$opstring .=		"</table>";
256
	$opstring .=	"</div>";
257
	$opstring .= "</div>";
258

  
259
	print($opstring);
260

  
261
	endif;
262

  
263
	if ((empty($clients)) && (empty($servers)) && (empty($sk_servers))) {
264
		print(gettext("No OpenVPN instances defined"));
265
	}
266
}
267

  
77 268
?>
78 269

  
79 270
<script type="text/javascript">
......
98 289
			function(index,row) { $(row).fadeOut(1000); }
99 290
		);
100 291
	}
101
//]]>
102
</script>
103
<div class="content">
104
<?php foreach ($servers as $server): ?>
105

  
106
<div class="widget panel panel-default">
107
	<div class="panel-heading"><h2 class="panel-title"><?=htmlspecialchars($server['name']);?></h2></div>
108
	<div class="table-responsive">
109
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
110
			<thead>
111
				<tr>
112
					<th><?=gettext('Name/Time')?></th>
113
					<th><?=gettext('Real/Virtual IP')?></th>
114
					<th></th>
115
				</tr>
116
			</thead>
117
			<tbody>
118
<?php
119
			$rowIndex = 0;
120
			foreach ($server['conns'] as $conn):
121
				$evenRowClass = $rowIndex % 2 ? " listMReven" : " listMRodd";
122
				$rowIndex++;
123
?>
124
				<tr name="<?php echo "r:{$server['mgmt']}:{$conn['remote_host']}"; ?>" class="<?=$evenRowClass?>">
125
					<td>
126
						<?=$conn['common_name'];?>
127
					</td>
128
					<td>
129
						<?=$conn['remote_host'];?>
130
					</td>
131
					<td>
132
						<i class="fa fa-times-circle"
133
							onclick="killClient('<?=$server['mgmt']; ?>', '<?=$conn['remote_host']; ?>');"
134
							style="cursor:pointer;"
135
							name="<?php echo "i:{$server['mgmt']}:{$conn['remote_host']}"; ?>"
136
							title=<?=sprintf(gettext('Kill client connection from %s'), $conn['remote_host']);?>>
137
						</i>
138
					</td>
139
				</tr>
140
				<tr name="<?php echo "r:{$server['mgmt']}:{$conn['remote_host']}"; ?>" class="<?=$evenRowClass?>">
141
					<td>
142
						<?=$conn['connect_time'];?>
143
					</td>
144
					<td>
145
						<?=$conn['virtual_addr'];?>
146
					</td>
147
					<td></td>
148
				</tr>
149
<?php
150
			endforeach;
151
?>
152
			</tbody>
153
		</table>
154
	</div>
155
</div>
156 292

  
157
<?php
158
endforeach;
293
	// REfreh the panel
294
	function get_update() {
295
		var ajaxRequest;
159 296

  
160
if (!empty($sk_servers)):
161
?>
162
<div class="widget panel panel-default">
163
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Peer to Peer Server Instance Statistics");?></h2></div>
164
	<div class="table-responsive">
165
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
166
			<thead>
167
				<tr>
168
					<th><?=gettext('Name/Time')?></th>
169
					<th><?=gettext('Remote/Virtual IP')?></th>
170
					<th></th>
171
				</tr>
172
			</thead>
173
			<tbody>
174
<?php
175
			foreach ($sk_servers as $sk_server):
176
?>
177
				<tr name='<?php echo "r:{$sk_server['port']}:{$sk_server['remote_host']}"; ?>'>
178
					<td>
179
						<?=$sk_server['name'];?>
180
					</td>
181
					<td>
182
						<?=$sk_server['remote_host'];?>
183
					</td>
184
					<td>
185
<?php
186
				if ($sk_server['status'] == "up") {
187
					/* tunnel is up */
188
					echo '<i class="fa fa-arrow-up text-success"></i>';
189
				} else {
190
					/* tunnel is down */
191
					echo '<i class="fa fa-arrow-down text-danger"></i>';
192
				}
193
?>
194
					</td>
195
				</tr>
196
				<tr name="<?php echo "r:{$sk_server['port']}:{$sk_server['remote_host']}"; ?>">
197
					<td>
198
						<?=$sk_server['connect_time'];?>
199
					</td>
200
					<td>
201
						<?=$sk_server['virtual_addr'];?>
202
					</td>
203
					<td></td>
204
				</tr>
205
<?php
206
			endforeach;
207
?>
208
			</tbody>
209
		</table>
210
	</div>
211
</div>
297
		ajaxRequest = $.ajax({
298
				url: "/widgets/widgets/openvpn.widget.php",
299
				type: "post",
300
				data: { ajax: "ajax"}
301
			});
212 302

  
213
<?php
214
endif;
303
		// Deal with the results of the above ajax call
304
		ajaxRequest.done(function (response, textStatus, jqXHR) {
305
			$('#mainpanel').html(response);
215 306

  
216
if (!empty($clients)):
217
?>
218
<div class="widget panel panel-default">
219
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Client Instance Statistics");?></h2></div>
220
	<div class="table-responsive">
221
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
222
			<thead>
223
				<tr>
224
					<th><?=gettext('Name/Time')?></th>
225
					<th><?=gettext('Remote/Virtual IP')?></th>
226
					<th></th>
227
				</tr>
228
			</thead>
229
			<tbody>
230
<?php
231
			foreach ($clients as $client):
232
?>
233
				<tr name="<?php echo "r:{$client['port']}:{$client['remote_host']}"; ?>">
234
					<td>
235
						<?=$client['name'];?>
236
					</td>
237
					<td>
238
					<?=$client['remote_host'];?>
239
					</td>
240
					<td>
241
<?php
242
				if ($client['status'] == "up") {
243
					/* tunnel is up */
244
					echo '<i class="fa fa-arrow-up text-success"></i>';
245
				} else {
246
					/* tunnel is down */
247
					echo '<i class="fa fa-arrow-down text-danger"></i>';
248
				}
249
?>
250
					</td>
251
				</tr>
252
				<tr name="<?php echo "r:{$client['port']}:{$client['remote_host']}"; ?>">
253
					<td>
254
						<?=$client['connect_time'];?>
255
					</td>
256
					<td>
257
						<?=$client['virtual_addr'];?>
258
					</td>
259
					<td></td>
260
				</tr>
261
<?php
262
			endforeach;
263
?>
264
			</tbody>
265
		</table>
266
	</div>
267
</div>
307
			// and do it again
308
			setTimeout(get_update, 5000);
309
		});
310
	}
268 311

  
269
<?php
270
endif;
312
	events.push(function(){
313
		// Start polling for updates some small random number of seconds from now (so that all the widgets don't
314
		// hit the server at exactly the same time)
315
        setTimeout(get_update, Math.floor((Math.random() * 10000) + 1000));
316
	});
317
//]]>
318
</script>
319
<div id="mainpanel" class="content">
271 320

  
272
if ((empty($clients)) && (empty($servers)) && (empty($sk_servers))) {
273
	echo gettext("No OpenVPN instances defined");
274
}
321
<?php
322
	printPanel();
275 323
?>
276 324
</div>

Also available in: Unified diff