diff --git a/nodejs/models/host.js b/nodejs/models/host.js index a6b0e71..04cf79d 100755 --- a/nodejs/models/host.js +++ b/nodejs/models/host.js @@ -16,74 +16,205 @@ const Host = require('../utils/redis_model')({ } }); -Host.lookupDict = {}; +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; -Host.make_lookup = async function(){ 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('.'); - let place = this.lookupDict; - while(fragments.length !==0){ - let current = fragments.pop(); - if(!place[current]){ - place[current] = {}; + // 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){ - place[current]['#record'] = await this.get(host) + pointer[fragment]['#record'] = await this.get(host) } - place = place[current]; + + // 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 = async function(host){ - let place = this.lookupDict; +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 = {}; - for(let fragment of host.split('.').slice().reverse()){ + // 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({...place, ...last_resort}[fragment]){ - place = {...place, ...last_resort}[fragment]; + // 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(); +})() + + +var net = require('net'); +var fs = require('fs'); + +// This server listens on a Unix socket at /var/run/mysocket +var unixServer = net.createServer(function(client) { + // Do something with the client connection +}); + +unixServer.on('connection', function(clientSocket){ + let buffer = ''; + + console.log('server EVENT connection from client:', clientSocket.remoteAddress); + + // When a connection is started, send a message informing the remote + // peer of our ID + + clientSocket.on('data', function(data){ + buffer += data.toString(); + try{ + // p2p.__read(JSON.parse(buffer), clientSocket.remoteAddress, clientSocket); + console.log('buffer', buffer, Host.lookUp(buffer)) + clientSocket.write(JSON.stringify(Host.lookUp(buffer)|| {host: 'none'})); + }catch(error){ + ; + } + }); + + clientSocket.on('close', function(){ + console.log('info', `server Peer ${clientSocket.remoteAddress} - ${clientSocket.peerID} droped.`); + + }); + clientSocket.on('error', function(error){ + console.log(error) + }) + +}); + +unixServer.on('error', function(error){ + console.log(error) +}) + +var SOCKETFILE = '/var/run/mysocket' + +unixServer.on('listening', function(){ + fs.chmodSync(SOCKETFILE, '777'); + console.log('info','p2p server listening on') +}); + + + +fs.stat(SOCKETFILE, function (err, stats) { + if (err) { + // start server + console.log('No leftover socket found.'); + server = createServer(SOCKETFILE); return; + } + // remove file then start server + console.log('Removing leftover socket.') + fs.unlink(SOCKETFILE, function(err){ + if(err){ + // This should never happen. + console.error(err); process.exit(0); + } + unixServer.listen(SOCKETFILE); + }); + }); + module.exports = {Host}; (async function(){ - await Host.make_lookup(); - // console.log(Host.lookupDict) - // console.log(Host.lookupDict['com']['vm42']) + await Host.lookUpReady(); - // console.log('test-res', await Host.lookUp('payments.test.com')) + // console.log(Host.lookUpObj) - 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') + // console.log(Host.lookUpObj['com']['vm42']) + + // console.log('test-res', await Host.lookUp('payments.718it.biz')) + + let count = 6 + console.log(count++, Host.lookUp('payments.718it.biz').host === 'payments.718it.biz') + 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('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.**') + console.log(count++, Host.lookUp('info.wma.users.718it.biz').host === 'info.*.users.718it.biz') + console.log(count++, Host.lookUp('infof.users.718it.biz') === undefined) + console.log(count++, Host.lookUp('blah.biz') === undefined) + console.log(count++, Host.lookUp('test.1.2.718it.net').host === 'test.*.*.718it.net') + console.log(count++, Host.lookUp('test1.exmaple.com').host === 'test1.exmaple.com') + console.log(count++, Host.lookUp('other.exmaple.com').host === '*.exmaple.com') + console.log(count++, Host.lookUp('info.payments.example.com').host === 'info.**') + console.log(count++, Host.lookUp('718it.biz').host === '718it.biz') - return ; })() diff --git a/nodejs/routes/host.js b/nodejs/routes/host.js index 3139adb..13d6b83 100755 --- a/nodejs/routes/host.js +++ b/nodejs/routes/host.js @@ -4,38 +4,11 @@ const router = require('express').Router(); const {Host} = require('../models/host'); -router.get('/:host', async function(req, res, next){ - try{ - - return res.json({ - host: req.params.host, - results: await Host.get({host: req.params.host}) - }); - }catch(error){ - return next(error); - } - -}); - router.get('/', async function(req, res, next){ try{ return res.json({ hosts: await Host[req.query.detail ? "listDetail" : "list"]() }); - }catch(error){ - next(error) - } -}); - -router.put('/:host', async function(req, res, next){ - try{ - req.body.updated_by = req.user.username; - let host = await Host.get(req.params.host); - await host.update(req.body); - - return res.json({ - message: `Host "${req.params.host}" updated.` - }); }catch(error){ return next(error); } @@ -50,13 +23,39 @@ router.post('/', async function(req, res, next){ message: `Host "${req.body.host}" added.` }); } catch (error){ - next(error); + return next(error); } +}); +router.get('/:host', async function(req, res, next){ + try{ + + return res.json({ + host: req.params.host, + results: await Host.get({host: req.params.host}) + }); + }catch(error){ + return next(error); + } +}); + +router.put('/:host', async function(req, res, next){ + try{ + req.body.updated_by = req.user.username; + let host = await Host.get(req.params.host); + await host.update(req.body); + + return res.json({ + message: `Host "${req.params.host}" updated.` + }); + + }catch(error){ + return next(error); + + } }); router.delete('/:host', async function(req, res, next){ - try{ let host = await Host.get(req.params); let count = await host.remove(host); @@ -66,7 +65,19 @@ router.delete('/:host', async function(req, res, next){ }); }catch(error){ - next(error); + return next(error); + } +}); + +router.get('/lookup/:host', async function(req, res, next){ + try{ + return res.json({ + string: req.params.host, + results: await Host.lookUp(req.params.host), + }); + + }catch(error){ + return next(error); } }); diff --git a/nodejs/routes/index.js b/nodejs/routes/index.js index d31b6a6..674d52d 100755 --- a/nodejs/routes/index.js +++ b/nodejs/routes/index.js @@ -1,19 +1,23 @@ +'use strict'; + var express = require('express'); var router = express.Router(); +const {Host} = require('../models/host'); + /* GET home page. */ router.get('/', async function(req, res, next) { - res.render('hosts', { title: 'Express' }); + res.render('hosts', {}); }); /* GET home page. */ router.get('/hosts', function(req, res, next) { - res.render('hosts', { title: 'Express' }); + res.render('hosts', {}); }); /* GET home page. */ router.get('/users', function(req, res, next) { - res.render('users', { title: 'Express' }); + res.render('users', {}); }); /* GET home page. */ @@ -21,4 +25,16 @@ router.get('/login', function(req, res, next) { res.render('login', {redirect: req.query.redirect}); }); +router.get('/lookup/:host', async function(req, res, next){ + try{ + return res.json({ + string: req.params.host, + results: await Host.lookUp(req.params.host), + }); + + }catch(error){ + return next(error); + } +}); + module.exports = router;