Skip to content

Commit a9a6cb1

Browse files
sam-githubcodebytere
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent 35d2397 commit a9a6cb1

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
@@ -923,49 +923,24 @@ The `callback` function, if specified, will be added as a listener for the
923923

924924
`tls.connect()` returns a [`tls.TLSSocket`][] object.
925925

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

929929
```js
930-
// This example assumes that you have created an echo server that is
931-
// listening on port 8000.
930+
// Assumes an echo server that is listening on port 8000.
932931
const tls = require('tls');
933932
const fs = require('fs');
934933

935934
const options = {
936-
// Necessary only if using the client certificate authentication
935+
// Necessary only if the server requires client certificate authentication.
937936
key: fs.readFileSync('client-key.pem'),
938937
cert: fs.readFileSync('client-cert.pem'),
939938

940-
// Necessary only if the server uses the self-signed certificate
941-
ca: [ fs.readFileSync('server-cert.pem') ]
942-
};
939+
// Necessary only if the server uses a self-signed certificate.
940+
ca: [ fs.readFileSync('server-cert.pem') ],
943941

944-
const socket = tls.connect(8000, options, () => {
945-
console.log('client connected',
946-
socket.authorized ? 'authorized' : 'unauthorized');
947-
process.stdin.pipe(socket);
948-
process.stdin.resume();
949-
});
950-
socket.setEncoding('utf8');
951-
socket.on('data', (data) => {
952-
console.log(data);
953-
});
954-
socket.on('end', () => {
955-
console.log('client ends');
956-
});
957-
```
958-
959-
Or
960-
961-
```js
962-
// This example assumes that you have created an echo server that is
963-
// listening on port 8000.
964-
const tls = require('tls');
965-
const fs = require('fs');
966-
967-
const options = {
968-
pfx: fs.readFileSync('client.pfx')
942+
// Necessary only if the server's cert isn't for "localhost".
943+
checkServerIdentity: () => { return null; },
969944
};
970945

971946
const socket = tls.connect(8000, options, () => {
@@ -979,7 +954,7 @@ socket.on('data', (data) => {
979954
console.log(data);
980955
});
981956
socket.on('end', () => {
982-
console.log('client ends');
957+
console.log('server ends connection');
983958
});
984959
```
985960

@@ -1198,10 +1173,10 @@ const options = {
11981173
key: fs.readFileSync('server-key.pem'),
11991174
cert: fs.readFileSync('server-cert.pem'),
12001175

1201-
// This is necessary only if using the client certificate authentication.
1176+
// This is necessary only if using client certificate authentication.
12021177
requestCert: true,
12031178

1204-
// This is necessary only if the client uses the self-signed certificate.
1179+
// This is necessary only if the client uses a self-signed certificate.
12051180
ca: [ fs.readFileSync('client-cert.pem') ]
12061181
};
12071182

@@ -1217,36 +1192,8 @@ server.listen(8000, () => {
12171192
});
12181193
```
12191194

1220-
Or
1221-
1222-
```js
1223-
const tls = require('tls');
1224-
const fs = require('fs');
1225-
1226-
const options = {
1227-
pfx: fs.readFileSync('server.pfx'),
1228-
1229-
// This is necessary only if using the client certificate authentication.
1230-
requestCert: true,
1231-
};
1232-
1233-
const server = tls.createServer(options, (socket) => {
1234-
console.log('server connected',
1235-
socket.authorized ? 'authorized' : 'unauthorized');
1236-
socket.write('welcome!\n');
1237-
socket.setEncoding('utf8');
1238-
socket.pipe(socket);
1239-
});
1240-
server.listen(8000, () => {
1241-
console.log('server bound');
1242-
});
1243-
```
1244-
1245-
This server can be tested by connecting to it using `openssl s_client`:
1246-
1247-
```sh
1248-
openssl s_client -connect 127.0.0.1:8000
1249-
```
1195+
The server can be tested by connecting to it using the example client from
1196+
[`tls.connect()`][].
12501197

12511198
## tls.getCiphers()
12521199
<!-- YAML

0 commit comments

Comments
 (0)