类: tls.Server
This class is a subclass of net.Server
and has the same methods on it.
Instead of accepting only raw TCP connections, this accepts encrypted
connections using TLS or SSL.
Event: 'tlsClientError'
function (exception, tlsSocket) { }
When a client connection emits an 'error'
event before a secure connection is
established it will be forwarded here.
tlsSocket
is the [tls.TLSSocket
][] that the error originated from.
Event: 'newSession'
function (sessionId, sessionData, callback) { }
Emitted on creation of a TLS session. May be used to store sessions in external
storage. callback
must be invoked eventually, otherwise no data will be
sent or received from the secure connection.
NOTE: adding this event listener will only have an effect on connections established after the addition of the event listener.
Event: 'OCSPRequest'
function (certificate, issuer, callback) { }
Emitted when the client sends a certificate status request. The server's
current certificate can be parsed to obtain the OCSP URL and certificate ID;
after obtaining an OCSP response callback(null, resp)
is then invoked, where
resp
is a Buffer
instance. Both certificate
and issuer
are Buffer
DER-representations of the primary and issuer's certificates. They can be used
to obtain the OCSP certificate ID and OCSP endpoint URL.
Alternatively, callback(null, null)
may be called, meaning that there was no
OCSP response.
Calling callback(err)
will result in a socket.destroy(err)
call.
Typical flow:
- Client connects to the server and sends an
'OCSPRequest'
to it (via status info extension in ClientHello). - Server receives the request and invokes the
'OCSPRequest'
event listener if present. - Server extracts the OCSP URL from either the
certificate
orissuer
and performs an [OCSP request] to the CA. - Server receives
OCSPResponse
from the CA and sends it back to the client via thecallback
argument - Client validates the response and either destroys the socket or performs a handshake.
NOTE: issuer
could be null
if the certificate is self-signed or if the
issuer is not in the root certificates list. (An issuer may be provided via the
ca
option.)
NOTE: adding this event listener will only have an effect on connections established after the addition of the event listener.
NOTE: An npm module like [asn1.js] may be used to parse the certificates.
Event: 'resumeSession'
function (sessionId, callback) { }
Emitted when the client wants to resume the previous TLS session. The event
listener may perform a lookup in external storage using the given sessionId
and invoke callback(null, sessionData)
once finished. If the session can't be
resumed (i.e., doesn't exist in storage) one may call callback(null, null)
.
Calling callback(err)
will terminate incoming connection and destroy the
socket.
NOTE: adding this event listener will only have an effect on connections established after the addition of the event listener.
Here's an example for using TLS session resumption:
var tlsSessionStore = {};
server.on('newSession', (id, data, cb) => {
tlsSessionStore[id.toString('hex')] = data;
cb();
});
server.on('resumeSession', (id, cb) => {
cb(null, tlsSessionStore[id.toString('hex')] || null);
});
Event: 'secureConnection'
function (tlsSocket) {}
This event is emitted after the handshaking process for a new connection has
successfully completed. The argument is an instance of [tls.TLSSocket
][] and
has all the common stream methods and events.
socket.authorized
is a boolean value which indicates if the
client has been verified by one of the supplied certificate authorities for the
server. If socket.authorized
is false, then socket.authorizationError
is
set to describe how authorization failed. Implied but worth mentioning:
depending on the settings of the TLS server, unauthorized connections may
be accepted.
socket.npnProtocol
is a string containing the selected NPN protocol
and socket.alpnProtocol
is a string containing the selected ALPN
protocol. When both NPN and ALPN extensions are received, ALPN takes
precedence over NPN and the next protocol is selected by ALPN. When
ALPN has no selected protocol, this returns false.
socket.servername
is a string containing the server name requested with
SNI.
server.addContext(hostname, context)
Add secure context that will be used if the client request's SNI hostname
matches the supplied hostname
(wildcards can be used). context
can contain
key
, cert
, ca
or any other properties from
[tls.createSecureContext()
][] options
argument.
server.address()
Returns the bound address, the address family name, and port of the
server as reported by the operating system. See [net.Server.address()
][] for
more information.
server.close([callback])
Stops the server from accepting new connections. This function is
asynchronous, the server is finally closed when the server emits a 'close'
event. Optionally, you can pass a callback to listen for the 'close'
event.
server.connections
The number of concurrent connections on the server.
server.getTicketKeys()
Returns a Buffer
instance holding the keys currently used for
encryption/decryption of the [TLS Session Tickets][]
server.listen(port[, hostname][, callback])
Begin accepting connections on the specified port
and hostname
. If the
hostname
is omitted, the server will accept connections on any IPv6 address
(::
) when IPv6 is available, or any IPv4 address (0.0.0.0
) otherwise. A
port value of zero will assign a random port.
This function is asynchronous. The last parameter callback
will be called
when the server has been bound.
See net.Server
for more information.
server.setTicketKeys(keys)
Updates the keys for encryption/decryption of the [TLS Session Tickets][].
NOTE: the buffer should be 48 bytes long. See ticketKeys
option in
tls.createServer for
more information on how it is used.
NOTE: the change is effective only for future server connections. Existing or currently pending server connections will use the previous keys.
server.maxConnections
Set this property to reject connections when the server's connection count exceeds the specified threshold.