|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*-coding:UTF-8 -* |
| 3 | + |
| 4 | +import idna |
| 5 | +import ipaddress |
| 6 | +import socket |
| 7 | + |
| 8 | +from publicsuffixlist import PublicSuffixList |
| 9 | +from urllib.parse import urlparse, urlunparse |
| 10 | + |
| 11 | +def _ensure_bytes(binary): |
| 12 | + if isinstance(binary, bytes): |
| 13 | + return binary |
| 14 | + else: |
| 15 | + return binary.encode('utf-8') |
| 16 | + |
| 17 | + |
| 18 | +def _ensure_str(string): |
| 19 | + if isinstance(string, str): |
| 20 | + return string |
| 21 | + else: |
| 22 | + return string.decode('utf-8') |
| 23 | + |
| 24 | + |
| 25 | +class UrlNotDecoded(Exception): |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +# https://github.com/MISP/PyMISP/blob/main/pymisp/tools/_psl_faup.py |
| 30 | +class PSLFaup: |
| 31 | + """ |
| 32 | + Fake Faup Python Library using PSL for Windows support |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self): |
| 36 | + self.decoded = False |
| 37 | + self.psl = PublicSuffixList() |
| 38 | + self._url = None |
| 39 | + self._retval = {} |
| 40 | + self.ip_as_host = '' |
| 41 | + |
| 42 | + def _clear(self): |
| 43 | + self.decoded = False |
| 44 | + self._url = None |
| 45 | + self._retval = {} |
| 46 | + self.ip_as_host = '' |
| 47 | + |
| 48 | + def decode(self, url): |
| 49 | + """ |
| 50 | + This function creates a dict of all the url fields. |
| 51 | + :param url: The URL to normalize |
| 52 | + """ |
| 53 | + self._clear() |
| 54 | + if isinstance(url, bytes) and b'//' not in url[:10]: |
| 55 | + url = b'//' + url |
| 56 | + elif '//' not in url[:10]: |
| 57 | + url = '//' + url |
| 58 | + self._url = urlparse(url) |
| 59 | + |
| 60 | + if self._url is None: |
| 61 | + raise UrlNotDecoded("Unable to parse URL") |
| 62 | + |
| 63 | + self.ip_as_host = '' |
| 64 | + if self._url.hostname is None: |
| 65 | + raise UrlNotDecoded("Unable to parse URL") |
| 66 | + hostname = _ensure_str(self._url.hostname) |
| 67 | + try: |
| 68 | + ipv4_bytes = socket.inet_aton(hostname) |
| 69 | + ipv4 = ipaddress.IPv4Address(ipv4_bytes) |
| 70 | + self.ip_as_host = ipv4.compressed |
| 71 | + except (OSError, ValueError): |
| 72 | + try: |
| 73 | + addr, _, _ = hostname.partition('%') |
| 74 | + ipv6 = ipaddress.IPv6Address(addr) |
| 75 | + self.ip_as_host = ipv6.compressed |
| 76 | + except ValueError: |
| 77 | + pass |
| 78 | + |
| 79 | + self.decoded = True |
| 80 | + self._retval = {} |
| 81 | + |
| 82 | + @property |
| 83 | + def url(self): |
| 84 | + if not self.decoded or not self._url: |
| 85 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 86 | + |
| 87 | + if host := self.get_host(): |
| 88 | + netloc = host + ('' if self.get_port() is None else f':{self.get_port()}') |
| 89 | + return _ensure_bytes( |
| 90 | + urlunparse( |
| 91 | + (self.get_scheme(), netloc, self.get_resource_path(), |
| 92 | + '', self.get_query_string(), self.get_fragment(),) |
| 93 | + ) |
| 94 | + ) |
| 95 | + return None |
| 96 | + |
| 97 | + def get_credential(self): |
| 98 | + if not self.decoded or not self._url: |
| 99 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 100 | + |
| 101 | + if self._url.username and self._url.password: |
| 102 | + return _ensure_str(self._url.username) + ':' + _ensure_str(self._url.password) |
| 103 | + if self._url.username: |
| 104 | + return _ensure_str(self._url.username) |
| 105 | + return None |
| 106 | + |
| 107 | + def get_scheme(self): |
| 108 | + """ |
| 109 | + Get the scheme of the url given in the decode function |
| 110 | + :returns: The URL scheme |
| 111 | + """ |
| 112 | + if not self.decoded or not self._url: |
| 113 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 114 | + return _ensure_str(self._url.scheme if self._url.scheme else '') |
| 115 | + |
| 116 | + def get_host(self): |
| 117 | + if not self.decoded or not self._url: |
| 118 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 119 | + |
| 120 | + if self._url.hostname is None: |
| 121 | + return None |
| 122 | + elif self._url.hostname.isascii(): |
| 123 | + return _ensure_str(self._url.hostname) |
| 124 | + else: |
| 125 | + return _ensure_str(idna.encode(self._url.hostname, uts46=True)) |
| 126 | + |
| 127 | + def get_domain(self): |
| 128 | + if not self.decoded or not self._url: |
| 129 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 130 | + |
| 131 | + if self.get_host() is not None and not self.ip_as_host: |
| 132 | + return self.psl.privatesuffix(self.get_host()) |
| 133 | + return None |
| 134 | + |
| 135 | + def get_domain_without_tld(self): |
| 136 | + if not self.decoded or not self._url: |
| 137 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 138 | + |
| 139 | + if self.get_tld() is not None and not self.ip_as_host: |
| 140 | + if domain := self.get_domain(): |
| 141 | + return domain.rsplit(self.get_tld(), 1)[0].rstrip('.') |
| 142 | + return None |
| 143 | + |
| 144 | + def get_subdomain(self): |
| 145 | + if not self.decoded or not self._url: |
| 146 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 147 | + |
| 148 | + if self.get_host() is not None and not self.ip_as_host: |
| 149 | + domain = self.get_domain() |
| 150 | + host = self.get_host() |
| 151 | + if domain and host and domain in host: |
| 152 | + return host.rsplit(domain, 1)[0].rstrip('.') or None |
| 153 | + return None |
| 154 | + |
| 155 | + def get_tld(self): |
| 156 | + if not self.decoded or not self._url: |
| 157 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 158 | + |
| 159 | + if self.get_host() is not None and not self.ip_as_host: |
| 160 | + return self.psl.publicsuffix(self.get_host()) |
| 161 | + return None |
| 162 | + |
| 163 | + def get_port(self): |
| 164 | + if not self.decoded or not self._url: |
| 165 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 166 | + return self._url.port |
| 167 | + |
| 168 | + def get_resource_path(self): |
| 169 | + if not self.decoded or not self._url: |
| 170 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 171 | + |
| 172 | + return _ensure_str(self._url.path) |
| 173 | + |
| 174 | + def get_query_string(self): |
| 175 | + if not self.decoded or not self._url: |
| 176 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 177 | + |
| 178 | + return _ensure_str(self._url.query) |
| 179 | + |
| 180 | + def get_fragment(self): |
| 181 | + if not self.decoded or not self._url: |
| 182 | + raise UrlNotDecoded("You must call pslfaup.decode() first") |
| 183 | + |
| 184 | + return _ensure_str(self._url.fragment) |
| 185 | + |
| 186 | + def get(self): |
| 187 | + self._retval["scheme"] = self.get_scheme() |
| 188 | + self._retval["tld"] = self.get_tld() |
| 189 | + self._retval["domain"] = self.get_domain() |
| 190 | + # self._retval["domain_without_tld"] = self.get_domain_without_tld() |
| 191 | + self._retval["subdomain"] = self.get_subdomain() |
| 192 | + self._retval["host"] = self.get_host() |
| 193 | + self._retval["port"] = self.get_port() |
| 194 | + self._retval["resource_path"] = self.get_resource_path() |
| 195 | + self._retval["query_string"] = self.get_query_string() |
| 196 | + self._retval["fragment"] = self.get_fragment() |
| 197 | + self._retval["url"] = self.url |
| 198 | + return self._retval |
| 199 | + |
| 200 | + |
| 201 | +def get_domain(url): |
| 202 | + f = PSLFaup() |
| 203 | + f.decode(url) |
| 204 | + return f.get_domain() |
| 205 | + |
| 206 | +def get_url(url): |
| 207 | + f = PSLFaup() |
| 208 | + f.decode(url) |
| 209 | + return f.url |
| 210 | + |
| 211 | +def unparse_url(url): |
| 212 | + f = PSLFaup() |
| 213 | + f.decode(url) |
| 214 | + return f.get() |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == '__main__': |
| 218 | + print(unparse_url('Example.COM')) |
0 commit comments