Merge pull request #31 from shocknet/socket-improvements
Socket improvements
This commit is contained in:
commit
2b7de83ab1
2 changed files with 42 additions and 78 deletions
|
|
@ -196,14 +196,7 @@ const server = program => {
|
|||
|
||||
const io = require('socket.io')(serverInstance)
|
||||
|
||||
const Sockets = require('./sockets')(
|
||||
io,
|
||||
lnd,
|
||||
program.user,
|
||||
program.pwd,
|
||||
program.limituser,
|
||||
program.limitpwd
|
||||
)
|
||||
const Sockets = require('./sockets')(io, lnd)
|
||||
|
||||
require('./routes')(app, defaults, Sockets, {
|
||||
serverHost: module.serverHost,
|
||||
|
|
|
|||
105
src/sockets.js
105
src/sockets.js
|
|
@ -3,13 +3,11 @@
|
|||
const logger = require("winston");
|
||||
|
||||
module.exports = (
|
||||
/** @type {import('socket.io').Server} */
|
||||
io,
|
||||
lnd,
|
||||
login,
|
||||
pass,
|
||||
limitlogin,
|
||||
limitpass
|
||||
) => {
|
||||
|
||||
const Mediator = require("../services/gunDB/Mediator/index.js");
|
||||
const EventEmitter = require("events");
|
||||
|
||||
|
|
@ -17,21 +15,6 @@ module.exports = (
|
|||
|
||||
const mySocketsEvents = new MySocketsEvents();
|
||||
|
||||
const clients = [];
|
||||
|
||||
const authEnabled = (login && pass) || (limitlogin && limitpass);
|
||||
|
||||
let userToken = null;
|
||||
let limitUserToken = null;
|
||||
if (login && pass) {
|
||||
userToken = Buffer.from(login + ":" + pass).toString("base64");
|
||||
}
|
||||
if (limitlogin && limitpass) {
|
||||
limitUserToken = Buffer.from(limitlogin + ":" + limitpass).toString(
|
||||
"base64"
|
||||
);
|
||||
}
|
||||
|
||||
// register the lnd invoices listener
|
||||
const registerLndInvoiceListener = socket => {
|
||||
socket._invoiceListener = {
|
||||
|
|
@ -57,65 +40,53 @@ module.exports = (
|
|||
unregisterLndInvoiceListener(socket);
|
||||
};
|
||||
|
||||
const getSocketAuthToken = socket => {
|
||||
if (socket.handshake.query.auth) {
|
||||
return socket.handshake.query.auth;
|
||||
} else if (socket.handshake.headers.authorization) {
|
||||
return socket.handshake.headers.authorization.substr(6);
|
||||
}
|
||||
|
||||
socket.disconnect("unauthorized");
|
||||
return null;
|
||||
};
|
||||
|
||||
io.on("connection", socket => {
|
||||
logger.info(`io.onconnection`)
|
||||
|
||||
logger.info("socket.handshake", socket.handshake);
|
||||
|
||||
/** printing out the client who joined */
|
||||
logger.info("New socket client connected (id=" + socket.id + ").");
|
||||
|
||||
const isOneTimeUseSocket = !!socket.handshake.query.IS_GUN_AUTH
|
||||
|
||||
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 {
|
||||
logger.info('New socket is NOT one time use')
|
||||
// this is where we create the websocket connection
|
||||
// with the GunDB service.
|
||||
Mediator.createMediator(socket);
|
||||
|
||||
logger.debug("socket.handshake", socket.handshake);
|
||||
|
||||
if (authEnabled) {
|
||||
try {
|
||||
const authorizationHeaderToken = getSocketAuthToken(socket);
|
||||
|
||||
if (authorizationHeaderToken === userToken) {
|
||||
socket._limituser = false;
|
||||
} else if (authorizationHeaderToken === limitUserToken) {
|
||||
socket._limituser = true;
|
||||
} else {
|
||||
socket.disconnect("unauthorized");
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
// probably because of missing authorization header
|
||||
logger.debug(err);
|
||||
socket.disconnect("unauthorized");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
socket._limituser = false;
|
||||
}
|
||||
|
||||
/** printing out the client who joined */
|
||||
logger.debug("New socket client connected (id=" + socket.id + ").");
|
||||
|
||||
socket.emit("hello", { limitUser: socket._limituser });
|
||||
|
||||
socket.broadcast.emit("hello", { remoteAddress: socket.handshake.address });
|
||||
|
||||
/** pushing new client to client array*/
|
||||
clients.push(socket);
|
||||
|
||||
registerSocketListeners(socket);
|
||||
|
||||
/** listening if client has disconnected */
|
||||
socket.on("disconnect", () => {
|
||||
clients.splice(clients.indexOf(socket), 1);
|
||||
unregisterSocketListeners(socket);
|
||||
logger.debug("client disconnected (id=" + socket.id + ").");
|
||||
});
|
||||
logger.info("client disconnected (id=" + socket.id + ").");
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
return mySocketsEvents;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue