Skip to content

Commit

Permalink
增加 自动更新whois文件的机制 fix:#95
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed Apr 1, 2024
1 parent 4c2e86d commit e21053c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
9 changes: 9 additions & 0 deletions domain_admin/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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__,
Expand Down Expand Up @@ -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)
7 changes: 7 additions & 0 deletions domain_admin/utils/whois_util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
18 changes: 16 additions & 2 deletions domain_admin/utils/whois_util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -86,3 +99,4 @@ def load_whois_servers():
dct[root.strip()] = server.strip()

return dct

19 changes: 17 additions & 2 deletions domain_admin/utils/whois_util/whois_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit e21053c

Please sign in to comment.