Skip to content

Commit

Permalink
Merge pull request #4571 from Countly/SER-829-proxy-settings-for-hooks
Browse files Browse the repository at this point in the history
[SER-829] [core] Proxy setting for api call outbound from Countly
  • Loading branch information
coskunaydinoglu authored Nov 18, 2023
2 parents 38c5413 + 3e8786a commit 117b1d1
Show file tree
Hide file tree
Showing 6 changed files with 662 additions and 241 deletions.
7 changes: 6 additions & 1 deletion api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ plugins.connectToAllDatabases().then(function() {
dashboard_additional_headers: "X-Frame-Options:deny\nX-XSS-Protection:1; mode=block\nStrict-Transport-Security:max-age=31536000 ; includeSubDomains\nX-Content-Type-Options: nosniff",
api_additional_headers: "X-Frame-Options:deny\nX-XSS-Protection:1; mode=block\nAccess-Control-Allow-Origin:*",
dashboard_rate_limit_window: 60,
dashboard_rate_limit_requests: 500
dashboard_rate_limit_requests: 500,
proxy_hostname: "",
proxy_port: "",
proxy_username: "",
proxy_password: "",
proxy_type: "https"
});

/**
Expand Down
38 changes: 38 additions & 0 deletions api/utils/countly-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

const got = require('got');
const FormData = require('form-data');
const plugins = require('../../../plugins/pluginManager.js');
const {HttpsProxyAgent, HttpProxyAgent} = require('hpagent');

var initParams = function(uri, options, callback) {

Expand All @@ -30,6 +32,42 @@ var initParams = function(uri, options, callback) {

params.callback = callback || params.callback;

var config = plugins.getConfig("security");

if (config && config.proxy_hostname) {
if (!params.options) {
params.options = {}; // Create options object if it's undefined
}

const proxyType = config.proxy_type || "https";
const proxyUrlBase = `${proxyType}://${config.proxy_hostname}:${config.proxy_port}`;

let proxyUrl = proxyUrlBase;

if (config.proxy_username && config.proxy_password) {
proxyUrl = `${proxyType}://${config.proxy_username}:${config.proxy_password}@${config.proxy_hostname}:${config.proxy_port}`;
}

const agentOptions = {
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: proxyUrl,
};

if (proxyType === "https") {
params.options.agent = {
https: new HttpsProxyAgent(agentOptions)
};
}
else {
params.options.agent = {
http: new HttpProxyAgent(agentOptions)
};
}
}

return params;
};
Expand Down
Loading

0 comments on commit 117b1d1

Please sign in to comment.