commit
5f1d2400fb
2 changed files with 83 additions and 50 deletions
|
|
@ -83,7 +83,12 @@
|
||||||
|
|
||||||
"no-undefined": "off",
|
"no-undefined": "off",
|
||||||
|
|
||||||
"no-process-env": "off"
|
"no-process-env": "off",
|
||||||
|
|
||||||
|
// I am now convinced TODO comments closer to the relevant code are better
|
||||||
|
// than GH issues. Especially when it only concerns a single function /
|
||||||
|
// routine.
|
||||||
|
"no-warning-comments": "off"
|
||||||
},
|
},
|
||||||
"parser": "babel-eslint",
|
"parser": "babel-eslint",
|
||||||
"env": {
|
"env": {
|
||||||
|
|
|
||||||
102
src/sockets.js
102
src/sockets.js
|
|
@ -1,9 +1,14 @@
|
||||||
/** @prettier */
|
/** @prettier */
|
||||||
// app/sockets.js
|
// @ts-check
|
||||||
|
|
||||||
const logger = require('winston')
|
const logger = require('winston')
|
||||||
const Encryption = require('../utils/encryptionStore')
|
const Encryption = require('../utils/encryptionStore')
|
||||||
const LightningServices = require('../utils/lightningServices')
|
const LightningServices = require('../utils/lightningServices')
|
||||||
|
const {
|
||||||
|
getGun,
|
||||||
|
getUser,
|
||||||
|
isAuthenticated
|
||||||
|
} = require('../services/gunDB/Mediator')
|
||||||
|
|
||||||
const onPing = (socket, subID) => {
|
const onPing = (socket, subID) => {
|
||||||
logger.warn('Subscribing to pings socket...' + subID)
|
logger.warn('Subscribing to pings socket...' + subID)
|
||||||
|
|
@ -21,8 +26,6 @@ module.exports = (
|
||||||
/** @type {import('socket.io').Server} */
|
/** @type {import('socket.io').Server} */
|
||||||
io
|
io
|
||||||
) => {
|
) => {
|
||||||
const Mediator = require('../services/gunDB/Mediator/index.js')
|
|
||||||
|
|
||||||
// This should be used for encrypting and emitting your data
|
// This should be used for encrypting and emitting your data
|
||||||
const emitEncryptedEvent = ({ eventName, data, socket }) => {
|
const emitEncryptedEvent = ({ eventName, data, socket }) => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -255,43 +258,19 @@ module.exports = (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
io.on('connection', socket => {
|
io.of('default').on('connection', socket => {
|
||||||
logger.info(`io.onconnection`)
|
logger.info(`io.onconnection`)
|
||||||
|
|
||||||
logger.info('socket.handshake', socket.handshake)
|
logger.info('socket.handshake', socket.handshake)
|
||||||
|
|
||||||
const isOneTimeUseSocket = !!socket.handshake.query.IS_GUN_AUTH
|
|
||||||
const isLNDSocket = !!socket.handshake.query.IS_LND_SOCKET
|
const isLNDSocket = !!socket.handshake.query.IS_LND_SOCKET
|
||||||
const isNotificationsSocket = !!socket.handshake.query
|
const isNotificationsSocket = !!socket.handshake.query
|
||||||
.IS_NOTIFICATIONS_SOCKET
|
.IS_NOTIFICATIONS_SOCKET
|
||||||
|
|
||||||
if (!isLNDSocket) {
|
if (!isLNDSocket) {
|
||||||
/** printing out the client who joined */
|
/** printing out the client who joined */
|
||||||
logger.info('New socket client connected (id=' + socket.id + ').')
|
logger.info('New socket client connected (id=' + socket.id + ').')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isOneTimeUseSocket) {
|
|
||||||
logger.info('New socket is one time use')
|
|
||||||
socket.on('IS_GUN_AUTH', () => {
|
|
||||||
try {
|
|
||||||
const isGunAuth = Mediator.isAuthenticated()
|
|
||||||
socket.emit('IS_GUN_AUTH', {
|
|
||||||
ok: true,
|
|
||||||
msg: {
|
|
||||||
isGunAuth
|
|
||||||
},
|
|
||||||
origBody: {}
|
|
||||||
})
|
|
||||||
socket.disconnect()
|
|
||||||
} catch (err) {
|
|
||||||
socket.emit('IS_GUN_AUTH', {
|
|
||||||
ok: false,
|
|
||||||
msg: err.message,
|
|
||||||
origBody: {}
|
|
||||||
})
|
|
||||||
socket.disconnect()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
if (isLNDSocket) {
|
if (isLNDSocket) {
|
||||||
const subID = Math.floor(Math.random() * 1000).toString()
|
const subID = Math.floor(Math.random() * 1000).toString()
|
||||||
const isNotifications = isNotificationsSocket ? 'notifications' : ''
|
const isNotifications = isNotificationsSocket ? 'notifications' : ''
|
||||||
|
|
@ -305,17 +284,66 @@ module.exports = (
|
||||||
cancelTransactionStream()
|
cancelTransactionStream()
|
||||||
cancelPingStream()
|
cancelPingStream()
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
io.of('gun').on('connect', socket => {
|
||||||
|
// TODO: off()
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!isAuthenticated()) {
|
||||||
|
socket.emit('$shock', 'NOT_AUTH')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.info('New socket is NOT one time use')
|
|
||||||
// this is where we create the websocket connection
|
|
||||||
// with the GunDB service.
|
|
||||||
Mediator.createMediator(socket)
|
|
||||||
|
|
||||||
/** listening if client has disconnected */
|
const { $shock } = socket.handshake.query
|
||||||
socket.on('disconnect', () => {
|
|
||||||
logger.info('client disconnected (id=' + socket.id + ').')
|
const [root, path, method] = $shock.split('::')
|
||||||
})
|
|
||||||
|
// eslint-disable-next-line init-declarations
|
||||||
|
let node
|
||||||
|
|
||||||
|
if (root === '$gun') {
|
||||||
|
node = getGun()
|
||||||
|
} else if (root === '$user') {
|
||||||
|
node = getUser()
|
||||||
|
} else {
|
||||||
|
node = getGun().user(root)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const bit of path.split('.')) {
|
||||||
|
node = node.get(bit)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {unknown} data
|
||||||
|
* @param {string} key
|
||||||
|
*/
|
||||||
|
const listener = (data, key) => {
|
||||||
|
try {
|
||||||
|
socket.emit('$shock', data, key)
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(
|
||||||
|
`Error for gun rpc socket, query ${$shock} -> ${err.message}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (method === 'on') {
|
||||||
|
node.on(listener)
|
||||||
|
} else if (method === 'open') {
|
||||||
|
node.open(listener)
|
||||||
|
} else if (method === 'map.on') {
|
||||||
|
node.map().on(listener)
|
||||||
|
} else if (method === 'map.once') {
|
||||||
|
node.map().once(listener)
|
||||||
|
} else {
|
||||||
|
throw new TypeError(
|
||||||
|
`Invalid method for gun rpc call : ${method}, query: ${$shock}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('GUNRPC: ' + err.message)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue