diff --git a/domain_admin/service/cache_domain_info_service.py b/domain_admin/service/cache_domain_info_service.py index b8762de622..7c8bee42b7 100644 --- a/domain_admin/service/cache_domain_info_service.py +++ b/domain_admin/service/cache_domain_info_service.py @@ -24,7 +24,7 @@ def get_domain_info(domain: str) -> CacheDomainInfoModel: # 不存在或者已过期,重新获取 if not row or row.is_expired is True: - domain_whois = whois_util.get_domain_whois(root_domain) + domain_whois = whois_util.get_domain_info(root_domain) if domain_whois is None: raise Exception("域名信息获取失败") diff --git a/domain_admin/utils/text_util.py b/domain_admin/utils/text_util.py new file mode 100644 index 0000000000..bea4bd0fa6 --- /dev/null +++ b/domain_admin/utils/text_util.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +""" +@File : text_util.py +@Date : 2023-05-30 +""" +import re + + +def has_chinese(text: str) -> bool: + """ + 判断是否包含中文 + :param text: + :return: + """ + result = re.match("[\u4e00-\u9fa5]+", text) + return True if result else False + + +def extract_chinese(text: str) -> str: + """ + 提取包含的中文 + :param text: + :return: + """ + result = re.match("[\u4e00-\u9fa5]+", text) + if result: + return result.group(0) + else: + return "" diff --git a/domain_admin/utils/whois_util/whois_util.py b/domain_admin/utils/whois_util/whois_util.py index fcbf68d50b..836e7915e4 100644 --- a/domain_admin/utils/whois_util/whois_util.py +++ b/domain_admin/utils/whois_util/whois_util.py @@ -10,13 +10,36 @@ from dateutil import parser from domain_admin.log import logger -from domain_admin.utils import json_util +from domain_admin.utils import json_util, text_util, domain_util from domain_admin.utils.whois_util.config import CUSTOM_WHOIS_CONFIGS, DEFAULT_WHOIS_CONFIG from domain_admin.utils.whois_util.util import parse_whois_raw, get_whois_raw, load_whois_servers WHOIS_CONFIGS = None +def resolve_domain(domain: str) -> str: + """ + 域名转换 + :param domain: + :return: + """ + # 解析出域名和顶级后缀 + extract_result = domain_util.extract_domain(domain) + + root_domain = extract_result.domain + suffix = extract_result.suffix + + # 处理包含中文的域名 + if text_util.has_chinese(root_domain): + chinese = text_util.extract_chinese(root_domain) + punycode = chinese.encode('punycode').decode() + root_domain = f"xn--{punycode}" + + domain_and_suffix = '.'.join([root_domain, suffix]) + + return domain_and_suffix + + def parse_time(time_str, time_format=None): """ 解析时间字符串为时间对象 @@ -124,6 +147,8 @@ def get_domain_info(domain: str): # 处理带端口号的域名 # if ':' in domain: # domain = domain.split(":")[0] + domain = resolve_domain(domain) + logger.debug("resolve_domain: %s", domain) res = get_domain_whois(domain) diff --git a/tests/utils/test_text_util.py b/tests/utils/test_text_util.py new file mode 100644 index 0000000000..3d398da046 --- /dev/null +++ b/tests/utils/test_text_util.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +""" +@File : test_text_util.py +@Date : 2023-05-30 +""" +from domain_admin.utils import text_util + + +def test_has_chinese(): + assert text_util.has_chinese("I love you") is False + assert text_util.has_chinese("我喜欢你") is True + assert text_util.has_chinese("我 love 你") is True diff --git a/tests/utils/test_whois_util.py b/tests/utils/test_whois_util.py index ab6e261fae..1bbffcfb12 100644 --- a/tests/utils/test_whois_util.py +++ b/tests/utils/test_whois_util.py @@ -4,8 +4,10 @@ @Date : 2022-10-22 @Author : Peng Shiyu """ +import re from domain_admin.utils import whois_util +from domain_admin.utils import text_util from domain_admin.utils.whois_util.config import ROOT_SERVER from domain_admin.utils.whois_util.util import get_whois_raw, parse_whois_raw @@ -30,7 +32,8 @@ def test_get_domain_info(): # 'dot.tk' # 'bilibili.tv' # 'wowma.jp' - 'www.otto.de' + # 'www.otto.de', + 'www.米梵家居.com' ] for domain in domain_list: