(logger=None)
| 55 | |
| 56 | |
| 57 | def main(logger=None): |
| 58 | if logger is None: |
| 59 | logger = LoggingUtil.create_log(__name__) |
| 60 | |
| 61 | now = datetime.now() |
| 62 | print("Starting: " + str(now)) |
| 63 | logger.info("Starting...") |
| 64 | |
| 65 | mongo_connector = MongoConnector.MongoConnector() |
| 66 | akamai_collection = mongo_connector.get_akamai_ips_connection() |
| 67 | jobs_manager = JobsManager.JobsManager(mongo_connector, "upload_akamai_data") |
| 68 | jobs_manager.record_job_start() |
| 69 | |
| 70 | # Clear previous data |
| 71 | akamai_collection.delete_many({}) |
| 72 | |
| 73 | AKAMAI_DATA = {} |
| 74 | |
| 75 | # Record the date that the data was updated. |
| 76 | AKAMAI_DATA["created"] = datetime.now() |
| 77 | |
| 78 | try: |
| 79 | response = _requests_retry_session().get( |
| 80 | ZIP_FILE_LOCATION, |
| 81 | stream=True, |
| 82 | timeout=120, |
| 83 | headers={ |
| 84 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127" |
| 85 | }, |
| 86 | ) |
| 87 | except requests.exceptions.HTTPError as e: |
| 88 | logger.error("FATAL: HTTP error retrieving Akamai data: " + str(e)) |
| 89 | jobs_manager.record_job_error() |
| 90 | exit(1) |
| 91 | except Exception as ex: |
| 92 | logger.error("FATAL: Error fetching Akamai data: " + str(ex)) |
| 93 | jobs_manager.record_job_error() |
| 94 | exit(1) |
| 95 | |
| 96 | if response.status_code == 200: |
| 97 | # Create the list of known IPV4 ranges. |
| 98 | AKAMAI_DATA["ranges"] = [] |
| 99 | |
| 100 | ipZip = ZipFile(BytesIO(response.content)) |
| 101 | for line in ipZip.open("akamai_ipv4_CIDRs.txt").readlines(): |
| 102 | AKAMAI_DATA["ranges"].append( |
| 103 | { |
| 104 | "cidr": line.decode("utf-8").rstrip(), |
| 105 | } |
| 106 | ) |
| 107 | |
| 108 | # Create the list of known IPV6 ranges. |
| 109 | AKAMAI_DATA["ipv6_ranges"] = [] |
| 110 | for line in ipZip.open("akamai_ipv6_CIDRs.txt").readlines(): |
| 111 | AKAMAI_DATA["ipv6_ranges"].append( |
| 112 | { |
| 113 | "cidr": line.decode("utf-8").rstrip(), |
| 114 | } |
no test coverage detected