token-server
TLS encrypted communication
This module simplifies SSL encrypted communication. It allows you to send messages (called tokens) from a client to a server and back.
Example
This example shows how a client and a server can be connected using self-signed certificates.
For some help on how to generate self-signed certificates see this comment.
const TokenServer TokenClient = /* ### Server ### */ const server = host: 'localhost' port: 8090 key: fs cert: fs ca: fs requestCert: true // ask for a client cert // Listen for tokens from the client.server /* ### Client ### */ const client = host: 'localhost' port: 8090 key: fs cert: fs ca: fs client const token = Bufferclient
API
Table of Contents
TokenClient
This class represents a client that can connect to a server.
TokenClient constructor
- options
- port
<number>
The port that the client should connect to. - host
<string>
The hostname of the server. - key
<string|Buffer>
The private key of the client. - cert
<string|Buffer>
The SSL certificate of the client. - ca
<string|Buffer>
The authority certificate (used for self signed certificates) - All other options accepted by tls.connect()
- port
TokenClient: "close"
This event is emitted when the client connection gets closed.
client.on'close',
- hadError
<boolean>
Istrue
if the client connection was closed by an error (e.g. if the server did not respond).
TokenClient: "connect"
This event is emitted when the client is successfully connected to the server.
client.on'connect',
TokenClient: "error"
This event is emitted when a connection error occured. It is always followed by a "close" event.
client.on'error',
- error
<Error>
The error that was emitted by the underlying TLS socket.
TokenClient: "token"
This event is emitted when the server sends a token in reponse to a token that was sent by this client.
client.on'token',
- token
<Buffer>
The token that was received. - connection
<Connection>
see here The connection that sent the token.
TokenClient#close()
This method is used to disconnect the client from the server.
client.close
This method returns true
if the connection was ended.
If the client was already disconnected, the method returns false
.
TokenClient#connect()
This method is used to reconnect the client to the server if the connection was closed.
client.connectdelay
- delay
<number>
(optional) Time in milliseconds to wait before a new connection to the server is created.
Returns true
if a new connection will be created. If the client is already connected, then false
will be returned.
TokenClient#send()
This method allows you to send a token to the server.
Please note that the server has no send()
method.
It can only respond if it receives a token of the client.
client.sendtoken
- token
<Buffer>
The token that should be sent to the server.
TokenServer
This class represents a server that accepts SSL encrypted connections.
TokenServer constructor
- options
- port
<number>
The port that the server should listen to. - host
<string>
The hostname to listen to. - key
<string|Buffer>
The private key of the server. - cert
<string|Buffer>
The SSL certificate of the server. - ca
<string|Buffer>
The authority certificate (used for self signed certificates) - All other options accepted by tls.createServer() and server.listen.
- port
TokenServer: "close"
This event is emitted when the server gets closed.
server.on'close',
- hadError
<boolean>
Istrue
if the server was closed by an error (e.g. if the port was already in use).
TokenServer: "connect"
This event is emitted when the server is successfully listening to the specified port.
server.on'connect',
TokenServer: "error"
This event is emitted when a server error occured. It is always followed by a "close" event.
server.on'error',
- error
<Error>
The error that was emitted by the underlying TLS server.
TokenServer: "token"
This event is emitted when the server receives a token from a client.
The server send a token back to the client by calling connection.send(token)
.
server.on'token',
- token
<Buffer>
The token that was received. - connection
<Connection>
see here The connection that sent the token.
TokenServer#close()
This method is used to disconnect the server. The server will wait for all connections to close before it stops listening to its port.
server.close
This method returns true
if the server was closed.
If the server was already closed, the method returns false
.
TokenServer#connect()
This method is used to reconnect the server to its port. You can use this method if the port was used before und you want to retry to listen to the port.
- delay
<number>
(optional) Time in milliseconds to wait before the server tries to listen to the port.
Returns true
if a new connection will be created. If the server is already connected, then false
will be returned.
Connection
This class is a wrapper for a stream or socket.
It parses all the data that goes through the stream and emits a "token"
event when a token gets sent.
The class can be used on both sides of a duplex stream to send and receive tokens.
Connection constructor
- socket
<net.Socket>
see here A connection that can be created e.g. with net.connect.
Connection: "token"
This event is emitted when a token arrives at the underlying socket.
connection.on'token',
- token
<Buffer>
The token that was received.
Connection#isDead
Is a boolean that is true
if the underlying socket is writable and false
if it is not.
Connection#close()
This method is used to disconnect from the server.
This method returns true
if the connection was ended.
If the client was already disconnected, the method returns false
.
Connection#send()
This method allows you to send a token through the connection.
The method returns true
if the message was written to the underlying socket. It returns false
if the connection is dead.