如何获取Firebase功能的IP地址或范围?

时间:2017-11-23 04:47:16

标签: firebase google-cloud-functions

如果我们想在项目中获取Firebase功能的IP地址或范围,我们该怎么办? 因为我们必须为特定服务器权限提供此IP范围。

2 个答案:

答案 0 :(得分:0)

在您的职能中,请求http://httpbin.org/ip来获取您的IP地址。

答案 1 :(得分:0)

使用此代码获取ip的实际列表

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)