Advertisement
Guest User

Ciencia Al Poder / checkForBlacklistedIP

a guest
Aug 15th, 2011
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. Colocar esto en el CSS personal para que esas IP aparezcan de color rojo:
  4.  
  5.  a.mw-userlink.IPBlackList {background: red; font-weight: bold;}
  6.  
  7. */
  8.  
  9. /*
  10.  * Utilidad para señalar los enlaces a las contribuciones de direcciones IP a partir de rangos de IP
  11.  * Autor: Jesús Martínez Novo - [[User:Ciencia Al Poder]]
  12.  */
  13. (function() {
  14.  
  15. var reIPContrib = /^Especial:Contribuciones\/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$/;
  16. var IPBlackList = [
  17.     // Definir aquí los rangos de IP a señalar.
  18.     //  Ejemplo:
  19.     //  {range: '127.0.0.0/8'},
  20.     //  {range: '172.16.0.0/12'},
  21.     //  {range: '192.168.0.0/24'} <-- El último sin la coma al final!!
  22.     {range: '62.43.0.0/16'}
  23. ];
  24.  
  25. function checkForBlacklistedIP() {
  26.     // Init cache object
  27.     for (var i = 0; i < IPBlackList.length; i++) {
  28.         IPBlackList[i].oIP = splitIP(IPBlackList[i].range);
  29.     }
  30.     var contLinks = $('#bodyContent').find('a.mw-userlink[title^="Especial:Contribuciones/"]').filter(checkJQAnchorIPBL);
  31.     if (contLinks.length) {
  32.         alert('IP sospechosas encontradas');
  33.     } else {
  34.         alert('No se han encontrado IP sospechosas');
  35.     }
  36.     contLinks.addClass('IPBlackList');
  37.     return contLinks;
  38. }
  39.  
  40. function checkJQAnchorIPBL() {
  41.     var t = this.title;
  42.     if (reIPContrib.test(t)) {
  43.         var oIP = splitIP(t.substr('Especial:Contribuciones/'.length));
  44.         var oBLIP = null;
  45.         for (var i = 0; i < IPBlackList.length; i++) {
  46.             oBLIP = IPBlackList[i].oIP;
  47.             if (matchIP(oIP, oBLIP)) return true;
  48.         }
  49.     }
  50.     return false;
  51. }
  52.  
  53. function splitIP(ip) {
  54.     var ip_net = ip.split('/');
  55.     var ip_parts = ip_net[0].split('.');
  56.     var netmask = 32;
  57.     if (ip_net.length == 2) {
  58.         netmask = parseInt(ip_net[1], 10);
  59.     }
  60.     return {ip: ip_parts, mask: netmask};
  61. }
  62.  
  63. function matchIP(oIP, oRange) {
  64.     var mask = oRange.mask;
  65.     for (var i = 0; i < 4; i++) {
  66.         if (mask / 8 > 1) {
  67.             if (oIP.ip[i] != oRange.ip[i]) {
  68.                 return false;
  69.             }
  70.         } else {
  71.             var bitmask = 8 - mask;
  72.             if (bitmask == 0) return true;
  73.             bitmask = Math.pow(2,bitmask);
  74.             if ( (parseInt(oIP.ip[i], 10) & bitmask) == (parseInt(oRange.ip[i], 10) & bitmask) ) return true;
  75.             return false;
  76.         }
  77.         mask -= 8;
  78.     }
  79. }
  80.  
  81. window.checkForBlacklistedIP = checkForBlacklistedIP;
  82.  
  83. }());
  84.  
  85. // Añadir enlace de chequeo en Especial:Contribuciones
  86. if (window.wgNamespaceNumber == -1 && window.wgCanonicalSpecialPageName == 'Recentchanges') {
  87.     $(function() {
  88.         addPortletLink('p-cactions', 'javascript:window.checkForBlacklistedIP();void(0);', 'Comprobar IP en ListaNegra');
  89.     });
  90.     // En vez de lo anterior, se puede hacer automáticamente así
  91.     //$(checkForBlacklistedIP);
  92.     // O incluso ponerlo fuera de este if, o cambiándolo para que funcione en otras páginas o en todas.
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement