docs: improvement tls example
This commit is contained in:
parent
5ca3dcd127
commit
cdec7e3ae5
@ -74,15 +74,20 @@ Here is a simple example echo server:
|
|||||||
var options = {
|
var options = {
|
||||||
key: fs.readFileSync('server-key.pem'),
|
key: fs.readFileSync('server-key.pem'),
|
||||||
cert: fs.readFileSync('server-cert.pem'),
|
cert: fs.readFileSync('server-cert.pem'),
|
||||||
|
|
||||||
|
// This is necessary only if using the client certificate authentication.
|
||||||
requestCert: true,
|
requestCert: true,
|
||||||
|
|
||||||
|
// This is necessary only if the client uses the self-signed certificate.
|
||||||
ca: [ fs.readFileSync('client-cert.pem') ]
|
ca: [ fs.readFileSync('client-cert.pem') ]
|
||||||
};
|
};
|
||||||
|
|
||||||
var server = tls.createServer(options, function (s) {
|
var server = tls.createServer(options, function(cleartextStream) {
|
||||||
console.log('server connected',
|
console.log('server connected',
|
||||||
s.authorized ? 'authorized' : 'not authorized');
|
cleartextStream.authorized ? 'authorized' : 'unauthorized');
|
||||||
s.write("welcome!\n");
|
cleartextStream.write("welcome!\n");
|
||||||
s.pipe(s);
|
cleartextStream.setEncoding('utf8');
|
||||||
|
cleartextStream.pipe(cleartextStream);
|
||||||
});
|
});
|
||||||
server.listen(8000, function() {
|
server.listen(8000, function() {
|
||||||
console.log('server bound');
|
console.log('server bound');
|
||||||
@ -100,10 +105,10 @@ You can test this server by connecting to it with `openssl s_client`:
|
|||||||
Creates a new client connection to the given `port` and `host`. (If `host`
|
Creates a new client connection to the given `port` and `host`. (If `host`
|
||||||
defaults to `localhost`.) `options` should be an object which specifies
|
defaults to `localhost`.) `options` should be an object which specifies
|
||||||
|
|
||||||
- `key`: A string or `Buffer` containing the private key of the server in
|
- `key`: A string or `Buffer` containing the private key of the client in
|
||||||
PEM format. (Required)
|
PEM format. (Required)
|
||||||
|
|
||||||
- `cert`: A string or `Buffer` containing the certificate key of the server in
|
- `cert`: A string or `Buffer` containing the certificate key of the client in
|
||||||
PEM format.
|
PEM format.
|
||||||
|
|
||||||
- `ca`: An array of strings or `Buffer`s of trusted certificates. If this is
|
- `ca`: An array of strings or `Buffer`s of trusted certificates. If this is
|
||||||
@ -111,9 +116,9 @@ defaults to `localhost`.) `options` should be an object which specifies
|
|||||||
These are used to authorize connections.
|
These are used to authorize connections.
|
||||||
|
|
||||||
- `NPNProtocols`: An array of string or `Buffer` containing supported NPN
|
- `NPNProtocols`: An array of string or `Buffer` containing supported NPN
|
||||||
protocols. `Buffer` should have following format: `0x05hello0x05world`, where
|
protocols. `Buffer` should have following format: `0x05hello0x05world`,
|
||||||
first byte is next protocol name's length. (Passing array should usually be
|
where first byte is next protocol name's length. (Passing array should
|
||||||
much simplier: `['hello', 'world']`.)
|
usually be much simplier: `['hello', 'world']`.)
|
||||||
|
|
||||||
- `servername`: Servername for SNI (Server Name Indication) TLS extension.
|
- `servername`: Servername for SNI (Server Name Indication) TLS extension.
|
||||||
|
|
||||||
@ -128,18 +133,26 @@ Here is an example of a client of echo server as described previously:
|
|||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
|
// These are necessary only if using the client certificate authentication
|
||||||
key: fs.readFileSync('client-key.pem'),
|
key: fs.readFileSync('client-key.pem'),
|
||||||
cert: fs.readFileSync('client-cert.pem'),
|
cert: fs.readFileSync('client-cert.pem'),
|
||||||
|
|
||||||
|
// This is necessary only if the server uses the self-signed certificate
|
||||||
ca: [ fs.readFileSync('server-cert.pem') ]
|
ca: [ fs.readFileSync('server-cert.pem') ]
|
||||||
};
|
};
|
||||||
|
|
||||||
var client = tls.connect(8000, options, function() {
|
var cleartextStream = tls.connect(8000, options, function() {
|
||||||
console.log('client connected',
|
console.log('client connected',
|
||||||
client.authorized ? 'authorized' : 'not authorized');
|
cleartextStream.authorized ? 'authorized' : 'unauthorized');
|
||||||
client.write("Hello, World!\n");
|
process.stdin.pipe(cleartextStream);
|
||||||
|
process.stdin.resume();
|
||||||
});
|
});
|
||||||
client.on('data', function(data) {
|
cleartextStream.setEncoding('utf8');
|
||||||
console.log(data.toString());
|
cleartextStream.on('data', function(data) {
|
||||||
|
console.log(data);
|
||||||
|
});
|
||||||
|
cleartextStream.on('end', function() {
|
||||||
|
server.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user