ln-docker-daemons 8.1.4 → 9.0.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.
@@ -0,0 +1,263 @@
1
+ const asyncAuto = require('async/auto');
2
+ const asyncify = require('async/asyncify');
3
+ const {returnResult} = require('asyncjs-util');
4
+
5
+ const {addPeer} = require('./../bitcoinrpc');
6
+ const checkChainSynced = require('./check_chain_synced');
7
+ const checkGrpcServer = require('./check_grpc_server');
8
+ const {dockerLoopServerImage} = require('./constants');
9
+ const {killDockers} = require('./../docker');
10
+ const {makeSelfSignedCert} = require('./../tls');
11
+ const retryWithin = require('./retry_within');
12
+ const {spawnBitcoindDocker} = require('./../bitcoind');
13
+ const {spawnDockerImage} = require('./../docker');
14
+ const {startTlsProxy} = require('./../tls');
15
+
16
+ const bitcoindRpcPort = 18443;
17
+ const bufferAsBase64 = buffer => Buffer.from(buffer).toString('base64');
18
+ const certPath = '/root/.lnd/tls.cert';
19
+ const imageName = ver => !!ver ? `lightninglabs/loopserver:${ver}` : null;
20
+ const interval = 100;
21
+ const isBase64 = n => typeof n === 'string' && /^[a-z0-9+/]+={0,2}$/i.test(n);
22
+ const isHex = n => typeof n === 'string' && /^([0-9a-f]{2})+$/i.test(n);
23
+ const localSocket = port => `localhost:${port}`; // Proxy is on both loopbacks
24
+ const loopbackSocket = port => `127.0.0.1:${port}`; // Where ports are bound
25
+ const lndRpcPort = 10009;
26
+ const macaroonPath = '/root/.lnd/admin.macaroon';
27
+ const termsPath = '/looprpc.SwapServer/LoopOutTerms';
28
+ const termsRequest = Buffer.from('0000000002080b', 'hex'); // Protocol v11
29
+ const maxSwapTokens = 5e6;
30
+ const rpcPort = '11009/tcp';
31
+ const waitMs = 1000 * 60;
32
+
33
+ /** Spawn a Lightning Labs regtest Loop server Docker image, backed by an LND
34
+
35
+ {
36
+ chain_socket: <LND Chain P2P Socket String>
37
+ lnd_cert: <LND Base64 Serialized TLS Cert String>
38
+ lnd_docker_host: <LND Docker Network Host String>
39
+ lnd_macaroon: <LND Admin Macaroon Hex String>
40
+ [rpc_port]: <Loop Server RPC Port Number>
41
+ }
42
+
43
+ @returns via cbk or Promise
44
+ {
45
+ cert: <TLS Socket Base64 Serialized TLS Cert String>
46
+ kill: ({}, [cbk]) => <Kill Loop Server Promise>
47
+ socket: <Loop Server RPC Host:Port Network Address String>
48
+ tls_socket: <Loop Server TLS RPC Host:Port Network Address String>
49
+ }
50
+ */
51
+ module.exports = (args, cbk) => {
52
+ return new Promise((resolve, reject) => {
53
+ return asyncAuto({
54
+ // Check arguments
55
+ validate: cbk => {
56
+ if (!args.chain_socket) {
57
+ return cbk([400, 'ExpectedChainSocketToSpawnLoopServerDocker']);
58
+ }
59
+
60
+ if (!args.lnd_cert) {
61
+ return cbk([400, 'ExpectedLndCertToSpawnLoopServerDocker']);
62
+ }
63
+
64
+ // The cert is base64, the way spawnLightningDocker returns it
65
+ if (!isBase64(args.lnd_cert) || args.lnd_cert.length % 4) {
66
+ return cbk([400, 'ExpectedBase64LndCertToSpawnLoopServerDocker']);
67
+ }
68
+
69
+ if (!args.lnd_docker_host) {
70
+ return cbk([400, 'ExpectedLndDockerHostToSpawnLoopServerDocker']);
71
+ }
72
+
73
+ if (!args.lnd_macaroon) {
74
+ return cbk([400, 'ExpectedLndMacaroonToSpawnLoopServerDocker']);
75
+ }
76
+
77
+ // The macaroon is hex, the way spawnLightningDocker returns it
78
+ if (!isHex(args.lnd_macaroon)) {
79
+ return cbk([400, 'ExpectedHexLndMacaroonToSpawnLoopServerDocker']);
80
+ }
81
+
82
+ return cbk();
83
+ },
84
+
85
+ // Decode the LND cert and macaroon to copy them into the container
86
+ lndFiles: ['validate', ({}, cbk) => {
87
+ return cbk(null, [
88
+ {data: Buffer.from(args.lnd_cert, 'base64'), path: certPath},
89
+ {data: Buffer.from(args.lnd_macaroon, 'hex'), path: macaroonPath},
90
+ ]);
91
+ }],
92
+
93
+ // Make a TLS certificate for the proxy in front of the Loop server
94
+ makeCert: ['validate', asyncify(({}) => makeSelfSignedCert({}))],
95
+
96
+ // Spin up a Bitcoin Core for the Loop server
97
+ spawnChain: ['lndFiles', 'makeCert', ({}, cbk) => {
98
+ return spawnBitcoindDocker({}, cbk);
99
+ }],
100
+
101
+ // The Loop server chain follows the chain of the LND
102
+ addChainPeer: ['spawnChain', ({spawnChain}, cbk) => {
103
+ return addPeer({
104
+ pass: spawnChain.rpc_pass,
105
+ port: spawnChain.rpc_port,
106
+ socket: args.chain_socket,
107
+ user: spawnChain.rpc_user,
108
+ },
109
+ err => {
110
+ // Stop the chain when it cannot follow the LND chain
111
+ if (!!err) {
112
+ return spawnChain.kill({}, () => cbk(err));
113
+ }
114
+
115
+ return cbk();
116
+ });
117
+ }],
118
+
119
+ // Wait for the Loop server chain to have the blocks of the LND chain
120
+ waitForChain: ['addChainPeer', 'spawnChain', ({spawnChain}, cbk) => {
121
+ // The LND chain is its peer, so it has the blocks the LND chain had
122
+ const method = cbk => checkChainSynced({
123
+ pass: spawnChain.rpc_pass,
124
+ port: spawnChain.rpc_port,
125
+ socket: args.chain_socket,
126
+ user: spawnChain.rpc_user,
127
+ },
128
+ cbk);
129
+
130
+ return retryWithin({interval, method, ms: waitMs}, err => {
131
+ // Stop the chain when it never has the LND blocks
132
+ if (!!err) {
133
+ return spawnChain.kill({}, () => cbk(err));
134
+ }
135
+
136
+ return cbk();
137
+ });
138
+ }],
139
+
140
+ // Spin up the docker image with the LND cert and macaroon in it
141
+ spawnDocker: [
142
+ 'lndFiles',
143
+ 'spawnChain',
144
+ 'waitForChain',
145
+ ({lndFiles, spawnChain}, cbk) =>
146
+ {
147
+ const chain = `${spawnChain.host}:${bitcoindRpcPort}`;
148
+
149
+ return spawnDockerImage({
150
+ arguments: [
151
+ 'daemon',
152
+ `--bitcoin.host=${chain}`,
153
+ `--bitcoin.password=${spawnChain.rpc_pass}`,
154
+ `--bitcoin.user=${spawnChain.rpc_user}`,
155
+ // The daemon serves TLS unless told to serve plain gRPC
156
+ '--insecure',
157
+ `--lnd.host=${args.lnd_docker_host}:${lndRpcPort}`,
158
+ `--lnd.macaroonpath=${macaroonPath}`,
159
+ `--lnd.tlspath=${certPath}`,
160
+ `--maxamt=${maxSwapTokens}`,
161
+ ],
162
+ files: lndFiles,
163
+ // A Loop server version can be picked in the environment
164
+ image: imageName(process.env.DOCKER_LOOP_SERVER_VERSION) ||
165
+ dockerLoopServerImage,
166
+ // Docker picks a free host port when no RPC port is given
167
+ ports: {[rpcPort]: args.rpc_port},
168
+ },
169
+ (err, res) => {
170
+ // Stop the chain when the Loop server does not start
171
+ if (!!err) {
172
+ return spawnChain.kill({}, () => cbk(err));
173
+ }
174
+
175
+ return cbk(null, res);
176
+ });
177
+ }],
178
+
179
+ // Wait for the Loop server to answer requests
180
+ waitForServer: [
181
+ 'spawnChain',
182
+ 'spawnDocker',
183
+ ({spawnChain, spawnDocker}, cbk) =>
184
+ {
185
+ const dockers = [spawnDocker, spawnChain];
186
+ const socket = loopbackSocket(spawnDocker.ports[rpcPort]);
187
+
188
+ // Loop server images without the health service answer swap terms
189
+ const fallback = {path: termsPath, request: termsRequest};
190
+
191
+ const method = cbk => checkGrpcServer({fallback, socket}, cbk);
192
+
193
+ return retryWithin({interval, method, ms: waitMs}, err => {
194
+ // Exit early when the server answers
195
+ if (!err) {
196
+ return cbk();
197
+ }
198
+
199
+ return killDockers({dockers}, () => {
200
+ return cbk([503, 'FailedToConnectToLoopServer', {err}]);
201
+ });
202
+ });
203
+ }],
204
+
205
+ // The Loop server takes gRPC without TLS, so TLS clients use a proxy
206
+ startProxy: [
207
+ 'makeCert',
208
+ 'spawnChain',
209
+ 'spawnDocker',
210
+ 'waitForServer',
211
+ ({makeCert, spawnChain, spawnDocker}, cbk) =>
212
+ {
213
+ return startTlsProxy({
214
+ cert: makeCert.cert,
215
+ key: makeCert.key,
216
+ port: spawnDocker.ports[rpcPort],
217
+ },
218
+ (err, res) => {
219
+ // Stop the Loop server and its chain when the proxy does not start
220
+ if (!!err) {
221
+ return killDockers({dockers: [spawnDocker, spawnChain]}, () => {
222
+ return cbk(err);
223
+ });
224
+ }
225
+
226
+ return cbk(null, res);
227
+ });
228
+ }],
229
+
230
+ // Final Loop server details
231
+ loopServer: [
232
+ 'makeCert',
233
+ 'spawnChain',
234
+ 'spawnDocker',
235
+ 'startProxy',
236
+ ({makeCert, spawnChain, spawnDocker, startProxy}, cbk) =>
237
+ {
238
+ const dockers = [spawnDocker, spawnChain];
239
+
240
+ return cbk(null, {
241
+ cert: bufferAsBase64(makeCert.cert),
242
+ kill: ({}, cbk) => new Promise((resolve, reject) => {
243
+ return asyncAuto({
244
+ // Stop passing connections on to the Loop server
245
+ closeProxy: cbk => startProxy.close({}, cbk),
246
+
247
+ // Stop the Loop server and its chain
248
+ killDockers: ['closeProxy', ({}, cbk) => {
249
+ return killDockers({dockers}, cbk);
250
+ }],
251
+ },
252
+ returnResult({reject, resolve}, cbk));
253
+ }),
254
+ // The socket checked for readiness, as gRPC without TLS takes an IP
255
+ socket: loopbackSocket(spawnDocker.ports[rpcPort]),
256
+ // A TLS host name, as grpc-js warns about IP addresses as TLS names
257
+ tls_socket: localSocket(startProxy.port),
258
+ });
259
+ }],
260
+ },
261
+ returnResult({reject, resolve, of: 'loopServer'}, cbk));
262
+ });
263
+ };
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "async": "3.2.6",
16
16
  "asyncjs-util": "1.2.12",
