Skip to content

Commit 39382ed

Browse files
sam-githubBridgeAR
authored andcommitted
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. PR-URL: #24235 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
1 parent a2c13fa commit 39382ed

File tree

1 file changed

+12
-65
lines changed

1 file changed

+12
-65
lines changed

doc/api/tls.md

+12-65
Original file line numberDiff line numberDiff line change
@@ -938,49 +938,24 @@ The `callback` function, if specified, will be added as a listener for the
938938

939939
`tls.connect()` returns a [`tls.TLSSocket`][] object.
940940

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

944944
```js
945-
// This example assumes that you have created an echo server that is
946-
// listening on port 8000.
945+
// Assumes an echo server that is listening on port 8000.
947946
const tls = require('tls');
948947
const fs = require('fs');
949948

950949
const options = {
951-
// Necessary only if using the client certificate authentication
950+
// Necessary only if the server requires client certificate authentication.
952951
key: fs.readFileSync('client-key.pem'),
953952
cert: fs.readFileSync('client-cert.pem'),
954953

955-
// Necessary only if the server uses the self-signed certificate
956-
ca: [ fs.readFileSync('server-cert.pem') ]
957-
};
954+
// Necessary only if the server uses a self-signed certificate.
955+
ca: [ fs.readFileSync('server-cert.pem') ],
958956

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

986961
const socket = tls.connect(8000, options, () => {
@@ -994,7 +969,7 @@ socket.on('data', (data) => {
994969
console.log(data);
995970
});
996971
socket.on('end', () => {
997-
console.log('client ends');
972+
console.log('server ends connection');
998973
});
999974
```
1000975

@@ -1217,10 +1192,10 @@ const options = {
12171192
key: fs.readFileSync('server-key.pem'),
12181193
cert: fs.readFileSync('server-cert.pem'),
12191194

1220-
// This is necessary only if using the client certificate authentication.
1195+
// This is necessary only if using client certificate authentication.
12211196
requestCert: true,
12221197

1223-
// This is necessary only if the client uses the self-signed certificate.
1198+
// This is necessary only if the client uses a self-signed certificate.
12241199
ca: [ fs.readFileSync('client-cert.pem') ]
12251200
};
12261201

@@ -1236,36 +1211,8 @@ server.listen(8000, () => {
12361211
});
12371212
```
12381213

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

12701217
## tls.getCiphers()
12711218
<!-- YAML

0 commit comments

Comments
 (0)