9

If we want to get the IP address or range of a Firebase functions in a project, how can we do? Because we have to provide this IP range for specific server permission.

shortstopmin
  • 175
  • 3
  • 8
  • See https://cloud.google.com/compute/docs/faq#where_can_i_find_product_name_short_ip_ranges – Frank van Puffelen Nov 23 '17 at 05:00
  • Cool, great. Thanks. – shortstopmin Nov 23 '17 at 05:27
  • @FrankvanPuffelen so Firebase IP addresses may change over a time? I want to share access to ElasticSearch instance using IP filtration so this way I can simply add Firebase Functions IP to the whitelist of IP addresses on ElasticSearch side. Can this be a solution for such kind of a problem? – neustart47 Jul 09 '18 at 14:05

2 Answers2

0

Use this code to get actual list of ips

from dns import resolver
import ipaddress
import re


domain_pattern = re.compile('(([\da-zA-Z_])([_\w-]{,62})\.){,127}(([\da-zA-Z])[_\w-]{,61})?([\da-zA-Z]\.((xn\-\-[a-zA-Z\d]+)|([a-zA-Z\d]{2,})))$', re.IGNORECASE)


def parse_spf(domain):
    ips = set()
    answers = resolver.query(domain.strip(), "TXT")
    for rdata in answers:
        splitted = rdata.to_text().split(' ')
        for split in splitted:
            split = split.strip()

            if split.startswith('ip4:') or split.startswith('ip6:'):
                split = split.lstrip('ip4:')
                split = split.lstrip('ip6:')

                try:
                    ipaddress.ip_address(split)
                    ips.add(split)
                    continue
                except ValueError:
                    pass

                try:
                    ipaddress.ip_network(split)
                    ips.add(split)
                    continue
                except ValueError:
                    pass

            if split.startswith('include:'):
                split = split.lstrip('include:')
                if domain_pattern.match(split):
                    ips.update(parse_spf(split))
                    continue

            print("Unrecognized entry", split)

    return ips


if __name__ == "__main__":
    domain = '_cloud-netblocks.googleusercontent.com'

    ips = parse_spf(domain=domain)

    for ip in ips:
        print(ip)
Pavel Cisar
  • 141
  • 1
  • 3
-1

In your function, request http://httpbin.org/ip to get your IP address.

northtree
  • 6,212
  • 9
  • 45
  • 66