wormhole/lib/handshake.js

23 lines
805 B
JavaScript
Raw Normal View History

2021-01-26 12:04:53 +02:00
const crypto = require('crypto');
const constants = require('./constants');
2021-01-26 12:04:53 +02:00
const generateWebsocketAcceptValue = (websocketKey) => {
if (typeof websocketKey !== 'string' || typeof constants.handshakeGUID !== 'string') {
2021-01-26 12:04:53 +02:00
// TODO: maybe throw error?
return;
}
const concatenated = websocketKey + constants.handshakeGUID;
2021-02-03 02:26:48 +02:00
const sha1HashInBase64 = crypto.createHash('sha1').update(concatenated, 'binary').digest('base64');
2021-01-26 12:04:53 +02:00
2021-02-03 02:26:48 +02:00
return sha1HashInBase64;
2021-01-26 12:04:53 +02:00
};
const validateHeaders = ({ upgradeHeader, websocketVersion }) => {
if (upgradeHeader !== constants.upgradeHeaderRequirement) return false;
if (websocketVersion !== constants.websocketVersionRequirement) return false;
return true;
};
module.exports = { generateWebsocketAcceptValue, validateHeaders };