17
17
  "dockerode": "5.0.1",
18
- "lightning": "13.1.10"
18
+ "lightning": "14.0.0"
19
19
  },
20
20
  "description": "Spawn and control LN Docker daemons",
21
21
  "engines": {
@@ -28,7 +28,9 @@
28
28
  "docker",
29
29
  "index.js",
30
30
  "lnd",
31
- "setup"
31
+ "loop",
32
+ "setup",
33
+ "tls"
32
34
  ],
33
35
  "keywords": [
34
36
  "docker",
@@ -45,5 +47,5 @@
45
47
  "integration-tests": "node --test \"test/integration/*.js\"",
46
48
  "test": "node --experimental-test-coverage --test \"test/!(integration)/**/*.js\""
47
49
  },
48
- "version": "8.1.4"
50
+ "version": "9.0.0"
49
51
  }
@@ -18,9 +18,9 @@ const interval = 100;
18
18
  const times = 1500;
19
19
 
20
20
  // Generate blocks, passing back a failure without async wrapping it
21
- const generateBlocks = ({generate, request}, cbk) => {
21
+ const generateBlocks = (args, cbk) => {
22
22
  return Promise.resolve()
23
- .then(() => generate(request))
23
+ .then(() => args.generate(args.request))
24
24
  .then(() => cbk(), cbk);
25
25
  };
26
26
 
@@ -22,15 +22,15 @@ module.exports = (args, cbk) => {
22
22
  return cbk([400, 'ExpectedAuthenticatedLndToWaitForChannelPending']);
23
23
  }
24
24
 
25
- const {id} = args;
26
-
27
25
  return asyncRetry({interval, times}, cbk => {
28
26
  return getPendingChannels({lnd: args.lnd}, (err, res) => {
29
27
  if (!!err) {
30
28
  return cbk(err);
31
29
  }
32
30
 
33
- const channel = res.pending_channels.find(n => n.transaction_id === id);
31
+ const channel = res.pending_channels.find(n => {
32
+ return n.transaction_id === args.id;
33
+ });
34
34
 
35
35
  if (!channel) {
36
36
  return cbk([503, 'FailedToFindPendingChannelWithTransactionId']);
package/tls/index.js ADDED
@@ -0,0 +1,4 @@
1
+ const makeSelfSignedCert = require('./make_self_signed_cert');
2
+ const startTlsProxy = require('./start_tls_proxy');
3
+
4
+ module.exports = {makeSelfSignedCert, startTlsProxy};
@@ -0,0 +1,167 @@
1
+ const {generateKeyPairSync} = require('node:crypto');
2
+ const {randomBytes} = require('node:crypto');
3
+ const {sign} = require('node:crypto');
4
+
5
+ const certBegin = '-----BEGIN CERTIFICATE-----';
6
+ const certEnd = '-----END CERTIFICATE-----';
7
+ const commonNameOid = '550403';
8
+ const dnsNameTag = 0x82;
9
+ const ecdsaWithSha256Oid = '2a8648ce3d040302';
10
+ const hexAsBuffer = hex => Buffer.from(hex, 'hex');
11
+ const ipAddressTag = 0x87;
12
+ const localhost = 'localhost';
13
+ const ipv4Loopback = '7f000001';
14
+ const ipv6Loopback = '00000000000000000000000000000001';
15
+ const makeKeys = () => generateKeyPairSync('ec', {namedCurve: 'prime256v1'});
16
+ const pemKey = key => key.export({format: 'pem', type: 'pkcs8'});
17
+ const pemLines = der => der.toString('base64').match(/.{1,64}/g);
18
+ const publicKeyDer = key => key.export({format: 'der', type: 'spki'});
19
+ const serialBytes = 16;
20
+ const signSha256 = (data, key) => sign('sha256', data, key);
21
+ const subjectAltNameOid = '551d11';
22
+ const tags = {
23
+ bitString: 0x03,
24
+ extensions: 0xa3,
25
+ integer: 0x02,
26
+ octetString: 0x04,
27
+ oid: 0x06,
28
+ sequence: 0x30,
29
+ set: 0x31,
30
+ utcTime: 0x17,
31
+ utf8String: 0x0c,
32
+ version: 0xa0,
33
+ };
34
+ const unusedBits = Buffer.alloc(1);
35
+ const skewMs = 1000 * 60 * 60 * 24;
36
+ const validityMs = 1000 * 60 * 60 * 24 * 365;
37
+ const versionThree = '02';
38
+
39
+ // DER encode the length of contents, using the long form when it is needed
40
+ const derLength = size => {
41
+ if (size < 0x80) {
42
+ return Buffer.from([size]);
43
+ }
44
+
45
+ if (size < 0x100) {
46
+ return Buffer.from([0x81, size]);
47
+ }
48
+
49
+ return Buffer.from([0x82, size >> 8, size & 0xff]);
50
+ };
51
+
52
+ // DER encode an element of a type with its contents
53
+ const der = (tag, ...contents) => {
54
+ const body = Buffer.concat(contents);
55
+
56
+ return Buffer.concat([Buffer.from([tag]), derLength(body.length), body]);
57
+ };
58
+
59
+ // DER encode an object identifier
60
+ const oid = hex => der(tags.oid, hexAsBuffer(hex));
61
+
62
+ // DER encode a date as a UTC time: YYMMDDHHMMSSZ
63
+ const utcTime = date => {
64
+ const digits = date.toISOString().slice(2, 19).replace(/[-T:]/g, '');
65
+
66
+ return der(tags.utcTime, Buffer.from(`${digits}Z`));
67
+ };
68
+
69
+ // The algorithm that signs the certificate: ECDSA with SHA-256
70
+ const signatureAlgorithm = () => der(tags.sequence, oid(ecdsaWithSha256Oid));
71
+
72
+ // The certificate is issued by and to localhost
73
+ const localhostSubject = () => der(
74
+ tags.sequence,
75
+ der(
76
+ tags.set,
77
+ der(
78
+ tags.sequence,
79
+ oid(commonNameOid),
80
+ der(tags.utf8String, Buffer.from(localhost))
81
+ )
82
+ )
83
+ );
84
+
85
+ // The certificate is valid from a day before now, allowing for clock skew,
86
+ // for a year, so a long running server keeps a valid certificate
87
+ const validity = now => der(
88
+ tags.sequence,
89
+ utcTime(new Date(now - skewMs)),
90
+ utcTime(new Date(now + validityMs))
91
+ );
92
+
93
+ // A random positive serial number, so each certificate has its own
94
+ const randomSerial = () => {
95
+ const serial = randomBytes(serialBytes);
96
+
97
+ // The high bit is clear to be positive, the next set to be minimally encoded
98
+ serial[0] = (serial[0] & 0x7f) | 0x40;
99
+
100
+ return serial;
101
+ };
102
+
103
+ // Clients can connect by the localhost host name or either loopback address
104
+ const subjectAltNames = () => der(
105
+ tags.sequence,
106
+ oid(subjectAltNameOid),
107
+ der(
108
+ tags.octetString,
109
+ der(
110
+ tags.sequence,
111
+ der(dnsNameTag, Buffer.from(localhost)),
112
+ der(ipAddressTag, hexAsBuffer(ipv4Loopback)),
113
+ der(ipAddressTag, hexAsBuffer(ipv6Loopback))
114
+ )
115
+ )
116
+ );
117
+
118
+ // The extensions of the certificate, which leave it unmarked as a CA so that
119
+ // clients which refuse CA certificates as server certificates accept it
120
+ const extensions = () => der(
121
+ tags.extensions,
122
+ der(tags.sequence, subjectAltNames())
123
+ );
124
+
125
+ // The part of the certificate that is signed
126
+ const toBeSigned = args => der(
127
+ tags.sequence,
128
+ der(tags.version, der(tags.integer, hexAsBuffer(versionThree))),
129
+ der(tags.integer, randomSerial()),
130
+ signatureAlgorithm(),
131
+ localhostSubject(),
132
+ validity(args.now),
133
+ localhostSubject(),
134
+ publicKeyDer(args.key),
135
+ extensions()
136
+ );
137
+
138
+ // PEM encode a certificate, with the base64 in lines of 64 characters
139
+ const pemCertificate = der => {
140
+ return [certBegin].concat(pemLines(der), certEnd, String()).join('\n');
141
+ };
142
+
143
+ /** Make a self-signed TLS certificate for a server on localhost
144
+
145
+ {}
146
+
147
+ @returns
148
+ {
149
+ cert: <PEM Encoded Certificate String>
150
+ key: <PEM Encoded Private Key String>
151
+ }
152
+ */
153
+ module.exports = ({}) => {
154
+ const {privateKey, publicKey} = makeKeys();
155
+
156
+ const tbs = toBeSigned({key: publicKey, now: Date.now()});
157
+
158
+ // The certificate is signed with its own key
159
+ const certificate = der(
160
+ tags.sequence,
161
+ tbs,
162
+ signatureAlgorithm(),
163
+ der(tags.bitString, unusedBits, signSha256(tbs, privateKey))
164
+ );
165
+
166
+ return {cert: pemCertificate(certificate), key: pemKey(privateKey)};
167
+ };
@@ -0,0 +1,196 @@
1
+ const {connect} = require('node:net');
2
+ const {createServer} = require('node:tls');
3
+
4
+ const asyncAuto = require('async/auto');
5
+ const asyncify = require('async/asyncify');
6
+ const {returnResult} = require('asyncjs-util');
7
+
8
+ const alpnProtocols = ['h2'];
9
+ const ipv4Loopback = '127.0.0.1';
10
+ const ipv6Loopback = '::1';
11
+ const maxPortTries = 10;
12
+ const noIpv6Codes = ['EADDRNOTAVAIL', 'EAFNOSUPPORT'];
13
+ const portInUseCode = 'EADDRINUSE';
14
+ const randomPort = 0;
15
+
16
+ // Listen on a port and host, passing back a listen failure as an error
17
+ //
18
+ // A callback passed to listen stays registered when listening fails, and a
19
+ // later listen would call it, so both outcomes are listened for here and
20
+ // each one removes the listener for the other
21
+ const listen = (args, cbk) => {
22
+ const failed = err => {
23
+ args.server.removeListener('listening', listening);
24
+
25
+ return cbk(err);
26
+ };
27
+
28
+ const listening = () => {
29
+ args.server.removeListener('error', failed);
30
+
31
+ return cbk();
32
+ };
33
+
34
+ args.server.once('error', failed);
35
+ args.server.once('listening', listening);
36
+
37
+ return args.server.listen(args.port, args.host);
38
+ };
39
+
40
+ // Listen on the same port on both loopback addresses, so localhost reaches
41
+ // only the proxy, whichever loopback address localhost resolves to
42
+ const listenOnLoopbacks = (args, cbk) => {
43
+ return listen({
44
+ host: ipv4Loopback,
45
+ port: randomPort,
46
+ server: args.ipv4,
47
+ },
48
+ err => {
49
+ if (!!err) {
50
+ return cbk([503, 'FailedToStartTlsProxy', {err}]);
51
+ }
52
+
53
+ const {port} = args.ipv4.address();
54
+
55
+ return listen({port, host: ipv6Loopback, server: args.ipv6}, err => {
56
+ // Exit early when the proxy listens on both loopback addresses
57
+ if (!err) {
58
+ return cbk(null, {port, is_ipv6: true});
59
+ }
60
+
61
+ // Exit early when there is no IPv6 loopback, so nothing is on ::1
62
+ if (noIpv6Codes.includes(err.code)) {
63
+ return cbk(null, {port});
64
+ }
65
+
66
+ return args.ipv4.close(() => {
67
+ // Another service has the port on ::1, so try a different port
68
+ if (err.code === portInUseCode && args.tries > 1) {
69
+ return listenOnLoopbacks({...args, tries: args.tries - 1}, cbk);
70
+ }
71
+
72
+ if (err.code === portInUseCode) {
73
+ return cbk([503, 'FailedToFindPortFreeOnBothLoopbackAddresses']);
74
+ }
75
+
76
+ return cbk([503, 'FailedToStartTlsProxy', {err}]);
77
+ });
78
+ });
79
+ });
80
+ };
81
+
82
+ /** Start a TLS server on localhost that passes connections on to a plain port
83
+
84
+ The TLS server listens on the same port on 127.0.0.1 and, when there is an
85
+ IPv6 loopback, on ::1, so a localhost socket always reaches the TLS server
86
+
87
+ {
88
+ cert: <PEM Encoded Certificate String>
89
+ key: <PEM Encoded Private Key String>
90
+ port: <Plain Connection Port Number>
91
+ }
92
+
93
+ @returns via cbk or Promise
94
+ {
95
+ close: <Close Proxy Function> ({}, [cbk]) => <Closed Promise>
96
+ port: <TLS Port Number>
97
+ }
98
+ */
99
+ module.exports = (args, cbk) => {
100
+ return new Promise((resolve, reject) => {
101
+ return asyncAuto({
102
+ // Check arguments
103
+ validate: cbk => {
104
+ if (!args.cert) {
105
+ return cbk([400, 'ExpectedCertificateToStartTlsProxy']);
106
+ }
107
+
108
+ if (!args.key) {
109
+ return cbk([400, 'ExpectedPrivateKeyToStartTlsProxy']);
110
+ }
111
+
112
+ if (!args.port) {
113
+ return cbk([400, 'ExpectedPlainPortToStartTlsProxy']);
114
+ }
115
+
116
+ return cbk();
117
+ },
118
+
119
+ // Make the TLS servers, which throws for a cert or key that is not valid
120
+ tlsServers: ['validate', asyncify(({}) => {
121
+ const sockets = new Set();
122
+
123
+ // Open connections are tracked so that closing ends them
124
+ const track = socket => {
125
+ sockets.add(socket);
126
+
127
+ return socket.once('close', () => sockets.delete(socket));
128
+ };
129
+
130
+ // Connections negotiate HTTP/2, as gRPC clients do
131
+ const options = {
132
+ ALPNProtocols: alpnProtocols,
133
+ cert: args.cert,
134
+ key: args.key,
135
+ };
136
+
137
+ // Each TLS connection is passed on to the plain port
138
+ const proxy = secure => {
139
+ // The plain port is published on IPv4 loopback, not on ::1
140
+ const plain = connect(args.port, ipv4Loopback);
141
+
142
+ track(plain);
143
+
144
+ // A failure on either side of the connection ends the other side
145
+ secure.on('error', () => plain.destroy());
146
+ plain.on('error', () => secure.destroy());
147
+
148
+ return secure.pipe(plain).pipe(secure);
149
+ };
150
+
151
+ const ipv4 = createServer(options, proxy);
152
+ const ipv6 = createServer(options, proxy);
153
+
154
+ // Connections are tracked before their TLS handshakes are done
155
+ ipv4.on('connection', socket => track(socket));
156
+ ipv6.on('connection', socket => track(socket));
157
+
158
+ return {ipv4, ipv6, sockets};
159
+ })],
160
+
161
+ // Listen for connections on localhost
162
+ proxy: ['tlsServers', ({tlsServers}, cbk) => {
163
+ const {ipv4, ipv6, sockets} = tlsServers;
164
+
165
+ return listenOnLoopbacks({
166
+ ipv4,
167
+ ipv6,
168
+ tries: maxPortTries,
169
+ },
170
+ (err, res) => {
171
+ if (!!err) {
172
+ return cbk(err);
173
+ }
174
+
175
+ const servers = !!res.is_ipv6 ? [ipv4, ipv6] : [ipv4];
176
+
177
+ return cbk(null, {
178
+ close: ({}, cbk) => new Promise((resolve, reject) => {
179
+ const closed = returnResult({reject, resolve}, cbk);
180
+
181
+ sockets.forEach(socket => socket.destroy());
182
+
183
+ const closing = servers.map(server => {
184
+ return new Promise(done => server.close(() => done()));
185
+ });
186
+
187
+ return Promise.all(closing).then(() => closed());
188
+ }),
189
+ port: res.port,
190
+ });
191
+ });
192
+ }],
193
+ },
194
+ returnResult({reject, resolve, of: 'proxy'}, cbk));
195
+ });
196
+ };