# -*- coding: utf-8 -*-1# This program is free software; you can redistribute it and/or modify2# it under the terms of the GNU General Public License as published by3# the Free Software Foundation; either version 2 of the License, or4# (at your option) any later version.5#6# This program is distributed in the hope that it will be useful,7# but WITHOUT ANY WARRANTY; without even the implied warranty of8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9# GNU General Public License for more details.10#11# You should have received a copy of the GNU General Public License12# along with this program; if not, write to the Free Software13# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,14# MA 02110-1301, USA.15#16# Author: Mauro Soria1718from __future__ import annotations1920from socket import getaddrinfo21from typing import Any2223_dns_cache: dict[tuple[str, int], list[Any]] = {}242526def cache_dns(domain: str, port: int, addr: str) -> None:27_dns_cache[domain, port] = getaddrinfo(addr, port)282930def cached_getaddrinfo(*args: Any, **kwargs: int) -> list[Any]:31"""32Replacement for socket.getaddrinfo, they are the same but this function33does cache the answer to improve the performance34"""3536host, port = args[:2]37if (host, port) not in _dns_cache:38_dns_cache[host, port] = getaddrinfo(*args, **kwargs)3940return _dns_cache[host, port]414243