Skip to content

Commit 17c453a

Browse files
committed
doc: fix echo example programs
Adjust to work with self-signed certificates, and certificates that do not name "localhost" as their host name. Removed duplicate examples, they differed only by using `pfx`. Its not necessary to show every option, and we don't, and the example wouldn't work with most pfx anyway, since it didn't specify a password.
1 parent 8884a98 commit 17c453a

File tree

1 file changed

+12
-65
lines changed

1 file changed

+12
-65
lines changed

doc/api/tls.md

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -934,49 +934,24 @@ The `callback` function, if specified, will be added as a listener for the
934934

935935
`tls.connect()` returns a [`tls.TLSSocket`][] object.
936936

937-
Here is an example of a client of echo server as described in
937+
The following illustrates a client for the echo server example from
938938
[`tls.createServer()`][]:
939939

940940
```js
941-
// This example assumes that you have created an echo server that is
942-
// listening on port 8000.
941+
// Assumes an echo server that is listening on port 8000.
943942
const tls = require('tls');
944943
const fs = require('fs');
945944

946945
const options = {
947-
// Necessary only if using the client certificate authentication
946+
// Necessary only if the server requires client certificate authentication.
948947
key: fs.readFileSync('client-key.pem'),
949948
cert: fs.readFileSync('client-cert.pem'),
950949

951-
// Necessary only if the server uses the self-signed certificate
952-
ca: [ fs.readFileSync('server-cert.pem') ]
953-
};
950+
// Necessary only if the server uses a self-signed certificate.
951+
ca: [ fs.readFileSync('server-cert.pem') ],
954952

955-
const socket = tls.connect(8000, options, () => {
956-
console.log('client connected',
957-
socket.authorized ? 'authorized' : 'unauthorized');
958-
process.stdin.pipe(socket);
959-
process.stdin.resume();
960-
});
961-
socket.setEncoding('utf8');
962-
socket.on('data', (data) => {
963-
console.log(data);
964-
});
965-
socket.on('end', () => {
966-
console.log('client ends');
967-
});
968-
```
969-
970-
Or
971-
972-
```js
973-
// This example assumes that you have created an echo server that is
974-
// listening on port 8000.
975-
const tls = require('tls');
976-
const fs = require('fs');
977-
978-
const options = {
979-
pfx: fs.readFileSync('client.pfx')
953+
// Necessary only if the server's cert isn't for "localhost".
954+
checkServerIdentity: () => { return null; },
980955
};
981956

982957
const socket = tls.connect(8000, options, () => {
@@ -990,7 +965,7 @@ socket.on('data', (data) => {
990965
console.log(data);
991966
});
992967
socket.on('end', () => {
993-
console.log('client ends');
968+
console.log('server ends connection');
994969
});
995970
```
996971

@@ -1213,10 +1188,10 @@ const options = {
12131188
key: fs.readFileSync('server-key.pem'),
12141189
cert: fs.readFileSync('server-cert.pem'),
12151190

1216-
// This is necessary only if using the client certificate authentication.
1191+
// This is necessary only if using client certificate authentication.
12171192
requestCert: true,
12181193

1219-
// This is necessary only if the client uses the self-signed certificate.
1194+
// This is necessary only if the client uses a self-signed certificate.
12201195
ca: [ fs.readFileSync('client-cert.pem') ]
12211196
};
12221197

@@ -1232,36 +1207,8 @@ server.listen(8000, () => {
12321207
});
12331208
```
12341209

1235-
Or
1236-
1237-
```js
1238-
const tls = require('tls');
1239-
const fs = require('fs');
1240-
1241-
const options = {
1242-
pfx: fs.readFileSync('server.pfx'),
1243-
1244-
// This is necessary only if using the client certificate authentication.
1245-
requestCert: true,
1246-
};
1247-
1248-
const server = tls.createServer(options, (socket) => {
1249-
console.log('server connected',
1250-
socket.authorized ? 'authorized' : 'unauthorized');
1251-
socket.write('welcome!\n');
1252-
socket.setEncoding('utf8');
1253-
socket.pipe(socket);
1254-
});
1255-
server.listen(8000, () => {
1256-
console.log('server bound');
1257-
});
1258-
```
1259-
1260-
This server can be tested by connecting to it using `openssl s_client`:
1261-
1262-
```sh
1263-
openssl s_client -connect 127.0.0.1:8000
1264-
```
1210+
The server can be tested by connecting to it using the example client from
1211+
[`tls.connect()`][].
12651212

12661213
## tls.getCiphers()
12671214
<!-- YAML

0 commit comments

Comments
 (0)