mitmproxy
): private createProxyAwareSlackClient(token: any) {
const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
const slackClientOptions = { logLevel: LogLevel.DEBUG };
if (proxyUrl) {
slackClientOptions['agent'] =
new URL(proxyUrl).protocol === 'http:' ? new HttpProxyAgent(proxyUrl, {rejectUnauthorized: false}) : new HttpsProxyAgent(proxyUrl, {rejectUnauthorized: false});
}
return new WebClient(token, slackClientOptions);
}
async firstHandshake(settings: any) {
const slackWebClient = this.createProxyAwareSlackClient(undefined);
let response: WebAPICallResult;
try {
response = await slackWebClient.oauth.v2.access({
client_id: process.env.SLACK_CLIENT_ID,
client_secret: process.env.SLACK_CLIENT_SECRET,
code: settings.slack_code,
redirect_uri: settings.redirect_uri
});
} catch (e) {
logger.error(e);
throw createError(500, 'Cannot get slack api key');
}
rejectUnauthorized: false
以避免验证 slack.com 证书。然而,证书验证发生并失败,并在客户端中出现以下错误(其余端点源代码):[WARN] web-api:WebClient:8 http request failed unable to verify the first certificate
[DEBUG] web-api:WebClient:8 http request url: https://slack.com/api/oauth.v2.access
[DEBUG] web-api:WebClient:8 http request body: {"client_id":"id","client_secret":"secret","code":"some_code","redirect_uri":"some_url"}
[DEBUG] web-api:WebClient:8 http request headers: {}
[WARN] web-api:WebClient:8 http request failed unable to verify the first certificate
并且在
mitmproxy
日志中看到以下错误:
[23:02:09.731][127.0.0.1:59940] client connect
[23:02:09.826][127.0.0.1:59940] server connect slack.com:443 (some_ip:443)
[23:02:09.911][127.0.0.1:59940] Client TLS handshake failed. The client disconnected during the handshake. If this happens consistently for slack.com, this may indicate that the client does not trust the proxy's certificate.
[23:02:09.913][127.0.0.1:59940] client disconnect
[23:02:09.914][127.0.0.1:59940] server disconnect slack.com:443 (some_ip:443)
需要注意的是:
HTTPS_PROXY 环境变量是
https://localhost:8080
(其中 mitmproxy
监听)
当 slack webclient 通过代理访问 slack.com 时,如何跳过 slack.com 证书验证?
如果 Web 服务环境中存在
NODE_TLS_REJECT_UNAUTHORIZED=0
env var,则没有问题,并且无需证书验证即可通过代理成功访问 slack API。但是,全局禁用证书验证是一个不好的做法。
您是否会尝试将
rejectUnauthorized
直接添加到 slackClientOptions
(除了已设置的之外)?
if (proxyUrl) {
slackClientOptions['agent'] =
new URL(proxyUrl).protocol === 'http:' ? new HttpProxyAgent(proxyUrl, {rejectUnauthorized: false}) : new HttpsProxyAgent(proxyUrl, {rejectUnauthorized: false});
//--- Here we add: ---
slackClientOptions.rejectUnauthorized = false;
//--- (finished) ---
}
这看起来与 HttpsProxyAgent 的 issue #35 有关,尽管尚不清楚它是否已解决。