60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
|
from flask import Blueprint, request, jsonify
|
||
|
import os
|
||
|
|
||
|
dns_bp = Blueprint('dns', __name__)
|
||
|
|
||
|
DNS_FILE = os.path.join(os.path.dirname(__file__), '..', 'ktvmanager', 'lib', 'DNS_list.txt')
|
||
|
|
||
|
def read_dns_list():
|
||
|
if not os.path.exists(DNS_FILE):
|
||
|
return []
|
||
|
with open(DNS_FILE, 'r') as f:
|
||
|
return [line.strip() for line in f.readlines() if line.strip()]
|
||
|
|
||
|
def write_dns_list(dns_list):
|
||
|
with open(DNS_FILE, 'w') as f:
|
||
|
for item in dns_list:
|
||
|
f.write(f"{item}\n")
|
||
|
|
||
|
@dns_bp.route('/dns', methods=['GET'])
|
||
|
def get_dns_list():
|
||
|
"""Gets the list of DNS entries."""
|
||
|
return jsonify(read_dns_list())
|
||
|
|
||
|
@dns_bp.route('/dns', methods=['POST'])
|
||
|
def add_dns():
|
||
|
"""Adds a new DNS entry."""
|
||
|
data = request.get_json()
|
||
|
if not data or 'dns_entry' not in data:
|
||
|
return jsonify({'error': 'Missing dns_entry in request body'}), 400
|
||
|
|
||
|
dns_entry = data.get('dns_entry')
|
||
|
if not dns_entry:
|
||
|
return jsonify({'error': 'DNS entry cannot be empty.'}), 400
|
||
|
|
||
|
dns_list = read_dns_list()
|
||
|
if dns_entry in dns_list:
|
||
|
return jsonify({'message': 'DNS entry already exists.'}), 200
|
||
|
|
||
|
dns_list.append(dns_entry)
|
||
|
write_dns_list(dns_list)
|
||
|
return jsonify({'message': 'DNS entry added successfully.'}), 201
|
||
|
|
||
|
@dns_bp.route('/dns', methods=['DELETE'])
|
||
|
def remove_dns():
|
||
|
"""Removes a DNS entry."""
|
||
|
data = request.get_json()
|
||
|
if not data or 'dns_entry' not in data:
|
||
|
return jsonify({'error': 'Missing dns_entry in request body'}), 400
|
||
|
|
||
|
dns_entry = data.get('dns_entry')
|
||
|
if not dns_entry:
|
||
|
return jsonify({'error': 'DNS entry cannot be empty.'}), 400
|
||
|
|
||
|
dns_list = read_dns_list()
|
||
|
if dns_entry not in dns_list:
|
||
|
return jsonify({'error': 'DNS entry not found.'}), 404
|
||
|
|
||
|
dns_list.remove(dns_entry)
|
||
|
write_dns_list(dns_list)
|
||
|
return jsonify({'message': 'DNS entry removed successfully.'}), 200
|