日志查询界面增加操作列,修复责增加删除规则不生效的问题,修复自定义规则优先级问题
This commit is contained in:
@@ -282,6 +282,9 @@ function loadLogs() {
|
||||
// 更新日志表格
|
||||
updateLogsTable(logs);
|
||||
|
||||
// 绑定操作按钮事件
|
||||
bindActionButtonsEvents();
|
||||
|
||||
// 更新分页信息
|
||||
updateLogsPagination();
|
||||
|
||||
@@ -388,6 +391,9 @@ function updateLogsTable(logs) {
|
||||
const cacheStatusClass = log.FromCache ? 'text-primary' : 'text-gray-500';
|
||||
const cacheStatusText = log.FromCache ? '缓存' : '非缓存';
|
||||
|
||||
// 检查域名是否被拦截
|
||||
const isBlocked = log.Result === 'blocked';
|
||||
|
||||
row.innerHTML = `
|
||||
<td class="py-3 px-4">
|
||||
<div class="text-sm font-medium">${formattedTime}</div>
|
||||
@@ -400,10 +406,36 @@ function updateLogsTable(logs) {
|
||||
<td class="py-3 px-4 text-sm">
|
||||
<div class="font-medium">${log.Domain}</div>
|
||||
<div class="text-xs text-gray-500 mt-1">类型: ${log.QueryType}, <span class="${statusClass}">${statusText}</span>, <span class="${cacheStatusClass}">${log.FromCache ? '缓存' : '实时'}</span>${log.DNSSEC ? ', <span class="text-green-500"><i class="fa fa-lock"></i> DNSSEC</span>' : ''}${log.EDNS ? ', <span class="text-blue-500"><i class="fa fa-exchange"></i> EDNS</span>' : ''}</div>
|
||||
<div class="text-xs text-gray-500 mt-1">DNS 服务器: ${log.DNSServer || '无'}, DNSSEC专用: ${log.DNSSECServer || '无'}</div>
|
||||
</td>
|
||||
<td class="py-3 px-4 text-sm">${log.ResponseTime}ms</td>
|
||||
<td class="py-3 px-4 text-sm text-gray-500">${log.BlockRule || '-'}</td>
|
||||
<td class="py-3 px-4 text-sm text-center">
|
||||
${isBlocked ?
|
||||
`<button class="unblock-btn px-3 py-1 bg-green-500 text-white rounded-md hover:bg-green-600 transition-colors text-xs" data-domain="${log.Domain}">放行</button>` :
|
||||
`<button class="block-btn px-3 py-1 bg-red-500 text-white rounded-md hover:bg-red-600 transition-colors text-xs" data-domain="${log.Domain}">拦截</button>`
|
||||
}
|
||||
</td>
|
||||
`;
|
||||
|
||||
// 绑定按钮事件
|
||||
const blockBtn = row.querySelector('.block-btn');
|
||||
if (blockBtn) {
|
||||
blockBtn.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
const domain = e.currentTarget.dataset.domain;
|
||||
blockDomain(domain);
|
||||
});
|
||||
}
|
||||
|
||||
const unblockBtn = row.querySelector('.unblock-btn');
|
||||
if (unblockBtn) {
|
||||
unblockBtn.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
const domain = e.currentTarget.dataset.domain;
|
||||
unblockDomain(domain);
|
||||
});
|
||||
}
|
||||
|
||||
tableBody.appendChild(row);
|
||||
});
|
||||
@@ -613,6 +645,146 @@ function updateLogsStatsFromWebSocket(stats) {
|
||||
}
|
||||
}
|
||||
|
||||
// 拦截域名
|
||||
async function blockDomain(domain) {
|
||||
try {
|
||||
console.log(`开始拦截域名: ${domain}`);
|
||||
|
||||
// 创建拦截规则,使用AdBlock Plus格式
|
||||
const blockRule = `||${domain}^`;
|
||||
console.log(`创建的拦截规则: ${blockRule}`);
|
||||
|
||||
// 调用API添加拦截规则
|
||||
console.log(`调用API添加拦截规则,路径: /shield, 方法: POST`);
|
||||
const response = await apiRequest('/shield', 'POST', { rule: blockRule });
|
||||
|
||||
console.log(`API响应:`, response);
|
||||
|
||||
// 处理不同的响应格式
|
||||
if (response && (response.success || response.status === 'success')) {
|
||||
// 重新加载日志,显示更新后的状态
|
||||
loadLogs();
|
||||
|
||||
// 刷新规则列表
|
||||
refreshRulesList();
|
||||
|
||||
// 显示成功通知
|
||||
if (typeof window.showNotification === 'function') {
|
||||
window.showNotification(`已成功拦截域名: ${domain}`, 'success');
|
||||
}
|
||||
} else {
|
||||
const errorMsg = response ? (response.message || '添加拦截规则失败') : '添加拦截规则失败: 无效的API响应';
|
||||
console.error(`拦截域名失败: ${errorMsg}`);
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('拦截域名失败:', error);
|
||||
|
||||
// 显示错误通知
|
||||
if (typeof window.showNotification === 'function') {
|
||||
window.showNotification(`拦截域名失败: ${error.message}`, 'danger');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 绑定操作按钮事件
|
||||
function bindActionButtonsEvents() {
|
||||
// 绑定拦截按钮事件
|
||||
const blockBtns = document.querySelectorAll('.block-btn');
|
||||
blockBtns.forEach(btn => {
|
||||
btn.addEventListener('click', async (e) => {
|
||||
e.preventDefault();
|
||||
const domain = e.currentTarget.dataset.domain;
|
||||
await blockDomain(domain);
|
||||
});
|
||||
});
|
||||
|
||||
// 绑定放行按钮事件
|
||||
const unblockBtns = document.querySelectorAll('.unblock-btn');
|
||||
unblockBtns.forEach(btn => {
|
||||
btn.addEventListener('click', async (e) => {
|
||||
e.preventDefault();
|
||||
const domain = e.currentTarget.dataset.domain;
|
||||
await unblockDomain(domain);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 刷新规则列表
|
||||
async function refreshRulesList() {
|
||||
try {
|
||||
// 调用API重新加载规则
|
||||
const response = await apiRequest('/shield', 'GET');
|
||||
|
||||
if (response) {
|
||||
// 处理规则列表响应
|
||||
let allRules = [];
|
||||
if (response && typeof response === 'object') {
|
||||
// 合并所有类型的规则到一个数组
|
||||
if (Array.isArray(response.domainRules)) allRules = allRules.concat(response.domainRules);
|
||||
if (Array.isArray(response.domainExceptions)) allRules = allRules.concat(response.domainExceptions);
|
||||
if (Array.isArray(response.regexRules)) allRules = allRules.concat(response.regexRules);
|
||||
if (Array.isArray(response.regexExceptions)) allRules = allRules.concat(response.regexExceptions);
|
||||
}
|
||||
|
||||
// 更新规则列表
|
||||
if (window.rules) {
|
||||
rules = allRules;
|
||||
filteredRules = [...rules];
|
||||
|
||||
// 更新规则数量统计
|
||||
if (window.updateRulesCount && typeof window.updateRulesCount === 'function') {
|
||||
window.updateRulesCount(rules.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('刷新规则列表失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 放行域名
|
||||
async function unblockDomain(domain) {
|
||||
try {
|
||||
console.log(`开始放行域名: ${domain}`);
|
||||
|
||||
// 创建放行规则,使用AdBlock Plus格式
|
||||
const allowRule = `@@||${domain}^`;
|
||||
console.log(`创建的放行规则: ${allowRule}`);
|
||||
|
||||
// 调用API添加放行规则
|
||||
console.log(`调用API添加放行规则,路径: /shield, 方法: POST`);
|
||||
const response = await apiRequest('/shield', 'POST', { rule: allowRule });
|
||||
|
||||
console.log(`API响应:`, response);
|
||||
|
||||
// 处理不同的响应格式
|
||||
if (response && (response.success || response.status === 'success')) {
|
||||
// 重新加载日志,显示更新后的状态
|
||||
loadLogs();
|
||||
|
||||
// 刷新规则列表
|
||||
refreshRulesList();
|
||||
|
||||
// 显示成功通知
|
||||
if (typeof window.showNotification === 'function') {
|
||||
window.showNotification(`已成功放行域名: ${domain}`, 'success');
|
||||
}
|
||||
} else {
|
||||
const errorMsg = response ? (response.message || '添加放行规则失败') : '添加放行规则失败: 无效的API响应';
|
||||
console.error(`放行域名失败: ${errorMsg}`);
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('放行域名失败:', error);
|
||||
|
||||
// 显示错误通知
|
||||
if (typeof window.showNotification === 'function') {
|
||||
window.showNotification(`放行域名失败: ${error.message}`, 'danger');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 定期更新日志统计数据(备用方案)
|
||||
setInterval(() => {
|
||||
// 只有在查询日志页面时才更新
|
||||
|
||||
Reference in New Issue
Block a user