fix proto sort
[tomato/davidwu.git] / release / src / router / www / restrict-edit.asp
blob00cbcd3663f117f25158cb09bac97f33794fa402
1 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0//EN'>
2 <!--
3 Tomato GUI
4 Copyright (C) 2006-2010 Jonathan Zarate
5 http://www.polarcloud.com/tomato/
7 For use with Tomato Firmware only.
8 No part of this file may be used without permission.
9 -->
10 <html>
11 <head>
12 <meta http-equiv='content-type' content='text/html;charset=utf-8'>
13 <meta name='robots' content='noindex,nofollow'>
14 <title>[<% ident(); %>] Edit Access Restrictions</title>
15 <link rel='stylesheet' type='text/css' href='tomato.css'>
16 <link rel='stylesheet' type='text/css' href='color.css'>
17 <script type='text/javascript' src='tomato.js'></script>
18 <script type='text/javascript' src='protocols.js'></script>
20 <!-- / / / -->
22 <style type='text/css'>
23 #res-comp-grid {
24 width: 60%;
26 #res-bp-grid .box1, #res-bp-grid .box2 {
27 width: 20%;
28 float: left;
30 #res-bp-grid .box3 {
31 width: 60%;
32 float: left;
34 #res-bp-grid .box4 {
35 width: 30%;
36 float: left;
37 clear: left;
38 padding-top: 2px;
40 #res-bp-grid .box5 {
41 width: 70%;
42 float: left;
43 padding-top: 2px;
45 #res-bp-grid .box6 {
46 width: 30%;
47 float: left;
48 padding-top: 2px;
50 #res-bp-grid .box7 {
51 width: 70%;
52 float: left;
53 padding-top: 2px;
56 textarea {
57 width: 99%;
58 height: 20em;
60 </style>
62 <script type='text/javascript' src='debug.js'></script>
64 <script type='text/javascript'>
65 // <% nvram(''); %> // http_id
67 // {enable}|{begin_mins}|{end_mins}|{dow}|{comp[<comp]}|{rules<rules[...]>}|{http[ ...]}|{http_file}|{desc}
68 // <% rrule(); %>
69 if ((rule = rrule.match(/^(\d+)\|(-?\d+)\|(-?\d+)\|(\d+)\|(.*?)\|(.*?)\|([^|]*?)\|(\d+)\|(.*)$/m)) == null) {
70 rule = ['', 1, 1380, 240, 31, '', '', '', 0, 'New Rule ' + (rruleN + 1)];
72 rule[2] *= 1;
73 rule[3] *= 1;
74 rule[4] *= 1;
75 rule[8] *= 1;
77 // <% layer7(); %>
78 layer7.sort();
79 for (i = 0; i < layer7.length; ++i)
80 layer7[i] = [layer7[i],layer7[i]];
81 layer7.unshift(['', 'Layer 7 (disabled)']);
83 var ipp2p = [
84 [0,'IPP2P (disabled)'],[0xFFFF,'All IPP2P Filters'],[1,'AppleJuice'],[2,'Ares'],[4,'BitTorrent'],[8,'Direct Connect'],
85 [16,'eDonkey'],[32,'Gnutella'],[64,'Kazaa'],[128,'Mute'],[4096,'PPLive/UUSee'],[256,'SoulSeek'],[512,'Waste'],[1024,'WinMX']
86 /* LINUX26-BEGIN */
87 ,[2048,'XDCC'],[8192,'Xunlei/QQCyclone']
88 /* LINUX26-END */
91 var dowNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
95 var cg = new TomatoGrid();
97 cg.verifyFields = function(row, quiet) {
98 var f = fields.getAll(row)[0];
99 if (v_mac(f, true)) return true;
100 if (_v_iptaddr(f, true, false, true, true)) return true;
102 ferror.set(f, 'Invalid MAC address or IP address/range', quiet);
103 return false;
106 cg.setup = function() {
107 var a, i, count, ex;
109 this.init('res-comp-grid', 'sort', 500, [ { type: 'text', maxlen: 32 } ] );
110 this.headerSet(['MAC / IP Address']);
111 this.showNewEditor();
112 this.resetNewEditor();
114 if (rule[5] == '~') return; // wireless disable rule
116 ex = 0;
117 count = 0;
118 a = rule[5].split('>');
119 for (i = 0; i < a.length; ++i) {
120 if (!a[i].length) continue;
121 if (a[i] == '!') {
122 ex = 1;
124 else {
125 cg.insertData(-1, [a[i]]);
126 ++count;
130 a = E('_f_comp_all')
131 if (count) {
132 a.value = ex ? 2 : 1;
134 else {
135 a.value = 0;
140 var bpg = new TomatoGrid();
142 bpg.verifyFields = function(row, quiet) {
143 var f = fields.getAll(row);
144 ferror.clearAll(f);
145 this.enDiFields(row);
147 if ((f[5].selectedIndex != 0) && ((!v_length(f[6], quiet, 1)) || (!_v_iptaddr(f[6], quiet, false, true, true)))) return 0;
148 if ((f[1].selectedIndex != 0) && (!v_iptport(f[2], quiet))) return 0;
150 if ((f[1].selectedIndex == 0) && (f[3].selectedIndex == 0) && (f[4].selectedIndex == 0) && (f[5].selectedIndex == 0)) {
151 var m = 'Please enter a specific address or port, or select an application match';
152 ferror.set(f[3], m, 1);
153 ferror.set(f[4], m, 1);
154 ferror.set(f[5], m, 1);
155 ferror.set(f[1], m, quiet);
156 return 0;
159 ferror.clear(f[1]);
160 ferror.clear(f[3]);
161 ferror.clear(f[4]);
162 ferror.clear(f[5]);
163 ferror.clear(f[6]);
164 return 1;
167 bpg.dataToView = function(data) {
168 var s, i;
170 s = '';
171 if (data[5] != 0) s = ((data[5] == 1) ? 'To ' : 'From ') + data[6] + ', ';
173 if (data[0] <= -2) s += (s.length ? 'a' : 'A') + 'ny protocol';
174 else if (data[0] == -1) s += 'TCP/UDP';
175 else if (data[0] >= 0) s += protocols[data[0]] || data[0];
177 if (data[0] >= -1) {
178 if (data[1] == 'd') s += ', dst port ';
179 else if (data[1] == 's') s += ', src port ';
180 else if (data[1] == 'x') s += ', port ';
181 else s += ', all ports';
182 if (data[1] != 'a') s += data[2].replace(/:/g, '-');
185 if (data[3] != 0) {
186 for (i = 0; i < ipp2p.length; ++i) {
187 if (data[3] == ipp2p[i][0]) {
188 s += ', IPP2P: ' + ipp2p[i][1];
189 break;
193 else if (data[4] != '') {
194 s += ', L7: ' + data[4];
197 return [s];
200 bpg.fieldValuesToData = function(row) {
201 var f = fields.getAll(row);
202 return [f[0].value, f[1].value, (f[1].selectedIndex == 0) ? '' : f[2].value, f[3].value, f[4].value, f[5].value, (f[5].selectedIndex == 0) ? '' : f[6].value];
205 bpg.resetNewEditor = function() {
206 var f = fields.getAll(this.newEditor);
207 f[0].selectedIndex = 0;
208 f[1].selectedIndex = 0;
209 f[2].value = '';
210 f[3].selectedIndex = 0;
211 f[4].selectedIndex = 0;
212 f[5].selectedIndex = 0;
213 f[6].value = '';
214 this.enDiFields(this.newEditor);
215 ferror.clearAll(fields.getAll(this.newEditor));
218 bpg._createEditor = bpg.createEditor;
219 bpg.createEditor = function(which, rowIndex, source) {
220 var row = this._createEditor(which, rowIndex, source);
221 if (which == 'edit') this.enDiFields(row);
222 return row;
225 bpg.enDiFields = function(row) {
226 var x;
227 var f = fields.getAll(row);
229 x = f[0].value;
230 x = ((x != -1) && (x != 6) && (x != 17));
231 f[1].disabled = x;
232 if (f[1].selectedIndex == 0) x = 1;
233 f[2].disabled = x;
234 f[3].disabled = (f[4].selectedIndex != 0);
235 f[4].disabled = (f[3].selectedIndex != 0);
236 f[6].disabled = (f[5].selectedIndex == 0);
239 bpg.setup = function() {
240 var a, i, r, count, protos;
242 protos = [[-2, 'Any Protocol'],[-1,'TCP/UDP'],[6,'TCP'],[17,'UDP']];
243 for (i = 0; i < 256; ++i) {
244 if ((i != 6) && (i != 17)) protos.push([i, protocols[i] || i]);
247 this.init('res-bp-grid', 'sort', 500, [ { multi: [
248 { type: 'select', prefix: '<div class="box1">', suffix: '</div>', options: protos },
249 { type: 'select', prefix: '<div class="box2">', suffix: '</div>',
250 options: [['a','Any Port'],['d','Dst Port'],['s','Src Port'],['x','Src or Dst']] },
251 { type: 'text', prefix: '<div class="box3">', suffix: '</div>', maxlen: 32 },
252 { type: 'select', prefix: '<div class="box4">', suffix: '</div>', options: ipp2p },
253 { type: 'select', prefix: '<div class="box5">', suffix: '</div>', options: layer7 },
254 { type: 'select', prefix: '<div class="box6">', suffix: '</div>',
255 options: [[0,'Any Address'],[1,'Dst IP'],[2,'Src IP']] },
256 { type: 'text', prefix: '<div class="box7">', suffix: '</div>', maxlen: 64 }
257 ] } ] );
258 this.headerSet(['Rules']);
259 this.showNewEditor();
260 this.resetNewEditor();
261 count = 0;
263 // ---- proto<dir<port<ipp2p<layer7[<addr_type<addr]
265 a = rule[6].split('>');
266 for (i = 0; i < a.length; ++i) {
267 r = a[i].split('<');
268 if (r.length == 5) {
269 // ---- fixup for backward compatibility
270 r.push('0');
271 r.push('');
273 if (r.length == 7) {
274 r[2] = r[2].replace(/:/g, '-');
275 this.insertData(-1, r);
276 ++count;
279 return count;
284 function verifyFields(focused, quiet)
286 var b, e;
288 tgHideIcons();
290 elem.display(PR('_f_sched_begin'), !E('_f_sched_allday').checked);
291 elem.display(PR('_f_sched_sun'), !E('_f_sched_everyday').checked);
293 b = E('rt_norm').checked;
294 elem.display(PR('_f_comp_all'), PR('_f_block_all'), b);
296 elem.display(PR('res-comp-grid'), b && E('_f_comp_all').value != 0);
297 elem.display(PR('res-bp-grid'), PR('_f_block_http'), PR('_f_activex'), b && !E('_f_block_all').checked);
299 ferror.clear('_f_comp_all');
301 e = E('_f_block_http');
302 e.value = e.value.replace(/[|"']/g, ' ');
303 if (!v_length(e, quiet, 0, 2048 - 16)) return 0;
305 e = E('_f_desc');
306 e.value = e.value.replace(/\|/g, '_');
307 if (!v_length(e, quiet, 1)) return 0;
309 return 1;
312 function cancel()
314 document.location = 'restrict.asp';
317 function remove()
319 if (!confirm('Delete this rule?')) return;
321 E('delete-button').disabled = 1;
323 e = E('_rrule');
324 e.name = 'rrule' + rruleN;
325 e.value = '';
326 form.submit('_fom');
329 function save()
331 if (!verifyFields(null, false)) return;
332 if ((cg.isEditing()) || (bpg.isEditing())) return;
334 var a, b, e, s, n, data;
336 data = [];
337 data.push(E('_f_enabled').checked ? '1' : '0');
338 if (E('_f_sched_allday').checked) data.push(-1, -1);
339 else data.push(E('_f_sched_begin').value, E('_f_sched_end').value);
341 if (E('_f_sched_everyday').checked) {
342 n = 0x7F;
344 else {
345 n = 0;
346 for (i = 0; i < 7; ++i) {
347 if (E('_f_sched_' + dowNames[i].toLowerCase()).checked) n |= (1 << i);
349 if (n == 0) n = 0x7F;
351 data.push(n);
353 if (E('rt_norm').checked) {
354 e = E('_f_comp_all');
355 if (e.value != 0) {
356 a = cg.getAllData();
357 if (a.length == 0) {
358 ferror.set(e, 'No MAC or IP address was specified', 0);
359 return;
361 if (e.value == 2) a.unshift('!');
362 data.push(a.join('>'));
364 else {
365 data.push('');
368 if (E('_f_block_all').checked) {
369 data.push('', '', '0');
371 else {
372 var check = 0;
373 a = bpg.getAllData();
374 check += a.length;
375 b = [];
376 for (i = 0; i < a.length; ++i) {
377 a[i][2] = a[i][2].replace(/-/g, ':');
378 b.push(a[i].join('<'));
380 data.push(b.join('>'));
382 a = E('_f_block_http').value.replace(/\r+/g, ' ').replace(/\n+/g, '\n').replace(/ +/g, ' ').replace(/^\s+|\s+$/g, '');
383 check += a.length;
384 data.push(a);
386 n = 0;
387 if (E('_f_activex').checked) n = 1;
388 if (E('_f_flash').checked) n |= 2;
389 if (E('_f_java').checked) n |= 4;
390 data.push(n);
392 if (((check + n) == 0) && (data[0] == 1)) {
393 alert('Please specify what items should be blocked.');
394 return;
398 else {
399 data.push('~');
400 data.push('', '', '', '0');
403 data.push(E('_f_desc').value);
404 data = data.join('|');
406 if (data.length >= 8192) {
407 alert('This rule is too big. Please reduce by ' + (data.length - 8192) + ' characters.');
408 return;
411 e = E('_rrule');
412 e.name = 'rrule' + rruleN;
413 e.value = data;
415 E('delete-button').disabled = 1;
416 form.submit('_fom');
419 function init()
421 cg.recolor();
422 bpg.recolor();
425 function earlyInit()
427 var count;
429 cg.setup();
431 count = bpg.setup();
432 E('_f_block_all').checked = (count == 0) && (rule[7].search(/[^\s\r\n]/) == -1) && (rule[8] == 0);
433 verifyFields(null, 1);
435 </script>
436 </head>
437 <body onload='init()'>
438 <form name='_fom' id='_fom' method='post' action='tomato.cgi'>
439 <table id='container' cellspacing=0>
440 <tr><td colspan=2 id='header'>
441 <div class='title'>Tomato</div>
442 <div class='version'>Version <% version() %></div>
443 </td></tr>
444 <tr id='body'><td id='navi'><script type='text/javascript'>navi()</script></td>
445 <td id='content'>
446 <div id='ident'><% ident(); %></div>
448 <!-- / / / -->
450 <input type='hidden' name='_nextpage' value='restrict.asp'>
451 <input type='hidden' name='_service' value='restrict-restart'>
452 <input type='hidden' name='rruleNN' id='_rrule' value=''>
454 <div class='section-title'>Access Restriction</div>
455 <div class='section'>
456 <script type='text/javascript'>
457 W('<div style="float:right"><small>'+ 'ID: ' + rruleN.pad(2) + '</small>&nbsp;</div><br>');
458 tm = [];
459 for (i = 0; i < 1440; i += 15) tm.push([i, timeString(i)]);
461 createFieldTable('', [
462 { title: 'Enabled', name: 'f_enabled', type: 'checkbox', value: rule[1] == '1' },
463 { title: 'Description', name: 'f_desc', type: 'text', maxlen: 32, size: 35, value: rule[9] },
464 { title: 'Schedule', multi: [
465 { name: 'f_sched_allday', type: 'checkbox', suffix: ' All Day &nbsp; ', value: (rule[2] < 0) || (rule[3] < 0) },
466 { name: 'f_sched_everyday', type: 'checkbox', suffix: ' Everyday', value: (rule[4] & 0x7F) == 0x7F } ] },
467 { title: 'Time', indent: 2, multi: [
468 { name: 'f_sched_begin', type: 'select', options: tm, value: (rule[2] < 0) ? 0 : rule[2], suffix: ' - ' },
469 { name: 'f_sched_end', type: 'select', options: tm, value: (rule[3] < 0) ? 0 : rule[3] } ] },
470 { title: 'Days', indent: 2, multi: [
471 { name: 'f_sched_sun', type: 'checkbox', suffix: ' Sun &nbsp; ', value: (rule[4] & 1) },
472 { name: 'f_sched_mon', type: 'checkbox', suffix: ' Mon &nbsp; ', value: (rule[4] & (1 << 1)) },
473 { name: 'f_sched_tue', type: 'checkbox', suffix: ' Tue &nbsp; ', value: (rule[4] & (1 << 2)) },
474 { name: 'f_sched_wed', type: 'checkbox', suffix: ' Wed &nbsp; ', value: (rule[4] & (1 << 3)) },
475 { name: 'f_sched_thu', type: 'checkbox', suffix: ' Thu &nbsp; ', value: (rule[4] & (1 << 4)) },
476 { name: 'f_sched_fri', type: 'checkbox', suffix: ' Fri &nbsp; ', value: (rule[4] & (1 << 5)) },
477 { name: 'f_sched_sat', type: 'checkbox', suffix: ' Sat', value: (rule[4] & (1 << 6)) } ] },
478 { title: 'Type', name: 'f_type', id: 'rt_norm', type: 'radio', suffix: ' Normal Access Restriction', value: (rule[5] != '~') },
479 { title: '', name: 'f_type', id: 'rt_wl', type: 'radio', suffix: ' Disable Wireless', value: (rule[5] == '~') },
480 { title: 'Applies To', name: 'f_comp_all', type: 'select', options: [[0,'All Computers / Devices'],[1,'The Following...'],[2,'All Except...']], value: 0 },
481 { title: '&nbsp;', text: '<table class="tomato-grid" cellspacing=1 id="res-comp-grid"></table>' },
482 { title: 'Blocked Resources', name: 'f_block_all', type: 'checkbox', suffix: ' Block All Internet Access', value: 0 },
483 { title: 'Port /<br>Application', indent: 2, text: '<table class="tomato-grid" cellspacing=1 id="res-bp-grid"></table>' },
484 { title: 'HTTP Request', indent: 2, name: 'f_block_http', type: 'textarea', value: rule[7] },
485 { title: 'HTTP Requested Files', indent: 2, multi: [
486 { name: 'f_activex', type: 'checkbox', suffix: ' ActiveX (ocx, cab) &nbsp;&nbsp;', value: (rule[8] & 1) },
487 { name: 'f_flash', type: 'checkbox', suffix: ' Flash (swf) &nbsp;&nbsp;', value: (rule[8] & 2) },
488 { name: 'f_java', type: 'checkbox', suffix: ' Java (class, jar) &nbsp;&nbsp;', value: (rule[8] & 4) } ] }
490 </script>
491 </div>
493 <!-- / / / -->
495 </td></tr>
496 <tr><td id='footer' colspan=2>
497 <span id='footer-msg'></span>
498 <input type='button' value='Delete...' id='delete-button' onclick='remove()'>
499 &nbsp;
500 <input type='button' value='Save' id='save-button' onclick='save()'>
501 <input type='button' value='Cancel' id='cancel-button' onclick='cancel()'>
502 </td></tr>
503 </table>
504 <br><br>
505 </form>
506 <script type='text/javascript'>earlyInit();</script>
507 </body>
508 </html>