Websockets Handshake Procedure

This commit is contained in:
Luis Fernando Molina committed 2021-08-31 09:23:10 +02:00
1 parent 02396798b8
commit 06d0f3bf53
8 files changed
+196 -12

No files matched your search

+1 -1
View File
@@ -163,7 +163,7 @@ try {
WEB_SOCKETS_INTERFACE = WEB_SOCKETS_INTERFACE.newWebSocketsInterface()
WEB_SOCKETS_INTERFACE.initialize()
WEB_SOCKETS_INTERFACE.run()
console.log('Web Sockets Interface ....................................... Listening at port ' + global.env.WEB_SOCKETS_INTERFACE_PORT)
console.log('Web Sockets Interface ....................................... Listening at port ' + global.env.CLIENT_WEB_SOCKETS_INTERFACE_PORT)
HTTP_INTERFACE = HTTP_INTERFACE.newHttpInterface()
HTTP_INTERFACE.initialize()
+1 -1
View File
@@ -10,7 +10,7 @@
const WEB_SOCKET = require('ws')
let socketServer
let port = global.env.WEB_SOCKETS_INTERFACE_PORT
let port = global.env.CLIENT_WEB_SOCKETS_INTERFACE_PORT
return thisObject
+1 -1
View File
@@ -2,7 +2,7 @@ exports.newEnvironment = function () {
let thisObject = {
WEB_SERVER_URL: 'localhost',
WEB_SOCKETS_INTERFACE_PORT: 18041,
CLIENT_WEB_SOCKETS_INTERFACE_PORT: 18041,
HTTP_INTERFACE_PORT: 34248,
PATH_TO_TASK_SERVER: './TaskServer',
PATH_TO_DATA_STORAGE: './Data-Storage',
+1 -1
View File
@@ -2,7 +2,7 @@ exports.newEnvironment = function () {
let thisObject = {
WEB_SERVER_URL: 'localhost',
WEB_SOCKETS_INTERFACE_PORT: 18041,
CLIENT_WEB_SOCKETS_INTERFACE_PORT: 18041,
HTTP_INTERFACE_PORT: 34248,
PATH_TO_TASK_SERVER: '../TaskServer',
PATH_TO_DATA_STORAGE: '../Data-Storage',
+2 -1
View File
@@ -42,7 +42,8 @@ Setting up external dependencies.
SA.nodeModules = {
fs: require('fs'),
nodeFetch: require('node-fetch'),
web3: require('web3')
web3: require('web3'),
ws: require('ws')
}
start()
+181
View File
@@ -0,0 +1,181 @@
exports.newWebSocketsInterface = function newWebSocketsInterface() {
let thisObject = {
initialize: initialize,
finalize: finalize,
run: run
}
let socketServer
let port = global.env.Network_WEB_SOCKETS_INTERFACE_PORT
return thisObject
function finalize() {
}
function initialize() {
}
function run() {
setUpWebSocketServer()
}
function setUpWebSocketServer() {
try {
socketServer = new SA.nodeModule.ws.Server({ port: port })
socketServer.on('connection', onConnection)
function onConnection(socket) {
let caller = {}
socket.on('message', onMenssage)
function onMenssage(message) {
try {
let messageHeader = JSON.parse(message)
/*
We will run some validations.
*/
if (messageHeader.messageType === undefined) {
let response = {
result: 'Error',
message: 'messageType Not Provided.'
}
socket.send(JSON.stringify(response))
return
}
switch (messageHeader.messageType) {
case "Handshake": {
handShakeProducedure(socket, caller, messageHeader)
break
}
case "Request": {
break
}
}
let origin = messageArray[0]
let nonce = messageArray[1]
let messageToEventServer = messageArray[2]
if (origin === 'Web Browser') {
try {
JSON.parse(messageToEventServer)
} catch (err) {
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> Message received from the browser is not a valid JSON. message = ' + message.substring(0, 1000))
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> Message received from the browser is not a valid JSON. messageToEventServer = ' + messageToEventServer)
return
}
let acknowledgeMessage = {
action: 'Acknowledge',
nonce: nonce
}
socket.send(JSON.stringify(acknowledgeMessage))
}
CL.servers.EVENT_SERVER.onMessage(messageToEventServer, onResponse)
function onResponse(message) {
socket.send(message)
}
} catch (err) {
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> Nonce received is less than Last Nonce. err = ' + err.stack)
}
}
}
function handShakeProducedure(socket, caller, messageHeader) {
/*
The caller needs to identify itself as either a Network Client or Peer.
*/
if (messageHeader.callerRole === undefined) {
let response = {
result: 'Error',
message: 'callerRole Not Provided.'
}
socket.send(JSON.stringify(response))
return
}
if (messageHeader.callerRole !== 'Network Client' && messageHeader.callerRole !== 'Network Peer') {
let response = {
result: 'Error',
message: 'callerRole Not Supported.'
}
socket.send(JSON.stringify(response))
return
}
caller.role = messageHeader.callerRole
/*
We will identify the caller by the signature at the message.
*/
if (messageHeader.signature === undefined) {
let response = {
result: 'Error',
message: 'signature Not Provided.'
}
socket.send(JSON.stringify(response))
return
}
let web3 = new SA.nodeModules.web3()
let blockchainAccount = web3.eth.accounts.recover(messageHeader.signature)
if (caller.blockchainAccount === undefined) {
let response = {
result: 'Error',
message: 'Bad signature.'
}
socket.send(JSON.stringify(response))
return
}
/*
The signature gives us the blockchain account, and the account the user profile.
*/
let userProfile = NT.projects.network.globals.memory.maps.USER_PROFILES_BY_BLOCHAIN_ACCOUNT.set(blockchainAccount)
if (userProfile === undefined) {
let response = {
result: 'Error',
message: 'userProfile Not Found.'
}
socket.send(JSON.stringify(response))
return
}
/*
We will remember the user profile behind this caller.
*/
caller.userProfile = userProfile
let response = {
result: 'Ok',
message: 'Handshake Successful.'
}
socket.send(JSON.stringify(response))
}
} catch (err) {
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> err.message = ' + err.message.substring(0, 1000))
}
}
}
+6 -5
View File
@@ -4,13 +4,14 @@ exports.newNetworkGlobalsMemory = function () {
*/
let thisObject = {
maps: {
USER_PROFILES_BY_ID: new Map(), // Here we will store the user profiles by User Profile Id.
USER_PROFILES_BY_HANDLE: new Map(), // Here we will store the user profiles by User Profile Handle.
EVENTS: new Map(), // This is the registry of all events received that prevents processing them more than once.
USER_PROFILES_BY_ID: new Map(), // Here we will store the user profiles by User Profile Id.
USER_PROFILES_BY_HANDLE: new Map(), // Here we will store the user profiles by User Profile Handle.
USER_PROFILES_BY_BLOCHAIN_ACCOUNT: new Map(), // Here we will store the user profiles by Blockchain Account.
EVENTS: new Map(), // This is the registry of all events received that prevents processing them more than once.
},
arrays: {
NETWORK_CLIENTS: [], // These are the Network Clients connected to this Network Node ordered by Ranking.
NETWORK_PEERS: [], // These are the Network Peers connected to this Network Node ordered by Ranking.
NETWORK_CLIENTS: [], // These are the Network Clients connected to this Network Node ordered by Ranking.
NETWORK_PEERS: [], // These are the Network Peers connected to this Network Node ordered by Ranking.
}
}
+3 -2
View File
@@ -36,7 +36,7 @@ exports.newBootstrap = function newBootstrap() {
let config = JSON.parse(userProfilePlugin.config)
let signatureObject = config.signature
let web3 = new SA.nodeModules.web3()
let bloackchainAddress = web3.eth.accounts.recover(signatureObject)
let blockchainAccount = web3.eth.accounts.recover(signatureObject)
let ranking = 0 // TODO: read the blockchain balance and transactions from the Treasury Account to calculate the profile ranking.
let userProfileId = userProfilePlugin.id
let userHandle = config.signature.message
@@ -45,12 +45,13 @@ exports.newBootstrap = function newBootstrap() {
userProfile.initialize(
userProfileId,
userHandle,
bloackchainAddress,
blockchainAccount,
ranking
)
NT.projects.network.globals.memory.maps.USER_PROFILES_BY_ID.set(userProfileId, userProfile)
NT.projects.network.globals.memory.maps.USER_PROFILES_BY_HANDLE.set(userHandle, userProfile)
NT.projects.network.globals.memory.maps.USER_PROFILES_BY_BLOCHAIN_ACCOUNT.set(blockchainAccount, userProfile)
}
}