|
|
|
@ -39,7 +39,7 @@ export default function socketClass(Server) {
|
|
|
|
|
* @param {String} options.host a tcp host name nornally not used as 0.0.0.0 is set by default
|
|
|
|
|
* @param {String} options.port a tcp
|
|
|
|
|
* @param {String | Boolean} options.path xeither full path to where socket should be created or if just 'true' then use default
|
|
|
|
|
* @param {Boolean} options.clientTracking track connected clients for push notifications - default: true
|
|
|
|
|
* @param {Boolean} options.consumerTracking track connected consumers for push notifications - default: true
|
|
|
|
|
* @param {Object} options.conPacket A json operson's property
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
@ -61,14 +61,14 @@ export default function socketClass(Server) {
|
|
|
|
|
this.allowAnonymous = (!opts.tokens || !!process.env.UCI_ANON || opts.allowAnonymous) ? true : false
|
|
|
|
|
this.tokens = opts.tokens || []
|
|
|
|
|
this.keepAlive = 'keepAlive' in opts ? opts.keepAlive : true
|
|
|
|
|
this.pingInterval = opts.pingInterval === false ? opts.pingInterval : (opts.pingInterval * 1000 || 5000)
|
|
|
|
|
this.clients = [] // track consumers (i.e. clients) TODO use a Map
|
|
|
|
|
this.nextClientID = 0 // incrementer for default initial client ID
|
|
|
|
|
this.pingInterval = opts.pingInterval === false ? 0 : (opts.pingInterval * 1000 || 5000)
|
|
|
|
|
this.consumers = new Map() // track consumers (i.e. clients) TODO use a Map
|
|
|
|
|
this.nextConsumerID = 0 // incrementer for default initial consumer ID
|
|
|
|
|
this.opts = opts // for use to recover from selected errors
|
|
|
|
|
this.errorCount = 0
|
|
|
|
|
//self bindings
|
|
|
|
|
this.create = this.create.bind(this)
|
|
|
|
|
this.authenticateClient = this.authenticateClient.bind(this)
|
|
|
|
|
this.authenticateConsumer = this.authenticateConsumer.bind(this)
|
|
|
|
|
this._authenticate = this._authenticate.bind(this)
|
|
|
|
|
this.close = promisify(this.close).bind(this)
|
|
|
|
|
log = logger({
|
|
|
|
@ -141,11 +141,12 @@ export default function socketClass(Server) {
|
|
|
|
|
this.emit('log', errors)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
let obj = {method:'create', line:54, msg:'socket server created and listening at', address:this.address()}
|
|
|
|
|
let msg = `socket ready and listening ${typeof this.address() ==='string' ? `at ${this.address()}` : `on port ${this.address().port}`}`
|
|
|
|
|
let obj = {method:'create', line:54, msg:msg}
|
|
|
|
|
log.info(obj)
|
|
|
|
|
this.on('connection', this._connectionHandler.bind(this))
|
|
|
|
|
this.emit('log:',)
|
|
|
|
|
resolve(`socket ready and listening at ${this.address().address}:${this.address().port}`)
|
|
|
|
|
resolve(msg)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
super.listen(this.opts)
|
|
|
|
@ -167,7 +168,7 @@ export default function socketClass(Server) {
|
|
|
|
|
enablePing () {
|
|
|
|
|
if (this.pingInterval > 499) {
|
|
|
|
|
this._ping = setInterval( async () =>{
|
|
|
|
|
if (this.clients.length > 0) this.push({pingInterval:this.pingInterval},'ping')
|
|
|
|
|
if (this.consumers.size > 0) this.push({pingInterval:this.pingInterval},'ping')
|
|
|
|
|
},this.pingInterval)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -212,7 +213,7 @@ export default function socketClass(Server) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* push - pushes a supplied UCI object packet to all connected clients
|
|
|
|
|
* push - pushes a supplied UCI object packet to all connected consumers
|
|
|
|
|
*
|
|
|
|
|
* @param {object} packet Description
|
|
|
|
|
* @param {string} id the header id string of the pushed packet, default: 'pushed'
|
|
|
|
@ -220,12 +221,12 @@ export default function socketClass(Server) {
|
|
|
|
|
*/
|
|
|
|
|
async push(packet={},id) {
|
|
|
|
|
packet._header = {id: id || 'pushed'}
|
|
|
|
|
if (this.clients.length > 0) {
|
|
|
|
|
if (this.consumers.size > 0) {
|
|
|
|
|
log.trace({method:'push', line:142, id:packet._header.id, opts: this.opts, packet: packet, msg:'pushing a packet to all connected consumers'})
|
|
|
|
|
// TODO should do a map and single promise
|
|
|
|
|
this.clients.forEach(async client => {
|
|
|
|
|
if (client.writable) {
|
|
|
|
|
let [err] = await btc(this._send)(client,packet)
|
|
|
|
|
this.consumers.forEach(async consumer => {
|
|
|
|
|
if (consumer.writable) {
|
|
|
|
|
let [err] = await btc(this._send)(consumer,packet)
|
|
|
|
|
if (err) log.error({msg:err, error:err})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
@ -234,56 +235,47 @@ export default function socketClass(Server) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO won't need this if moving to a Map
|
|
|
|
|
getClientIndex(sid) {
|
|
|
|
|
return this.clients.findIndex(client => {return client.sid === sid })
|
|
|
|
|
|
|
|
|
|
removeConsumer (sid) {
|
|
|
|
|
let consumer=this.consumers.get(sid)
|
|
|
|
|
this.emit('log',{level:'info', msg:'a consumer disconnected', consumer:consumer.data, sid:consumer.sid})
|
|
|
|
|
this.emit('connection:consumer',{state:'disconnected', msg:'a consumer disconnected', consumer:consumer.data, sid:consumer.sid})
|
|
|
|
|
consumer.removeAllListeners()
|
|
|
|
|
consumer.stream.removeAllListeners()
|
|
|
|
|
this.consumers.delete(sid)
|
|
|
|
|
log.warn({msg:'consumer removed from tracking',sid:sid, curConsumerCount:this.consumers.size})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getClient(sid) {
|
|
|
|
|
return this.clients[this._getClientIndex(sid)]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeClient (sid) {
|
|
|
|
|
let index = this.getClientIndex(sid)
|
|
|
|
|
let client=this.clients[index]
|
|
|
|
|
this.emit('log',{level:'info', msg:'a consumer disconnected', name:client.name, id:client.id})
|
|
|
|
|
this.emit('connection:consumer',{state:'disconnected', msg:'a consumer disconnected', name:client.name, id:client.id})
|
|
|
|
|
client.removeAllListeners()
|
|
|
|
|
client.stream.removeAllListeners()
|
|
|
|
|
this.clients.splice(index,1)
|
|
|
|
|
log.warn({msg:'consumer removed from tracking',sid:sid, curClientCount:this.clients.length})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async authenticateClient(client) {
|
|
|
|
|
async authenticateConsumer(consumer) {
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
|
// when consumer gets the handshake they must follow with authentication
|
|
|
|
|
client.stream.on('message', authenticate.bind(this,client))
|
|
|
|
|
let [err] = await btc(this._send)(client,{_handshake: true, sid:client.sid})
|
|
|
|
|
consumer.stream.on('message', authenticate.bind(this,consumer))
|
|
|
|
|
let [err] = await btc(this._send)(consumer,{_handshake: true, sid:consumer.sid})
|
|
|
|
|
if (err) {
|
|
|
|
|
log.error({msg:'error in handshake send', error:err})
|
|
|
|
|
reject(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function authenticate (client,packet) {
|
|
|
|
|
log.debug({msg:`authentication packet from client ${client.name}:${client.id}:${client.sid}`, packet:packet})
|
|
|
|
|
client.stream.removeAllListeners('message')
|
|
|
|
|
if (!packet._authenticate) reject('first client packet was not authentication')
|
|
|
|
|
async function authenticate (consumer,packet) {
|
|
|
|
|
log.debug({msg:`authentication packet from consumer ${consumer.name}:${consumer.id}:${consumer.sid}`, packet:packet})
|
|
|
|
|
consumer.stream.removeAllListeners('message')
|
|
|
|
|
if (!packet._authenticate) reject('first consumer packet was not authentication')
|
|
|
|
|
else {
|
|
|
|
|
let [err, res] = await btc(this._authenticate)(packet)
|
|
|
|
|
client.authenticated = this.allowAnonymous ? 'anonymous' : (err ? false : res)
|
|
|
|
|
client.name = packet.clientName
|
|
|
|
|
packet.authenticated = client.authenticated
|
|
|
|
|
consumer.authenticated = this.allowAnonymous ? 'anonymous' : (err ? false : res)
|
|
|
|
|
consumer.data = packet.data
|
|
|
|
|
packet.authenticated = consumer.authenticated
|
|
|
|
|
packet.reason = err || null
|
|
|
|
|
log.debug({msg:'sending authorization result to client', packet:packet})
|
|
|
|
|
await this._send(client,packet) // send either way
|
|
|
|
|
log.debug({msg:'sending authorization result to consumer', packet:packet})
|
|
|
|
|
await this._send(consumer,packet) // send either way
|
|
|
|
|
if (err && !this.allowAnonymous) {
|
|
|
|
|
log.info({msg:'client authentication failed', client:client.name, client_sid:client.sid, reason:err})
|
|
|
|
|
log.info({msg:`consumer ${consumer.data.name} authentication failed`, consumer:consumer.data, consumer_sid:consumer.sid, reason:err})
|
|
|
|
|
reject(packet.reason)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
log.info({msg:'client authenticated successfuly', client:client.name})
|
|
|
|
|
if (this.allowAnonymous) log.warn({msg:'socket consumer connected anonymously', consumer:client.name})
|
|
|
|
|
resolve(client.authenticated)
|
|
|
|
|
log.info({msg:`consumer ${consumer.data.name} authenticated successfuly`, consumer:consumer.data})
|
|
|
|
|
if (this.allowAnonymous) log.warn({msg:`consumer ${consumer.data.name}, connected anonymously`, consumer:consumer.data})
|
|
|
|
|
resolve(consumer.authenticated)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -299,29 +291,30 @@ export default function socketClass(Server) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// default authenticator - reject value should be reason which is returned to client
|
|
|
|
|
// default authenticator - reject value should be reason which is returned to consumer
|
|
|
|
|
async _authenticate (packet) {
|
|
|
|
|
if (!this._validateToken(packet.token)) return Promise.reject('invalid token')
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// async _connectionHandler({consumer, server}) { // this gets called for each client connection and is unique to
|
|
|
|
|
async _connectionHandler(consumer) { // this gets called for each client connection and is unique to each
|
|
|
|
|
// async _connectionHandler({consumer, server}) { // this gets called for each consumer connection and is unique to
|
|
|
|
|
async _connectionHandler(consumer) { // this gets called for each consumer connection and is unique to each
|
|
|
|
|
|
|
|
|
|
const stream = new JSONStream()
|
|
|
|
|
consumer.stream = stream
|
|
|
|
|
consumer.data = {}
|
|
|
|
|
consumer.connected = true
|
|
|
|
|
|
|
|
|
|
// add listeners
|
|
|
|
|
consumer.on('error', (err) => {
|
|
|
|
|
log.error({msg:'client connection error',error:err})
|
|
|
|
|
log.error({msg:'consumer connection error',error:err})
|
|
|
|
|
// TODO do more handling than just logging
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
consumer.on('end', (err) => {
|
|
|
|
|
log.error({msg:'consumer connected ended',error:err})
|
|
|
|
|
if (consumer.sid) this.removeClient(consumer.sid)
|
|
|
|
|
log.error({msg:`'consumer connection ended: ${consumer.data.name}`, error:err})
|
|
|
|
|
if (consumer.sid) this.removeConsumer(consumer.sid)
|
|
|
|
|
else {
|
|
|
|
|
consumer.removeAllListeners()
|
|
|
|
|
consumer.stream.removeAllListeners()
|
|
|
|
@ -335,9 +328,9 @@ export default function socketClass(Server) {
|
|
|
|
|
// TODO do more handling than just logging
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
consumer.authenticated = true
|
|
|
|
|
// consumer.authenticated = true
|
|
|
|
|
|
|
|
|
|
let [err] = await btc(this.authenticateClient)(consumer)
|
|
|
|
|
let [err] = await btc(this.authenticateConsumer)(consumer)
|
|
|
|
|
if (!this.allowAnonymous) {
|
|
|
|
|
if (err) {
|
|
|
|
|
consumer.removeAllListeners()
|
|
|
|
@ -347,18 +340,17 @@ export default function socketClass(Server) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// authenticated consumer, add to list of clients
|
|
|
|
|
consumer.sid = ++this.nextClientID // server assigned ID
|
|
|
|
|
consumer.socketSide = true
|
|
|
|
|
consumer.authenticated = true
|
|
|
|
|
this.clients.push(consumer) // add current consumer to clients
|
|
|
|
|
// authenticated consumer, add to list of consumers
|
|
|
|
|
consumer.sid = ++this.nextConsumerID // server assigned ID
|
|
|
|
|
// consumer.authenticated = true
|
|
|
|
|
this.consumers.set(consumer.sid, consumer) // add current consumer to consumers
|
|
|
|
|
consumer.setKeepAlive(this.keepAlive,30)
|
|
|
|
|
const clientCloseHandler = (sid) => {
|
|
|
|
|
const consumerCloseHandler = (sid) => {
|
|
|
|
|
log.warn({msg:'consumer connection was closed',sid:sid})
|
|
|
|
|
this.removeClient(sid)
|
|
|
|
|
this.removeConsumer(sid)
|
|
|
|
|
}
|
|
|
|
|
consumer.on('close', clientCloseHandler.bind(this,consumer.sid))
|
|
|
|
|
log.debug({method:'_listen', line:364, msg:'new consumer connected/authenticated', cname:consumer.name, cid:consumer.id, totalConsumers:this.clients.length})
|
|
|
|
|
consumer.on('close', consumerCloseHandler.bind(this,consumer.sid))
|
|
|
|
|
log.debug({method:'_listen', line:364, msg:'new consumer connected/authenticated', cname:consumer.name, cid:consumer.id, totalConsumers:this.consumers.size})
|
|
|
|
|
// all's set enable main incoming message processor
|
|
|
|
|
stream.on('message', messageProcess.bind(this, consumer))
|
|
|
|
|
|
|
|
|
@ -369,12 +361,17 @@ export default function socketClass(Server) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.emit('log',{level:'info', msg:'a consumer connected and authenticated', name:consumer.name, id:consumer.id})
|
|
|
|
|
this.emit('connection:consumer',{state:'connected', msg:'a consumer connected and authenticated', name:consumer.name, id:consumer.id})
|
|
|
|
|
this.emit('connection:consumer',{state:'connected', msg:`consumer ${(consumer.data ||{}).name} connected and authenticated to socket ${this.id}`,
|
|
|
|
|
name:(consumer.data ||{}).name ||(consumer.data ||{}).id || consumer.sid,
|
|
|
|
|
sid:consumer.sid,
|
|
|
|
|
data:consumer.data,
|
|
|
|
|
authenticated:consumer.authenticated
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// that's it. Connection is active
|
|
|
|
|
|
|
|
|
|
async function messageProcess(client, packet) {
|
|
|
|
|
log.debug({method:'_listen', line:179, packet: packet, client:client.name, msg:'incoming packet on socket side'})
|
|
|
|
|
async function messageProcess(consumer, packet) {
|
|
|
|
|
log.debug({method:'_listen', line:179, packet: packet, consumer:consumer.data, msg:'incoming packet on socket side'})
|
|
|
|
|
let res = (await this._packetProcess(clone(packet))) || {}
|
|
|
|
|
if (Object.keys(res).length === 0)
|
|
|
|
|
res = {
|
|
|
|
@ -390,7 +387,7 @@ export default function socketClass(Server) {
|
|
|
|
|
res._header.responder = { name: this.name, instanceID: this.id }
|
|
|
|
|
res._header.socket = this.address()
|
|
|
|
|
if (!res.cmd) res.cmd = this.defaultReturnCmd || 'reply' // by default return command is 'reply'
|
|
|
|
|
let [err] = await btc(this._send)(client,res)
|
|
|
|
|
let [err] = await btc(this._send)(consumer,res)
|
|
|
|
|
if (err) log.error({msg:err, error:err})
|
|
|
|
|
} // end message process
|
|
|
|
|
|
|
|
|
@ -402,9 +399,9 @@ export default function socketClass(Server) {
|
|
|
|
|
// this.push()
|
|
|
|
|
clearInterval(this._ping)
|
|
|
|
|
await this.close()
|
|
|
|
|
this.clients.forEach(client => {
|
|
|
|
|
client.removeAllListeners()
|
|
|
|
|
client.stream.removeAllListeners()
|
|
|
|
|
this.consumers.forEach(consumer => {
|
|
|
|
|
consumer.removeAllListeners()
|
|
|
|
|
consumer.stream.removeAllListeners()
|
|
|
|
|
})
|
|
|
|
|
log.debug({method:'_destroy', line:219, msg:'all connections closed....exiting'})
|
|
|
|
|
process.exit()
|
|
|
|
@ -417,18 +414,18 @@ export default function socketClass(Server) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _send(client, packet) {
|
|
|
|
|
log.trace({msg:`sending to client:${client.id}`, packet:packet})
|
|
|
|
|
async _send(consumer, packet) {
|
|
|
|
|
log.trace({msg:`sending to consumer:${consumer.sid}:${consumer.data.name}`, consumer:consumer.data, packet:packet})
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
|
if (!client.writable) {
|
|
|
|
|
if (!consumer.writable) {
|
|
|
|
|
reject('socket stream closed can not send packet')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let [err,ser] = await btc(client.stream.serialize)(packet)
|
|
|
|
|
let [err,ser] = await btc(consumer.stream.serialize)(packet)
|
|
|
|
|
if (err) reject('unable to serialze the packet')
|
|
|
|
|
const cb = () => resolve('packet written to socket stream')
|
|
|
|
|
if (!client.write(ser)) {
|
|
|
|
|
client.once('drain', cb)
|
|
|
|
|
if (!consumer.write(ser)) {
|
|
|
|
|
consumer.once('drain', cb)
|
|
|
|
|
} else {
|
|
|
|
|
process.nextTick(cb)
|
|
|
|
|
}
|
|
|
|
|