fixed prefix issue
This commit is contained in:
+4
-1
@@ -10,5 +10,8 @@ module.exports = {
|
||||
userFilter: '(objectClass=inetOrgPerson)',
|
||||
userNameAttribute: 'uid'
|
||||
},
|
||||
socketFile: '/var/run/proxy_lookup.socket'
|
||||
socketFile: '/var/run/proxy_lookup.socket',
|
||||
redis: {
|
||||
prefix: 'proxy_'
|
||||
}
|
||||
};
|
||||
|
||||
+188
-181
@@ -1,11 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
const RedisModel = require('../utils/redis_model');
|
||||
const Table = require('../utils/redis_model');
|
||||
|
||||
const Host = RedisModel({
|
||||
_name: 'host',
|
||||
_key: 'host',
|
||||
_keyMap: {
|
||||
|
||||
class Host extends Table{
|
||||
static _key = 'host';
|
||||
static _keyMap = {
|
||||
'created_by': {isRequired: true, type: 'string', min: 3, max: 500},
|
||||
'created_on': {default: function(){return (new Date).getTime()}},
|
||||
'updated_by': {default:"__NONE__", isRequired: false, type: 'string',},
|
||||
@@ -17,196 +17,200 @@ const Host = RedisModel({
|
||||
'targetssl': {isRequired: false, default: false, type: 'boolean'},
|
||||
'created_by': {isRequired: true, type: 'string', min: 3, max: 500},
|
||||
'is_cache': {default: false, isRequired: false, type: 'boolean',},
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
const Cached = RedisModel({
|
||||
_name: 'cached',
|
||||
_key: 'host',
|
||||
_keyMap: {
|
||||
static lookUpObj = {};
|
||||
static __lookUpIsReady = false;
|
||||
|
||||
|
||||
|
||||
async addCache(host, parentOBJ){
|
||||
try{
|
||||
await this.add({...parentOBJ, host, is_cache: true}, true)
|
||||
await Cached.add({
|
||||
host: host,
|
||||
parent: parentOBJ.host
|
||||
});
|
||||
}catch(error){
|
||||
console.error('add cahce error', {...parentOBJ, host, is_cache: true}, error)
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async bustCache(parent){
|
||||
try{
|
||||
let cached = await Cached.listDetail();
|
||||
for(let cache of cached){
|
||||
if(cache.parent == parent){
|
||||
let host = await Host.get(cache.host);
|
||||
await this.remove.apply(host);
|
||||
await cache.remove();
|
||||
}
|
||||
}
|
||||
|
||||
}catch(error){
|
||||
console.error('bust cache error', error)
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async add(...args){
|
||||
try{
|
||||
let out = await super.add(...args)
|
||||
await this.buildLookUpObj()
|
||||
|
||||
return out;
|
||||
} catch(error){
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async update(...args){
|
||||
try{
|
||||
let out = await super.update(...args)
|
||||
await this.bustCache(this.host)
|
||||
await Host.buildLookUpObj()
|
||||
|
||||
return out;
|
||||
} catch(error){
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async remove(...args){
|
||||
try{
|
||||
let out = await super.remove(...args)
|
||||
await Host.buildLookUpObj()
|
||||
await this.bustCache(this.host)
|
||||
|
||||
return out;
|
||||
} catch(error){
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async buildLookUpObj(){
|
||||
/*
|
||||
Build a look up tree for domain records in the redis back end to allow
|
||||
complex looks with wildcards.
|
||||
*/
|
||||
|
||||
// Hold lookUp ready while the look up object is being built.
|
||||
this.__lookUpIsReady = false;
|
||||
this.lookUpObj = {};
|
||||
|
||||
try{
|
||||
|
||||
// Loop over all the hosts in the redis.
|
||||
for(let host of await this.list()){
|
||||
|
||||
// Spit the hosts on "." into its fragments .
|
||||
let fragments = host.split('.');
|
||||
|
||||
// Hold a pointer to the root of the lookup tree.
|
||||
let pointer = this.lookUpObj;
|
||||
|
||||
// Walk over each fragment, popping from right to left.
|
||||
while(fragments.length){
|
||||
let fragment = fragments.pop();
|
||||
|
||||
// Add a branch to the lookup at the current position
|
||||
if(!pointer[fragment]){
|
||||
pointer[fragment] = {};
|
||||
}
|
||||
|
||||
// Add the record(leaf) when we hit the a full host name.
|
||||
// #record denotes a leaf node on this tree.
|
||||
if(fragments.length === 0){
|
||||
pointer[fragment]['#record'] = await this.get(host)
|
||||
}
|
||||
|
||||
// Advance the pointer to the next level of the tree.
|
||||
pointer = pointer[fragment];
|
||||
}
|
||||
}
|
||||
|
||||
// When the look up tree is finished, remove the ready hold.
|
||||
this.__lookUpIsReady = true;
|
||||
|
||||
}catch(error){
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
static lookUp(host){
|
||||
/*
|
||||
Perform a complex lookup of @host on the look up tree.
|
||||
*/
|
||||
|
||||
|
||||
// Hold a pointer to the root of the look up tree
|
||||
let place = this.lookUpObj;
|
||||
|
||||
// Hold the last passed long wild card.
|
||||
let last_resort = {};
|
||||
|
||||
// Walk over each fragment of the host, from right to left
|
||||
for(let fragment of host.split('.').reverse()){
|
||||
|
||||
// If a long wild card is found on this level, hold on to it
|
||||
if(place['**']) last_resort = place['**'];
|
||||
|
||||
// If we have a match for the current fragment, update the current pointer
|
||||
// A match in the lookup tree takes priority being a more exact match.
|
||||
if({...last_resort, ...place}[fragment]){
|
||||
place = {...last_resort, ...place}[fragment];
|
||||
// If we have a not exact fragment match, a wild card will do.
|
||||
}else if(place['*']){
|
||||
place = place['*']
|
||||
// If no fragment can be matched, continue with the long wild card branch.
|
||||
}else if(last_resort){
|
||||
place = last_resort;
|
||||
}
|
||||
}
|
||||
|
||||
// After the tree has been traversed, see if we have leaf node to return.
|
||||
if(place && place['#record']) return place['#record'];
|
||||
}
|
||||
|
||||
static async lookUpReady(){
|
||||
/*
|
||||
Wait for the lookup tree to be built.
|
||||
*/
|
||||
|
||||
// Check every 5ms to see if the look up tree is ready
|
||||
while(!this.__lookUpIsReady) await new Promise(r => setTimeout(r, 5));
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Cached extends Table{
|
||||
static _key = 'host';
|
||||
static _keyMap = {
|
||||
'host': {isRequired: true, type: 'string', min: 3, max: 500},
|
||||
'parent': {isRequired: true, type: 'string', min: 3, max: 500},
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Host.addCache = async function(host, parentOBJ){
|
||||
try{
|
||||
await Host.__proto__.add.apply(this, [{...parentOBJ, host, is_cache: true}, true])
|
||||
await Cached.add({
|
||||
host: host,
|
||||
parent: parentOBJ.host
|
||||
});
|
||||
}catch(error){
|
||||
console.error('add cahce error', {...parentOBJ, host, is_cache: true}, error)
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Host.bustCache = async function(parent){
|
||||
try{
|
||||
let cached = await Cached.listDetail();
|
||||
for(let cache of cached){
|
||||
if(cache.parent == parent){
|
||||
let host = await Host.get(cache.host);
|
||||
await Host.__proto__.remove.apply(host);
|
||||
await cache.remove();
|
||||
}
|
||||
}
|
||||
|
||||
}catch(error){
|
||||
console.error('bust cache error', error)
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Host.add = async function(){
|
||||
try{
|
||||
let out = await Host.__proto__.add.apply(this, arguments)
|
||||
await Host.buildLookUpObj()
|
||||
|
||||
return out;
|
||||
} catch(error){
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Host.update = async function(data, key){
|
||||
try{
|
||||
let out = await Host.__proto__.update.apply(this, arguments)
|
||||
await Host.bustCache(this.host)
|
||||
await Host.buildLookUpObj()
|
||||
|
||||
return out;
|
||||
} catch(error){
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Host.remove = async function(){
|
||||
try{
|
||||
let out = await Host.__proto__.remove.apply(this, arguments)
|
||||
await Host.buildLookUpObj()
|
||||
await Host.bustCache(this.host)
|
||||
|
||||
return out;
|
||||
} catch(error){
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Host.lookUpObj = {};
|
||||
|
||||
Host.buildLookUpObj = async function(){
|
||||
/*
|
||||
Build a look up tree for domain records in the redis back end to allow
|
||||
complex looks with wildcards.
|
||||
*/
|
||||
|
||||
// Hold lookUp ready while the look up object is being built.
|
||||
this.__lookUpIsReady = false;
|
||||
this.lookUpObj = {};
|
||||
|
||||
try{
|
||||
|
||||
// Loop over all the hosts in the redis.
|
||||
for(let host of await this.list()){
|
||||
|
||||
// Spit the hosts on "." into its fragments .
|
||||
let fragments = host.split('.');
|
||||
|
||||
// Hold a pointer to the root of the lookup tree.
|
||||
let pointer = this.lookUpObj;
|
||||
|
||||
// Walk over each fragment, popping from right to left.
|
||||
while(fragments.length){
|
||||
let fragment = fragments.pop();
|
||||
|
||||
// Add a branch to the lookup at the current position
|
||||
if(!pointer[fragment]){
|
||||
pointer[fragment] = {};
|
||||
}
|
||||
|
||||
// Add the record(leaf) when we hit the a full host name.
|
||||
// #record denotes a leaf node on this tree.
|
||||
if(fragments.length === 0){
|
||||
pointer[fragment]['#record'] = await this.get(host)
|
||||
}
|
||||
|
||||
// Advance the pointer to the next level of the tree.
|
||||
pointer = pointer[fragment];
|
||||
}
|
||||
}
|
||||
|
||||
// When the look up tree is finished, remove the ready hold.
|
||||
this.__lookUpIsReady = true;
|
||||
|
||||
}catch(error){
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
|
||||
Host.lookUp = function(host){
|
||||
/*
|
||||
Perform a complex lookup of @host on the look up tree.
|
||||
*/
|
||||
|
||||
|
||||
// Hold a pointer to the root of the look up tree
|
||||
let place = this.lookUpObj;
|
||||
|
||||
// Hold the last passed long wild card.
|
||||
let last_resort = {};
|
||||
|
||||
// Walk over each fragment of the host, from right to left
|
||||
for(let fragment of host.split('.').reverse()){
|
||||
|
||||
// If a long wild card is found on this level, hold on to it
|
||||
if(place['**']) last_resort = place['**'];
|
||||
|
||||
// If we have a match for the current fragment, update the current pointer
|
||||
// A match in the lookup tree takes priority being a more exact match.
|
||||
if({...last_resort, ...place}[fragment]){
|
||||
place = {...last_resort, ...place}[fragment];
|
||||
// If we have a not exact fragment match, a wild card will do.
|
||||
}else if(place['*']){
|
||||
place = place['*']
|
||||
// If no fragment can be matched, continue with the long wild card branch.
|
||||
}else if(last_resort){
|
||||
place = last_resort;
|
||||
}
|
||||
}
|
||||
|
||||
// After the tree has been traversed, see if we have leaf node to return.
|
||||
if(place && place['#record']) return place['#record'];
|
||||
};
|
||||
|
||||
Host.__lookUpIsReady = false;
|
||||
|
||||
Host.lookUpReady = async function(){
|
||||
/*
|
||||
Wait for the lookup tree to be built.
|
||||
*/
|
||||
|
||||
// Check every 5ms to see if the look up tree is ready
|
||||
while(!this.__lookUpIsReady) await new Promise(r => setTimeout(r, 5));
|
||||
return true;
|
||||
};
|
||||
|
||||
(async function(){
|
||||
|
||||
await Host.buildLookUpObj();
|
||||
})()
|
||||
|
||||
module.exports = {Host};
|
||||
|
||||
// (async function(){
|
||||
(async function(){
|
||||
try{
|
||||
|
||||
// await Host.lookUpReady();
|
||||
|
||||
// // console.log(Host.lookUpObj)
|
||||
// await Host.lookUpReady();
|
||||
|
||||
// console.log(Host.lookUpObj)
|
||||
|
||||
// console.log(await Host.listDetail())
|
||||
|
||||
// // console.log(Host.lookUpObj['com']['vm42'])
|
||||
|
||||
@@ -217,7 +221,7 @@ module.exports = {Host};
|
||||
// console.log(count++, Host.lookUp('sd.blah.test.vm42.com') === undefined)
|
||||
// console.log(count++, Host.lookUp('payments.test.com').host === 'payments.**')
|
||||
// console.log(count++, Host.lookUp('test.sample.other.exmaple.com').host === '**.exmaple.com')
|
||||
// // console.log(count++, Host.lookUp('stan.test.vm42.com').host === 'stan.test.vm42.com')
|
||||
// console.log(count++, Host.lookUp('stan.test.vm42.com').host === 'stan.test.vm42.com')
|
||||
// console.log(count++, Host.lookUp('test.vm42.com').host === 'test.vm42.com')
|
||||
// console.log(count++, Host.lookUp('blah.test.vm42.com').host === '*.test.vm42.com')
|
||||
// console.log(count++, Host.lookUp('payments.example.com').host === 'payments.**')
|
||||
@@ -231,4 +235,7 @@ module.exports = {Host};
|
||||
// console.log(count++, Host.lookUp('718it.biz').host === '718it.biz')
|
||||
|
||||
|
||||
// })()
|
||||
}catch(error){
|
||||
console.log('IIFE test area error:', error)
|
||||
}
|
||||
})()
|
||||
@@ -81,7 +81,11 @@ class User extends Table{
|
||||
static async login(data){
|
||||
try{
|
||||
|
||||
console.log('login data', data)
|
||||
|
||||
let user = await User.get(data);
|
||||
|
||||
console.log('login user', user)
|
||||
let auth = await bcrypt.compare(data.password, user.password);
|
||||
|
||||
if(auth){
|
||||
@@ -90,6 +94,7 @@ class User extends Table{
|
||||
throw this.errors.login();
|
||||
}
|
||||
}catch(error){
|
||||
console.error('!!!!!!!!!!', error)
|
||||
if (error == 'Authentication failure'){
|
||||
throw this.errors.login()
|
||||
}
|
||||
@@ -101,14 +106,12 @@ class User extends Table{
|
||||
}
|
||||
|
||||
module.exports = {User};
|
||||
module.exports = {User};
|
||||
|
||||
|
||||
(async function(){
|
||||
var defaultUser = 'proxyadmin2'
|
||||
var defaultUser = 'proxyadmin3'
|
||||
try{
|
||||
let user = await User.get(defaultUser);
|
||||
|
||||
}catch(error){
|
||||
try{
|
||||
let user = await User.add({
|
||||
|
||||
Generated
+826
-464
File diff suppressed because it is too large
Load Diff
+5
-5
@@ -12,13 +12,13 @@
|
||||
"start": "node ./bin/www"
|
||||
},
|
||||
"dependencies": {
|
||||
"bcrypt": "^5.0.0",
|
||||
"ejs": "^3.0.1",
|
||||
"bcrypt": "^5.1.1",
|
||||
"ejs": "^3.1.9",
|
||||
"express": "^4.18.2",
|
||||
"extend": "^3.0.2",
|
||||
"ldapts": "^2.2.1",
|
||||
"linux-sys-user": "^1.1.0",
|
||||
"redis": "^4.6.5"
|
||||
"ldapts": "^2.12.0",
|
||||
"linux-sys-user": "^1.1.8",
|
||||
"redis": "^4.6.7"
|
||||
},
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
const {createClient} = require('redis');
|
||||
const {promisify} = require('util');
|
||||
|
||||
const config = {
|
||||
prefix: 'proxy_'
|
||||
}
|
||||
|
||||
var _client = createClient(config);
|
||||
_client.connect()
|
||||
|
||||
|
||||
module.exports = {
|
||||
client: _client,
|
||||
// HGET: promisify(_client.HGET).bind(_client),
|
||||
// HDEL: promisify(_client.HDEL).bind(_client),
|
||||
// SADD: promisify(_client.SADD).bind(_client),
|
||||
// SREM: promisify(_client.SREM).bind(_client),
|
||||
// DEL: promisify(_client.DEL).bind(_client),
|
||||
// HSET: promisify(_client.HSET).bind(_client),
|
||||
// HGETALL: promisify(_client.HGETALL).bind(_client),
|
||||
// SMEMBERS: promisify(_client.SMEMBERS).bind(_client),
|
||||
// RENAME: promisify(_client.RENAME).bind(_client),
|
||||
};
|
||||
+44
-14
@@ -1,8 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
const client = require('../utils/redis');
|
||||
const {createClient} = require('redis');
|
||||
const objValidate = require('../utils/object_validate');
|
||||
const conf = require('../conf/conf');
|
||||
|
||||
const client = createClient({});
|
||||
client.connect()
|
||||
|
||||
function redisPrefix(key){
|
||||
return `${conf.redis.prefix}${key}`;
|
||||
}
|
||||
|
||||
class Table{
|
||||
constructor(data){
|
||||
@@ -15,12 +22,14 @@ class Table{
|
||||
try{
|
||||
|
||||
if(typeof index === 'object'){
|
||||
index = index[this._key]
|
||||
index = index[this._key];
|
||||
}
|
||||
|
||||
let result = await client.HGETALL(`${this.prototype.constructor.name}_${index}`);
|
||||
let result = await client.HGETALL(
|
||||
redisPrefix(`${this.prototype.constructor.name}_${index}`)
|
||||
);
|
||||
|
||||
if(!result){
|
||||
if(!Object.keys(result).length){
|
||||
let error = new Error('EntryNotFound');
|
||||
error.name = 'EntryNotFound';
|
||||
error.message = `${this.prototype.constructor.name}:${index} does not exists`;
|
||||
@@ -32,7 +41,7 @@ class Table{
|
||||
// back to native values.
|
||||
result = objValidate.parseFromString(this._keyMap, result);
|
||||
|
||||
return new this.prototype.constructor(result)
|
||||
return new this.prototype.constructor(result);
|
||||
|
||||
}catch(error){
|
||||
throw error;
|
||||
@@ -44,7 +53,7 @@ class Table{
|
||||
try{
|
||||
await this.get(data);
|
||||
|
||||
return true
|
||||
return true;
|
||||
}catch(error){
|
||||
return false;
|
||||
}
|
||||
@@ -53,7 +62,9 @@ class Table{
|
||||
static async list(){
|
||||
// return a list of all the index keys for this table.
|
||||
try{
|
||||
return await client.SMEMBERS(this.prototype.constructor.name);
|
||||
return await client.SMEMBERS(
|
||||
redisPrefix(this.prototype.constructor.name)
|
||||
);
|
||||
|
||||
}catch(error){
|
||||
throw error;
|
||||
@@ -68,7 +79,7 @@ class Table{
|
||||
out.push(await this.get(entry));
|
||||
}
|
||||
|
||||
return out
|
||||
return out;
|
||||
}
|
||||
|
||||
static async add(data){
|
||||
@@ -89,11 +100,18 @@ class Table{
|
||||
}
|
||||
|
||||
// Add the key to the members for this redis table
|
||||
await client.SADD(this.prototype.constructor.name, data[this._key]);
|
||||
await client.SADD(
|
||||
redisPrefix(this.prototype.constructor.name),
|
||||
data[this._key]
|
||||
);
|
||||
|
||||
// Add the values for this entry.
|
||||
for(let key of Object.keys(data)){
|
||||
await client.HSET(`${this.prototype.constructor.name}_${data[this._key]}`, key, objValidate.parseToString(data[key]));
|
||||
await client.HSET(
|
||||
redisPrefix(`${this.prototype.constructor.name}_${data[this._key]}`),
|
||||
key,
|
||||
objValidate.parseToString(data[key])
|
||||
);
|
||||
}
|
||||
|
||||
// return the created redis entry as entry instance.
|
||||
@@ -117,8 +135,12 @@ class Table{
|
||||
|
||||
// Create a new record for the updated entry. If that succeeds,
|
||||
// delete the old recored
|
||||
if(await this.add(newData)) await this.remove();
|
||||
let newObject = await this.constructor.add(newData);
|
||||
|
||||
if(newObject){
|
||||
await this.remove();
|
||||
return newObject;
|
||||
}
|
||||
}else{
|
||||
// Update what ever fields that where passed.
|
||||
|
||||
@@ -128,7 +150,10 @@ class Table{
|
||||
// Loop over the data fields and apply them to redis
|
||||
for(let key of Object.keys(data)){
|
||||
this[key] = data[key];
|
||||
await client.HSET(`${this.constructor.name}_${this[this.constructor._key]}`, key, data[key]);
|
||||
await client.HSET(
|
||||
redisPrefix(`${this.constructor.name}_${this[this.constructor._key]}`),
|
||||
key, String(data[key])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,10 +171,15 @@ class Table{
|
||||
try{
|
||||
// Remove the index key from the tables members list.
|
||||
|
||||
await client.SREM(this.constructor.name, this[this.constructor._key]);
|
||||
await client.SREM(
|
||||
redisPrefix(this.constructor.name),
|
||||
this[this.constructor._key]
|
||||
);
|
||||
|
||||
// Remove the entries hash values.
|
||||
let count = await client.DEL(`${this.constructor.name}_${this[this.constructor._key]}`);
|
||||
let count = await client.DEL(
|
||||
redisPrefix(`${this.constructor.name}_${this[this.constructor._key]}`)
|
||||
);
|
||||
|
||||
// Return the number of removed values to the caller.
|
||||
return count;
|
||||
|
||||
Reference in New Issue
Block a user