|
9 | 9 | import re
|
10 | 10 | import sys
|
11 | 11 | from urllib import parse as urlparse
|
12 |
| -from urllib.error import URLError |
13 | 12 |
|
14 | 13 | # Third-Party Libraries
|
15 | 14 | import OpenSSL
|
16 |
| - |
17 |
| -# Unable to find type stubs for the publicsuffix package. |
18 |
| -from publicsuffix import PublicSuffixList, fetch # type: ignore |
| 15 | +from publicsuffixlist.compat import PublicSuffixList # type: ignore |
| 16 | +from publicsuffixlist.update import updatePSL # type: ignore |
19 | 17 | import requests
|
20 | 18 |
|
21 | 19 | # Unable to find type stubs for the sslyze package.
|
@@ -1594,21 +1592,33 @@ def load_preload_list():
|
1594 | 1592 | return fully_preloaded
|
1595 | 1593 |
|
1596 | 1594 |
|
1597 |
| -# Returns an instantiated PublicSuffixList object, and the |
1598 |
| -# list of lines read from the file. |
1599 |
| -def load_suffix_list(): |
| 1595 | +# Returns an instantiated PublicSuffixList object. |
| 1596 | +def load_suffix_list(cache_suffix_list=None, update_list=False): |
1600 | 1597 | """Download and load the public suffix list."""
|
1601 |
| - # File does not exist, download current list and cache it at given location. |
1602 |
| - utils.debug("Downloading the Public Suffix List...", divider=True) |
1603 |
| - try: |
1604 |
| - cache_file = fetch() |
1605 |
| - except URLError as err: |
1606 |
| - logging.exception("Unable to download the Public Suffix List...") |
1607 |
| - utils.debug(err) |
1608 |
| - return [] |
1609 |
| - content = cache_file.readlines() |
1610 |
| - suffixes = PublicSuffixList(content) |
1611 |
| - return suffixes, content |
| 1598 | + if update_list: |
| 1599 | + utils.debug("Downloading the Public Suffix List...", divider=True) |
| 1600 | + try: |
| 1601 | + # Update the local copy |
| 1602 | + if cache_suffix_list: |
| 1603 | + updatePSL(cache_suffix_list) |
| 1604 | + # Update the built-in copy |
| 1605 | + else: |
| 1606 | + updatePSL() |
| 1607 | + except Exception as err: |
| 1608 | + logging.exception("Unable to download the Public Suffix List...") |
| 1609 | + utils.debug(err) |
| 1610 | + return None |
| 1611 | + |
| 1612 | + # Use the local copy |
| 1613 | + if cache_suffix_list: |
| 1614 | + utils.debug("Using cached Public Suffix List.", divider=True) |
| 1615 | + with codecs.open(cache_suffix_list, encoding="utf-8") as cache_file: |
| 1616 | + suffixes = PublicSuffixList(cache_file) |
| 1617 | + # Use the built-in copy |
| 1618 | + else: |
| 1619 | + suffixes = PublicSuffixList() |
| 1620 | + |
| 1621 | + return suffixes |
1612 | 1622 |
|
1613 | 1623 |
|
1614 | 1624 | def initialize_external_data(
|
@@ -1696,18 +1706,14 @@ def initialize_external_data(
|
1696 | 1706 |
|
1697 | 1707 | # Load Mozilla's current Public Suffix list.
|
1698 | 1708 | if SUFFIX_LIST is None:
|
1699 |
| - if cache_suffix_list and os.path.exists(cache_suffix_list): |
1700 |
| - utils.debug("Using cached suffix list.", divider=True) |
1701 |
| - with codecs.open(cache_suffix_list, encoding="utf-8") as cache_file: |
1702 |
| - SUFFIX_LIST = PublicSuffixList(cache_file) |
| 1709 | + if cache_suffix_list: |
| 1710 | + # Retrieve the list if the path does not exist otherwise use the cached copy |
| 1711 | + SUFFIX_LIST = load_suffix_list( |
| 1712 | + cache_suffix_list, not os.path.exists(cache_suffix_list) |
| 1713 | + ) |
1703 | 1714 | else:
|
1704 |
| - SUFFIX_LIST, raw_content = load_suffix_list() |
1705 |
| - |
1706 |
| - if cache_suffix_list: |
1707 |
| - utils.debug( |
1708 |
| - "Caching suffix list at %s", cache_suffix_list, divider=True |
1709 |
| - ) |
1710 |
| - utils.write("".join(raw_content), cache_suffix_list) |
| 1715 | + # Load the built-in PSL |
| 1716 | + SUFFIX_LIST = load_suffix_list() |
1711 | 1717 |
|
1712 | 1718 |
|
1713 | 1719 | def inspect_domains(domains, options):
|
|
0 commit comments