@ohos-ports/rai 0.0.0-stage → 0.1.12-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.travis.yml +12 -0
- package/LICENSE +16 -0
- package/README.md +161 -2
- package/cert/server.crt +18 -0
- package/cert/server.key +28 -0
- package/lib/mockup.js +126 -0
- package/lib/rai.js +479 -0
- package/lib/starttls.js +85 -0
- package/package.json +38 -4
package/.travis.yml
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Copyright (c) 2012 Andris Reinman
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
11
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
12
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
13
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
14
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
15
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
16
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,162 @@
|
|
|
1
|
-
#
|
|
1
|
+
# RAI - Request-Answer-Interface
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**rai** is a node.js module to easily generate text based command line servers.
|
|
4
|
+
When a client sends something to the server, the first word of the line is
|
|
5
|
+
treated as a command and the rest of the line as binary payload.
|
|
6
|
+
|
|
7
|
+
[](http://travis-ci.org/andris9/rai)
|
|
8
|
+
|
|
9
|
+
In addition to line based commands, there's also a data mode, to transmit
|
|
10
|
+
everygting received. And there's also an option to switch to TLS mode for
|
|
11
|
+
secure connections.
|
|
12
|
+
|
|
13
|
+
This way it is trivial to create SMTP, POP3 or similar servers.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
npm install rai
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Simple server
|
|
22
|
+
|
|
23
|
+
var RAIServer = require("rai").RAIServer;
|
|
24
|
+
|
|
25
|
+
// create a RAIServer on port 1234
|
|
26
|
+
var server = new RAIServer();
|
|
27
|
+
server.listen(1234);
|
|
28
|
+
|
|
29
|
+
// Start listening for client connections
|
|
30
|
+
server.on("connect", function(client){
|
|
31
|
+
|
|
32
|
+
// Greet the client
|
|
33
|
+
client.send("Hello!");
|
|
34
|
+
|
|
35
|
+
// Wait for a command
|
|
36
|
+
client.on("command", function(command, payload){
|
|
37
|
+
|
|
38
|
+
if(command == "STATUS"){
|
|
39
|
+
client.send("Status is OK!");
|
|
40
|
+
}else if(command == "QUIT"){
|
|
41
|
+
client.send("Goodbye");
|
|
42
|
+
client.end();
|
|
43
|
+
}else{
|
|
44
|
+
client.send("Unknown command");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
Server only emits `'connect'` and `'error'` events, while the client
|
|
52
|
+
objects emit `'timeout'`, `'error'` and `'end'` in addition to data
|
|
53
|
+
related events.
|
|
54
|
+
|
|
55
|
+
### Starting a server
|
|
56
|
+
|
|
57
|
+
Server can be started with `new RAIServer([options])` where options is an optional
|
|
58
|
+
parameters object with the following properties:
|
|
59
|
+
|
|
60
|
+
* **debug** - if set to true print traffic to console
|
|
61
|
+
* **disconnectOnTimeout** - if set to true close the connection on disconnect
|
|
62
|
+
* **secureConnection** - if set to true close the connection on disconnect
|
|
63
|
+
* **credentials** - credentials for secureConnection and STARTTLS
|
|
64
|
+
* **timeout** - timeout in milliseconds for disconnecting the client, defaults to 0 (no timeout)
|
|
65
|
+
|
|
66
|
+
Once the server has been set up, it can start listening for client connections
|
|
67
|
+
with `server.listen(port[, hostname][, callback])`. Callback function gets an error
|
|
68
|
+
object as a parameter if the listening failed.
|
|
69
|
+
|
|
70
|
+
var server = new RAIServer();
|
|
71
|
+
server.listen(25); // start listening for port 25 on "localhost"
|
|
72
|
+
|
|
73
|
+
### Closing server
|
|
74
|
+
|
|
75
|
+
Server can be closed with `server.end([callback])` where callback is run when
|
|
76
|
+
the server is finally closed.
|
|
77
|
+
|
|
78
|
+
### Sending data
|
|
79
|
+
|
|
80
|
+
Data can be sent with `client.send(data)` where `data` is either a String or
|
|
81
|
+
a Buffer. `"\r\n"` is automatically appended to the data.
|
|
82
|
+
|
|
83
|
+
client.send("Greetings!");
|
|
84
|
+
|
|
85
|
+
### Forcing connection close
|
|
86
|
+
|
|
87
|
+
Connections can be ended with `client.end()`
|
|
88
|
+
|
|
89
|
+
if(command == "QUIT"){
|
|
90
|
+
client.send("Good bye!");
|
|
91
|
+
client.end();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
### TLS mode
|
|
95
|
+
|
|
96
|
+
TLS can be switched on with `client.startTLS([credentials][, callback])` and the status can
|
|
97
|
+
be listened with `'tls'` (emitted when secure connection is established)
|
|
98
|
+
|
|
99
|
+
`credentials` is an object with strings of pem encoded `key`, `cert` and optionally an
|
|
100
|
+
array `ca`. If `credentials` is not supplied, an autogenerated value is used.
|
|
101
|
+
|
|
102
|
+
if(command == "STARTTLS"){
|
|
103
|
+
client.startTLS();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
client.on("tls", function(){
|
|
107
|
+
console.log("Switched to secure connection");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
If `callback` is not set `'tls'` will be emitted on connection upgrade.
|
|
111
|
+
|
|
112
|
+
### Data mode
|
|
113
|
+
|
|
114
|
+
Data mode can be turned on with `client.startDataMode([endSequence])` and incoming
|
|
115
|
+
chunks can be received with `'data'`. The end of data mode can be detected by
|
|
116
|
+
`'ready'`.
|
|
117
|
+
|
|
118
|
+
`endSequence` is a String for matching the end (entire line) of the data stream.
|
|
119
|
+
By default it's `"."` which is suitable for SMTP and POP3.
|
|
120
|
+
|
|
121
|
+
if(command == "DATA"){
|
|
122
|
+
client.send("End data with <CR><LF>.<CR><LF>");
|
|
123
|
+
client.startDataMode();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
client.on("data", function(chunk){
|
|
127
|
+
console.log("Data from client:", chunk);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
client.on("ready", function(){
|
|
131
|
+
client.send("Data received");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
## Testing
|
|
135
|
+
|
|
136
|
+
There is a possibility to set up a mockup client which sends a batch of commands
|
|
137
|
+
one by one to the server and returns the last response and an array of all responses(except the TLS negotiation).
|
|
138
|
+
|
|
139
|
+
var runClientMockup = require("rai").runClientMockup;
|
|
140
|
+
|
|
141
|
+
var cmds = ["EHLO FOOBAR", "STARTTLS", "QUIT"];
|
|
142
|
+
runClientMockup(25, "mail.hot.ee", cmds, function(lastResponse, allResponses){
|
|
143
|
+
console.log("Final:", lastResponse.toString("utf-8").trim());
|
|
144
|
+
console.log("All:", allResponses.map(function(e){
|
|
145
|
+
return e.toString("utf-8").trim()
|
|
146
|
+
}).join(', '));
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
`runClientMockup` has he following parameters in the following order:
|
|
150
|
+
|
|
151
|
+
* **port** - Port number
|
|
152
|
+
* **host** - Hostname to connect to
|
|
153
|
+
* **commands** - Command list (an array) to be sent to server
|
|
154
|
+
* **callback** - Callback function to run on completion
|
|
155
|
+
* **debug** - if set to true log all input/output
|
|
156
|
+
|
|
157
|
+
Response from the callback function is a Buffer and contains the
|
|
158
|
+
last data received from the server and an array of Buffers with all data received from the server.
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
**MIT**
|
package/cert/server.crt
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
-----BEGIN CERTIFICATE-----
|
|
2
|
+
MIIC6DCCAdCgAwIBAgIUTSE76t13dPcLSzSYDkGB8WRI1JowDQYJKoZIhvcNAQEL
|
|
3
|
+
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI2MTEyODAyMzkzNFoXDTQ2MTEy
|
|
4
|
+
MzAyMzkzNFowFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF
|
|
5
|
+
AAOCAQ8AMIIBCgKCAQEA0at3/fsSmK6MwgUyXb7rM8SXc3ndDwgRaYYCcWh9Nixb
|
|
6
|
+
1G7VnuLkTZlKjhbhZtdiCoC5DqDE3vDx8cAAaDL9xabWUBsBFC8z2LMZNdez1ppm
|
|
7
|
+
l5iKh4o8IKJyA/OgQdkX5fX3/x4rfBWBUJSkFdQMT2Vh0lXAzvCvK/U+gEINEnZ4
|
|
8
|
+
X5qQPx/5RAxp9Iwlckdg/6waaca6jp9XIFYtwgusV4ywGfNcW5gLPv3fI9N/G0y/
|
|
9
|
+
CrFttaRthZ1/JxJADBXpijAWudNGPgxivWVvlSoHeNszvIu1ng5ooRps/Z7VGBm/
|
|
10
|
+
PIKSlrrykWajnt9euyJ5q1kNFRTpbnd8n/tn8G98wQIDAQABozIwMDAdBgNVHQ4E
|
|
11
|
+
FgQUdRoDLNsfTz5QIulX/EY26s6j+JowDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG
|
|
12
|
+
9w0BAQsFAAOCAQEAdFGEoZSS7xCnmOGc3EEY+1/rEZqa1i6E2yFrUKwQynijI3p2
|
|
13
|
+
N/45+Z1v9XjRZo7Py50CE7LJ2yaPMrorVYfdDbP/0nz2NTINX/2TNyJCqSdJFyxw
|
|
14
|
+
Yqw5SgbcxTt+pYcegdDXpsRIss3s7j+YU9MscoFb+1flCEhES1O5fC0bVtEhQoq6
|
|
15
|
+
a5IQ2DQ9ND0ngZI265FJ/39aMJ0fA0QJan2mDajBXO/du4pMia+vQAJsEjk9PXtV
|
|
16
|
+
svJZa/FnrW4H51ysS5j6AtfNoW0KtSgkvkdIJ+v4lPi56qLxhfK8aavV0gHuB/D5
|
|
17
|
+
+xe7ERe5LQSKh3in5RnYWZ+w1WQenTlpya02+A==
|
|
18
|
+
-----END CERTIFICATE-----
|
package/cert/server.key
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
-----BEGIN PRIVATE KEY-----
|
|
2
|
+
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDRq3f9+xKYrozC
|
|
3
|
+
BTJdvuszxJdzed0PCBFphgJxaH02LFvUbtWe4uRNmUqOFuFm12IKgLkOoMTe8PHx
|
|
4
|
+
wABoMv3FptZQGwEULzPYsxk117PWmmaXmIqHijwgonID86BB2Rfl9ff/Hit8FYFQ
|
|
5
|
+
lKQV1AxPZWHSVcDO8K8r9T6AQg0SdnhfmpA/H/lEDGn0jCVyR2D/rBppxrqOn1cg
|
|
6
|
+
Vi3CC6xXjLAZ81xbmAs+/d8j038bTL8KsW21pG2FnX8nEkAMFemKMBa500Y+DGK9
|
|
7
|
+
ZW+VKgd42zO8i7WeDmihGmz9ntUYGb88gpKWuvKRZqOe3167InmrWQ0VFOlud3yf
|
|
8
|
+
+2fwb3zBAgMBAAECggEAF5YnAvrWHjfwnLjx8ArUsbepY/OvEwxQzSKmrrRW9Zuq
|
|
9
|
+
3uxy+pNCpJocgXyAcc3D+TN+3e7nBr9rMEttoEswMiZxEIThK7CdsACB0wVHdVpc
|
|
10
|
+
NSyTVOkPl4o+skQbYJz368YuVlRCBwzyRarbBJucoOfiR8Oe04jO4+6MT+heSRe3
|
|
11
|
+
iobP4L3EngKfO7S/FC0q6NM7WqHPLUUX6cpfd1dSwXNQtnLRICtJj1453LBQ9nw3
|
|
12
|
+
G7dnfdmXFXdwnTsEquSZt2njoRur/Ud/3fY/qvaj+kypIbrNuzLA7sinC2jOMgVv
|
|
13
|
+
tQuOTbao6lfGHmXCvDj236K0qe7KN+WCEsIkj/DY4QKBgQDxWk+lo+Z0bJIbBZMm
|
|
14
|
+
6107Lo8SUqbgSXFREl1MTiFyCeB+Z830zBMjT29B+gtnDtQxbUvaRE3cia6BnQfA
|
|
15
|
+
pPyeKk9WOL8FYbGySqgduwadPGz1m8bqnhDUuD0bNtHf4z57eGssNKZe1N1mDhlh
|
|
16
|
+
DqTPUaaghAV2xxzp9wIspsm1CQKBgQDeZO03z//h8YeQdoRdcl33JgZwCk5Ec83i
|
|
17
|
+
iUz0fcQztrn9uQmoY9/kLiLx/vqB5KvjinRg2+KBQJlFtNVSa90vG+ca2u708HE4
|
|
18
|
+
RWLt+sSRxWA3e3OlKyKbIX7t3+BUeg5etlB5Nhqqp2rks6hQoQ9T5wT4GUhjrm33
|
|
19
|
+
kzYx9iPv+QKBgQCZT4LUdx6GygitPYdR66RXWaXsP7BdPInMs9usAt1f8UI3SX0c
|
|
20
|
+
iR2HCohj4o+nsVW85+6NAWJsOtEs6DDqgGMvnTS0UmuU4Y/c72uHJyx2aNHV2lQq
|
|
21
|
+
6KlMVpQKW3JJw7IHAGEjvXYJOzJAB6i7bG/oebdcs0A9SRHm1M7+yhvK8QKBgFDy
|
|
22
|
+
T12XOb3VmdA9MySIMid5EbLKLX7A39lqvDXpaPahtcTiCt/dqmp1WIbYlfmdb+sc
|
|
23
|
+
RAvVPPZqut8iWNrtI863y0B9G6jPs4YVy1Jvigi83K6VIpylvKzWXkrax943pu9W
|
|
24
|
+
UKgtxzsCQOgJdGQUm22gkrBzE2Qk4yXP0VQSETaRAoGBALZDwqutMaOby+M1vw3s
|
|
25
|
+
uM01g1pc9QQKpZtfcYjOhtruDXp7rA9CX8Pya3DAVu54MPRgUKB6DZ2IwW7whPqb
|
|
26
|
+
mwLeocXuH1H0bdSE2FjKXM6DYZBia5kQiT1yOKqLZTJFhYBJlN5B1i7wMVixc7YD
|
|
27
|
+
f1jqiUvkrINecrPMAJRuWv+E
|
|
28
|
+
-----END PRIVATE KEY-----
|
package/lib/mockup.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var net = require("net"),
|
|
4
|
+
crypto = require("crypto"),
|
|
5
|
+
tlslib = require("tls");
|
|
6
|
+
|
|
7
|
+
// monkey patch net and tls to support nodejs 0.4
|
|
8
|
+
if(!net.connect && net.createConnection){
|
|
9
|
+
net.connect = net.createConnection;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if(!tlslib.connect && tlslib.createConnection){
|
|
13
|
+
tlslib.connect = tlslib.createConnection;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @namespace Mockup module
|
|
18
|
+
* @name mockup
|
|
19
|
+
*/
|
|
20
|
+
module.exports = runClientMockup;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* <p>Runs a batch of commands against a server</p>
|
|
24
|
+
*
|
|
25
|
+
* <pre>
|
|
26
|
+
* var cmds = ["EHLO FOOBAR", "STARTTLS", "QUIT"];
|
|
27
|
+
* runClientMockup(25, "mail.hot.ee", cmds, function(resp){
|
|
28
|
+
* console.log("Final:", resp.toString("utf-8").trim());
|
|
29
|
+
* });
|
|
30
|
+
* </pre>
|
|
31
|
+
*
|
|
32
|
+
* @memberOf mockup
|
|
33
|
+
* @param {Number} port Port number
|
|
34
|
+
* @param {String} host Hostname to connect to
|
|
35
|
+
* @param {Array} commands Command list to be sent to server
|
|
36
|
+
* @param {Function} callback Callback function to run on completion,
|
|
37
|
+
* has the last response from the server as a param
|
|
38
|
+
* @param {Boolean} [debug] if set to true log all input/output
|
|
39
|
+
*/
|
|
40
|
+
function runClientMockup(port, host, commands, callback, debug){
|
|
41
|
+
host = host || "localhost";
|
|
42
|
+
port = port || 25;
|
|
43
|
+
commands = Array.isArray(commands) ? commands : [];
|
|
44
|
+
|
|
45
|
+
var command, ignore_data = false, responses = [], pair;
|
|
46
|
+
|
|
47
|
+
var socket = net.connect(port, host);
|
|
48
|
+
socket.on("connect", function(){
|
|
49
|
+
socket.on("data", function(chunk){
|
|
50
|
+
if(ignore_data){
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if(debug){
|
|
55
|
+
console.log("S: "+chunk.toString("utf-8").trim());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if(!commands.length){
|
|
59
|
+
socket.end();
|
|
60
|
+
if(typeof callback == "function"){
|
|
61
|
+
responses.push(chunk);
|
|
62
|
+
callback(chunk, responses);
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}else{
|
|
66
|
+
responses.push(chunk);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if(["STARTTLS", "STLS"].indexOf((command || "").trim().toUpperCase())>=0){
|
|
70
|
+
ignore_data = true;
|
|
71
|
+
if(debug){
|
|
72
|
+
console.log("Initiated TLS connection");
|
|
73
|
+
}
|
|
74
|
+
// Upgrade the existing plaintext socket to TLS (client side).
|
|
75
|
+
// crypto.createCredentials/tls.createSecurePair are removed in
|
|
76
|
+
// modern Node.js — use tls.connect({socket: ...}) instead.
|
|
77
|
+
// rejectUnauthorized is disabled to keep the original
|
|
78
|
+
// "do not verify the server certificate" behaviour.
|
|
79
|
+
pair = tlslib.connect({
|
|
80
|
+
socket: socket,
|
|
81
|
+
rejectUnauthorized: false
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
pair.on("secureConnect", function(){
|
|
85
|
+
if(debug){
|
|
86
|
+
console.log("TLS connection secured");
|
|
87
|
+
}
|
|
88
|
+
command = commands.shift();
|
|
89
|
+
if(debug){
|
|
90
|
+
console.log("C: "+command);
|
|
91
|
+
}
|
|
92
|
+
pair.write(command+"\r\n");
|
|
93
|
+
|
|
94
|
+
pair.on("data", function(chunk){
|
|
95
|
+
if(debug){
|
|
96
|
+
console.log("S: "+chunk.toString("utf-8").trim());
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if(!commands.length){
|
|
100
|
+
pair.end();
|
|
101
|
+
if(typeof callback == "function"){
|
|
102
|
+
responses.push(chunk);
|
|
103
|
+
callback(chunk, responses);
|
|
104
|
+
}
|
|
105
|
+
return;
|
|
106
|
+
}else{
|
|
107
|
+
responses.push(chunk);
|
|
108
|
+
}
|
|
109
|
+
command = commands.shift();
|
|
110
|
+
pair.write(command+"\r\n");
|
|
111
|
+
if(debug){
|
|
112
|
+
console.log("C: "+command);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}else{
|
|
117
|
+
command = commands.shift();
|
|
118
|
+
socket.write(command+"\r\n");
|
|
119
|
+
if(debug){
|
|
120
|
+
console.log("C: "+command);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
}
|
package/lib/rai.js
ADDED
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileOverview This is the main file for the RAI library to create text based servers
|
|
5
|
+
* @author <a href="mailto:andris@node.ee">Andris Reinman</a>
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
var netlib = require("net"),
|
|
9
|
+
utillib = require("util"),
|
|
10
|
+
EventEmitter = require('events').EventEmitter,
|
|
11
|
+
starttls = require("./starttls").starttls,
|
|
12
|
+
tlslib = require("tls"),
|
|
13
|
+
fs = require("fs");
|
|
14
|
+
|
|
15
|
+
// Default credentials for starting TLS server
|
|
16
|
+
var defaultCredentials = {
|
|
17
|
+
key: fs.readFileSync(__dirname+"/../cert/server.key"),
|
|
18
|
+
cert: fs.readFileSync(__dirname+"/../cert/server.crt")
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Expose to the world
|
|
22
|
+
module.exports.RAIServer = RAIServer;
|
|
23
|
+
module.exports.runClientMockup = require("./mockup");
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* <p>Creates instance of RAIServer</p>
|
|
27
|
+
*
|
|
28
|
+
* <p>Options object has the following properties:</p>
|
|
29
|
+
*
|
|
30
|
+
* <ul>
|
|
31
|
+
* <li><b>debug</b> - if set to true print traffic to console</li>
|
|
32
|
+
* <li><b>disconnectOnTimeout</b> - if set to true close the connection on disconnect</li>
|
|
33
|
+
* <li><b>secureConnection</b> - if set to true close the connection on disconnect</li>
|
|
34
|
+
* <li><b>credentials</b> - credentials for secureConnection and STARTTLS</li>
|
|
35
|
+
* <li><b>timeout</b> - timeout in milliseconds for disconnecting the client,
|
|
36
|
+
* defaults to 0 (no timeout)</li>
|
|
37
|
+
* </ul>
|
|
38
|
+
*
|
|
39
|
+
* <p><b>Events</b></p>
|
|
40
|
+
*
|
|
41
|
+
* <ul>
|
|
42
|
+
* <li><b>'connect'</b> - emitted if a client connects to the server, param
|
|
43
|
+
* is a client ({@link RAISocket}) object</li>
|
|
44
|
+
* <li><b>'error'</b> - emitted on error, has an error object as a param</li>
|
|
45
|
+
* </ul>
|
|
46
|
+
*
|
|
47
|
+
* @constructor
|
|
48
|
+
* @param {Object} [options] Optional options object
|
|
49
|
+
*/
|
|
50
|
+
function RAIServer(options){
|
|
51
|
+
EventEmitter.call(this);
|
|
52
|
+
|
|
53
|
+
this.options = options || {};
|
|
54
|
+
|
|
55
|
+
if (this.options.debug) {
|
|
56
|
+
this._logger = this.options.debug === true ? console.log : this.options.debug;
|
|
57
|
+
} else {
|
|
58
|
+
this._logger = function(){};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
this._createServer();
|
|
62
|
+
}
|
|
63
|
+
utillib.inherits(RAIServer, EventEmitter);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* <p>Starts listening on selected port</p>
|
|
67
|
+
*
|
|
68
|
+
* @param {Number} port The port to listen
|
|
69
|
+
* @param {String} [host] The IP address to listen
|
|
70
|
+
* @param {Function} callback The callback function to be run after the server
|
|
71
|
+
* is listening, the only param is an error message if the operation failed
|
|
72
|
+
*/
|
|
73
|
+
RAIServer.prototype.listen = function(port, host, callback){
|
|
74
|
+
if(!callback && typeof host=="function"){
|
|
75
|
+
callback = host;
|
|
76
|
+
host = undefined;
|
|
77
|
+
}
|
|
78
|
+
this._port = port;
|
|
79
|
+
this._host = host;
|
|
80
|
+
|
|
81
|
+
this._connected = false;
|
|
82
|
+
if(callback){
|
|
83
|
+
this._server.on("listening", (function(){
|
|
84
|
+
this._connected = true;
|
|
85
|
+
callback(null);
|
|
86
|
+
}).bind(this));
|
|
87
|
+
|
|
88
|
+
this._server.on("error", (function(err){
|
|
89
|
+
if(!this._connected){
|
|
90
|
+
callback(err);
|
|
91
|
+
}
|
|
92
|
+
}).bind(this));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
this._server.listen(this._port, this._host);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* <p>Stops the server</p>
|
|
100
|
+
*
|
|
101
|
+
* @param {Function} callback Is run when the server is closed
|
|
102
|
+
*/
|
|
103
|
+
RAIServer.prototype.end = function(callback){
|
|
104
|
+
this._server.on("close", callback);
|
|
105
|
+
this._server.close();
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* <p>Creates a server with listener callback</p>
|
|
110
|
+
*/
|
|
111
|
+
RAIServer.prototype._createServer = function(){
|
|
112
|
+
if(this.options.secureConnection){
|
|
113
|
+
this._server = tlslib.createServer(
|
|
114
|
+
this.options.credentials || defaultCredentials,
|
|
115
|
+
this._serverListener.bind(this));
|
|
116
|
+
}else{
|
|
117
|
+
this._server = netlib.createServer(this._serverListener.bind(this));
|
|
118
|
+
}
|
|
119
|
+
this._server.on("error", this._onError.bind(this));
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* <p>Listens for errors</p>
|
|
124
|
+
*
|
|
125
|
+
* @event
|
|
126
|
+
* @param {Object} err Error object
|
|
127
|
+
*/
|
|
128
|
+
RAIServer.prototype._onError = function(err){
|
|
129
|
+
if(this._connected){
|
|
130
|
+
this.emit("error", err);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* <p>Server listener that is run on client connection</p>
|
|
136
|
+
*
|
|
137
|
+
* <p>{@link RAISocket} object instance is created based on the client socket
|
|
138
|
+
* and a <code>'connection'</code> event is emitted</p>
|
|
139
|
+
*
|
|
140
|
+
* @param {Object} socket The socket to the client
|
|
141
|
+
*/
|
|
142
|
+
RAIServer.prototype._serverListener = function(socket){
|
|
143
|
+
this._logger("CONNECTION FROM "+socket.remoteAddress);
|
|
144
|
+
|
|
145
|
+
var handler = new RAISocket(socket, this.options);
|
|
146
|
+
|
|
147
|
+
socket.on("data", handler._onReceiveData.bind(handler));
|
|
148
|
+
socket.on("end", handler._onEnd.bind(handler));
|
|
149
|
+
socket.on("error", handler._onError.bind(handler));
|
|
150
|
+
socket.on("timeout", handler._onTimeout.bind(handler));
|
|
151
|
+
socket.on("close", handler._onClose.bind(handler));
|
|
152
|
+
|
|
153
|
+
if("setKeepAlive" in socket){
|
|
154
|
+
socket.setKeepAlive(true); // plaintext server
|
|
155
|
+
}else if(socket.encrypted && "setKeepAlive" in socket.encrypted){
|
|
156
|
+
socket.encrypted.setKeepAlive(true); // secure server
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
this.emit("connect", handler);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* <p>Creates a instance for interacting with a client (socket)</p>
|
|
164
|
+
*
|
|
165
|
+
* <p>Optional options object is the same that is passed to the parent
|
|
166
|
+
* {@link RAIServer} object</p>
|
|
167
|
+
*
|
|
168
|
+
* <p><b>Events</b></p>
|
|
169
|
+
*
|
|
170
|
+
* <ul>
|
|
171
|
+
* <li><b>'command'</b> - emitted if a client sends a command. Gets two
|
|
172
|
+
* params - command (String) and payload (Buffer)</li>
|
|
173
|
+
* <li><b>'data'</b> - emitted when a chunk is received in data mode, the
|
|
174
|
+
* param being the payload (Buffer)</li>
|
|
175
|
+
* <li><b>'ready'</b> - emitted when data stream ends and normal command
|
|
176
|
+
* flow is recovered</li>
|
|
177
|
+
* <li><b>'tls'</b> - emitted when the connection is secured by TLS</li>
|
|
178
|
+
* <li><b>'error'</b> - emitted when an error occurs. Connection to the
|
|
179
|
+
* client is disconnected automatically. Param is an error object.</l>
|
|
180
|
+
* <li><b>'timeout'</b> - emitted when a timeout occurs. Connection to the
|
|
181
|
+
* client is disconnected automatically if disconnectOnTimeout option
|
|
182
|
+
* is set to true.</l>
|
|
183
|
+
* <li><b>'end'</b> - emitted when the client disconnects</l>
|
|
184
|
+
* </ul>
|
|
185
|
+
*
|
|
186
|
+
* @constructor
|
|
187
|
+
* @param {Object} socket Socket for the client
|
|
188
|
+
* @param {Object} [options] Optional options object
|
|
189
|
+
*/
|
|
190
|
+
function RAISocket(socket, options){
|
|
191
|
+
EventEmitter.call(this);
|
|
192
|
+
|
|
193
|
+
this.socket = socket;
|
|
194
|
+
this.options = options || {};
|
|
195
|
+
|
|
196
|
+
if (this.options.debug) {
|
|
197
|
+
this._logger = this.options.debug === true ? console.log : this.options.debug;
|
|
198
|
+
} else {
|
|
199
|
+
this._logger = function(){};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
this.remoteAddress = socket.remoteAddress;
|
|
203
|
+
|
|
204
|
+
this._dataMode = false;
|
|
205
|
+
this._endDataModeSequence = "\r\n.\r\n";
|
|
206
|
+
this._endDataModeSequenceRegEx = /\r\n\.\r\n/;
|
|
207
|
+
|
|
208
|
+
this.secureConnection = !!this.options.secureConnection;
|
|
209
|
+
this._destroyed = false;
|
|
210
|
+
this._remainder = "";
|
|
211
|
+
|
|
212
|
+
this._ignore_data = false;
|
|
213
|
+
|
|
214
|
+
if(this.options.timeout){
|
|
215
|
+
socket.setTimeout(this.options.timeout);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
utillib.inherits(RAISocket, EventEmitter);
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* <p>Sends some data to the client. <code><CR><LF></code> is automatically appended to
|
|
222
|
+
* the data</p>
|
|
223
|
+
*
|
|
224
|
+
* @param {String|Buffer} data Data to be sent to the client
|
|
225
|
+
*/
|
|
226
|
+
RAISocket.prototype.send = function(data){
|
|
227
|
+
var buffer;
|
|
228
|
+
if(Buffer.isBuffer(data)){
|
|
229
|
+
buffer = Buffer.alloc(data.length+2);
|
|
230
|
+
buffer[buffer.length-2] = 0xD;
|
|
231
|
+
buffer[buffer.length-1] = 0xA;
|
|
232
|
+
data.copy(buffer);
|
|
233
|
+
}else{
|
|
234
|
+
buffer = Buffer.from((data || "").toString()+"\r\n", "binary");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
this._logger("OUT: \"" +buffer.toString("utf-8").trim()+"\"");
|
|
238
|
+
|
|
239
|
+
if(this.socket && this.socket.writable){
|
|
240
|
+
this.socket.write(buffer);
|
|
241
|
+
}else{
|
|
242
|
+
this.socket.end();
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* <p>Instructs the server to be listening for mixed data instead of line based
|
|
248
|
+
* commands</p>
|
|
249
|
+
*
|
|
250
|
+
* @param {String} [sequence="."] - optional sequence on separate line for
|
|
251
|
+
* matching the data end
|
|
252
|
+
*/
|
|
253
|
+
RAISocket.prototype.startDataMode = function(sequence){
|
|
254
|
+
this._dataMode = true;
|
|
255
|
+
if(sequence){
|
|
256
|
+
sequence = sequence.replace(/([\.\=\(\)\-\?\*\\\[\]\^\+\:\|\,])/g, "\\$1");
|
|
257
|
+
this._endDataModeSequence = "\r\n"+sequence+"\r\n";
|
|
258
|
+
this._endDataModeSequenceRegEx = new RegExp("\\r\\n" + sequence + "\\r\\n");
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* <p>Instructs the server to upgrade the connection to secure TLS connection</p>
|
|
264
|
+
*
|
|
265
|
+
* <p>Fires <code>callback</code> on successful connection upgrade if set,
|
|
266
|
+
* otherwise emits <code>'tls'</code></p>
|
|
267
|
+
*
|
|
268
|
+
* @param {Object} [credentials] An object with PEM encoded key and
|
|
269
|
+
* certificate <code>{key:"---BEGIN...", cert:"---BEGIN..."}</code>,
|
|
270
|
+
* if not set autogenerated values will be used.
|
|
271
|
+
* @param {Function} [callback] If calback is set fire it after successful connection
|
|
272
|
+
* upgrade, otherwise <code>'tls'</code> is emitted
|
|
273
|
+
*/
|
|
274
|
+
RAISocket.prototype.startTLS = function(credentials, callback){
|
|
275
|
+
|
|
276
|
+
if(this.secureConnection){
|
|
277
|
+
return this._onError(new Error("Secure connection already established"));
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if(!callback && typeof credentials == "function"){
|
|
281
|
+
callback = credentials;
|
|
282
|
+
credentials = undefined;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
credentials = credentials || this.options.credentials || defaultCredentials;
|
|
286
|
+
|
|
287
|
+
this._ignore_data = true;
|
|
288
|
+
|
|
289
|
+
var secure_connector = starttls(this.socket, credentials, (function(ssl_socket){
|
|
290
|
+
|
|
291
|
+
if(!ssl_socket.authorized){
|
|
292
|
+
this._logger("WARNING: TLS ERROR ("+ssl_socket.authorizationError+")");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
this._remainder = "";
|
|
296
|
+
this._ignore_data = false;
|
|
297
|
+
|
|
298
|
+
this.secureConnection = true;
|
|
299
|
+
|
|
300
|
+
this.socket = ssl_socket;
|
|
301
|
+
this.socket.on("data", this._onReceiveData.bind(this));
|
|
302
|
+
this.socket.on("error", this._onError.bind(this));
|
|
303
|
+
|
|
304
|
+
this._logger("TLS CONNECTION STARTED");
|
|
305
|
+
|
|
306
|
+
if(callback){
|
|
307
|
+
callback();
|
|
308
|
+
}else{
|
|
309
|
+
this.emit("tls");
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
}).bind(this));
|
|
313
|
+
|
|
314
|
+
secure_connector.on("error", (function(err){
|
|
315
|
+
this._onError(err);
|
|
316
|
+
}).bind(this));
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* <p>Closes the connection to the client</p>
|
|
321
|
+
*/
|
|
322
|
+
RAISocket.prototype.end = function(){
|
|
323
|
+
this.socket.end();
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* <p>Called when a chunk of data arrives from the client. If currently in data
|
|
328
|
+
* mode, transmit the data otherwise send it to <code>_processData</code></p>
|
|
329
|
+
*
|
|
330
|
+
* @event
|
|
331
|
+
* @param {Buffer|String} chunk Data sent by the client
|
|
332
|
+
*/
|
|
333
|
+
RAISocket.prototype._onReceiveData = function(chunk){
|
|
334
|
+
|
|
335
|
+
if(this._ignore_data){ // if currently setting up TLS connection
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
var str = typeof chunk=="string"?chunk:chunk.toString("binary"),
|
|
340
|
+
dataRemainderMatch, data;
|
|
341
|
+
|
|
342
|
+
if(this._dataMode){
|
|
343
|
+
|
|
344
|
+
// prefix the incoming chunk with the remainder of the previous chunk
|
|
345
|
+
str = this._remainder + str;
|
|
346
|
+
this._remainder = "";
|
|
347
|
+
|
|
348
|
+
// check if a data end sequence is found from the data
|
|
349
|
+
if((dataRemainderMatch = str.match(this._endDataModeSequenceRegEx))){
|
|
350
|
+
// if the sequence is not on byte 0 emit remaining data
|
|
351
|
+
if(dataRemainderMatch.index){
|
|
352
|
+
data = Buffer.from(str.substr(0, dataRemainderMatch.index), "binary");
|
|
353
|
+
this._logger("DATA:", data.toString("utf-8"));
|
|
354
|
+
this.emit("data", data);
|
|
355
|
+
}
|
|
356
|
+
// emit data ready
|
|
357
|
+
this.emit("ready");
|
|
358
|
+
this._dataMode = false;
|
|
359
|
+
// send the remaining data for processing
|
|
360
|
+
this._processData(str.substr(dataRemainderMatch.index + dataRemainderMatch[0].length)+"\r\n");
|
|
361
|
+
}else{
|
|
362
|
+
// check if there's not something in the end of the data that resembles
|
|
363
|
+
// end sequence - if so, cut it off and save it to the remainder
|
|
364
|
+
for(var i = Math.min(this._endDataModeSequence.length-1, str.length); i>0; i--){
|
|
365
|
+
if(str.substr(-i) == this._endDataModeSequence.substr(0, i)){
|
|
366
|
+
this._remainder = str.substr(-i);
|
|
367
|
+
str = str.substr(0, str.length - i);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// if there's some data left, emit it
|
|
372
|
+
if(str.length){
|
|
373
|
+
data = Buffer.from(str, "binary");
|
|
374
|
+
this._logger("DATA:", data.toString("utf-8"));
|
|
375
|
+
this.emit("data", data);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}else{
|
|
379
|
+
// Not in data mode, process as command
|
|
380
|
+
this._processData(str);
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* <p>Processed incoming command lines and emits found data as
|
|
386
|
+
* <code>'command'</code> with the command name as the first param and the rest
|
|
387
|
+
* of the data as second (Buffer)</p>
|
|
388
|
+
*
|
|
389
|
+
* @param {String} str Binary string to be processed
|
|
390
|
+
*/
|
|
391
|
+
RAISocket.prototype._processData = function(str){
|
|
392
|
+
var lines = (this._remainder+str).split("\r\n"),
|
|
393
|
+
match, command;
|
|
394
|
+
|
|
395
|
+
this._remainder = lines.pop();
|
|
396
|
+
|
|
397
|
+
for(var i=0, len = lines.length; i<len; i++){
|
|
398
|
+
if(this._ignore_data){
|
|
399
|
+
// If TLS upgrade is initiated do not process current buffer
|
|
400
|
+
this._remainder = "";
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
if(!this._dataMode){
|
|
404
|
+
if((match = lines[i].match(/\s*[\S]+\s?/)) || (match = lines[i].match(/^$/))){
|
|
405
|
+
command = (match[0] || "").trim();
|
|
406
|
+
this._logger("COMMAND:", lines[i]);
|
|
407
|
+
this.emit("command", command, Buffer.from(lines[i].substr(match.index + match[0].length), "binary"));
|
|
408
|
+
}
|
|
409
|
+
}else{
|
|
410
|
+
if(this._remainder){
|
|
411
|
+
this._remainder += "\r\n";
|
|
412
|
+
}
|
|
413
|
+
this._onReceiveData(lines.slice(i).join("\r\n"));
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* <p>Called when the connection is or is going to be ended</p>
|
|
421
|
+
*/
|
|
422
|
+
RAISocket.prototype._destroy = function(){
|
|
423
|
+
if(this._destroyed){
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
this._destroyed = true;
|
|
427
|
+
|
|
428
|
+
this.removeAllListeners();
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* <p>Called when the connection is ended. Emits <code>'end'</code></p>
|
|
433
|
+
*
|
|
434
|
+
* @event
|
|
435
|
+
*/
|
|
436
|
+
RAISocket.prototype._onEnd = function(){
|
|
437
|
+
this.emit("end");
|
|
438
|
+
this._destroy();
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* <p>Called when an error has appeared. Emits <code>'error'</code> with
|
|
443
|
+
* the error object as a parameter.</p>
|
|
444
|
+
*
|
|
445
|
+
* @event
|
|
446
|
+
* @param {Object} err Error object
|
|
447
|
+
*/
|
|
448
|
+
RAISocket.prototype._onError = function(err){
|
|
449
|
+
this.emit("error", err);
|
|
450
|
+
this._destroy();
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* <p>Called when a timeout has occured. Connection will be closed and
|
|
455
|
+
* <code>'timeout'</code> is emitted.</p>
|
|
456
|
+
*
|
|
457
|
+
* @event
|
|
458
|
+
*/
|
|
459
|
+
RAISocket.prototype._onTimeout = function(){
|
|
460
|
+
if(this.options.disconnectOnTimeout){
|
|
461
|
+
if(this.socket && !this.socket.destroyed){
|
|
462
|
+
this.socket.end();
|
|
463
|
+
}
|
|
464
|
+
this.emit("timeout");
|
|
465
|
+
this._destroy();
|
|
466
|
+
}else{
|
|
467
|
+
this.emit("timeout");
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* <p>Called when the connection is closed</p>
|
|
473
|
+
*
|
|
474
|
+
* @event
|
|
475
|
+
* @param {Boolean} hadError did the connection end because of an error?
|
|
476
|
+
*/
|
|
477
|
+
RAISocket.prototype._onClose = function(/* hadError */){
|
|
478
|
+
this._destroy();
|
|
479
|
+
};
|
package/lib/starttls.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// SOURCE: https://gist.github.com/848444
|
|
4
|
+
|
|
5
|
+
// Modernized: crypto.createCredentials and tls.createSecurePair were removed
|
|
6
|
+
// from Node.js. The TLS upgrade is now performed with
|
|
7
|
+
// tls.createSecureContext() + new tls.TLSSocket(socket, {isServer: true, ...}),
|
|
8
|
+
// which upgrades the existing plaintext connection in place (STARTTLS
|
|
9
|
+
// semantics). Data that arrives on the socket during the upgrade is consumed
|
|
10
|
+
// by the TLS handshake machinery of TLSSocket and re-emitted as decrypted
|
|
11
|
+
// 'data' events, so nothing is lost.
|
|
12
|
+
|
|
13
|
+
// Target API:
|
|
14
|
+
//
|
|
15
|
+
// var s = require('net').createStream(25, 'smtp.example.com');
|
|
16
|
+
// s.on('connect', function() {
|
|
17
|
+
// require('starttls')(s, options, function() {
|
|
18
|
+
// if (!s.authorized) {
|
|
19
|
+
// s.destroy();
|
|
20
|
+
// return;
|
|
21
|
+
// }
|
|
22
|
+
//
|
|
23
|
+
// s.end("hello world\n");
|
|
24
|
+
// });
|
|
25
|
+
// });
|
|
26
|
+
//
|
|
27
|
+
//
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @namespace STARTTLS module
|
|
32
|
+
* @name starttls
|
|
33
|
+
*/
|
|
34
|
+
module.exports.starttls = starttls;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* <p>Upgrades a socket to a secure TLS connection</p>
|
|
38
|
+
*
|
|
39
|
+
* @memberOf starttls
|
|
40
|
+
* @param {Object} socket Plaintext socket to be upgraded
|
|
41
|
+
* @param {Object} options Certificate data for the server
|
|
42
|
+
* @param {Function} callback Callback function to be run after upgrade
|
|
43
|
+
*/
|
|
44
|
+
function starttls(socket, options, callback) {
|
|
45
|
+
var tlslib = require('tls'),
|
|
46
|
+
cleartext;
|
|
47
|
+
|
|
48
|
+
// remove plaintext data listeners, the TLSSocket takes over reading
|
|
49
|
+
// from the underlying socket
|
|
50
|
+
socket.removeAllListeners("data");
|
|
51
|
+
|
|
52
|
+
var secureContext = tlslib.createSecureContext(options);
|
|
53
|
+
|
|
54
|
+
// Upgrade the existing connection in place. The TLSSocket starts the
|
|
55
|
+
// TLS handshake on the very same TCP socket (isServer: true).
|
|
56
|
+
cleartext = new tlslib.TLSSocket(socket, {
|
|
57
|
+
isServer: true,
|
|
58
|
+
secureContext: secureContext
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// keep the original cleartext.authorized / cleartext.authorizationError
|
|
62
|
+
// semantics (own writable property, shadowing the prototype getter)
|
|
63
|
+
Object.defineProperty(cleartext, "authorized", {
|
|
64
|
+
value: false,
|
|
65
|
+
writable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
enumerable: true
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// the TLSSocket emits 'secure' once the handshake has completed
|
|
71
|
+
// (both for server and client side raw TLSSockets)
|
|
72
|
+
cleartext.once('secure', function() {
|
|
73
|
+
var verifyError = cleartext.authorizationError;
|
|
74
|
+
|
|
75
|
+
if (verifyError) {
|
|
76
|
+
cleartext.authorized = false;
|
|
77
|
+
} else {
|
|
78
|
+
cleartext.authorized = true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
callback(cleartext);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return cleartext;
|
|
85
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohos-ports/rai",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "Request-Answer-Interface for generating text based command servers (SMTP, POP etc)",
|
|
4
|
+
"version": "0.1.12-beta.0",
|
|
5
|
+
"author": "Andris Reinman",
|
|
6
|
+
"maintainers": [
|
|
7
|
+
{
|
|
8
|
+
"name": "andris",
|
|
9
|
+
"email": "andris@node.ee"
|
|
10
|
+
}
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/ohos-ports/ohos-ports.git",
|
|
15
|
+
"directory": "ports/rai/0.1.12"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "nodeunit test"
|
|
19
|
+
},
|
|
20
|
+
"main": "./lib/rai",
|
|
21
|
+
"licenses": [
|
|
22
|
+
{
|
|
23
|
+
"type": "MIT",
|
|
24
|
+
"url": "http://github.com/andris9/rai/blob/master/LICENSE"
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"nodeunit": "*"
|
|
29
|
+
},
|
|
30
|
+
"engines": [
|
|
31
|
+
"node >=0.4.0"
|
|
32
|
+
],
|
|
33
|
+
"keywords": [
|
|
34
|
+
"servers",
|
|
35
|
+
"text-based"
|
|
36
|
+
],
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/ohos-ports/ohos-ports/issues"
|
|
39
|
+
}
|
|
40
|
+
}
|