better wild cards
This commit is contained in:
@@ -16,4 +16,77 @@ const Host = require('../utils/redis_model')({
|
||||
}
|
||||
});
|
||||
|
||||
Host.lookupDict = {};
|
||||
|
||||
Host.make_lookup = async function(){
|
||||
try{
|
||||
for(let host of await this.list()){
|
||||
let fragments = host.split('.');
|
||||
let place = this.lookupDict;
|
||||
|
||||
while(fragments.length !==0){
|
||||
let current = fragments.pop();
|
||||
if(!place[current]){
|
||||
place[current] = {};
|
||||
}
|
||||
|
||||
if(fragments.length === 0){
|
||||
place[current]['#record'] = await this.get(host)
|
||||
}
|
||||
place = place[current];
|
||||
}
|
||||
}
|
||||
|
||||
}catch(error){
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
Host.lookUp = async function(host){
|
||||
let place = this.lookupDict;
|
||||
let last_resort = {};
|
||||
|
||||
for(let fragment of host.split('.').slice().reverse()){
|
||||
// console.log('fragment', fragment, last_resort)
|
||||
|
||||
if(place['**']) last_resort = place['**'];
|
||||
|
||||
if({...place, ...last_resort}[fragment]){
|
||||
place = {...place, ...last_resort}[fragment];
|
||||
}else if(place['*']){
|
||||
place = place['*']
|
||||
}else if(last_resort){
|
||||
place = last_resort;
|
||||
}
|
||||
}
|
||||
|
||||
if(place && place['#record']) return place['#record'];
|
||||
|
||||
}
|
||||
|
||||
module.exports = {Host};
|
||||
|
||||
(async function(){
|
||||
await Host.make_lookup();
|
||||
// console.log(Host.lookupDict)
|
||||
|
||||
// console.log(Host.lookupDict['com']['vm42'])
|
||||
|
||||
// console.log('test-res', await Host.lookUp('payments.test.com'))
|
||||
|
||||
let count = 5
|
||||
console.log(count++, (await Host.lookUp('payments.test.com')).host === 'payments.**')
|
||||
console.log(count++, (await Host.lookUp('test.sample.other.exmaple.com')).host === '**.exmaple.com')
|
||||
console.log(count++, (await Host.lookUp('stan.test.vm42.com')).host === 'stan.test.vm42.com')
|
||||
console.log(count++, (await Host.lookUp('test.vm42.com')).host === 'test.vm42.com')
|
||||
console.log(count++, (await Host.lookUp('blah.test.vm42.com')).host === '*.test.vm42.com')
|
||||
console.log(count++, (await Host.lookUp('payments.example.com')).host === 'payments.**')
|
||||
console.log(count++, (await Host.lookUp('info.wma.users.718it.biz')).host === 'info.*.users.718it.biz')
|
||||
console.log(count++, (await Host.lookUp('info.users.718it.biz')) === undefined)
|
||||
console.log(count++, (await Host.lookUp('test.1.2.718it.net')).host === 'test.*.*.718it.net')
|
||||
console.log(count++, (await Host.lookUp('test1.exmaple.com')).host === 'test1.exmaple.com')
|
||||
console.log(count++, (await Host.lookUp('other.exmaple.com')).host === '*.exmaple.com')
|
||||
|
||||
|
||||
return ;
|
||||
})()
|
||||
|
||||
Reference in New Issue
Block a user