summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-cshark/luasrc/view/cshark.htm
blob: b71c7fb6ce99a25518f7b0d1ea871ef4e95bcd44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<%#
LuCI - Lua Configuration Interface

Copyright (C) 2014, QA Cafe, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

$Id$

-%>

<fieldset class="cbi-section">
	<legend><%:Start network capture%></legend>
	<div class="cbi-section-node">
		<table class="cbi-section-table">
			<tr>
				<th><%:Interface%></th>
				<th colspan='2'><%:seconds, packets, bytes%></th>
				<th><%:Filter%></th>
				<th><%:Actions%></th>
			</tr>
			<tr>
				<td>
					<select title="<%:Interface%>" style="width:auto" id="s_interfaces">
					<%
						local nixio = require "nixio"
						for k, v in ipairs(nixio.getifaddrs()) do
							if v.family == "packet" then
							%>
								<option value="<%=v.name%>"><%=v.name%> </option>
							<%
							end
						end
					%>
						<option value="any"><%:any%></option>
					</select>
				</td>
				<td colspan='2'>
					<input id="tx_value" type="text" value="0" />
					<select title="<%:timeout, bytes, seconds%>" id="s_value_type" style="width:auto">
						<option value="T"><%:seconds%></option>
						<option value="P"><%:packets%></option>
						<option value="S"><%:bytes%></option>
					</select>
				</td>
				<td>
					<input style="margin: 5px 0" type="text" title="<%:Filter%>" placeholder="filter" id="i_filter" />
				</td>
				<td>
					<input type="button" id="bt_action" data-action="start" value="<%:Start capture%>" class="cbi-button" />
				</td>
			</tr>
		</table>
	</div>
</fieldset>

<fieldset class="cbi-section">
	<span id="cshark-rc-output"></span>
</fieldset>

<hr/>

<fieldset class="cbi-section">
	<legend><%:Capture links%></legend>
	<div class="cbi-section-node">
		<table id="t_link_list" class="cbi-section-table">
			<tr class="cbi-section-table-titles">
				<th class="cbi-section-table-cell"><%:Capture URL%></th>
				<th class="cbi-section-table-cell"><%:Capture time%></th>
			</tr>
		</table>
	</div>
</fieldset>

<fieldset class="cbi-section">
  <a href="https://support.cloudshark.org/openwrt/openwrt-cloudshark.html" target="_blank">Visit support.cloudshark.org for help.</a>
</fieldset>

<hr/>

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

	var capture_running = 0;
	var pid_file = 0;
	var bt_action = document.getElementById('bt_action');
	var a_clear_links = document.getElementById('a_clear_links');
	var output = document.getElementById('cshark-rc-output');
	var loader = '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" width="16" height="16" style="vertical-align:middle" /> ';
	var msg = { 'start' : '<%:Waiting for capture to complete...%>', 'stop' : '<%:Waiting for upload to complete...%>' };
	var status_msg = msg['start'];

	function get_date(timestamp)
	{
		function pad_str(str)
		{
			return (str < 10) ? "0" + str : str;
		}

		var current_date = new Date(timestamp * 1000);
		return current_date.getFullYear() + "-" +
				pad_str(current_date.getMonth() + 1) + "-" +
				pad_str(current_date.getDate()) + " " +
				pad_str(current_date.getHours()) + ":" +
				pad_str(current_date.getMinutes()) + ":" +
				pad_str(current_date.getSeconds());
	}

	bt_action.onclick = function()
	{
		var action = this.getAttribute("data-action");
		var csxhr = new XHR();

		if (action == "stop")
		{
			update_status(action);

			bt_action.disabled = true;

			csxhr.get('<%=luci.dispatcher.build_url("admin", "network")%>/cshark_iface_dump_stop', null,
			function(x)
			{
				if (!x || x.responseText.trim() != "0")
				{
					update_status("failed", "Invalid response on stop.");
				}
			});

		}
		else if (action == "start")
		{

			var s_interfaces = document.getElementById('s_interfaces');
			var s_value_type = document.getElementById('s_value_type');
			var i_filter = document.getElementById('i_filter');

			var if_n = s_interfaces.selectedIndex;
			var t_n = s_value_type.selectedIndex;
			var ifname = s_interfaces.options[if_n].value.trim();
			var filter_val = i_filter.value.trim();
			var tx_val = document.getElementById('tx_value').value.trim();
			var type_val = s_value_type.options[t_n].value.trim();

			if (type_val != 'P' && type_val != 'T' && type_val != 'S') type_val = 'T';

			if (!ifname || !type_val) return;

			if (isNaN(tx_val)) return alert("<%:value for [seconds, packets, bytes] must be Integer%>");

			update_status(action);

			csxhr.get('<%=luci.dispatcher.build_url("admin", "network")%>/cshark_iface_dump_start/' + ifname + '/' + tx_val + '/' + type_val + '/'+ filter_val, null,
			function(x)
			{
				if (!x)
					update_status("failed", "Invalid response on start.");
				else
					update_status("running");
			});
		}
	}

	function update_status(status, message)
	{
		switch (status)
		{
			case 'start':
			case 'stop':
				status_msg = msg[status];
				output.innerHTML = loader + status_msg;
			break

			case 'running':
				if (capture_running) break;;

				output.innerHTML = loader + status_msg;

				bt_action.value = '<%:Stop capture%>';
				bt_action.setAttribute('data-action', 'stop');
				capture_running = 1;
			break;

			case 'completed':
			case 'failed':
				if (!capture_running) break;

				if (status == "completed")
				{
					link_list_update();
				}

				output.innerHTML = "<pre>" + message + "</pre>";
				bt_action.value = '<%:Start capture%>';
				bt_action.setAttribute('data-action', 'start');
				bt_action.disabled = false;
				capture_running = 0;
			break;
		}
	}


	function check_status()
	{

		XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "network")%>/cshark_check_status', null,
		function(x, data)
		{
			if (!x)
			{
				if (capture_running)
					update_status("failed", "Invalid response when fetching status.");

				return;
			}
			console.log(data)

			update_status( (data.status == 1) && "running" || "completed", data.msg);
		})
	}

	function link_list_clear()
	{
		var csxhr_del = new XHR();
		csxhr_del.get('<%=luci.dispatcher.build_url("admin", "network")%>/cshark_link_list_clear', null,
		function(x)
		{
			if (!x)
				return false;

			link_list_update();
		});
	}


	function link_list_update()
	{
		var t_link = document.getElementById("t_link_list");
		if (!t_link) return;

		var row_count = t_link.rows.length;
		while(--row_count) t_link.deleteRow(row_count);

		var cell = t_link.insertRow(-1).insertCell(0);
		cell.colSpan = 2;
		cell.innerHTML = loader;

		var csxhr_link = new XHR();
		csxhr_link.get('<%=luci.dispatcher.build_url("admin", "network")%>/cshark_link_list_get', null,
		function(x, entries)
		{
			var row = t_link.deleteRow(1);

			if (!x) return;

			if (!entries || !entries.length)
			{
				var cell = t_link.insertRow(-1).insertCell(0);
				cell.colSpan = 2;
				cell.innerHTML = '<em><br />There are no captures available yet.</em>';

				return;
			}

			for (var i = 0, len = entries.length; i < len ; i++)
			{
				var entry = entries[i][0];
				if (!entry) continue;

				var data = entry.split(",");
				var url = data[0];
				var timestamp = data[1];

				var row = t_link.insertRow(-1);
				row.insertCell(0).innerHTML = '<a href="'+url+'" target="_blank">'+url+'</a>';
				row.insertCell(1).innerHTML = get_date(timestamp);
			}

			var cell = t_link.insertRow(-1).insertCell(0);
			cell.colSpan = 2;
			cell.style.textAlign="center";
			cell.innerHTML = '<input type="button" onclick="link_list_clear()" class="cbi-button" value ="<%:Clear list%>" />';
		})
	}

	check_status();
	link_list_update();
//]]></script>