-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose Connector in the public API (#906)
* Expose Connector in the public API * Updated test * Add ca fingerprint example * Improve types * Updated test * Nit * Make connector a function instead of a class * Updated test * Updated examples * Updated docs * Updated docs
- Loading branch information
Showing
13 changed files
with
415 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Connector | ||
|
||
Undici creates the underlying socket via the connector builder. | ||
Normally, this happens automatically and you don't need to care about this, | ||
but if you need to perform some additional check over the currently used socket, | ||
this is the right place. | ||
|
||
If you want to create a custom connector, you must import the `buildConnector` utility. | ||
|
||
#### Parameter: `buildConnector.BuildOptions` | ||
|
||
Every Tls option, see [here](https://nodejs.org/api/tls.html#tls_tls_connect_options_callback). | ||
Furthermore, the following options can be passed: | ||
|
||
* **socketPath** `string | null` (optional) - Default: `null` - An IPC endpoint, either Unix domain socket or Windows named pipe. | ||
* **maxCachedSessions** `number | null` (optional) - Default: `100` - Maximum number of TLS cached sessions. Use 0 to disable TLS session caching. Default: 100. | ||
* **timeout** `number | null` (optional) - Default `10e3` | ||
* **servername** `string | null` (optional) | ||
|
||
Once you call `buildConnector`, it will return a connector function, which takes the following parameters. | ||
|
||
#### Parameter: `connector.Options` | ||
|
||
* **hostname** `string` (required) | ||
* **host** `string` (optional) | ||
* **protocol** `string` (required) | ||
* **port** `number` (required) | ||
* **servername** `string` (optional) | ||
|
||
### Basic example | ||
|
||
```js | ||
'use strict' | ||
|
||
import { Client, buildConnector } from 'undici' | ||
|
||
const connector = buildConnector({ rejectUnauthorized: false }) | ||
const client = new Client('https://localhost:3000', { | ||
connect (opts, cb) { | ||
connector(opts, (err, socket) => { | ||
if (err) { | ||
cb(err) | ||
} else if (/* assertion */) { | ||
socket.destroy() | ||
cb(new Error('kaboom')) | ||
} else { | ||
cb(null, socket) | ||
} | ||
}) | ||
} | ||
}) | ||
``` | ||
|
||
### Example: validate the CA fingerprint | ||
|
||
```js | ||
'use strict' | ||
|
||
import { Client, buildConnector } from 'undici' | ||
|
||
const caFingerprint = 'FO:OB:AR' | ||
const connector = buildConnector({ rejectUnauthorized: false }) | ||
const client = new Client('https://localhost:3000', { | ||
connect (opts, cb) { | ||
connector(opts, (err, socket) => { | ||
if (err) { | ||
cb(err) | ||
} else if (getIssuerCertificate(socket).fingerprint256 !== caFingerprint) { | ||
socket.destroy() | ||
cb(new Error('Fingerprint does not match')) | ||
} else { | ||
cb(null, socket) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
client.request({ | ||
path: '/', | ||
method: 'GET' | ||
}, (err, data) => { | ||
if (err) throw err | ||
|
||
const bufs = [] | ||
data.body.on('data', (buf) => { | ||
bufs.push(buf) | ||
}) | ||
data.body.on('end', () => { | ||
console.log(Buffer.concat(bufs).toString('utf8')) | ||
client.close() | ||
}) | ||
}) | ||
|
||
function getIssuerCertificate (socket) { | ||
let certificate = socket.getPeerCertificate(true) | ||
while (certificate && Object.keys(certificate).length > 0) { | ||
if (certificate.issuerCertificate !== undefined) { | ||
// For self-signed certificates, `issuerCertificate` may be a circular reference. | ||
if (certificate.fingerprint256 === certificate.issuerCertificate.fingerprint256) { | ||
break | ||
} | ||
certificate = certificate.issuerCertificate | ||
} else { | ||
break | ||
} | ||
} | ||
return certificate | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict' | ||
|
||
const crypto = require('crypto') | ||
const https = require('https') | ||
const { Client, buildConnector } = require('../..') | ||
const pem = require('https-pem') | ||
|
||
const caFingerprint = getFingerprint(pem.cert.toString() | ||
.split('\n') | ||
.slice(1, -1) | ||
.map(line => line.trim()) | ||
.join('') | ||
) | ||
|
||
const server = https.createServer(pem, (req, res) => { | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.end('hello') | ||
}) | ||
|
||
server.listen(0, function () { | ||
const connector = buildConnector({ rejectUnauthorized: false }) | ||
const client = new Client(`https://localhost:${server.address().port}`, { | ||
connect (opts, cb) { | ||
connector(opts, (err, socket) => { | ||
if (err) { | ||
cb(err) | ||
} else if (getIssuerCertificate(socket).fingerprint256 !== caFingerprint) { | ||
socket.destroy() | ||
cb(new Error('Fingerprint does not match')) | ||
} else { | ||
cb(null, socket) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
client.request({ | ||
path: '/', | ||
method: 'GET' | ||
}, (err, data) => { | ||
if (err) throw err | ||
|
||
const bufs = [] | ||
data.body.on('data', (buf) => { | ||
bufs.push(buf) | ||
}) | ||
data.body.on('end', () => { | ||
console.log(Buffer.concat(bufs).toString('utf8')) | ||
client.close() | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
|
||
function getIssuerCertificate (socket) { | ||
let certificate = socket.getPeerCertificate(true) | ||
while (certificate && Object.keys(certificate).length > 0) { | ||
if (certificate.issuerCertificate !== undefined) { | ||
// For self-signed certificates, `issuerCertificate` may be a circular reference. | ||
if (certificate.fingerprint256 === certificate.issuerCertificate.fingerprint256) { | ||
break | ||
} | ||
certificate = certificate.issuerCertificate | ||
} else { | ||
break | ||
} | ||
} | ||
return certificate | ||
} | ||
|
||
function getFingerprint (content, inputEncoding = 'base64', outputEncoding = 'hex') { | ||
const shasum = crypto.createHash('sha256') | ||
shasum.update(content, inputEncoding) | ||
const res = shasum.digest(outputEncoding) | ||
return res.toUpperCase().match(/.{1,2}/g).join(':') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.