Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active January 12, 2022 05:51
Show Gist options
  • Save katowulf/6fffffb45ee5cbfbca6c3511e5d19528 to your computer and use it in GitHub Desktop.
Save katowulf/6fffffb45ee5cbfbca6c3511e5d19528 to your computer and use it in GitHub Desktop.
Print IP address and headers in Cloud Functions
const functions = require('firebase-functions');
const util = require('util');
exports.helloWorld = functions.https.onRequest((req, res) => {
// For Firebase Hosting URIs, use req.headers['fastly-client-ip']
// For callable functions, use rawRequest
// Some users have better success with req.headers['x-appengine-user-ip']
const ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const headers = JSON.stringify(req.headers, null, 2);
const message = util.format("<pre>Hello world!\n\nYour IP address: %s\n\nRequest headers: %s</pre>", ipAddress, headers);
res.send(message);
});
@vishalvijay
Copy link

vishalvijay commented Aug 9, 2018

Hello world!

Your IP address: 130.211.129.169, 130.211.129.169

Request headers: {
  "host": "us-central1-xyz-xyz.cloudfunctions.net",
  "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
  "accept-language": "en-US,en;q=0.9",
  "cache-control": "no-cache, no-store",
  "fastly-client": "1",
  "fastly-client-ip": "103.228.220.xxx",
  "fastly-ff": "s48hDkrIORDrAz3sjuyh5gIWcInqIjSROZ47KAY+w/I=!MAA!cache-maa18323-MAA, s48hDkrIORDrAz3sjuyh5gIWcInqIjSROZ47KAY+w/I=!MAA!cache-maa18320-MAA",
  "fastly-orig-accept-encoding": "gzip, deflate, br",
  "fastly-ssl": "1",
  "fastly-temp-xff": "103.228.220.xxx, 103.228.220.xxx",
  "function-execution-id": "pxsrd61ahc7h",
  "pragma": "no-cache",
  "upgrade-insecure-requests": "1",
  "x-appengine-api-ticket": "22dc0a59f7506e99",
  "x-appengine-city": "?",
  "x-appengine-citylatlong": "0.000000,0.000000",
  "x-appengine-country": "US",
  "x-appengine-https": "on",
  "x-appengine-region": "?",
  "x-appengine-user-ip": "130.211.129.169",
  "x-cloud-trace-context": "ab97ae8fc4f1d6d09d7cd7b951556b09/6319039451338514513;o=1",
  "x-forwarded-for": "130.211.129.169, 130.211.129.169",
  "x-forwarded-host": "fun-api.xyz.com",
  "x-forwarded-proto": "https",
  "x-forwarded-server": "cache-maa18323-MAA",
  "x-forwarded-url": "/helloWorld",
  "x-nginx-proxy": "true",
  "x-real-ip": "157.52.84.20",
  "x-timer": "S1533821652.566928,VS0",
  "x-varnish": "2985375639, 3236888159",
  "accept-encoding": "gzip"
}

The above code is incorrect if the request is rewritten by firebase hosting. Here my actual IP is in header fastly-client-ip

@lpellegr
Copy link

It seems this has changed recently without any announcements :X

@jaschaio
Copy link

jaschaio commented Feb 23, 2019

This is still working for me using:

req.headers['x-appengine-user-ip'] || req.header['x-forwarded-for'] || req.connection.remoteAddress

@rolandszpond
Copy link

if callable functions, the request headers will be in context.rawRequest.
See documenation: https://firebase.google.com/docs/reference/functions/functions.https.html?authuser=0#.CallableContext

@yzalvov
Copy link

yzalvov commented Jul 2, 2019

@rolandszpond, thanks for the tip

@buryo
Copy link

buryo commented Jul 27, 2019

My req.header and req.headers both doesn't have anything. Can u check this please https://stackoverflow.com/questions/57104165/not-all-headers-are-shown-with-firebase-onrequest

@DharmarajX24
Copy link

I don't know why I have to use res.rawRequest.headers['x-forwarded-for']

@BrodaNoel
Copy link

2022 and Google still not published any documentation about how to get the client IP.
Increible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment