初始化1
This commit is contained in:
126
dns-api/app.py
Normal file
126
dns-api/app.py
Normal file
@@ -0,0 +1,126 @@
|
||||
"""
|
||||
DNS 解析管理 API — 独立版本
|
||||
部署在 101.132.183.138:32210 上,仅提供阿里云 DNS 解析管理
|
||||
从 10.1.1.160 迁移而来,去掉了 Nginx/SSL 相关依赖
|
||||
"""
|
||||
import os
|
||||
import logging
|
||||
from flask import Flask, request
|
||||
from dns_service import DnsService
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s [%(levelname)s] %(name)s: %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['JSON_AS_ASCII'] = False # 中文不转 Unicode
|
||||
|
||||
# 初始化 DNS 服务(阿里云 AccessKey 从环境变量读取)
|
||||
dns_service = DnsService()
|
||||
|
||||
# 默认域名 zone,通过环境变量配置,默认 zgitm.com
|
||||
DEFAULT_ZONE = os.environ.get('DNS_DEFAULT_ZONE', 'zgitm.com')
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
# 健康检查
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
|
||||
@app.route('/api/nginx/health', methods=['GET'])
|
||||
def health_check():
|
||||
"""健康检查"""
|
||||
ali_key = os.environ.get('Ali_Key', '')
|
||||
return {
|
||||
'code': 0,
|
||||
'message': 'DNS Manager API is running',
|
||||
'data': {
|
||||
'service': 'dns-api',
|
||||
'version': '2.0',
|
||||
'default_zone': DEFAULT_ZONE,
|
||||
'ali_key_configured': bool(ali_key),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
# DNS 解析管理接口
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
|
||||
@app.route('/api/nginx/dns/records', methods=['GET'])
|
||||
def list_dns_records():
|
||||
"""获取 DNS 解析记录列表"""
|
||||
try:
|
||||
zone = request.args.get('zone', DEFAULT_ZONE)
|
||||
rr = request.args.get('rr', None)
|
||||
rtype = request.args.get('type', None)
|
||||
records = dns_service.list_records(zone, rr_keyword=rr, record_type=rtype)
|
||||
return {'code': 0, 'message': 'success', 'data': records}
|
||||
except Exception as e:
|
||||
logger.error(f"获取DNS记录失败: {e}")
|
||||
return {'code': -1, 'message': str(e), 'data': []}
|
||||
|
||||
|
||||
@app.route('/api/nginx/dns/records', methods=['POST'])
|
||||
def add_dns_record():
|
||||
"""添加 DNS 解析记录"""
|
||||
try:
|
||||
body = request.get_json(force=True)
|
||||
zone = body.get('zone', DEFAULT_ZONE)
|
||||
rr = body.get('rr', '').strip()
|
||||
record_type = body.get('type', 'A').upper()
|
||||
value = body.get('value', '').strip()
|
||||
ttl = body.get('ttl', 600)
|
||||
|
||||
if not rr:
|
||||
return {'code': -1, 'message': '主机记录(rr)不能为空'}, 400
|
||||
if not value:
|
||||
return {'code': -1, 'message': '记录值(value)不能为空'}, 400
|
||||
|
||||
result = dns_service.add_record(zone, rr, record_type, value, ttl)
|
||||
return {'code': 0, 'message': f'{result["full_domain"]} 记录已添加', 'data': result}
|
||||
except Exception as e:
|
||||
logger.error(f"添加DNS记录失败: {e}")
|
||||
return {'code': -1, 'message': str(e)}, 500
|
||||
|
||||
|
||||
@app.route('/api/nginx/dns/records/<record_id>', methods=['DELETE'])
|
||||
def delete_dns_record(record_id):
|
||||
"""删除 DNS 解析记录"""
|
||||
try:
|
||||
dns_service.delete_record(record_id)
|
||||
return {'code': 0, 'message': 'DNS记录已删除'}
|
||||
except Exception as e:
|
||||
logger.error(f"删除DNS记录失败: {e}")
|
||||
return {'code': -1, 'message': str(e)}, 500
|
||||
|
||||
|
||||
@app.route('/api/nginx/dns/records/<record_id>', methods=['PUT'])
|
||||
def update_dns_record(record_id):
|
||||
"""修改 DNS 解析记录"""
|
||||
try:
|
||||
body = request.get_json(force=True)
|
||||
rr = body.get('rr', '@')
|
||||
record_type = body.get('type', 'A').upper()
|
||||
value = body.get('value', '').strip()
|
||||
ttl = body.get('ttl', 600)
|
||||
|
||||
if not value:
|
||||
return {'code': -1, 'message': '记录值(value)不能为空'}, 400
|
||||
|
||||
dns_service.update_record(record_id, rr, record_type, value, ttl)
|
||||
return {'code': 0, 'message': 'DNS记录已更新'}
|
||||
except Exception as e:
|
||||
logger.error(f"修改DNS记录失败: {e}")
|
||||
return {'code': -1, 'message': str(e)}, 500
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
# 启动入口
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
|
||||
if __name__ == '__main__':
|
||||
logger.info(f"DNS Manager API 启动在端口 32210, 默认 zone: {DEFAULT_ZONE}")
|
||||
app.run(host='0.0.0.0', port=32210, debug=False)
|
||||
Reference in New Issue
Block a user