bench程序增加对sip、eip合法性检测

This commit is contained in:
Wu Jian Ping 2022-07-22 14:05:04 +08:00
parent 573c259c76
commit deb995feb0
2 changed files with 19 additions and 2 deletions

View File

@ -14,7 +14,7 @@ const VectorIndexLength = 256 * 256 * (4 + 4)
// 二分索引项的字节数 // 二分索引项的字节数
const SegmentIndexSize = 14 const SegmentIndexSize = 14
// IPv4检查正则 // IPv4检查正则
const IP_REGEX = /((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))/ const IP_REGEX = /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/
const getStartEndPtr = Symbol('#getStartEndPtr') const getStartEndPtr = Symbol('#getStartEndPtr')
const getBuffer = Symbol('#getBuffer') const getBuffer = Symbol('#getBuffer')
@ -86,7 +86,7 @@ class Searcher {
ioCount: 0 ioCount: 0
} }
if (!IP_REGEX.test(ip)) { if (!isValidIp(ip)) {
throw new Error(`IP: ${ip} is invalid`) throw new Error(`IP: ${ip} is invalid`)
} }
@ -194,6 +194,10 @@ const _checkFile = dbPath => {
} }
} }
const isValidIp = ip => {
return IP_REGEX.test(ip)
}
const newWithFileOnly = dbPath => { const newWithFileOnly = dbPath => {
_checkFile(dbPath) _checkFile(dbPath)
@ -238,6 +242,7 @@ const loadContentFromFile = dbPath => {
} }
module.exports = { module.exports = {
isValidIp,
loadVectorIndexFromFile, loadVectorIndexFromFile,
loadContentFromFile, loadContentFromFile,
newWithFileOnly, newWithFileOnly,

View File

@ -86,9 +86,21 @@ const main = async () => {
const list = line.split('|') const list = line.split('|')
const sip = list[0] const sip = list[0]
const eip = list[1] const eip = list[1]
if (!Searcher.isValidIp(sip)) {
throw new Error(`IP: ${sip} is invalid`)
}
if (!Searcher.isValidIp(eip)) {
throw new Error(`IP: ${eip} is invalid`)
}
const sipInt = ipToInt(sip) const sipInt = ipToInt(sip)
const eipInt = ipToInt(eip) const eipInt = ipToInt(eip)
if (sipInt > eipInt) {
throw new Error(`start ip(${sip}) should not be greater than end ip(${eip})`)
}
const mipInt = Math.floor((sipInt + eipInt) / 2) const mipInt = Math.floor((sipInt + eipInt) / 2)
const mip = intToIp(mipInt) const mip = intToIp(mipInt)