Advertisement
rs232

advanced-adblock.asp

Dec 31st, 2021 (edited)
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 14.25 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <!--
  3.     Tomato GUI
  4.     Copyright (C) 2007-2022 FreshTomato
  5.     ver="v2.72b - 04/23" # rs232
  6.     https://www.freshtomato.org/
  7.     For use with Tomato Firmware only.
  8.     No part of this file may be used without permission.
  9. -->
  10. <html lang="en-GB">
  11. <head>
  12. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  13. <meta name="robots" content="noindex,nofollow">
  14. <title>[<% ident(); %>] Advanced: Adblock (DNS filtering)</title>
  15. <link rel="stylesheet" type="text/css" href="tomato.css">
  16. <% css(); %>
  17. <script src="tomato.js"></script>
  18. <script>
  19.  
  20. //  <% nvram("adblock_enable,adblock_blacklist,adblock_blacklist_custom,adblock_whitelist,adblock_path,adblock_limit,adblock_logs"); %>
  21.  
  22. var cprefix = 'advanced_adblock';
  23. var adblockg = new TomatoGrid();
  24. var adblock_refresh = cookie.get(cprefix+'_refresh');
  25.  
  26. adblockg.exist = function(f, v) {
  27.     var data = this.getAllData();
  28.     for (var i = 0; i < data.length; ++i) {
  29.         if (data[i][f] == v) return true;
  30.     }
  31.  
  32.     return false;
  33. }
  34.  
  35. adblockg.dataToView = function(data) {
  36.     return [(data[0] != '0') ? '&#x2b50' : '', data[1], data[2]];
  37. }
  38.  
  39. adblockg.fieldValuesToData = function(row) {
  40.     var f = fields.getAll(row);
  41.  
  42.     return [f[0].checked ? 1 : 0, f[1].value, f[2].value];
  43. }
  44.  
  45. adblockg.verifyFields = function(row, quiet) {
  46.     var ok = 1;
  47.  
  48.     return ok;
  49. }
  50. function verifyFields(focused, quiet) {
  51.     var ok = 1;
  52.         cookie.set(cprefix+'_refresh', adblock_refresh);
  53.     return ok;
  54. }
  55.  
  56. var ref = new TomatoRefresh(' ', ' ', 3, 'advanced_adblock_refresh');
  57.     ref.refresh = function(text) {
  58.     try {
  59.         eval(text);
  60.         }
  61.         catch (ex) {
  62.         }
  63.     adblockStatus();
  64. }
  65.  
  66. adblockg.resetNewEditor = function() {
  67.     var f;
  68.  
  69.     f = fields.getAll(this.newEditor);
  70.     ferror.clearAll(f);
  71.     f[0].checked = 1;
  72.     f[1].value = '';
  73.     f[2].value = '';
  74. }
  75.  
  76. adblockg.setup = function() {
  77.     this.init('adblock-grid', '', 50, [
  78.         { type: 'checkbox', prefix: '<div class="centered">', suffix: '<\/div>' },
  79.         { type: 'text', maxlen: 130 },
  80.         { type: 'text', maxlen: 40 }
  81.     ]);
  82.     this.headerSet(['On', 'Blacklist URL', 'Description']);
  83.     var s = nvram.adblock_blacklist.split('>');
  84.     for (var i = 0; i < s.length; ++i) {
  85.         var t = s[i].split('<');
  86.         if (t.length == 3) this.insertData(-1, t);
  87.     }
  88.     this.showNewEditor();
  89.     this.resetNewEditor();
  90. }
  91.  
  92. function save() {
  93.     var data = adblockg.getAllData();
  94.     var blacklist = '';
  95.     for (var i = 0; i < data.length; ++i) {
  96.         blacklist += data[i].join('<') + '>';
  97.     }
  98.  
  99.     var fom = E('t_fom');
  100.     fom.adblock_enable.value = E('_f_adblock_enable').checked ? 1 : 0;
  101.     fom.adblock_logs.value = fom.f_adblock_logs.value;
  102.     fom.adblock_limit.value = fom.f_adblock_limit.value;
  103.     fom.adblock_path.value = fom.f_adblock_path.value.replace(/\/+$/, '');
  104.     fom.adblock_blacklist.value = blacklist;
  105.     form.submit(fom, 1);
  106.     setTimeout(function() { adblockStatus(); }, 2000);
  107. }
  108.  
  109. function init() {
  110.     if (((c = cookie.get(cprefix + '_notes_vis')) != null) && (c == '1')) {
  111.         toggleVisibility(cprefix, "notes");
  112.     }
  113.     if (((c = cookie.get(cprefix + '_advanced_vis')) != null) && (c == '1')) {     
  114.         toggleVisibility(cprefix, "advanced");
  115.     }
  116.     adblockg.recolor();
  117.     adblockStatus();
  118.     ref.initPage();
  119. }
  120.  
  121. function adblockMe(command) {
  122.     var c = '/usr/sbin/adblock ' +  command ;
  123.     if (command == 'snapshot' ) {
  124.         alert('Result saved in /tmp/adblock.snapshot.$now');
  125.     }
  126.     var cmd = new XmlHttp();
  127.     cmd.post('shell.cgi', 'action=execute&command='+escapeCGI(c.replace(/\r/g, '')));
  128.     setTimeout(function() { adblockStatus(); }, 500);
  129. }
  130.  
  131. function displayStatus() { 
  132. elem.setInnerHTML(E('status'), cmdresult );
  133. cmdresult = '';
  134. }
  135.  
  136. function adblockStatus() {
  137. cmd = new XmlHttp();
  138. cmd.onCompleted = function(text, xml) {
  139. eval(text);
  140. displayStatus();
  141. }
  142. cmd.onError = function(x) {
  143. cmdresult = 'ERROR: '+x;
  144. displayStatus();
  145. }
  146. var commands = '/usr/sbin/adblock status-gui';
  147. cmd.post('shell.cgi', 'action=execute&command='+escapeCGI(commands.replace(/\r/g, '')));
  148. }
  149.  
  150. function earlyInit() {
  151.     adblockg.setup();
  152.     verifyFields(null, true);
  153. }
  154.  
  155. // Determine Delimiter/Separator
  156. function determineDelimiter(inputString) {
  157.   const lines = inputString.split(/\r?\n/);
  158.   let isSpaceDelimited = false;
  159.   var i = 0
  160.   for (const line of lines) {
  161.     const trimmedLine = line.trim();
  162.     if (trimmedLine.startsWith('#') || trimmedLine === '') {
  163.         continue;
  164.     }
  165.     const units = trimmedLine.split(' ');
  166.     if (units.length > 1) {
  167.         return ' ';
  168.     } else if (i > 1) {
  169.         return '\n';
  170.     }
  171.     i += 1;
  172.   }
  173. }
  174.  
  175. // Sort Domains
  176. function sortDomains(element) {
  177.     var textarea = document.getElementById(element);
  178.     var delimiter = determineDelimiter(textarea.value.trim());
  179.     var splitDomains = textarea.value.split(delimiter).map((domain) => domain.trim().split(".").reverse());
  180.     const regex = /[%!#+\s]/g
  181.     splitDomains.sort((a, b) => {
  182.         const aList = a.map(item => item.replace(regex, ''));
  183.         const bList = b.map(item => item.replace(regex, ''));
  184.         var aSeg = aList[1], bSeg = bList[1];
  185.  
  186.         if ( aSeg === undefined || bSeg === undefined) { return 0; }
  187.         if (a.length > 2 && aList[0].length === 2 && aSeg.length <= 3) {
  188.             aSeg = aList[2];
  189.         }
  190.         if (b.length > 2 && bList[0].length === 2 && bSeg.length <= 3) {
  191.             bSeg = bList[2];
  192.         }
  193.         var domainCompare = aSeg.toLowerCase().localeCompare(bSeg.toLowerCase());
  194.         if (domainCompare !== 0) return domainCompare;
  195.        
  196.         var tldCompare = aList[0].toLowerCase().localeCompare(bList[0].toLowerCase());
  197.         if (tldCompare !== 0) return tldCompare;
  198.        
  199.         var i = 1;
  200.         while ( true ) {
  201.             var aSeg = aList[i], bSeg = bList[i];
  202.  
  203.             if ( aSeg === undefined && bSeg === undefined) {
  204.                 return 0;
  205.             } else if (aSeg === undefined)  {
  206.                 return -1;
  207.             } else if ( bSeg === undefined ) {
  208.                 return  1;
  209.             }
  210.  
  211.             var subCompare = aSeg.toLowerCase().localeCompare(bSeg.toLowerCase());
  212.             if (subCompare !== 0) return subCompare;
  213.  
  214.             i += 1;
  215.         }
  216.     });
  217.     var sortedDomains = splitDomains.map((segments) => segments.reverse().join("."));
  218.     textarea.value = sortedDomains.join(delimiter).trim();
  219. }
  220.  
  221. </script>
  222. </head>
  223.  
  224. <body onload="init()">
  225. <form id="t_fom" method="post" action="tomato.cgi">
  226. <table id="container">
  227. <tr><td colspan="2" id="header">
  228.     <div class="title">FreshTomato</div>
  229.     <div class="version">Version <% version(); %> on <% nv("t_model_name"); %></div>
  230. </td></tr>
  231. <tr id="body"><td id="navi"><script>navi()</script></td>
  232. <td id="content">
  233. <div id="ident"><% ident(); %> | <script>wikiLink();</script></div>
  234.  
  235. <!-- / / / -->
  236.  
  237. <input type="hidden" name="_nextpage" value="advanced-adblock.asp">
  238. <input type="hidden" name="_service" value="adblock-restart">
  239. <input type="hidden" name="adblock_enable">
  240. <input type="hidden" name="adblock_logs">
  241. <input type="hidden" name="adblock_path">
  242. <input type="hidden" name="adblock_limit">
  243. <input type="hidden" name="adblock_blacklist">
  244.  
  245. <!-- / / / -->
  246.  
  247. <div class="section-title">Adblock (DNS filtering) - Settings</div>
  248. <div class="section">
  249. <div class="section">
  250.     <script>
  251.         createFieldTable('', [
  252.             { title: 'Enable', name: 'f_adblock_enable', type: 'checkbox', value: nvram.adblock_enable != '0' },
  253.             { title: 'Max Log Level', indent: 2, name: 'f_adblock_logs', type: 'select', options: [[0,'Only Basic'],[3,'3 Error (default)'],[4,'4 Warning'],[5,'5 Notification'],[6,'6 Info'],[7,'7 Debug + trace mode']], value: nvram.adblock_logs },
  254.             { title: 'Blockfile size limit', indent: 2, name: 'f_adblock_limit', type: 'text', placeholder: 'empty = reset', maxlen: 32, size: 15, suffix: '&nbsp;<small>Bytes<\/small>', value: nvram.adblock_limit },
  255.             { title: 'Custom path (optional)', indent: 2, name: 'f_adblock_path', type: 'text', placeholder: 'empty = /tmp', maxlen: 64, size: 15, suffix: '<small>/adblock/<\/small>', value: nvram.adblock_path }
  256.         ]);
  257.     </script>
  258. </div>
  259.  
  260. <!-- / / / -->
  261.  
  262. <div class="section-title">Domain blacklist URLs & Group-of-lists</div>
  263. <div class="section">
  264.     <div class="tomato-grid" id="adblock-grid"></div>
  265. </div>
  266.  
  267. <!-- / / / -->
  268.  
  269. <div class="section-title">Domain blacklist custom</div><input type="button" value="Sort domains backward a-z ↓" onclick="sortDomains('domain-blacklist')" id="sort-button-blacklist" style="float: right;">
  270. <div class="section">
  271.     <script>
  272.         createFieldTable('', [
  273.             { title: 'Individual domains and/or path to external file/s.<br>Domains defined with a prepending <b>+<\/b> will have any found subdomain pruned from the blockfile.<br>Prepend <b>#<\/b> to comment.', name: 'adblock_blacklist_custom', type: 'textarea', placeholder: 'baddomain.com&#10;/mnt/usb/list-of-bad-domains.list&#10;/mnt/usb/list-of-blacklisted-urls.list&#10;+prune-subdomains.com', value: nvram.adblock_blacklist_custom, id: 'domain-blacklist' }
  274.         ]);
  275.     </script>
  276. </div>
  277.  
  278.  
  279. <!-- / / / -->
  280.  
  281. <div class="section-title">Domain whitelist</div>
  282. <input type="button" value="Sort domains backward a-z ↓" onclick="sortDomains('domain-whitelist')" id="sort-button-whitelist" style="float: right;">
  283. <div class="section">
  284.     <script>
  285.         createFieldTable('', [
  286.             { title: 'Individual domains and/or path to external file/s.<br>Domains defined with a prepending <b>%<\/b> will not have the own subdomains blocked.<br>Prepend <b>#<\/b> to comment.', name: 'adblock_whitelist', type: 'textarea', placeholder: 'gooddomain.com\&#10;/mnt/usb/list-of-good-domains.list&#10;/mnt/usb/file-cointaining-list-of-urls.list&#10;%onlythis-nosubdomains.com', value: nvram.adblock_whitelist, id: 'domain-whitelist' }
  287.         ]);
  288.     </script>
  289. </div>
  290.  
  291. <!-- / / / -->
  292. <div class="section-title">Advanced</div>
  293. <div class="section">
  294.     <table cellspacing=1 cellpadding=2 border=0>
  295.     <tr><td>Controls -</td><td>Status -</td></tr>
  296.     <tr><td>&nbsp;</td>
  297.     <th rowspan=10 valign="top" style="text-align: left; padding-left: 30px; padding-top: 5px; font-size:9px; width: 100%; border: 1px solid #aaaaaa"><div id="status"><wbr></div></th></tr>
  298.     <tr><td><input type="button" style="width:130px" value="▶️ Load " id="adblock-start" onclick="adblockMe('start');"></td></tr>
  299.     <tr><td><input type="button" style="width:130px" value="⏏️ Unload" id="adblock-stop" onclick="adblockMe('stop');"></td></tr>
  300.     <tr><td><input type="button" style="width:130px" value="🔄 Update" id="adblock-update" onclick="adblockMe('update');"></td></tr>
  301.     <tr><td><input type="button" style="width:130px" value="♻️ Reset limit" id="adblock-reset" onclick="adblockMe('reset');"></td></tr>
  302.     <tr><td><input type="button" style="width:130px" value="🧹 Clear all files" id="adblock-clear" onclick="adblockMe('clear');"></td></tr>
  303.     <tr><td><input type="button" style="width:130px" value="📷 Snapshot" id="adblock-snapshot" onclick="adblockMe('snapshot');"></td></tr>
  304.     <tr><td><input type="button" style="width:130px" value="☑️ Enable only" id="adblock-enable" onclick="adblockMe('enable');"></td></tr>
  305.     <tr><td><input type="button" style="width:130px" value="⬜ Disable only" id="adblock-disable" onclick="adblockMe('disable');"></td></tr>
  306.     <tr><td>&nbsp;</td>
  307.     <tr><th colspan=2>
  308.     <div id="survey-controls">
  309.         <img src="spin.gif" alt="" id="refresh-spinner">
  310.         <small>Status - </small><script>genStdTimeList('refresh-time', 'One off', 5);</script>
  311.         <input type="button" value="Refresh" onclick="ref.toggle()" id="refresh-button">
  312.     </div>
  313.     </div></th></tr>
  314.     </table></div>
  315. </div>
  316.  
  317. <div class="section-title">Notes <small><i><a href='javascript:toggleVisibility(cprefix,"notes");'><span id="sesdiv_notes_showhide">(Show)</span></a></i></small></div>
  318. <div class="section" id="sesdiv_notes" style="display:none">
  319.     <ul>
  320.         <li><b>Enable</b> - Used to activate/deactivate the adblock function. When enable is set the script runs after a save, a manual Load/Update, it autostart at boot and set autoupdate to run daily at a random time between 3am and 6am (excluding mins 59,00,01).</li>
  321.         <li><b>Blockfile size limit</b> - Defined in Bytes, it's an automatically calculated hard limit for the dnsmasq.adblock file. This limit can be overwritten manually. Removing the number and saving will trigger an internal calculation performed at the next run.</li>
  322.         <li><b>Custom path</b> - Optional, allows to save the potentially large adblock files on permanent storage like USB/CIFS/etc. This indirectly also means lower RAM usage and additional list control to avoid downloads/processing when not necessary.</li>
  323.         <li><b>Blacklist URL & Group-of-lists</b> - Supported blacklist can come in multiple format. as long as they are text and with maximum one domain reference per line. Empty lines and lines starting with "#" or "!" are always ignored. A particular note on the Group-of-lists format where the content of the defined list contains references to external URLs e.g.<br><code>[https://provider.com/badaddresses.txt] --> containing a list of URLs</code>.</li>
  324.         <li><b>Blacklist Custom</b> - Optional, newline separated: domain1.com domain2.com domain3.com. It also accepts external files as a source e.g. <code>/mnt/usb/blacklist</code>, with one domain per line. Prepending a '+' to the domain will force a removal of all the child domains from the blocklist file keeping only the custom defined one (blocking all its subdomains).</li>
  325.         <li><b>Whitelist</b> - Optional, newline separated: domain1.com domain2.com domain3.com. It also accepts external files as a source e.g. <code>/mnt/usb/whitelist</code>, with one domain per line. Please note by default given a domain, any of its subdomains will be whitelisted. To have a domain strictly whitelisted (subdomains blocked) prepend a <B>%</B> to the domain.</li>
  326.         <li><b>Files</b> - Do not defined your custom files within the adblock folder as this is periodically cleaned up
  327.         <li><b>Caution</b> - Configuring large blocklists in adblock is not ideal. Add one list at the time and monitor the RAM usage. There are multiple protections in place but the most important is to trim down your final blocklist if too many resources are needed, this is reflected in the <code>Blockfile size limit</code> field</li>
  328.         <li><b>Hold-time</b> - There's a 30 min hold-time between consecutive updates to avoid false positive calls. This can be manually overridden performing an unload + update</li>
  329. </ul>
  330. </div>
  331.  
  332. <!-- / / / -->
  333.  
  334. <div id="footer">
  335.     <span id="footer-msg"></span>
  336.     <input type="button" value="Save" id="save-button" onclick="save()">
  337.     <input type="button" value="Cancel" id="cancel-button" onclick="reloadPage();">
  338. </div>
  339. </td></tr>
  340. </table>
  341. </form>
  342. <script>earlyInit();</script>
  343. </body>
  344. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement