121 lines
3.5 KiB
Python
121 lines
3.5 KiB
Python
from datetime import datetime
|
|
|
|
from flask import Blueprint, Response, jsonify, request
|
|
|
|
from ..services.analysis import analyze_ips, apply_filters
|
|
from ..services.exports import (
|
|
generate_apache,
|
|
generate_cidr,
|
|
generate_csv,
|
|
generate_firewalld,
|
|
generate_ipset,
|
|
generate_iptables,
|
|
generate_mikrotik,
|
|
generate_nginx,
|
|
)
|
|
from ..services.parsing import parse_ip_list
|
|
|
|
api_bp = Blueprint('api', __name__)
|
|
|
|
|
|
@api_bp.route('/api/analyze', methods=['POST'])
|
|
def api_analyze():
|
|
data = request.get_json()
|
|
if not data:
|
|
return jsonify({'error': 'Invalid JSON'}), 400
|
|
|
|
ips = parse_ip_list(data.get('ips', ''))
|
|
if not ips:
|
|
return jsonify({'error': 'No valid IPs found'}), 400
|
|
|
|
results, stats = analyze_ips(ips)
|
|
return jsonify({'results': results, 'stats': stats})
|
|
|
|
|
|
@api_bp.route('/api/filter', methods=['POST'])
|
|
def api_filter():
|
|
data = request.get_json()
|
|
if not data:
|
|
return jsonify({'error': 'Invalid JSON'}), 400
|
|
|
|
results = data.get('results', [])
|
|
filters = data.get('filters', {})
|
|
filtered = apply_filters(results, filters)
|
|
return jsonify({'filtered': filtered, 'count': len(filtered)})
|
|
|
|
|
|
@api_bp.route('/api/export/ipset', methods=['POST'])
|
|
def api_export_ipset():
|
|
data = request.get_json()
|
|
if not data:
|
|
return Response('Invalid JSON', status=400)
|
|
return Response(generate_ipset(data.get('ips', []), data.get('timeout', 86400)), mimetype='text/plain')
|
|
|
|
|
|
@api_bp.route('/api/export/iptables', methods=['POST'])
|
|
def api_export_iptables():
|
|
data = request.get_json()
|
|
if not data:
|
|
return Response('Invalid JSON', status=400)
|
|
return Response(generate_iptables(data.get('ips', [])), mimetype='text/plain')
|
|
|
|
|
|
@api_bp.route('/api/export/nginx', methods=['POST'])
|
|
def api_export_nginx():
|
|
data = request.get_json()
|
|
if not data:
|
|
return Response('Invalid JSON', status=400)
|
|
return Response(generate_nginx(data.get('ips', [])), mimetype='text/plain')
|
|
|
|
|
|
@api_bp.route('/api/export/apache', methods=['POST'])
|
|
def api_export_apache():
|
|
data = request.get_json()
|
|
if not data:
|
|
return Response('Invalid JSON', status=400)
|
|
return Response(generate_apache(data.get('ips', [])), mimetype='text/plain')
|
|
|
|
|
|
@api_bp.route('/api/export/firewalld', methods=['POST'])
|
|
def api_export_firewalld():
|
|
data = request.get_json()
|
|
if not data:
|
|
return Response('Invalid JSON', status=400)
|
|
return Response(generate_firewalld(data.get('ips', [])), mimetype='text/plain')
|
|
|
|
|
|
@api_bp.route('/api/export/mikrotik', methods=['POST'])
|
|
def api_export_mikrotik():
|
|
data = request.get_json()
|
|
if not data:
|
|
return Response('Invalid JSON', status=400)
|
|
return Response(generate_mikrotik(data.get('ips', [])), mimetype='text/plain')
|
|
|
|
|
|
@api_bp.route('/api/export/cidr', methods=['POST'])
|
|
def api_export_cidr():
|
|
data = request.get_json()
|
|
if not data:
|
|
return Response('Invalid JSON', status=400)
|
|
return Response(generate_cidr(data.get('results', [])), mimetype='text/plain')
|
|
|
|
|
|
@api_bp.route('/api/export/csv', methods=['POST'])
|
|
def api_export_csv():
|
|
data = request.get_json()
|
|
if not data:
|
|
return Response('Invalid JSON', status=400)
|
|
|
|
csv_content = generate_csv(data.get('results', []))
|
|
timestamp = datetime.now().strftime('%Y-%m-%d')
|
|
return Response(
|
|
csv_content,
|
|
mimetype='text/csv',
|
|
headers={'Content-Disposition': f'attachment; filename=ip-analysis-{timestamp}.csv'},
|
|
)
|
|
|
|
|
|
@api_bp.route('/analyze', methods=['POST'])
|
|
def analyze_legacy():
|
|
return api_analyze()
|