This function performs the new method of fetching the JSON file.
(google_dns, logger)
| 117 | |
| 118 | |
| 119 | def json_search(google_dns, logger): |
| 120 | """ |
| 121 | This function performs the new method of fetching the JSON file. |
| 122 | """ |
| 123 | |
| 124 | ipv4_ranges = [] |
| 125 | ipv6_ranges = [] |
| 126 | |
| 127 | try: |
| 128 | req = _requests_retry_session().get( |
| 129 | "https://www.gstatic.com/ipranges/cloud.json", timeout=120 |
| 130 | ) |
| 131 | except Exception as ex: |
| 132 | logger.error("Google GCP range download attempts failed!") |
| 133 | logger.error(str(ex)) |
| 134 | return None, None |
| 135 | |
| 136 | if req.status_code != 200: |
| 137 | logger.debug("HTTP Error fetching GCP ranges!") |
| 138 | return None |
| 139 | |
| 140 | json_results = json.loads(req.text) |
| 141 | |
| 142 | already_seen = set() |
| 143 | |
| 144 | try: |
| 145 | for entry in json_results["prefixes"]: |
| 146 | if "ipv4Prefix" in entry and entry["ipv4Prefix"] not in already_seen: |
| 147 | ipv4_ranges.append( |
| 148 | { |
| 149 | "ip_prefix": entry["ipv4Prefix"], |
| 150 | "service": entry["service"], |
| 151 | "scope": entry["scope"], |
| 152 | } |
| 153 | ) |
| 154 | already_seen.add(entry["ipv4Prefix"]) |
| 155 | elif "ipv6Prefix" in entry and entry["ipv6Prefix"] not in already_seen: |
| 156 | ipv6_ranges.append( |
| 157 | { |
| 158 | "ipv6_prefix": entry["ipv6Prefix"], |
| 159 | "service": entry["service"], |
| 160 | "scope": entry["scope"], |
| 161 | } |
| 162 | ) |
| 163 | already_seen.add(entry["ipv6Prefix"]) |
| 164 | except Exception as ex: |
| 165 | logger.error("Error parsing GCP ranges!") |
| 166 | logger.error(str(ex)) |
| 167 | return None, None |
| 168 | |
| 169 | return ipv4_ranges, ipv6_ranges |
| 170 | |
| 171 | |
| 172 | def main(logger=None): |
no test coverage detected