Files
jwxt/app/templates/admin_ip.html
T

624 lines
38 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{% extends "base_tailwind.html" %}
{% block title %}IP管理 - {{ brand_name }}{% endblock %}
{% block content %}
<div class="space-y-6">
<!-- 页面标题 -->
<div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4 list-item-enter" style="animation-delay: 0ms;">
<div>
<h1 class="text-2xl md:text-3xl font-bold text-neutral-900 dark:text-white">IP 管理</h1>
<p class="text-neutral-500 dark:text-neutral-400 mt-1">访问控制、封禁管理与访问统计</p>
</div>
<div class="flex gap-3 flex-wrap">
<button onclick="openBanModal()" class="btn-primary px-5 py-2.5 rounded-full text-sm font-bold">
<svg class="w-4 h-4 inline-block mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18.364 18.364A9 9 0 005.636 5.636m12.728 12.728A9 9 0 015.636 5.636m12.728 12.728L5.636 5.636"/>
</svg>
添加封禁
</button>
<button onclick="openConfigModal()" class="px-5 py-2.5 rounded-full text-sm font-bold border border-neutral-200 dark:border-neutral-700 text-neutral-700 dark:text-neutral-300 hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors">
频率设置
</button>
<a href="{{ url_for('main.admin_users') }}" class="px-5 py-2.5 rounded-full text-sm font-bold border border-neutral-200 dark:border-neutral-700 text-neutral-700 dark:text-neutral-300 hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors flex items-center gap-1.5">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 17l-5-5m0 0l5-5m-5 5h12"/>
</svg>
返回管理后台
</a>
</div>
</div>
<!-- 统计概览 -->
<div class="grid grid-cols-2 md:grid-cols-4 gap-4">
<div class="glass rounded-2xl p-5 layer-2 list-item-enter" style="animation-delay: 50ms;">
<p class="text-sm text-neutral-500 dark:text-neutral-400">今日访问</p>
<p class="text-2xl font-bold text-neutral-900 dark:text-white mt-1" id="statTotal">-</p>
</div>
<div class="glass rounded-2xl p-5 layer-2 list-item-enter" style="animation-delay: 100ms;">
<p class="text-sm text-neutral-500 dark:text-neutral-400">独立IP</p>
<p class="text-2xl font-bold text-neutral-900 dark:text-white mt-1" id="statUnique">-</p>
</div>
<div class="glass rounded-2xl p-5 layer-2 list-item-enter" style="animation-delay: 150ms;">
<p class="text-sm text-neutral-500 dark:text-neutral-400">被拦截</p>
<p class="text-2xl font-bold text-red-500 mt-1" id="statBlocked">-</p>
</div>
<div class="glass rounded-2xl p-5 layer-2 list-item-enter" style="animation-delay: 200ms;">
<p class="text-sm text-neutral-500 dark:text-neutral-400">封禁规则</p>
<p class="text-2xl font-bold text-neutral-900 dark:text-white mt-1" id="statBans">-</p>
</div>
</div>
<!-- 标签页切换 -->
<div class="border-b border-neutral-200 dark:border-neutral-800 list-item-enter" style="animation-delay: 250ms;">
<nav class="flex gap-6">
<button onclick="switchTab('stats')" id="tab-stats" class="pb-3 text-sm font-medium border-b-2 border-neutral-900 dark:border-white text-neutral-900 dark:text-white transition-colors">
统计图表
</button>
<button onclick="switchTab('logs')" id="tab-logs" class="pb-3 text-sm font-medium border-b-2 border-transparent text-neutral-500 dark:text-neutral-400 hover:text-neutral-700 dark:hover:text-neutral-200 transition-colors">
访问日志
</button>
<button onclick="switchTab('bans')" id="tab-bans" class="pb-3 text-sm font-medium border-b-2 border-transparent text-neutral-500 dark:text-neutral-400 hover:text-neutral-700 dark:hover:text-neutral-200 transition-colors">
封禁管理
</button>
</nav>
</div>
<!-- 统计图表 -->
<div id="panel-stats" class="space-y-4">
<div class="glass rounded-2xl p-5 layer-2 list-item-enter" style="animation-delay: 300ms;">
<div class="flex items-center justify-between mb-3">
<h3 class="text-sm font-semibold text-neutral-700 dark:text-neutral-300">访问时段分布</h3>
<select id="statsHours" onchange="loadStats()" class="text-xs rounded-lg border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-900 px-2 py-1 text-neutral-700 dark:text-neutral-300">
<option value="1">最近1小时</option>
<option value="6">最近6小时</option>
<option value="24" selected>最近24小时</option>
<option value="72">最近3天</option>
<option value="168">最近7天</option>
</select>
</div>
<div id="chartHour" class="w-full h-64"></div>
</div>
<div class="grid md:grid-cols-2 gap-4">
<div class="glass rounded-2xl p-5 layer-2 list-item-enter" style="animation-delay: 350ms;">
<h3 class="text-sm font-semibold text-neutral-700 dark:text-neutral-300 mb-3">来源国家/地区 Top10</h3>
<div id="chartCountry" class="w-full h-64"></div>
</div>
<div class="glass rounded-2xl p-5 layer-2 list-item-enter" style="animation-delay: 400ms;">
<h3 class="text-sm font-semibold text-neutral-700 dark:text-neutral-300 mb-3">中国省份分布 Top15</h3>
<div id="chartRegion" class="w-full h-64"></div>
</div>
</div>
<div class="glass rounded-2xl p-5 layer-2 list-item-enter" style="animation-delay: 450ms;">
<div class="flex items-center justify-between mb-3">
<h3 class="text-sm font-semibold text-neutral-700 dark:text-neutral-300">访问最多 IP Top10</h3>
</div>
<div id="topIpTable" class="space-y-2 max-h-64 overflow-y-auto"></div>
</div>
</div>
<!-- 访问日志 -->
<div id="panel-logs" class="hidden space-y-4">
<div class="glass rounded-2xl p-5 layer-2 list-item-enter" style="animation-delay: 300ms;">
<!-- 过滤栏 -->
<div class="flex flex-wrap gap-3 mb-4">
<input id="filterIp" type="text" placeholder="IP过滤..."
class="rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-900 text-sm px-3 py-2 text-neutral-700 dark:text-neutral-300 w-40">
<select id="filterBlocked" class="rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-900 text-sm px-3 py-2 text-neutral-700 dark:text-neutral-300">
<option value="">全部</option>
<option value="true">仅拦截</option>
</select>
<button onclick="loadLogs(1)" class="px-4 py-2 rounded-xl bg-blue-500 text-white text-sm font-medium hover:bg-blue-600 transition-colors">搜索</button>
<button onclick="openCleanModal()" class="px-4 py-2 rounded-xl bg-neutral-100 dark:bg-neutral-800 text-neutral-700 dark:text-neutral-300 text-sm font-medium hover:bg-neutral-200 dark:hover:bg-neutral-700 transition-colors">🗑 清理日志</button>
</div>
<!-- 日志表格 -->
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr class="text-left text-neutral-500 dark:text-neutral-400 border-b border-neutral-200 dark:border-neutral-700">
<th class="pb-3 pr-4 font-medium">时间</th>
<th class="pb-3 pr-4 font-medium">IP</th>
<th class="pb-3 pr-4 font-medium">地区</th>
<th class="pb-3 pr-4 font-medium">路径</th>
<th class="pb-3 pr-4 font-medium">状态</th>
<th class="pb-3 font-medium">操作</th>
</tr>
</thead>
<tbody id="logsTableBody" class="divide-y divide-neutral-100 dark:divide-neutral-800">
<tr><td colspan="6" class="py-8 text-center text-neutral-400">加载中...</td></tr>
</tbody>
</table>
</div>
<!-- 分页 -->
<div id="logsPagination" class="flex items-center justify-between mt-4">
<span id="logsInfo" class="text-sm text-neutral-500"></span>
<div class="flex gap-2" id="paginationBtns"></div>
</div>
</div>
</div>
<!-- 封禁管理 -->
<div id="panel-bans" class="hidden space-y-4">
<div class="glass rounded-2xl p-5 layer-2 list-item-enter" style="animation-delay: 300ms;">
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr class="text-left text-neutral-500 dark:text-neutral-400 border-b border-neutral-200 dark:border-neutral-700">
<th class="pb-3 pr-4 font-medium">类型</th>
<th class="pb-3 pr-4 font-medium">值</th>
<th class="pb-3 pr-4 font-medium">原因</th>
<th class="pb-3 pr-4 font-medium">到期时间</th>
<th class="pb-3 pr-4 font-medium">创建时间</th>
<th class="pb-3 pr-4 font-medium">操作者</th>
<th class="pb-3 font-medium">操作</th>
</tr>
</thead>
<tbody id="bansTableBody" class="divide-y divide-neutral-100 dark:divide-neutral-800">
<tr><td colspan="7" class="py-8 text-center text-neutral-400">加载中...</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- 添加封禁 Modal -->
<div id="banModal" class="fixed inset-0 z-50 hidden">
<div class="absolute inset-0 bg-black/60 backdrop-blur-sm" onclick="closeBanModal()"></div>
<div class="absolute inset-0 flex items-center justify-center p-4 pointer-events-none">
<div class="bg-white dark:bg-neutral-900 rounded-2xl shadow-xl w-full max-w-md pointer-events-auto">
<div class="p-6">
<h3 class="text-lg font-bold text-neutral-900 dark:text-white mb-5">添加封禁规则</h3>
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">封禁类型</label>
<select id="banType" class="w-full rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 text-sm px-3 py-2.5 text-neutral-700 dark:text-neutral-300">
<option value="ip">单个IP</option>
<option value="cidr">IP段 (CIDR,如 1.2.3.0/24)</option>
<option value="country">国家/地区代码 (如 US)</option>
</select>
</div>
<div>
<label class="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">封禁值</label>
<input id="banValue" type="text" placeholder="IP / CIDR / 国家代码"
class="w-full rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 text-sm px-3 py-2.5 text-neutral-700 dark:text-neutral-300">
</div>
<div>
<label class="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">封禁时长</label>
<select id="banDuration" class="w-full rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 text-sm px-3 py-2.5 text-neutral-700 dark:text-neutral-300">
<option value="">永久封禁</option>
<option value="1h">1小时</option>
<option value="6h">6小时</option>
<option value="1d">1天</option>
<option value="3d">3天</option>
<option value="1w">1周</option>
<option value="1m">1个月</option>
</select>
</div>
<div>
<label class="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">封禁原因</label>
<input id="banReason" type="text" placeholder="可选"
class="w-full rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 text-sm px-3 py-2.5 text-neutral-700 dark:text-neutral-300">
</div>
</div>
<div class="flex gap-3 mt-6">
<button onclick="submitBan()" class="flex-1 btn-primary py-2.5 rounded-xl text-sm font-bold">确认封禁</button>
<button onclick="closeBanModal()" class="flex-1 px-4 py-2.5 rounded-xl text-sm font-bold border border-neutral-200 dark:border-neutral-700 text-neutral-700 dark:text-neutral-300 hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors">取消</button>
</div>
</div>
</div>
</div>
</div>
<!-- 快速封禁 Modal -->
<div id="quickBanModal" class="fixed inset-0 z-50 hidden">
<div class="absolute inset-0 bg-black/60 backdrop-blur-sm" onclick="document.getElementById('quickBanModal').classList.add('hidden')"></div>
<div class="absolute inset-0 flex items-center justify-center p-4 pointer-events-none">
<div class="bg-white dark:bg-neutral-900 rounded-2xl shadow-xl w-full max-w-md pointer-events-auto">
<div class="p-6">
<h3 class="text-lg font-bold text-neutral-900 dark:text-white mb-2">快速封禁 IP</h3>
<p class="text-sm text-neutral-500 mb-5">IP:<strong id="quickBanIpDisplay" class="text-neutral-800 dark:text-neutral-200 font-mono"></strong></p>
<input type="hidden" id="quickBanIp">
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">封禁时长</label>
<select id="quickBanDuration" class="w-full rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 text-sm px-3 py-2.5 text-neutral-700 dark:text-neutral-300">
<option value="">永久封禁</option>
<option value="1h">1小时</option>
<option value="6h">6小时</option>
<option value="1d">1天</option>
<option value="3d">3天</option>
<option value="1w">1周</option>
<option value="1m">1个月</option>
</select>
</div>
<div>
<label class="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">原因</label>
<input id="quickBanReason" type="text" value="管理员从日志封禁"
class="w-full rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 text-sm px-3 py-2.5 text-neutral-700 dark:text-neutral-300">
</div>
</div>
<div class="flex gap-3 mt-6">
<button onclick="submitQuickBan()" class="flex-1 bg-red-500 text-white py-2.5 rounded-xl text-sm font-bold hover:bg-red-600 transition-colors">确认封禁</button>
<button onclick="document.getElementById('quickBanModal').classList.add('hidden')" class="flex-1 px-4 py-2.5 rounded-xl text-sm font-bold border border-neutral-200 dark:border-neutral-700 text-neutral-700 dark:text-neutral-300 hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors">取消</button>
</div>
</div>
</div>
</div>
</div>
<!-- 频率设置 Modal -->
<div id="configModal" class="fixed inset-0 z-50 hidden">
<div class="absolute inset-0 bg-black/60 backdrop-blur-sm" onclick="document.getElementById('configModal').classList.add('hidden')"></div>
<div class="absolute inset-0 flex items-center justify-center p-4 pointer-events-none">
<div class="bg-white dark:bg-neutral-900 rounded-2xl shadow-xl w-full max-w-md pointer-events-auto">
<div class="p-6">
<h3 class="text-lg font-bold text-neutral-900 dark:text-white mb-5">登录频率限制设置</h3>
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">统计时间窗口(分钟)</label>
<input id="cfgWindow" type="number" min="1" max="60"
class="w-full rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 text-sm px-3 py-2.5 text-neutral-700 dark:text-neutral-300">
<p class="text-xs text-neutral-400 mt-1">在此时间窗口内统计失败次数</p>
</div>
<div>
<label class="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">最大失败次数</label>
<input id="cfgMax" type="number" min="1" max="100"
class="w-full rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 text-sm px-3 py-2.5 text-neutral-700 dark:text-neutral-300">
<p class="text-xs text-neutral-400 mt-1">超过此次数自动封禁1小时</p>
</div>
<div class="flex items-center gap-3">
<input id="cfgChinaOnly" type="checkbox" class="w-4 h-4 rounded">
<label class="text-sm font-medium text-neutral-700 dark:text-neutral-300">启用中国IP限制(关闭后允许境外访问)</label>
</div>
</div>
<div class="flex gap-3 mt-6">
<button onclick="submitConfig()" class="flex-1 btn-primary py-2.5 rounded-xl text-sm font-bold">保存配置</button>
<button onclick="document.getElementById('configModal').classList.add('hidden')" class="flex-1 px-4 py-2.5 rounded-xl text-sm font-bold border border-neutral-200 dark:border-neutral-700 text-neutral-700 dark:text-neutral-300 hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors">取消</button>
</div>
</div>
</div>
</div>
</div>
<!-- 清理日志 Modal -->
<div id="cleanModal" class="fixed inset-0 z-50 hidden">
<div class="absolute inset-0 bg-black/60 backdrop-blur-sm" onclick="document.getElementById('cleanModal').classList.add('hidden')"></div>
<div class="absolute inset-0 flex items-center justify-center p-4 pointer-events-none">
<div class="bg-white dark:bg-neutral-900 rounded-2xl shadow-xl w-full max-w-sm pointer-events-auto">
<div class="p-6">
<h3 class="text-lg font-bold text-neutral-900 dark:text-white mb-4">清理访问日志</h3>
<div>
<label class="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">清理多少天前的日志</label>
<select id="cleanDays" class="w-full rounded-xl border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 text-sm px-3 py-2.5 text-neutral-700 dark:text-neutral-300">
<option value="1">1天前</option>
<option value="3">3天前</option>
<option value="7" selected>7天前</option>
<option value="30">30天前</option>
</select>
</div>
<div class="flex gap-3 mt-6">
<button onclick="submitClean()" class="flex-1 bg-red-500 text-white py-2.5 rounded-xl text-sm font-bold hover:bg-red-600 transition-colors">确认清理</button>
<button onclick="document.getElementById('cleanModal').classList.add('hidden')" class="flex-1 px-4 py-2.5 rounded-xl text-sm font-bold border border-neutral-200 dark:border-neutral-700 text-neutral-700 dark:text-neutral-300 hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors">取消</button>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block extra_js %}
<script src="{{ url_for('static', filename='echarts.min.js') }}"></script>
<script>
// ==================== 工具函数 ====================
function showToast(msg, type='success') {
const t = document.getElementById('toast');
if (!t) return;
const inner = document.getElementById('toastInner');
inner.textContent = msg;
inner.className = `rounded-2xl px-5 py-3 text-sm font-medium shadow-xl text-white ${type === 'success' ? 'bg-green-500' : 'bg-red-500'}`;
t.classList.remove('hidden');
setTimeout(() => t.classList.add('hidden'), 3000);
}
async function apiFetch(url, options={}) {
const resp = await fetch(url, {
headers: {'Content-Type': 'application/json', ...(options.headers || {})},
...options
});
return resp.json();
}
// ==================== Tab 切换 ====================
let currentTab = 'stats';
function switchTab(tab) {
['stats','logs','bans'].forEach(t => {
document.getElementById(`panel-${t}`).classList.add('hidden');
document.getElementById(`tab-${t}`).className = t === tab
? 'pb-3 text-sm font-medium border-b-2 border-neutral-900 dark:border-white text-neutral-900 dark:text-white transition-colors'
: 'pb-3 text-sm font-medium border-b-2 border-transparent text-neutral-500 dark:text-neutral-400 hover:text-neutral-700 dark:hover:text-neutral-200 transition-colors';
});
document.getElementById(`panel-${tab}`).classList.remove('hidden');
currentTab = tab;
if (tab === 'logs' && !logsLoaded) loadLogs(1);
if (tab === 'bans' && !bansLoaded) loadBans();
}
// ==================== 统计图表 ====================
let chartHour = null, chartCountry = null, chartRegion = null;
let logsLoaded = false, bansLoaded = false;
async function loadStats() {
const hours = document.getElementById('statsHours').value;
const data = await apiFetch(`/api/admin/ip/stats?hours=${hours}`);
if (!data.success) { showToast('加载统计失败: ' + data.message, 'error'); return; }
const s = data.stats;
document.getElementById('statTotal').textContent = s.total.toLocaleString();
document.getElementById('statUnique').textContent = s.unique_ips.toLocaleString();
document.getElementById('statBlocked').textContent = s.blocked.toLocaleString();
document.getElementById('statBans').textContent = s.active_bans;
// 时段分布
const now = new Date();
const allHours = [];
for (let i = parseInt(hours) - 1; i >= 0; i--) {
const d = new Date(now.getTime() - i * 3600000);
if (parseInt(hours) <= 24) {
allHours.push(d.toISOString().slice(11, 16));
} else {
allHours.push(d.toISOString().slice(5, 10));
}
}
const hourMap = {};
s.by_hour.forEach(h => hourMap[String(h.hour).padStart(2,'0')] = h.count);
const hourData = allHours.map((_, idx) => {
if (parseInt(hours) <= 24) return hourMap[String(idx).padStart(2,'0')] || 0;
const d = new Date(now.getTime() - (parseInt(hours) - 1 - idx) * 3600000);
return hourMap[String(d.getUTCHours()).padStart(2,'0')] || 0;
});
renderHourBar('chartHour', allHours, hourData);
// 国家分布
const topCountries = s.by_country.slice(0, 10);
renderCountryBar('chartCountry', topCountries.map(c => c.country || '-'), topCountries.map(c => c.count));
// 省份分布
const topRegions = s.by_region.slice(0, 15);
renderRegionBar('chartRegion', topRegions.map(r => r.region || '-'), topRegions.map(r => r.count));
// Top IP
document.getElementById('topIpTable').innerHTML = s.top_ips.slice(0, 10).map((ip, i) => `
<div class="flex items-center justify-between py-2 border-b border-neutral-100 dark:border-neutral-800">
<div class="flex items-center gap-3">
<span class="w-5 text-xs text-neutral-400 font-mono">${i+1}</span>
<div>
<p class="text-sm font-mono text-neutral-800 dark:text-neutral-200">${ip.ip}</p>
<p class="text-xs text-neutral-400">${ip.country||''} ${ip.region||''} ${ip.city||''}</p>
</div>
</div>
<div class="flex items-center gap-3">
<span class="text-sm font-bold text-neutral-700 dark:text-neutral-300">${ip.count}</span>
<button onclick="openQuickBan('${ip.ip}')" class="text-xs px-2 py-1 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-colors">封禁</button>
</div>
</div>`).join('');
}
function renderHourBar(domId, labels, data) {
const dom = document.getElementById(domId);
if (!dom) return;
if (chartHour) { chartHour.dispose(); chartHour = null; }
const isDark = document.documentElement.classList.contains('dark');
chartHour = echarts.init(dom);
chartHour.setOption({
tooltip: { trigger: 'axis', backgroundColor: isDark ? '#262626' : '#fff', textStyle: { color: isDark ? '#fff' : '#333' } },
grid: { left: '3%', right: '4%', bottom: '3%', top: '10%', containLabel: true },
xAxis: { type: 'category', data: labels, axisLabel: { color: isDark ? '#888' : '#666', rotate: labels.length > 12 ? 45 : 0 }, axisLine: { lineStyle: { color: isDark ? '#444' : '#ddd' } } },
yAxis: { type: 'value', axisLabel: { color: isDark ? '#888' : '#666' }, splitLine: { lineStyle: { color: isDark ? '#333' : '#eee' } } },
series: [{ type: 'bar', data: data, itemStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#3b82f6' }, { offset: 1, color: '#60a5fa' }]), borderRadius: [4, 4, 0, 0] }, emphasis: { itemStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#2563eb' }, { offset: 1, color: '#3b82f6' }]) } } }]
});
window.addEventListener('resize', () => chartHour && chartHour.resize());
}
function renderCountryBar(domId, labels, data) {
const dom = document.getElementById(domId);
if (!dom) return;
if (chartCountry) { chartCountry.dispose(); chartCountry = null; }
const isDark = document.documentElement.classList.contains('dark');
chartCountry = echarts.init(dom);
chartCountry.setOption({
tooltip: { trigger: 'axis', backgroundColor: isDark ? '#262626' : '#fff', textStyle: { color: isDark ? '#fff' : '#333' } },
grid: { left: '3%', right: '4%', bottom: '15%', top: '10%', containLabel: true },
xAxis: { type: 'category', data: labels, axisLabel: { color: isDark ? '#888' : '#666', rotate: 30 }, axisLine: { lineStyle: { color: isDark ? '#444' : '#ddd' } } },
yAxis: { type: 'value', axisLabel: { color: isDark ? '#888' : '#666' }, splitLine: { lineStyle: { color: isDark ? '#333' : '#eee' } } },
series: [{ type: 'bar', data: data, itemStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#10b981' }, { offset: 1, color: '#34d399' }]), borderRadius: [4, 4, 0, 0] }, emphasis: { itemStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#059669' }, { offset: 1, color: '#10b981' }]) } } }]
});
window.addEventListener('resize', () => chartCountry && chartCountry.resize());
}
function renderRegionBar(domId, labels, data) {
const dom = document.getElementById(domId);
if (!dom) return;
if (chartRegion) { chartRegion.dispose(); chartRegion = null; }
const isDark = document.documentElement.classList.contains('dark');
chartRegion = echarts.init(dom);
chartRegion.setOption({
tooltip: { trigger: 'axis', backgroundColor: isDark ? '#262626' : '#fff', textStyle: { color: isDark ? '#fff' : '#333' } },
grid: { left: '3%', right: '4%', bottom: '15%', top: '10%', containLabel: true },
xAxis: { type: 'category', data: labels, axisLabel: { color: isDark ? '#888' : '#666', rotate: 30 }, axisLine: { lineStyle: { color: isDark ? '#444' : '#ddd' } } },
yAxis: { type: 'value', axisLabel: { color: isDark ? '#888' : '#666' }, splitLine: { lineStyle: { color: isDark ? '#333' : '#eee' } } },
series: [{ type: 'bar', data: data, itemStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#f59e0b' }, { offset: 1, color: '#fbbf24' }]), borderRadius: [4, 4, 0, 0] }, emphasis: { itemStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#d97706' }, { offset: 1, color: '#f59e0b' }]) } } }]
});
window.addEventListener('resize', () => chartRegion && chartRegion.resize());
}
// ==================== 访问日志 ====================
let currentPage = 1;
async function loadLogs(page=1) {
currentPage = page;
logsLoaded = true;
const ip = document.getElementById('filterIp').value.trim();
const blocked = document.getElementById('filterBlocked').value;
let url = `/api/admin/ip/logs?page=${page}&per_page=50`;
if (ip) url += `&ip=${encodeURIComponent(ip)}`;
if (blocked) url += `&blocked=${blocked}`;
const data = await apiFetch(url);
if (!data.success) { showToast('加载日志失败', 'error'); return; }
const tbody = document.getElementById('logsTableBody');
tbody.innerHTML = data.logs.length === 0 ? '<tr><td colspan="6" class="py-8 text-center text-neutral-400">暂无日志</td></tr>' :
data.logs.map(l => `
<tr class="hover:bg-neutral-50 dark:hover:bg-neutral-800/50">
<td class="py-2.5 pr-4 text-xs text-neutral-500 whitespace-nowrap">${formatTime(l.created_at)}</td>
<td class="py-2.5 pr-4"><span class="font-mono text-xs text-neutral-800 dark:text-neutral-200">${l.ip}</span></td>
<td class="py-2.5 pr-4 text-xs text-neutral-500">${l.country||''} ${l.region||''}</td>
<td class="py-2.5 pr-4 text-xs text-neutral-600 dark:text-neutral-400 max-w-[200px] truncate" title="${l.path||''}">${l.method||''} ${l.path||''}</td>
<td class="py-2.5 pr-4">${l.blocked ? `<span class="text-xs px-2 py-0.5 rounded-full bg-red-100 text-red-600">拦截 ${l.block_reason||''}</span>` : `<span class="text-xs px-2 py-0.5 rounded-full bg-green-100 text-green-600">放行</span>`}</td>
<td class="py-2.5"><button onclick="openQuickBan('${l.ip}')" class="text-xs px-2 py-1 rounded-lg bg-red-50 text-red-500 hover:bg-red-100 transition-colors">封禁</button></td>
</tr>`).join('');
document.getElementById('logsInfo').textContent = `共 ${data.total} 条,第 ${page}/${data.pages} 页`;
const paginationDiv = document.getElementById('paginationBtns');
paginationDiv.innerHTML = '';
const totalPages = data.pages;
const start = Math.max(1, page-2), end = Math.min(totalPages, page+2);
if (page > 1) addPageBtn(paginationDiv, page-1, '‹');
for (let p=start; p<=end; p++) addPageBtn(paginationDiv, p, p, p===page);
if (page < totalPages) addPageBtn(paginationDiv, page+1, '›');
}
function addPageBtn(container, page, label, active=false) {
const btn = document.createElement('button');
btn.textContent = label;
btn.className = `px-3 py-1.5 rounded-lg text-sm font-medium transition-colors ${active ? 'bg-blue-500 text-white' : 'bg-neutral-100 dark:bg-neutral-800 text-neutral-700 dark:text-neutral-300 hover:bg-neutral-200 dark:hover:bg-neutral-700'}`;
btn.onclick = () => loadLogs(page);
container.appendChild(btn);
}
// ==================== 封禁管理 ====================
async function loadBans() {
bansLoaded = true;
const data = await apiFetch('/api/admin/ip/bans');
if (!data.success) { showToast('加载封禁列表失败', 'error'); return; }
const tbody = document.getElementById('bansTableBody');
tbody.innerHTML = data.bans.length === 0 ? '<tr><td colspan="7" class="py-8 text-center text-neutral-400">暂无封禁规则</td></tr>' :
data.bans.map(b => `
<tr class="${b.is_active ? '' : 'opacity-40'} hover:bg-neutral-50 dark:hover:bg-neutral-800/50">
<td class="py-2.5 pr-4"><span class="text-xs px-2 py-0.5 rounded-full font-medium ${b.ban_type==='ip'?'bg-red-100 text-red-600':b.ban_type==='cidr'?'bg-orange-100 text-orange-600':'bg-purple-100 text-purple-600'}">${b.ban_type.toUpperCase()}</span></td>
<td class="py-2.5 pr-4 font-mono text-sm text-neutral-800 dark:text-neutral-200">${b.value}</td>
<td class="py-2.5 pr-4 text-xs text-neutral-500 max-w-[160px] truncate" title="${b.reason||''}">${b.reason||'-'}</td>
<td class="py-2.5 pr-4 text-xs text-neutral-500">${b.is_permanent ? '<span class="text-red-500 font-semibold">永久</span>' : formatTime(b.expires_at)}</td>
<td class="py-2.5 pr-4 text-xs text-neutral-500">${formatTime(b.created_at)}</td>
<td class="py-2.5 pr-4 text-xs text-neutral-500">${b.created_by||'-'}</td>
<td class="py-2.5"><button onclick="unbanById(${b.id},'${b.value}')" class="text-xs px-2 py-1 rounded-lg bg-green-50 text-green-600 hover:bg-green-100 transition-colors">解封</button></td>
</tr>`).join('');
}
async function unbanById(id, value) {
if (!confirm(`确认解封 ${value}?`)) return;
const data = await apiFetch('/api/admin/ip/unban', { method: 'POST', body: JSON.stringify({id}) });
showToast(data.message, data.success ? 'success' : 'error');
if (data.success) { bansLoaded = false; loadBans(); loadStats(); }
}
// ==================== 添加封禁 Modal ====================
function openBanModal() { document.getElementById('banModal').classList.remove('hidden'); }
function closeBanModal() { document.getElementById('banModal').classList.add('hidden'); }
async function submitBan() {
const value = document.getElementById('banValue').value.trim();
if (!value) { showToast('请填写封禁值', 'error'); return; }
const data = await apiFetch('/api/admin/ip/ban', {
method: 'POST',
body: JSON.stringify({
value,
ban_type: document.getElementById('banType').value,
duration: document.getElementById('banDuration').value || null,
reason: document.getElementById('banReason').value
})
});
showToast(data.message, data.success ? 'success' : 'error');
if (data.success) { closeBanModal(); bansLoaded = false; if (currentTab === 'bans') loadBans(); loadStats(); }
}
// ==================== 快速封禁 ====================
function openQuickBan(ip) {
document.getElementById('quickBanIp').value = ip;
document.getElementById('quickBanIpDisplay').textContent = ip;
document.getElementById('quickBanModal').classList.remove('hidden');
}
async function submitQuickBan() {
const ip = document.getElementById('quickBanIp').value;
const data = await apiFetch('/api/admin/ip/ban_from_log', {
method: 'POST',
body: JSON.stringify({
ip,
duration: document.getElementById('quickBanDuration').value || null,
reason: document.getElementById('quickBanReason').value
})
});
showToast(data.message, data.success ? 'success' : 'error');
if (data.success) {
document.getElementById('quickBanModal').classList.add('hidden');
if (currentTab === 'logs') loadLogs(currentPage);
if (currentTab === 'bans') loadBans();
loadStats();
}
}
// ==================== 频率限制配置 Modal ====================
async function openConfigModal() {
const data = await apiFetch('/api/admin/ip/config');
if (data.success) {
document.getElementById('cfgWindow').value = data.config.login_window_minutes;
document.getElementById('cfgMax').value = data.config.login_max_attempts;
document.getElementById('cfgChinaOnly').checked = data.config.enable_china_only === 'true';
}
document.getElementById('configModal').classList.remove('hidden');
}
async function submitConfig() {
const data = await apiFetch('/api/admin/ip/config', {
method: 'POST',
body: JSON.stringify({
login_window_minutes: parseInt(document.getElementById('cfgWindow').value),
login_max_attempts: parseInt(document.getElementById('cfgMax').value),
enable_china_only: document.getElementById('cfgChinaOnly').checked
})
});
showToast(data.message, data.success ? 'success' : 'error');
if (data.success) document.getElementById('configModal').classList.add('hidden');
}
// ==================== 清理日志 Modal ====================
function openCleanModal() { document.getElementById('cleanModal').classList.remove('hidden'); }
async function submitClean() {
const days = parseInt(document.getElementById('cleanDays').value);
const data = await apiFetch('/api/admin/ip/clean_logs', {
method: 'POST',
body: JSON.stringify({days})
});
showToast(data.message, data.success ? 'success' : 'error');
if (data.success) {
document.getElementById('cleanModal').classList.add('hidden');
loadLogs(1);
}
}
// ==================== 初始化 ====================
document.addEventListener('DOMContentLoaded', () => {
loadStats();
apiFetch('/api/admin/ip/bans').then(d => {
if (d.success) document.getElementById('statBans').textContent = d.bans.filter(b=>b.is_active).length;
});
});
</script>
{% endblock %}