diff --git a/domain_admin/main.py b/domain_admin/main.py index a5b01cd338..99be70f6a4 100644 --- a/domain_admin/main.py +++ b/domain_admin/main.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- from __future__ import print_function, unicode_literals, absolute_import, division +import traceback + from flask import request, make_response, send_file from flask_cors import CORS from werkzeug.middleware.proxy_fix import ProxyFix @@ -16,6 +18,7 @@ from domain_admin.utils.flask_ext import handler from domain_admin.utils.flask_ext import register from domain_admin.utils.flask_ext.flask_app import FlaskApp +from domain_admin.utils.whois_util import whois_util app = FlaskApp( import_name=__name__, @@ -114,5 +117,11 @@ def init_app(flask_app): # 初始化全局常量配置 system_service.init_system_config(flask_app) + # 尝试更新whois_servers文件 + try: + whois_util.update_whois_servers() + except Exception as e: + logger.error(traceback.format_exc()) + init_app(app) diff --git a/domain_admin/utils/whois_util/config.py b/domain_admin/utils/whois_util/config.py index 9da45d5708..7b2c8c04b5 100644 --- a/domain_admin/utils/whois_util/config.py +++ b/domain_admin/utils/whois_util/config.py @@ -5,6 +5,13 @@ """ from __future__ import print_function, unicode_literals, absolute_import, division +from os import path + +from domain_admin.config import TEMP_DIR + +DEFAULT_WHOIS_SERVERS_PATH = path.join(path.dirname(__file__), 'whois-servers.txt') +TEMP_WHOIS_SERVERS_PATH = path.join(TEMP_DIR, 'whois-servers.txt') + # 根服务器地址 ROOT_SERVER = 'whois.iana.org' diff --git a/domain_admin/utils/whois_util/util.py b/domain_admin/utils/whois_util/util.py index 3a028a6256..f34731c56f 100644 --- a/domain_admin/utils/whois_util/util.py +++ b/domain_admin/utils/whois_util/util.py @@ -5,9 +5,14 @@ """ from __future__ import print_function, unicode_literals, absolute_import, division +import io +import os import socket from os import path -import io + +import requests + +from domain_admin.utils.whois_util.config import TEMP_WHOIS_SERVERS_PATH, DEFAULT_WHOIS_SERVERS_PATH def parse_whois_raw(whois_raw): @@ -73,8 +78,16 @@ def load_whois_servers(): } """ dct = {} + + # 获取文件路径 + whois_servers_file_path = None + if os.path.exists(TEMP_WHOIS_SERVERS_PATH): + whois_servers_file_path = TEMP_WHOIS_SERVERS_PATH + else: + whois_servers_file_path = DEFAULT_WHOIS_SERVERS_PATH + # fix:Python2 encoding error - with io.open(path.join(path.dirname(__file__), 'whois-servers.txt'), 'r', encoding='utf-8') as f: + with io.open(whois_servers_file_path, 'r', encoding='utf-8') as f: for line in f: if line.startswith(";"): pass @@ -86,3 +99,4 @@ def load_whois_servers(): dct[root.strip()] = server.strip() return dct + diff --git a/domain_admin/utils/whois_util/whois_util.py b/domain_admin/utils/whois_util/whois_util.py index a8063306d7..7b734e513c 100644 --- a/domain_admin/utils/whois_util/whois_util.py +++ b/domain_admin/utils/whois_util/whois_util.py @@ -8,11 +8,14 @@ """ from __future__ import print_function, unicode_literals, absolute_import, division + +import io import json import re from copy import deepcopy from datetime import datetime +import requests from dateutil import parser from domain_admin.log import logger @@ -21,8 +24,8 @@ CUSTOM_WHOIS_CONFIGS, DEFAULT_WHOIS_CONFIG, ROOT_SERVER, - REGISTRAR_CONFIG_MAP -) + REGISTRAR_CONFIG_MAP, + TEMP_WHOIS_SERVERS_PATH) from domain_admin.utils.whois_util.util import parse_whois_raw, get_whois_raw, load_whois_servers WHOIS_CONFIGS = None @@ -224,6 +227,18 @@ def get_domain_info(domain): return res +def update_whois_servers(): + logger.info("update whois-servers") + # https://github.com/WooMai/whois-servers/blob/master/list.txt + # url = 'https://raw.githubusercontent.com/WooMai/whois-servers/master/list.txt' + url = 'https://raw.gitmirror.com/WooMai/whois-servers/master/list.txt' + res = requests.get(url) + + if res.ok: + with io.open(TEMP_WHOIS_SERVERS_PATH, 'w', encoding='utf-8') as f: + f.write(res.text) + + if __name__ == '__main__': ret = get_domain_info('baidu.com') print(ret)