service for complex look up
This commit is contained in:
@@ -45,6 +45,12 @@ like Digital Ocean will do just fine.
|
|||||||
```bash
|
```bash
|
||||||
apt install luarocks
|
apt install luarocks
|
||||||
sudo luarocks install lua-resty-auto-ssl
|
sudo luarocks install lua-resty-auto-ssl
|
||||||
|
sudo luarocks install lua-resty-socket
|
||||||
|
sudo luarocks install lua-socket
|
||||||
|
sudo luarocks install socket
|
||||||
|
sudo luarocks install luasocket
|
||||||
|
sudo luarocks install luasocket-unix
|
||||||
|
sudo luarocks install lua-cjson
|
||||||
```
|
```
|
||||||
|
|
||||||
* openresty config
|
* openresty config
|
||||||
|
|||||||
@@ -40,6 +40,10 @@ app.use('/api/user', middleware.auth, require('./routes/user'));
|
|||||||
// API routes for working with hosts. All endpoints need to be have valid user.
|
// API routes for working with hosts. All endpoints need to be have valid user.
|
||||||
app.use('/api/host', middleware.auth, require('./routes/host'));
|
app.use('/api/host', middleware.auth, require('./routes/host'));
|
||||||
|
|
||||||
|
app.controler = {
|
||||||
|
host: require('./controler/host')
|
||||||
|
}
|
||||||
|
|
||||||
// Catch 404 and forward to error handler. If none of the above routes are
|
// Catch 404 and forward to error handler. If none of the above routes are
|
||||||
// used, this is what will be called.
|
// used, this is what will be called.
|
||||||
app.use(function(req, res, next) {
|
app.use(function(req, res, next) {
|
||||||
|
|||||||
+2
-1
@@ -9,5 +9,6 @@ module.exports = {
|
|||||||
searchBase: 'ou=people,dc=theta42,dc=com',
|
searchBase: 'ou=people,dc=theta42,dc=com',
|
||||||
userFilter: '(objectClass=inetOrgPerson)',
|
userFilter: '(objectClass=inetOrgPerson)',
|
||||||
userNameAttribute: 'uid'
|
userNameAttribute: 'uid'
|
||||||
}
|
},
|
||||||
|
socketFile: '/var/run/proxy_lookup.socket'
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const {Host} = require('../models/host');
|
||||||
|
const {SocketServerJson} = require('../models/socket_server_json');
|
||||||
|
const conf = require('../app').conf;
|
||||||
|
|
||||||
|
|
||||||
|
const socket = new SocketServerJson({
|
||||||
|
socketFile: conf.socketFile,
|
||||||
|
onData: function(data, clientSocket) {
|
||||||
|
clientSocket.write(JSON.stringify(Host.lookUp(data['domain']) || {host: 'none'}));
|
||||||
|
},
|
||||||
|
onListen: function(){
|
||||||
|
console.log('listening')
|
||||||
|
}
|
||||||
|
});
|
||||||
+53
-89
@@ -16,6 +16,39 @@ const Host = require('../utils/redis_model')({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Host.add = async function(){
|
||||||
|
try{
|
||||||
|
let out = await this.__proto__.add(...arguments)
|
||||||
|
await this.buildLookUpObj()
|
||||||
|
|
||||||
|
return out;
|
||||||
|
} catch(error){
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Host.update = async function(){
|
||||||
|
try{
|
||||||
|
let out = await this.__proto__.update(...arguments)
|
||||||
|
await this.buildLookUpObj()
|
||||||
|
|
||||||
|
return out;
|
||||||
|
} catch(error){
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Host.remove = async function(){
|
||||||
|
try{
|
||||||
|
let out = await this.__proto__.remove(...arguments)
|
||||||
|
await this.buildLookUpObj()
|
||||||
|
|
||||||
|
return out;
|
||||||
|
} catch(error){
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Host.lookUpObj = {};
|
Host.lookUpObj = {};
|
||||||
|
|
||||||
Host.buildLookUpObj = async function(){
|
Host.buildLookUpObj = async function(){
|
||||||
@@ -117,80 +150,11 @@ Host.lookUpReady = async function(){
|
|||||||
await Host.buildLookUpObj();
|
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};
|
module.exports = {Host};
|
||||||
|
|
||||||
// (async function(){
|
(async function(){
|
||||||
|
|
||||||
// await Host.lookUpReady();
|
await Host.lookUpReady();
|
||||||
|
|
||||||
// console.log(Host.lookUpObj)
|
// console.log(Host.lookUpObj)
|
||||||
|
|
||||||
@@ -198,23 +162,23 @@ module.exports = {Host};
|
|||||||
|
|
||||||
// console.log('test-res', await Host.lookUp('payments.718it.biz'))
|
// console.log('test-res', await Host.lookUp('payments.718it.biz'))
|
||||||
|
|
||||||
// let count = 6
|
let count = 6
|
||||||
// console.log(count++, Host.lookUp('payments.718it.biz').host === 'payments.718it.biz')
|
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('sd.blah.test.vm42.com') === undefined)
|
||||||
// console.log(count++, Host.lookUp('payments.test.com').host === 'payments.**')
|
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('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('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('blah.test.vm42.com').host === '*.test.vm42.com')
|
||||||
// console.log(count++, Host.lookUp('payments.example.com').host === 'payments.**')
|
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('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('infof.users.718it.biz') === undefined)
|
||||||
// console.log(count++, Host.lookUp('blah.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('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('test1.exmaple.com').host === 'test1.exmaple.com')
|
||||||
// console.log(count++, Host.lookUp('other.exmaple.com').host === '*.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('info.payments.example.com').host === 'info.**')
|
||||||
// console.log(count++, Host.lookUp('718it.biz').host === '718it.biz')
|
console.log(count++, Host.lookUp('718it.biz').host === '718it.biz')
|
||||||
|
|
||||||
|
|
||||||
// })()
|
})()
|
||||||
|
|||||||
@@ -0,0 +1,89 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const net = require('net');
|
||||||
|
const fs = require('fs');
|
||||||
|
const {CallbackQueue} = require('../utils/callback_queue')
|
||||||
|
|
||||||
|
class SocketServerJson {
|
||||||
|
constructor(args){
|
||||||
|
this.socketFile = args.socketFile;
|
||||||
|
this.onData = new CallbackQueue(args.onData, this);
|
||||||
|
this.onListen = new CallbackQueue(args.onListen, this);
|
||||||
|
this.onError = new CallbackQueue(args.onError);
|
||||||
|
this.onCLientNew = new CallbackQueue(args.onCLientNew);
|
||||||
|
this.onCLientClose = new CallbackQueue(args.onCLientClose);
|
||||||
|
this.onCLientError = new CallbackQueue(args.onCLientClose);
|
||||||
|
|
||||||
|
this.onListen.push(function(){
|
||||||
|
fs.chmodSync(args.socketFile, '777');
|
||||||
|
})
|
||||||
|
|
||||||
|
this.listen();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
__resetSocketFile(callback){
|
||||||
|
let instance = this;
|
||||||
|
|
||||||
|
fs.stat(this.socketFile, function (err, stats) {
|
||||||
|
if (stats) {
|
||||||
|
fs.unlink(instance.socketFile, function(err){
|
||||||
|
if(err){
|
||||||
|
// This should never happen.
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
callback(...arguments)
|
||||||
|
});
|
||||||
|
}else{
|
||||||
|
callback()
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__setUpServer(){
|
||||||
|
|
||||||
|
let instance = this;
|
||||||
|
this.socket = net.createServer();
|
||||||
|
|
||||||
|
this.socket.on('connection', function(clientSocket){
|
||||||
|
let buffer = '';
|
||||||
|
|
||||||
|
clientSocket.on('data', function(data){
|
||||||
|
buffer += data.toString();
|
||||||
|
try{
|
||||||
|
console.log('buffer', buffer)
|
||||||
|
instance.onData.call(JSON.parse(data), clientSocket)
|
||||||
|
buffer = ''
|
||||||
|
// clientSocket.write(JSON.stringify(Host.lookUp(buffer)|| {host: 'none'}));
|
||||||
|
|
||||||
|
}catch(error){
|
||||||
|
;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
clientSocket.on('close', instance.onCLientClose.call.bind(instance.onCLientClose));
|
||||||
|
|
||||||
|
clientSocket.on('error', instance.onCLientError.call.bind(instance.onCLientError));
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
this.socket.on('error', this.onError.call.bind(this.onError))
|
||||||
|
|
||||||
|
this.socket.on('listening', this.onListen.call.bind(this.onListen));
|
||||||
|
}
|
||||||
|
|
||||||
|
listen(){
|
||||||
|
let instance = this;
|
||||||
|
|
||||||
|
this.__setUpServer();
|
||||||
|
|
||||||
|
this.__resetSocketFile(function(){
|
||||||
|
instance.socket.listen(instance.socketFile);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {SocketServerJson};
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
class CallbackQueue{
|
||||||
|
constructor(callbacks){
|
||||||
|
this.__callbacks = [];
|
||||||
|
|
||||||
|
for(let callback of Array.isArray(callbacks) ? callbacks : [callbacks]){
|
||||||
|
this.push(callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
push(callback){
|
||||||
|
if(callback instanceof Function){
|
||||||
|
this.__callbacks.push(callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
call(){
|
||||||
|
let args = arguments;
|
||||||
|
this.__callbacks.forEach(function(callback){
|
||||||
|
callback(...args);
|
||||||
|
}.bind(this))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {CallbackQueue};
|
||||||
Reference in New Issue
Block a user