【问题标题】:Firebase/NodeJS behind proxy server代理服务器后面的 Firebase/NodeJS
【发布时间】:2017-08-23 18:23:42
【问题描述】:

当计算机位于公司代理后面时,如何在 NodeJS 上运行 Firebase 管理员?

npm 已经配置代理和 https-proxy。 npm 命令执行正常。

但 Firebase 会尝试直接访问互联网:

错误:通过“credential”属性提供给 initializeApp() 的凭据实现未能获取有效的 Google OAuth2 访问令牌,并出现以下错误:“connect ETIMEDOUT 216.58.203.45:443”。

我尝试更新firebase-admin下的faye-websocket\lib\faye\websocket\client.js来读取

  var Client = function(_url, protocols, options) {
  options = options || {};
  options.proxy = {
    origin: 'http://proxy.server.local:8080'
  };

我尝试了几种变体,但 nodejs 仍在尝试直接访问 216.58.203.45:443。我还应该更新什么才能使其正常工作?

【问题讨论】:

  • 您收到的错误不是来自实时数据库,而是来自 Google 端点 (https://accounts.google.com/o/oauth2/token),用于将您的服务帐户生成的 JWT 交换为 Google OAuth2 访问令牌。您可能需要将该端点列入白名单。
  • 感谢您的回复。我可以从浏览器访问 URL,其他与 google auth 相关的 URL 可以在浏览器中正常工作。是的,你是对的,这是关于令牌刷新的。它没有正确通过代理,但我不知道如何强制它。
  • 抱歉,我不太了解如何绕过代理。您可能需要与运行您的公司代理的人交谈。

标签: node.js firebase proxy admin firebase-admin


【解决方案1】:

这就是我在公司代理后面运行 FirebaseAdmin 的方式。

请安装最新的 Firebase 管理节点版本,即 v6.4.0 及更高版本。此外,您还需要安装一个 tunnel2 库。

npm install firebase-admin@6.4.0
npm install tunnel2


var admin = require('firebase-admin');
var serviceAccount = require('path/to/serviceAccountKey.json');
const tunnel = require('tunnel2')
// Create your Proxy Agent 
// Please choose your tunneling method accordingly, my case
// is httpsoverHttp, yours might be httpsoverHttps
const proxyAgent = tunnel.httpsOverHttp({
    proxy: {
      host: 'yourProxyHost',
      port: yourProxyPort,
      proxyAuth: 'user:password' // Optional, required only if your proxy require authentication
    }
});
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount, proxyAgent),
  databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});

希望对您有所帮助。我为此写了一篇文章。你可以参考那里 the article here

【讨论】:

    【解决方案2】:

    因为我花了一段时间才弄清楚它是如何在这里工作的,所以它适用于任何搜索:

    const proxyAgent = tunnel.httpsOverHttp({
      proxy: {
        host: 'YOUR_PROXY_HOST',
        port: YOUR_PROXY_PORT,
        proxyAuth: 'user:password', // Optional, required only if your proxy require authentication
      },
    });
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount, proxyAgent),
      databaseURL: 'https://<DATABASE_NAME>.firebaseio.com',
      httpAgent: proxyAgent, //this is what I was missing
    });
    

    【讨论】:

    • 它对我有用,即使我在 initializeApp() 调用中没有 httpAgent: 对象
    • 您的 Firebase Admin SDK 实例在代理后面运行并且没有它也能正常工作?对我来说它没有,我不得不搜索很长时间才能找出丢失的东西,所以我在这里发表了评论。仅在 hiranya 的最后一条评论中github.com/firebase/firebase-admin-node/issues/719 此处提及
    • 我刚刚检查了我所有的脚本。有些有 httpgent:,有些没有(仅在 admin.credential.cert() 中提供)。但是它们都在代理后面工作。顺便说一句,您的代码在 httpAgent: 之前缺少一个逗号
    猜你喜欢
    • 2019-08-03
    • 2014-07-17
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    相关资源
    最近更新 更多