Skip to content

Commit de67f14

Browse files
authored
Add support for proxy to connect to the Slack API (#750)
* add support for proxy to connect to slack api * add changelog entry
1 parent 6c58bd5 commit de67f14

File tree

8 files changed

+29
-4
lines changed

8 files changed

+29
-4
lines changed

changelog.d/750.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for proxy to connect to the Slack API.

config/config.sample-complete.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ rtm:
2727

2828
slack_hook_port: 9898
2929

30+
slack_proxy: "https://proxy.server.here:3128"
31+
3032
inbound_uri_prefix: "https://my.server.here:9898/"
3133

3234
oauth2:

config/config.sample.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ rtm:
7575
#
7676
slack_hook_port: 9898
7777

78+
# Optional. Proxy to connect to the Slack API.
79+
#
80+
#slack_proxy: "https://proxy.server.here:3128"
81+
7882
# Prefix of incoming requests to strip. This is NOT the bind host.
7983
# Unlike most of the other urls, this one cannot use localhost,
8084
# as this one must be publicly visible to the Slack API.

config/slack-config-schema.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ properties:
5454
type: string
5555
slack_hook_port:
5656
type: integer
57+
slack_proxy:
58+
type: string
5759
inbound_uri_prefix:
5860
type: string
5961
oauth2:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"axios": "^0.27.2",
4343
"classnames": "^2.3.2",
4444
"escape-string-regexp": "^4.0.0",
45+
"https-proxy-agent": "^5.0.1",
4546
"matrix-appservice-bridge": "^8.1.1",
4647
"matrix-widget-api": "^1.1.1",
4748
"minimist": "^1.2.6",

src/IConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export interface IConfig {
9090

9191
slack_hook_port?: number;
9292
slack_client_opts?: WebClientOptions;
93+
slack_proxy?: string;
9394
enable_metrics: boolean;
9495

9596
db?: {

src/SlackClientFactory.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Datastore, TeamEntry } from "./datastore/Models";
22
import { WebClient, WebClientOptions, LogLevel, Logger as SlackLogger, WebAPIPlatformError } from "@slack/web-api";
33
import { Logger } from "matrix-appservice-bridge";
44
import { TeamInfoResponse, AuthTestResponse, UsersInfoResponse } from "./SlackResponses";
5+
import { HttpsProxyAgent } from 'https-proxy-agent';
56

67
const webLog = new Logger("slack-api");
78
const log = new Logger("SlackClientFactory");
@@ -18,6 +19,7 @@ const AUTH_INTERVAL_MS = 5 * 60000;
1819

1920
interface RequiredConfigOptions {
2021
slack_client_opts?: WebClientOptions;
22+
slack_proxy?: string;
2123
auth_interval_ms?: number;
2224
}
2325

@@ -39,7 +41,12 @@ export class SlackClientFactory {
3941
}
4042

4143
public async createClient(token: string): Promise<WebClient> {
42-
const opts = this.config.slack_client_opts ? this.config.slack_client_opts : undefined;
44+
const opts = this.config.slack_client_opts ? this.config.slack_client_opts : {};
45+
46+
if (this.config.slack_proxy) {
47+
opts.agent = new HttpsProxyAgent(this.config.slack_proxy);
48+
}
49+
4350
return new WebClient(token, {
4451
logger: {
4552
getLevel: () => LogLevel.DEBUG,

src/SlackRTMHandler.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RTMClient, LogLevel } from "@slack/rtm-api";
1+
import { RTMClient, LogLevel, RTMClientOptions } from "@slack/rtm-api";
22
import { Main, ISlackTeam } from "./Main";
33
import { SlackEventHandler } from "./SlackEventHandler";
44
import { Logger } from "matrix-appservice-bridge";
@@ -10,6 +10,7 @@ import { BridgedRoom } from "./BridgedRoom";
1010
import { SlackGhost } from "./SlackGhost";
1111
import { DenyReason } from "./AllowDenyList";
1212
import { createDM } from "./RoomCreation";
13+
import { HttpsProxyAgent } from 'https-proxy-agent';
1314

1415
const log = new Logger("SlackRTMHandler");
1516

@@ -200,7 +201,7 @@ export class SlackRTMHandler extends SlackEventHandler {
200201
const LOG_LEVELS = ["debug", "info", "warn", "error", "silent"];
201202
const connLog = new Logger(`RTM-${logLabel.slice(0, LOG_TEAM_LEN)}`);
202203
const logLevel = LOG_LEVELS.indexOf(this.main.config.rtm?.log_level || "silent");
203-
const rtm = new RTMClient(token, {
204+
const rtmOpts = {
204205
logLevel: LogLevel.DEBUG, // We will filter this ourselves.
205206
logger: {
206207
getLevel: () => LogLevel.DEBUG,
@@ -211,7 +212,13 @@ export class SlackRTMHandler extends SlackEventHandler {
211212
info: logLevel <= 2 ? connLog.info.bind(connLog) : () => {},
212213
error: logLevel <= 3 ? connLog.error.bind(connLog) : () => {},
213214
} as SlackLogger,
214-
});
215+
} as RTMClientOptions;
216+
217+
if (this.main.config.slack_proxy) {
218+
rtmOpts.agent = new HttpsProxyAgent(this.main.config.slack_proxy);
219+
}
220+
221+
const rtm = new RTMClient(token, rtmOpts);
215222

216223
rtm.on("error", (error) => {
217224
// We must handle this lest the process be killed.

0 commit comments

Comments
 (0)