mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
feat add http proxy to undicy (#487)
* feat add http proxy to undicy * fix typo * fix typo * add http proxy testcase
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const {Client, Pool} = require('undici');
|
||||
const {request, getGlobalDispatcher, setGlobalDispatcher, Dispatcher, ProxyAgent, Client, Pool} = require('undici');
|
||||
const parseUrl = require('parse-url');
|
||||
const assert = require('assert');
|
||||
const BaseRequestor = require('./base-requestor');
|
||||
@@ -10,6 +10,10 @@ const {
|
||||
HTTP_POOLSIZE,
|
||||
HTTP_PIPELINING,
|
||||
HTTP_TIMEOUT,
|
||||
HTTP_PROXY_IP,
|
||||
HTTP_PROXY_PORT,
|
||||
HTTP_PROXY_PROTOCOL,
|
||||
NODE_ENV,
|
||||
} = require('../config');
|
||||
|
||||
const toBase64 = (str) => Buffer.from(str || '', 'utf8').toString('base64');
|
||||
@@ -21,6 +25,15 @@ function basicAuth(username, password) {
|
||||
return {Authorization: header};
|
||||
}
|
||||
|
||||
const defaultDispatcher = HTTP_PROXY_IP ?
|
||||
new ProxyAgent(`${HTTP_PROXY_PROTOCOL}://${HTTP_PROXY_IP}${HTTP_PROXY_PORT ? `:${HTTP_PROXY_PORT}` : ''}`) :
|
||||
getGlobalDispatcher();
|
||||
|
||||
setGlobalDispatcher(new class extends Dispatcher {
|
||||
dispatch(options, handler) {
|
||||
return defaultDispatcher.dispatch(options, handler);
|
||||
}
|
||||
}());
|
||||
|
||||
class HttpRequestor extends BaseRequestor {
|
||||
constructor(logger, account_sid, hook, secret) {
|
||||
@@ -60,6 +73,18 @@ class HttpRequestor extends BaseRequestor {
|
||||
if (u.port) this.client = new Client(`${u.protocol}://${u.resource}:${u.port}`);
|
||||
else this.client = new Client(`${u.protocol}://${u.resource}`);
|
||||
}
|
||||
|
||||
if (NODE_ENV == 'test' && process.env.JAMBONES_HTTP_PROXY_IP) {
|
||||
const defDispatcher =
|
||||
new ProxyAgent(`${process.env.JAMBONES_HTTP_PROXY_PROTOCOL}://${process.env.JAMBONES_HTTP_PROXY_IP}${
|
||||
process.env.JAMBONES_HTTP_PROXY_PORT ? `:${process.env.JAMBONES_HTTP_PROXY_PORT}` : ''}`);
|
||||
|
||||
setGlobalDispatcher(new class extends Dispatcher {
|
||||
dispatch(options, handler) {
|
||||
return defDispatcher.dispatch(options, handler);
|
||||
}
|
||||
}());
|
||||
}
|
||||
}
|
||||
|
||||
get baseUrl() {
|
||||
@@ -139,7 +164,18 @@ class HttpRequestor extends BaseRequestor {
|
||||
};
|
||||
const absUrl = this._isRelativeUrl(url) ? `${this.baseUrl}${url}` : url;
|
||||
this.logger.debug({url, absUrl, hdrs}, 'send webhook');
|
||||
const {statusCode, headers, body} = await client.request({
|
||||
const {statusCode, headers, body} = HTTP_PROXY_IP ? await request(
|
||||
this.baseUrl,
|
||||
{
|
||||
path,
|
||||
query,
|
||||
method,
|
||||
headers: hdrs,
|
||||
...('POST' === method && {body: JSON.stringify(payload)}),
|
||||
timeout: HTTP_TIMEOUT,
|
||||
followRedirects: false
|
||||
}
|
||||
) : await client.request({
|
||||
path,
|
||||
query,
|
||||
method,
|
||||
|
||||
Reference in New Issue
Block a user