150 lines
4.3 KiB
Python
150 lines
4.3 KiB
Python
# -*- coding: UTF-8 -*-
|
|
'''
|
|
@Project :domainScanDemo
|
|
@File :domain_processor.py
|
|
@IDE :PyCharm
|
|
@Author :梦伴
|
|
@Date :2026/4/8 23:51
|
|
@explain : 域名处理器
|
|
'''
|
|
|
|
import re
|
|
import tldextract
|
|
from loguru import logger
|
|
from app.utils.database import Database
|
|
from app.utils.domain_utils import normalize_domain
|
|
|
|
|
|
class DomainProcessor:
|
|
"""
|
|
域名处理器
|
|
"""
|
|
|
|
def __init__(self):
|
|
"""
|
|
初始化域名处理器
|
|
"""
|
|
self.db = Database()
|
|
|
|
def process_domain(self, domain):
|
|
"""
|
|
处理域名
|
|
|
|
:param domain: 域名
|
|
:return: dict - 处理结果
|
|
"""
|
|
result = {
|
|
'original': domain,
|
|
'normalized': None,
|
|
'tld': None,
|
|
'valid': False,
|
|
'reason': ''
|
|
}
|
|
|
|
try:
|
|
# 标准化域名
|
|
normalized = normalize_domain(domain)
|
|
if not normalized:
|
|
result['reason'] = '无效域名格式'
|
|
return result
|
|
|
|
# 提取顶级域名
|
|
ext = tldextract.extract(normalized)
|
|
tld = ext.suffix
|
|
|
|
# 检查顶级域名
|
|
if tld not in ['com', 'net']:
|
|
result['reason'] = '不支持的顶级域名'
|
|
return result
|
|
|
|
result['normalized'] = normalized
|
|
result['tld'] = tld
|
|
result['valid'] = True
|
|
|
|
return result
|
|
except Exception as e:
|
|
logger.error(f"处理域名出错: {e}")
|
|
result['reason'] = f"处理出错: {str(e)}"
|
|
return result
|
|
|
|
def batch_process(self, domains):
|
|
"""
|
|
批量处理域名
|
|
|
|
:param domains: 域名列表
|
|
:return: list - 处理结果列表
|
|
"""
|
|
results = []
|
|
for domain in domains:
|
|
result = self.process_domain(domain)
|
|
results.append(result)
|
|
return results
|
|
|
|
def filter_valid_domains(self, domains):
|
|
"""
|
|
过滤有效的域名
|
|
|
|
:param domains: 域名列表
|
|
:return: list - 有效的域名列表
|
|
"""
|
|
valid_domains = []
|
|
for domain in domains:
|
|
result = self.process_domain(domain)
|
|
if result['valid']:
|
|
valid_domains.append(result['normalized'])
|
|
return valid_domains
|
|
|
|
def update_domain_status(self, domain_id, status_type, status_value):
|
|
"""
|
|
更新域名状态
|
|
|
|
:param domain_id: 域名ID
|
|
:param status_type: 状态类型
|
|
:param status_value: 状态值
|
|
:return: bool - 是否更新成功
|
|
"""
|
|
try:
|
|
if status_type == 'use_status':
|
|
return self.db.update_domain_use_status(domain_id, status_value)
|
|
elif status_type == 'detect_status':
|
|
return self.db.update_domain_detect_status(domain_id, status_value)
|
|
elif status_type == 'register_status':
|
|
return self.db.update_domain_register_status(domain_id, status_value)
|
|
else:
|
|
logger.error(f"未知的状态类型: {status_type}")
|
|
return False
|
|
except Exception as e:
|
|
logger.error(f"更新域名状态出错: {e}")
|
|
return False
|
|
|
|
def blacklist_domain(self, domain_id, reason):
|
|
"""
|
|
将域名加入黑名单
|
|
|
|
:param domain_id: 域名ID
|
|
:param reason: 黑名单原因
|
|
:return: bool - 是否操作成功
|
|
"""
|
|
try:
|
|
# 更新检测状态为黑名单
|
|
if self.db.update_domain_detect_status(domain_id, 4): # 4 表示黑名单
|
|
# 添加到黑名单表
|
|
domain = self.db.get_domain_by_id(domain_id)
|
|
if domain:
|
|
return self.db.add_to_blacklist(domain['domain'], reason)
|
|
return False
|
|
except Exception as e:
|
|
logger.error(f"将域名加入黑名单出错: {e}")
|
|
return False
|
|
|
|
def get_domain_statistics(self):
|
|
"""
|
|
获取域名统计信息
|
|
|
|
:return: dict - 统计信息
|
|
"""
|
|
try:
|
|
return self.db.get_domain_statistics()
|
|
except Exception as e:
|
|
logger.error(f"获取域名统计信息出错: {e}")
|
|
return {} |