Skip to content

Commit

Permalink
Remove duplicates of getUnsafeAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Jan 10, 2022
1 parent 03d2e15 commit b665425
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 48 deletions.
18 changes: 2 additions & 16 deletions app/apps/server/bridges/http.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import http from 'http';
import https from 'https';

import { fetch } from 'meteor/fetch';
import { HttpBridge } from '@rocket.chat/apps-engine/server/bridges/HttpBridge';
import { IHttpResponse } from '@rocket.chat/apps-engine/definition/accessors';
import { IHttpBridgeRequestInfo } from '@rocket.chat/apps-engine/server/bridges';

import { AppServerOrchestrator } from '../orchestrator';

const getUnsafeAgent = (protocol: string): http.Agent | https.Agent | null => {
if (protocol === 'http:') {
return new http.Agent();
}
if (protocol === 'https:') {
return new https.Agent({
rejectUnauthorized: false,
});
}
return null;
};
import { getUnsafeAgent } from '../../../../server/lib/getUnsafeAgent';

export class AppHttpBridge extends HttpBridge {
// eslint-disable-next-line no-empty-function
Expand Down Expand Up @@ -84,7 +70,7 @@ export class AppHttpBridge extends HttpBridge {
headers,
...(((request.hasOwnProperty('strictSSL') && !request.strictSSL) ||
(request.hasOwnProperty('rejectUnauthorized') && request.rejectUnauthorized)) && {
agent: getUnsafeAgent(url.protocol),
agent: getUnsafeAgent(url.protocol === 'https:' ? 'https:' : 'http:'),
}),
});

Expand Down
18 changes: 2 additions & 16 deletions app/integrations/server/lib/triggerHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import http from 'http';
import https from 'https';
import vm from 'vm';

import { Meteor } from 'meteor/meteor';
Expand All @@ -18,6 +16,7 @@ import { settings } from '../../../settings/server';
import { getRoomByNameOrIdWithOptionToJoin, processWebhookMessage } from '../../../lib/server';
import { outgoingLogger } from '../logger';
import { integrations } from '../../lib/rocketchat';
import { getUnsafeAgent } from '../../../../server/lib/getUnsafeAgent';

export class RocketChatIntegrationHandler {
constructor() {
Expand Down Expand Up @@ -762,19 +761,6 @@ export class RocketChatIntegrationHandler {
httpCallData: opts.data,
});

const getUnsafeAgent = (protocol) => {
const options = {
rejectUnauthorized: false,
};
if (protocol === 'http') {
return new http.Agent(options);
}
if (protocol === 'https') {
return new https.Agent(options);
}
return null;
};

if (opts.data) {
opts.headers['Content-Type'] = 'application/json';
}
Expand All @@ -783,7 +769,7 @@ export class RocketChatIntegrationHandler {
method: opts.method,
headers: opts.headers,
...(settings.get('Allow_Invalid_SelfSigned_Certs') && {
agent: getUnsafeAgent(opts.url.startsWith('https') ? 'https' : 'http'),
agent: getUnsafeAgent(opts.url.startsWith('https:') ? 'https:' : 'http:'),
}),
...(opts.data && { body: JSON.stringify(opts.data) }),
})
Expand Down
18 changes: 2 additions & 16 deletions app/oembed/server/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import http from 'http';
import https from 'https';
import URL from 'url';
import querystring from 'querystring';

Expand All @@ -14,10 +12,11 @@ import jschardet from 'jschardet';
import { Messages } from '../../models/server';
import { OEmbedCache } from '../../models/server/raw';
import { callbacks } from '../../../lib/callbacks';
import { settings } from '../../settings';
import { settings } from '../../settings/server';
import { isURL } from '../../utils/lib/isURL';
import { SystemLogger } from '../../../server/lib/logger/system';
import { Info } from '../../utils/server';
import { getUnsafeAgent } from '../../../server/lib/getUnsafeAgent';

const OEmbed = {};

Expand Down Expand Up @@ -63,19 +62,6 @@ const toUtf8 = function (contentType, body) {
return iconv.decode(body, getCharset(contentType, body));
};

const getUnsafeAgent = (protocol) => {
const options = {
rejectUnauthorized: false,
};
if (protocol === 'http:') {
return new http.Agent(options);
}
if (protocol === 'https:') {
return new https.Agent(options);
}
return null;
};

const getUrlContent = async function (urlObj, redirectCount = 5) {
if (_.isString(urlObj)) {
urlObj = URL.parse(urlObj);
Expand Down
14 changes: 14 additions & 0 deletions server/lib/getUnsafeAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import http from 'http';
import https from 'https';

export function getUnsafeAgent(protocol: 'http:' | 'https:'): http.Agent | https.Agent | null {
if (protocol === 'http:') {
return new http.Agent();
}
if (protocol === 'https:') {
return new https.Agent({
rejectUnauthorized: false,
});
}
return null;
}

0 comments on commit b665425

Please sign in to comment.