[d24f17c] | 1 | # Connecting through a proxy
|
---|
| 2 |
|
---|
| 3 | Connecting through a proxy is possible by:
|
---|
| 4 |
|
---|
| 5 | - Using [AgentProxy](../api/ProxyAgent.md).
|
---|
| 6 | - Configuring `Client` or `Pool` constructor.
|
---|
| 7 |
|
---|
| 8 | The proxy url should be passed to the `Client` or `Pool` constructor, while the upstream server url
|
---|
| 9 | should be added to every request call in the `path`.
|
---|
| 10 | For instance, if you need to send a request to the `/hello` route of your upstream server,
|
---|
| 11 | the `path` should be `path: 'http://upstream.server:port/hello?foo=bar'`.
|
---|
| 12 |
|
---|
| 13 | If you proxy requires basic authentication, you can send it via the `proxy-authorization` header.
|
---|
| 14 |
|
---|
| 15 | ### Connect without authentication
|
---|
| 16 |
|
---|
| 17 | ```js
|
---|
| 18 | import { Client } from 'undici'
|
---|
| 19 | import { createServer } from 'http'
|
---|
| 20 | import proxy from 'proxy'
|
---|
| 21 |
|
---|
| 22 | const server = await buildServer()
|
---|
| 23 | const proxyServer = await buildProxy()
|
---|
| 24 |
|
---|
| 25 | const serverUrl = `http://localhost:${server.address().port}`
|
---|
| 26 | const proxyUrl = `http://localhost:${proxyServer.address().port}`
|
---|
| 27 |
|
---|
| 28 | server.on('request', (req, res) => {
|
---|
| 29 | console.log(req.url) // '/hello?foo=bar'
|
---|
| 30 | res.setHeader('content-type', 'application/json')
|
---|
| 31 | res.end(JSON.stringify({ hello: 'world' }))
|
---|
| 32 | })
|
---|
| 33 |
|
---|
| 34 | const client = new Client(proxyUrl)
|
---|
| 35 |
|
---|
| 36 | const response = await client.request({
|
---|
| 37 | method: 'GET',
|
---|
| 38 | path: serverUrl + '/hello?foo=bar'
|
---|
| 39 | })
|
---|
| 40 |
|
---|
| 41 | response.body.setEncoding('utf8')
|
---|
| 42 | let data = ''
|
---|
| 43 | for await (const chunk of response.body) {
|
---|
| 44 | data += chunk
|
---|
| 45 | }
|
---|
| 46 | console.log(response.statusCode) // 200
|
---|
| 47 | console.log(JSON.parse(data)) // { hello: 'world' }
|
---|
| 48 |
|
---|
| 49 | server.close()
|
---|
| 50 | proxyServer.close()
|
---|
| 51 | client.close()
|
---|
| 52 |
|
---|
| 53 | function buildServer () {
|
---|
| 54 | return new Promise((resolve, reject) => {
|
---|
| 55 | const server = createServer()
|
---|
| 56 | server.listen(0, () => resolve(server))
|
---|
| 57 | })
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | function buildProxy () {
|
---|
| 61 | return new Promise((resolve, reject) => {
|
---|
| 62 | const server = proxy(createServer())
|
---|
| 63 | server.listen(0, () => resolve(server))
|
---|
| 64 | })
|
---|
| 65 | }
|
---|
| 66 | ```
|
---|
| 67 |
|
---|
| 68 | ### Connect with authentication
|
---|
| 69 |
|
---|
| 70 | ```js
|
---|
| 71 | import { Client } from 'undici'
|
---|
| 72 | import { createServer } from 'http'
|
---|
| 73 | import proxy from 'proxy'
|
---|
| 74 |
|
---|
| 75 | const server = await buildServer()
|
---|
| 76 | const proxyServer = await buildProxy()
|
---|
| 77 |
|
---|
| 78 | const serverUrl = `http://localhost:${server.address().port}`
|
---|
| 79 | const proxyUrl = `http://localhost:${proxyServer.address().port}`
|
---|
| 80 |
|
---|
| 81 | proxyServer.authenticate = function (req, fn) {
|
---|
| 82 | fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`)
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | server.on('request', (req, res) => {
|
---|
| 86 | console.log(req.url) // '/hello?foo=bar'
|
---|
| 87 | res.setHeader('content-type', 'application/json')
|
---|
| 88 | res.end(JSON.stringify({ hello: 'world' }))
|
---|
| 89 | })
|
---|
| 90 |
|
---|
| 91 | const client = new Client(proxyUrl)
|
---|
| 92 |
|
---|
| 93 | const response = await client.request({
|
---|
| 94 | method: 'GET',
|
---|
| 95 | path: serverUrl + '/hello?foo=bar',
|
---|
| 96 | headers: {
|
---|
| 97 | 'proxy-authorization': `Basic ${Buffer.from('user:pass').toString('base64')}`
|
---|
| 98 | }
|
---|
| 99 | })
|
---|
| 100 |
|
---|
| 101 | response.body.setEncoding('utf8')
|
---|
| 102 | let data = ''
|
---|
| 103 | for await (const chunk of response.body) {
|
---|
| 104 | data += chunk
|
---|
| 105 | }
|
---|
| 106 | console.log(response.statusCode) // 200
|
---|
| 107 | console.log(JSON.parse(data)) // { hello: 'world' }
|
---|
| 108 |
|
---|
| 109 | server.close()
|
---|
| 110 | proxyServer.close()
|
---|
| 111 | client.close()
|
---|
| 112 |
|
---|
| 113 | function buildServer () {
|
---|
| 114 | return new Promise((resolve, reject) => {
|
---|
| 115 | const server = createServer()
|
---|
| 116 | server.listen(0, () => resolve(server))
|
---|
| 117 | })
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | function buildProxy () {
|
---|
| 121 | return new Promise((resolve, reject) => {
|
---|
| 122 | const server = proxy(createServer())
|
---|
| 123 | server.listen(0, () => resolve(server))
|
---|
| 124 | })
|
---|
| 125 | }
|
---|
| 126 | ```
|
---|
| 127 |
|
---|