Documentation Or Examples For ''newsession'' And 'resumesession' Events In Tls Module For Node.js
I am having a hard time finding documentation and examples for the 'newSession' and 'resumeSession' events for the TLS module in node.js. Any links or help in this direction is app
Solution 1:
It's pretty easy: Documentation.
sessionData
is a simple object, which you could JSON.stringify(sessionData)
and save in a Redis Database. Later on, you can JSON.parse(sessionData)
it again and resume the session.
The server itself only needs the session ID (that is sent by the client) to find its session data (if available). If the server can't find the corresponding session data to a session ID, it will start a new session.
/**
* Module dependencies.
*/var tls = require("tls");
/**
* Initialize a new TLS server.
*/var opts = {
cert: fs.readFileSync("./ssl/cert.pem")
, key: fs.readFileSync("./ssl/key.pem")
}
, server = tls.createServer(opts).listen(443);
/**
* TLS session management.
*/var sessions = {};
server.on("newSession", function(sessionId, sessionData) {
sessions[sessionId.toString("hex")] = sessionData;
});
server.on("resumeSession", function(sessionId, callback) {
sessionId = sessionId.toString("hex");
if(sessionId in sessions)
callback(null, sessions[sessionId]); // resume an existing sessionelse
callback(null, null); // new session will be started// you could also emit an error, which wil terminate the connection// callback(new Error("lol wut"));
});
/**
* Request handler.
*/
server.on("request", function(req, res) {
res.end("Hello World");
});
Post a Comment for "Documentation Or Examples For ''newsession'' And 'resumesession' Events In Tls Module For Node.js"