Alex
Alex

Reputation: 397

Firebase/NodeJS behind proxy server

How do I run Firebase admin on NodeJS when the computer is behind a corporate proxy?

npm has already config proxy and https-proxy. npm commands executes alright.

Firebase however, tries to directly access the internet:

Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "connect ETIMEDOUT 216.58.203.45:443".

I tried to update the faye-websocket\lib\faye\websocket\client.js under firebase-admin to read

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

I tried several variations, but nodejs is still trying to directly access 216.58.203.45:443. What else should I update to make it work?

Upvotes: 2

Views: 4879

Answers (2)

CRoNiC
CRoNiC

Reputation: 409

Since it took me quiet a while to figure out how it works here it is for anybody searching:

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
});

Upvotes: 0

Tek Loon
Tek Loon

Reputation: 266

This is how I run my FirebaseAdmin behind a corporate proxy.

Please install the latest Firebase Admin Node version, which is v6.4.0 and above. Besides, you also require to install a tunnel2 library.

npm install [email protected]
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'
});

Hope it helps you. I wrote an article for this. You can refer there the article here

Upvotes: 4

Related Questions