Bez tytułu

z k4be, 5 lata temu, napisane w JavaScript, wyświetlone 409 razy.
URL https://pastebin.k4be.pl/view/722ef110 Udostępnij
Pobierz wklejkę lub Pokaż surowy tekst
  1. var irc = require('irc');
  2. var fs = require('fs');
  3. var https = require('https');
  4. var xml2js = require('xml2js');
  5.  
  6. var fs = require('fs');
  7. var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
  8.  
  9. var hopmPattern = /^(?:(CHECK) -> )?OPEN PROXY(?: -> [^ ]+)? ([0-9a-fA-F.:]+):([0-9]{2,5}) \(([^)]+)\) \[([^\]]+)\]$/;
  10.  
  11. function timeConverter(UNIX_timestamp){
  12.         var a = new Date(UNIX_timestamp * 1000);
  13.         var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  14.         var year = a.getFullYear();
  15.         var month = months[a.getMonth()];
  16.         var date = a.getDate();
  17.         var hour = a.getHours();
  18.         var min = a.getMinutes();
  19.         var sec = a.getSeconds();
  20.         var time = date + ' ' + (month<10?'0':'') + month + ' ' + year + ' ' + hour + ':' + (min<10?'0':'') + min + ':' (sec<10?'0':'') + sec ;
  21.         return time;
  22. }
  23.  
  24. var listeners = {
  25.         'registered': [
  26.                 function handler(message){
  27.                         bot.send("OPER", config.operLogin, config.operPassword);
  28.                         bot.send("MODE", bot.nick, "+B");
  29.                         bot.send("JOIN", "0");
  30.                         bot.join(config.channel, function(message){});
  31.                 }
  32.         ],
  33.         'ctcp-version': [
  34.                 function handler(from, to, message){
  35.                         bot.ctcp(from, "notice", "VERSION PIRCbot/nodejs/irc (k4be) v0.02");
  36.                 }
  37.         ],
  38.         'error': [
  39.                 function handler(message){
  40.                         console.log('error: ', message);
  41.                 }
  42.         ],
  43.         'message': [
  44.                 function handler(nick, to, text, message){
  45.                         if(to != config.channel) return; // only channel messages
  46.                         if(config.hopmNicks.indexOf(nick) !== -1){
  47.                                 var match = hopmPattern.exec(text);
  48.                                 if(match){
  49.                                         switch(match[4]){
  50.                                                 case 'HTTP': case 'HTTPPOST': var type = 9; break;
  51.                                                 case 'SOCKS4': case 'SOCKS5': var type = 8; break;
  52.                                                 default: var type = 6; bot.say(config.channel, "DRONEBL: type "+match[4]+" is unknown to me! Reported as 6"); break;
  53.                                         }
  54.                                         dronebl_report(match[2], type, false, match[3]);
  55.                                         //bot.say(config.channel, "DRONEBL: reported ip "+match[2]);
  56.                                 }
  57.                         } else if(config.operNicks.indexOf(nick) !== -1 && text.indexOf(bot.nick) == 0){
  58.                                 var commandstr = text.substr(text.indexOf(' ')+1);
  59.                                 if(!commandstr) return;
  60.                                 command = commandstr.split(' ');
  61.                                 switch(command[0]){
  62.                                         default: case "help": bot.notice(nick, "Available commands: 'help', 'listtypes', 'report [ip] [type] [comment] [port]' (comment or port=false for none), 'check [ip]', 'remove [id]' (id obtained with check)"); break;
  63.                                         case "listtypes":
  64.                                                 if(!dronebl.types){
  65.                                                         bot.notice(nick, "Type data not available");
  66.                                                         break;
  67.                                                 }
  68.                                                 for(var i=0; i < dronebl.types.length; i++){
  69.                                                         bot.notice(nick, "Type "+dronebl.types[i].type+": "+dronebl.types[i].name);
  70.                                                 }
  71.                                                 break;
  72.                                         case "check":
  73.                                                 if(command.length != 2){
  74.                                                         bot.notice(nick, "Usage: check [ip]");
  75.                                                         break;
  76.                                                 }
  77.                                                 dronebl.check(command[1]);
  78.                                                 break;
  79.                                         case "report":
  80.                                                 if(command.length < 3 || command.length > 5){
  81.                                                         bot.notice(nick, "Usage: report [ip] [type] [comment] [port]' (comment or port=false for none)");
  82.                                                         break;
  83.                                                 }
  84.                                                 switch(command.length){
  85.                                                         case 3: dronebl.report(command[1], command[2]); break;
  86.                                                         case 4: dronebl.report(command[1], command[2], command[3]); break;
  87.                                                         case 5: dronebl.report(command[1], command[2], command[3], command[4]); break;
  88.                                                 }
  89.                                                 break;
  90.                                         case "remove":
  91.                                                 if(command.length != 2){
  92.                                                         bot.notice(nick, "Usage: remove [id]");
  93.                                                         break;
  94.                                                 }
  95.                                                 dronebl.remove(command[1]);
  96.                                                 break;
  97.                                 }
  98.                         }
  99.                 }
  100.         ]
  101. };
  102.  
  103. var dronebl = {
  104.         'types': false,
  105.         'getTypes': function(){
  106.                 var xml = '<typelist />';
  107.                 dronebl.access(xml, function(err, result){
  108.                         if(result.response.typelist === undefined){
  109.                                 console.log('WARNING: empty typelist!'); // why would they send an empty list?
  110.                         } else {
  111.                                 dronebl.types = [];
  112.                                 for(var i=0; i<result.response.typelist.length; i++){
  113.                                         dronebl.types.push({ "name": result.response.typelist[i].$.description, "type": result.response.typelist[i].$.type });
  114.                                 }
  115.                         }
  116.                 });
  117.         },
  118.         'getTypeName': function(type){
  119.                 if(dronebl.types == false){
  120.                         dronebl.getTypes(); // shouldn't happen
  121.                         return type;
  122.                 }
  123.                 for(var i=0; i<dronebl.types.length; i++){
  124.                         if(dronebl.types[i].type == type) return dronebl.types[i].name;
  125.                 }
  126.                 return type;
  127.         },
  128.         'report': function(ip, type, comment=false, port=false){
  129.                 var post_data = '<add ip="'+ip+'" type="'+type+'"';
  130.                 if(comment){
  131.                         post_data += ' comment="'+comment+'"';
  132.                 }
  133.                 if(port){
  134.                         post_data += ' port="'+port+'"';
  135.                 }
  136.                 post_data += ' />';
  137.                 dronebl.access(post_data, function(err, result){
  138.                         try {
  139.                                 if(err) throw err;
  140.                                 if(result.response.warning !== undefined){
  141.                                         for(var i=0; i<result.response.warning.length; i++){
  142.                                                 bot.say(config.channel, 'IP ' + result.response.warning[i].$.ip + ': WARNING: ' + result.response.warning[i].$.data);
  143.                                         }
  144.                                 }
  145.                                 if(result.response.success !== undefined){
  146.                                         for(var i=0; i<result.response.success.length; i++){
  147.                                                 bot.say(config.channel, 'IP ' + result.response.success[i].$.ip + ': SUCCESS: ' + result.response.success[i].$.data);
  148.                                         }
  149.                                 }
  150.                         } catch(e){
  151.                                 console.log("Report exception: ", e);
  152.                         }
  153.                 });
  154.         },
  155.         'remove': function(id){
  156.                 var post_data = '<remove id="' + id + '" />';
  157.                 dronebl.access(post_data, function(err, result){
  158.                         try {
  159.                                 if(err) throw err;
  160.                                 if(result.response.warning !== undefined){
  161.                                         for(var i=0; i<result.response.warning.length; i++){
  162.                                                 bot.say(config.channel, 'id ' + result.response.warning[i].$.id + ': WARNING: ' + result.response.warning[i].$.data);
  163.                                         }
  164.                                 }
  165.                                 if(result.response.success !== undefined){
  166.                                         for(var i=0; i<result.response.success.length; i++){
  167.                                                 bot.say(config.channel, 'id ' + result.response.success[i].$.id + ': SUCCESS: ' + result.response.success[i].$.data);
  168.                                         }
  169.                                 }
  170.                         } catch(e){
  171.                                 console.log("Remove exception: ", e);
  172.                         }
  173.                 });
  174.         },
  175.         'check': function(ip){
  176.                 var post_data = '<lookup ip="' + ip + '" />';
  177.                 dronebl.access(post_data, function(err, result){
  178.                         try {
  179.                                 if(err) throw err;
  180.                                 if(result.response.result === undefined){
  181.                                         bot.say(config.channel, "No entries");
  182.                                 } else {
  183.                                         for(var i=0; i<result.response.result.length; i++){
  184.                                                 var date = timeConverter(result.response.result[i].$.timestamp);
  185.                                                 bot.say(config.channel, result.response.result[i].$.ip + ': LOOKUP: type="' + dronebl.getTypeName(result.response.result[i].$.type) + '", listing ' + (result.response.result[i].$.listed==0?'not ':'') + 'active, added on ' + date + ', id=' + result.response.result[i].$.id + ', comments: "' + result.response.result[i].$.comment+'"');
  186.                                         }
  187.                                 }
  188.                         } catch(e){
  189.                                 console.log("Check exception: ", e);
  190.                         }
  191.                 });
  192.         },
  193.         'access': function(xml, callback=false){
  194.                 if(callback == false){
  195.                         callback = function(){};
  196.                 }
  197.                 var post_data = '<?xml version="1.0"?><request key="'+config.dronebl.rpckey+'"';
  198.                 if(config.dronebl.staging){
  199.                         post_data += ' staging="1"';
  200.                 }
  201.                 post_data += '>';
  202.                
  203.                 post_data += xml; // just assume that the caller did not create malformed xml
  204.                
  205.                 post_data += '</request>';
  206.                 var options = {
  207.                         host: config.dronebl.host,
  208.                         port: 443,
  209.                         path: config.dronebl.path,
  210.                         method: 'POST',
  211.                         headers: {
  212.                                 'Content-Type': 'text/xml'
  213.                         }
  214.                 };
  215.                 var request = https.request(options, function(res){
  216.                         res.on('data', function(chunk){
  217.                                 var text = chunk.toString('utf8');
  218.                                 xml2js.parseString(text, callback);
  219.                         });
  220.                 });
  221.                 console.log('post_data: ', post_data);
  222.                 request.write(post_data);
  223.                 request.end();
  224.         }
  225. };
  226.  
  227. dronebl.getTypes();
  228. var bot = new irc.Client(config.server, config.nick, config.options);
  229.  
  230. for(var key in listeners){
  231.         var functions = listeners[key];
  232.         for(var i=0; i<functions.length; i++){
  233.                 bot.addListener(key, functions[i]);
  234.         }
  235. }
  236.  
  237. /*
  238. message = {
  239.     prefix: "The prefix for the message (optional)",
  240.     nick: "The nickname portion of the prefix (optional)",
  241.     user: "The username portion of the prefix (optional)",
  242.     host: "The hostname portion of the prefix (optional)",
  243.     server: "The servername (if the prefix was a servername)",
  244.     rawCommand: "The command exactly as sent from the server",
  245.     command: "Human readable version of the command",
  246.     commandType: "normal, error, or reply",
  247.     args: ['arguments', 'to', 'the', 'command'],
  248. }
  249. */

odpowiedź "Bez tytułu"

Tutaj możesz odpowiedzieć na wklejkę z góry

captcha