qqwry-find 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env node
  2. const program = require('commander');
  3. const toRegex = require('to-regex');
  4. const qqwry = require('..');
  5. const getFomatFn = require('../lib/format');
  6. program
  7. .name('find')
  8. .usage('<keyword> [keywords...]')
  9. .description('通过关键字反查IP段')
  10. .option('-c, --count', '只是统计记录数')
  11. .option('--have <value>', '过滤关键字')
  12. // .option('-f, --format <value>', '让IP段模式输出的特定格式 支持 "json" or "csv"', /^(json|csv)$/)
  13. .option('-i, --ignore-case', '不区分大小写模式')
  14. .option('-E, --extended-regexp', '启用正则表达式查询')
  15. .parse(process.argv);
  16. if (program.args.length < 1) {
  17. program.help();
  18. } else if (program.args.length) {
  19. try {
  20. let lib = qqwry();
  21. let { ignoreCase, count, filter, extendedRegexp } = program.opts();
  22. let keyword = program.args;
  23. let stdout = process.stdout;
  24. lib.speed();
  25. let sum = 0;
  26. let formatFn = getFomatFn('text');
  27. let isMatch = (function() {
  28. let reg;
  29. if (extendedRegexp) {
  30. reg = toRegex(keyword, { contains: true, nocase: ignoreCase });
  31. } else {
  32. // reg = mm.makeRe(keyword[0],{matchBase: false,nocase: ignoreCase})
  33. // reg = new RegExp(keyword[0], ignoreCase ? 'i' : '');
  34. let _isMatch;
  35. if (ignoreCase) {
  36. keyword = keyword.map(v => v.toLocaleLowerCase());
  37. _isMatch = function(str, key) {
  38. return ~str.toLocaleLowerCase().indexOf(key);
  39. };
  40. } else {
  41. _isMatch = function(str, key) {
  42. return str.indexOf(key) != -1;
  43. };
  44. }
  45. return str => {
  46. for (let key of keyword) {
  47. if (_isMatch(str, key)) return true;
  48. }
  49. };
  50. }
  51. return str => {
  52. return reg.test(str);
  53. };
  54. })();
  55. let Filter = (function() {
  56. if (filter) {
  57. if (ignoreCase) {
  58. filter = filter.toLocaleLowerCase();
  59. return function(str) {
  60. return ~str.toLocaleLowerCase().indexOf(filter);
  61. };
  62. } else {
  63. return function(str) {
  64. return str.indexOf(filter) != -1;
  65. };
  66. }
  67. }
  68. })();
  69. lib.searchIPScopeStream(0, 0xffffffff, { format: 'object' })
  70. .on('data', function(obj) {
  71. if (isMatch(obj[4]) || (isMatch(obj[5]) && (!Filter || (Filter(obj[4]) || Filter(obj[5]))))) {
  72. sum++;
  73. if (!count) {
  74. stdout.write(formatFn(obj));
  75. }
  76. return;
  77. }
  78. })
  79. .on('end', function() {
  80. count && console.log(sum);
  81. });
  82. } catch (err) {
  83. console.error(err.message || err);
  84. }
  85. }