-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
779 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,8 @@ lib-cov | |
*.out | ||
*.pid | ||
*.gz | ||
|
||
*.tgz | ||
data/*.tmp | ||
pids | ||
logs | ||
results | ||
|
||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require('stream.pipeline-shim/auto'); | ||
const zlib = require('zlib'); | ||
const util = require('util'); | ||
const fs = require('fs'); | ||
const stream = require('stream'); | ||
const axios = require('axios'); | ||
const GBK = require('gbk.js'); | ||
const pipeline = util.promisify(stream.pipeline); | ||
const ProgressBar = require('progress'); | ||
|
||
const urls = { | ||
copywrite: 'http://update.cz88.net/ip/copywrite.rar', | ||
qqwry: 'http://update.cz88.net/ip/qqwry.rar' | ||
}; | ||
|
||
class QqwryDecode extends stream.Transform { | ||
constructor(key, options) { | ||
super(options); | ||
this._key = key; | ||
this._writeN = 0x200; | ||
} | ||
_transform(chunk, encoding, callback) { | ||
if (this._writeN <= 0) return callback(null, chunk); | ||
let max = this._writeN > chunk.length ? chunk.length : this._writeN; | ||
let key = this._key; | ||
this._writeN -= max; | ||
for (let i = 0; i < max; i++) { | ||
key *= 0x805; | ||
key++; | ||
key &= 0xff; | ||
chunk[i] = chunk[i] ^ key; | ||
} | ||
this._key = key; | ||
return callback(null, chunk); | ||
} | ||
_flush(cb) { | ||
cb(); | ||
} | ||
} | ||
|
||
// async function getURLFile(url, resType = 'arraybuffer') { | ||
async function getURLFile(url, showProgressBar = false) { | ||
return axios.get(url, { responseType: 'stream' }).then(res => { | ||
if(showProgressBar){ | ||
let bar = new ProgressBar('downloading [:bar]:percent :rate/bps :etas', { | ||
complete: '=', | ||
incomplete: ' ', | ||
width: 20, | ||
total: +res.headers['content-length'] | ||
}); | ||
res.data.on('data',function(buffer){ | ||
bar.tick(buffer.length); | ||
}) | ||
} | ||
return res.data | ||
}); | ||
} | ||
|
||
let get_copywrite = showBar => getURLFile(urls.copywrite, showBar); | ||
let get_qqwry = showBar => getURLFile(urls.qqwry, showBar); | ||
|
||
async function getLastInfo() { | ||
let copywrite = await get_copywrite(); | ||
copywrite.read(20); | ||
let key = copywrite.read(4).readUIntLE(0, 4); | ||
let version = GBK.decode(copywrite.read(128)).replace(/\0/g, ''); | ||
return { | ||
version, | ||
key | ||
}; | ||
} | ||
|
||
async function update(dataPath, key) { | ||
if (!key) { | ||
key = (await getLastInfo()).key; | ||
} | ||
let _temPath = dataPath + ".tmp" | ||
return pipeline( | ||
await get_qqwry(true), | ||
new QqwryDecode(key), | ||
zlib.createInflate(), | ||
fs.createWriteStream(_temPath) | ||
).then(()=>{ | ||
fs.renameSync(_temPath,dataPath); | ||
}); | ||
} | ||
|
||
exports.getLastInfo = getLastInfo; | ||
exports.update = update; | ||
exports.QqwryDecode = QqwryDecode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env node | ||
|
||
const program = require('commander'); | ||
const chalk = require('chalk'); | ||
const qqwry = require('../'); | ||
const child_process = require('child_process') | ||
const path = require('path') | ||
|
||
program | ||
.usage('<command> [options]') | ||
.option('-v, --version', '当前版本信息') | ||
.option('--remote', '获取最新IP库信息') | ||
.command('search', '默认指令 搜索IP/IP段', { isDefault: true }) | ||
.command('find', '查找区域信息') | ||
.command('update', '更新IP库(qqwry.dat)') | ||
// .command('remote', '获取最新IP库信息').alias('last'); | ||
|
||
//版本 | ||
program.on('option:version', function() { | ||
try { | ||
let v_lib = require('../package').version; | ||
let v_data = qqwry().searchIP('255.255.255.255').Area; | ||
console.log(`v${v_lib}`, `(Local data: ${chalk.bold(v_data)})`); | ||
} catch (err) { | ||
console.error(err.message || err); | ||
} | ||
process.exit(0); | ||
}); | ||
program.on('option:remote',function(){ | ||
try{ | ||
var log = child_process.execFileSync(process.argv[0], [path.join(__dirname , 'qqwry-remote')]); | ||
process.stdout.write(log); | ||
}catch(err){ | ||
console.error(err.message || err); | ||
} | ||
process.exit(0); | ||
}) | ||
|
||
if (process.argv.length < 3) { | ||
// program.help(); | ||
} | ||
|
||
program.parse(process.argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/usr/bin/env node | ||
|
||
const program = require('commander'); | ||
const toRegex = require('to-regex'); | ||
const qqwry = require('..'); | ||
const getFomatFn = require('../lib/format') | ||
|
||
program | ||
.name('find') | ||
.usage('<keyword> [keywords...]') | ||
.description('搜索区域信息') | ||
.option('-c, --count', '只是统计记录数') | ||
// .option('-f, --format <value>', '让IP段模式输出的特定格式 支持 "json" or "csv"', /^(json|csv)$/) | ||
.option('-i, --ignore-case', '不区分大小写模式') | ||
.option('-E, --extended-regexp', '启用正则表达式查询') | ||
.parse(process.argv); | ||
|
||
if (program.args.length < 1) { | ||
program.help(); | ||
} else if (program.args.length) { | ||
try { | ||
let lib = qqwry(); | ||
let { ignoreCase, count, format, extendedRegexp } = program.opts(); | ||
let keyword = program.args; | ||
let stdout = process.stdout; | ||
lib.speed(); | ||
let sum = 0; | ||
let formatFn = getFomatFn('text'); | ||
let isMatch = (function(){ | ||
let reg; | ||
if(extendedRegexp){ | ||
reg = toRegex(keyword,{contains: true, nocase: ignoreCase}); | ||
}else{ | ||
// reg = mm.makeRe(keyword[0],{matchBase: false,nocase: ignoreCase}) | ||
// reg = new RegExp(keyword[0], ignoreCase ? 'i' : ''); | ||
let _isMatch; | ||
if(ignoreCase){ | ||
keyword = keyword.map(v=>v.toLocaleLowerCase()); | ||
_isMatch = function(str,key){ | ||
return ~str.toLocaleLowerCase().indexOf(key); | ||
} | ||
}else{ | ||
_isMatch = function(str,key){ | ||
return str.indexOf(key) != -1; | ||
} | ||
} | ||
return (str)=>{ | ||
for(let key of keyword){ | ||
if(_isMatch(str,key)) return true; | ||
} | ||
} | ||
} | ||
return (str)=>{ | ||
return reg.test(str); | ||
} | ||
}()); | ||
// mm.isMatch | ||
|
||
lib.searchIPScopeStream(0, '255.255.255.255', { format: 'object' }) | ||
.on('data', function(obj) { | ||
// let reg = new RegExp(extendedRegexp?keyword:keyword.repeat(/[\.\{\*]/g,), ignoreCase ? 'i' : ''); | ||
// for(let key of keys){ | ||
// if(~obj[4].indexOf(key) || ~obj[5].indexOf(key)){ | ||
// if (isMatch(obj.slice(4).join(' '))) { | ||
if (isMatch(obj[4]) || isMatch(obj[5])) { | ||
sum++; | ||
// count || stdout.write(obj.slice(2, 4).join(' - ') + ' ' + obj.slice(4).join(' ') + '\n'); | ||
if(!count){ | ||
|
||
}stdout.write(formatFn(obj)); | ||
return; | ||
} | ||
// } | ||
// stdout.write('') | ||
}) | ||
.on('end', function() { | ||
count && console.log(sum); | ||
}); | ||
} catch (err) { | ||
console.error(err.message || err); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env node | ||
|
||
const program = require('commander'); | ||
const qqwryUpdate = require('./lib/qqwryUpdate'); | ||
|
||
async function main(){ | ||
let {version} = await qqwryUpdate.getLastInfo(); | ||
let info = /\d+年\d+月\d+日/.exec(version); | ||
if(info){ | ||
console.log('Remote data:',info[0]+'数据'); | ||
} | ||
}; | ||
|
||
program | ||
.name('remote') | ||
.description('获取服务端 IP库信息') | ||
.parse(process.argv); | ||
|
||
main().catch(console.error); |
Oops, something went wrong.