@@ -923,49 +923,24 @@ The `callback` function, if specified, will be added as a listener for the
923
923
924
924
` tls.connect() ` returns a [ ` tls.TLSSocket ` ] [ ] object.
925
925
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
927
927
[ ` tls.createServer() ` ] [ ] :
928
928
929
929
``` 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.
932
931
const tls = require (' tls' );
933
932
const fs = require (' fs' );
934
933
935
934
const options = {
936
- // Necessary only if using the client certificate authentication
935
+ // Necessary only if the server requires client certificate authentication.
937
936
key: fs .readFileSync (' client-key.pem' ),
938
937
cert: fs .readFileSync (' client-cert.pem' ),
939
938
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' ) ],
943
941
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 ; },
969
944
};
970
945
971
946
const socket = tls .connect (8000 , options, () => {
@@ -979,7 +954,7 @@ socket.on('data', (data) => {
979
954
console .log (data);
980
955
});
981
956
socket .on (' end' , () => {
982
- console .log (' client ends' );
957
+ console .log (' server ends connection ' );
983
958
});
984
959
```
985
960
@@ -1198,10 +1173,10 @@ const options = {
1198
1173
key: fs .readFileSync (' server-key.pem' ),
1199
1174
cert: fs .readFileSync (' server-cert.pem' ),
1200
1175
1201
- // This is necessary only if using the client certificate authentication.
1176
+ // This is necessary only if using client certificate authentication.
1202
1177
requestCert: true ,
1203
1178
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.
1205
1180
ca: [ fs .readFileSync (' client-cert.pem' ) ]
1206
1181
};
1207
1182
@@ -1217,36 +1192,8 @@ server.listen(8000, () => {
1217
1192
});
1218
1193
```
1219
1194
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() ` ] [ ] .
1250
1197
1251
1198
## tls.getCiphers()
1252
1199
<!-- YAML
0 commit comments