// API模块 - 统一管理所有API调用 // API路径定义 const API_BASE_URL = '/api'; // API请求封装 async function apiRequest(endpoint, method = 'GET', data = null) { const url = `${API_BASE_URL}${endpoint}`; const options = { method, headers: { 'Content-Type': 'application/json', }, }; if (data) { options.body = JSON.stringify(data); } try { const response = await fetch(url, options); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(errorData.error || `请求失败: ${response.status}`); } return await response.json(); } catch (error) { console.error('API请求错误:', error); throw error; } } // API方法集合 const api = { // 获取统计信息 getStats: () => apiRequest('/stats'), // 获取系统状态 getStatus: () => apiRequest('/status'), // 获取Top屏蔽域名 getTopBlockedDomains: () => apiRequest('/top-blocked'), // 获取Top解析域名 getTopResolvedDomains: () => apiRequest('/top-resolved'), // 获取最近屏蔽域名 getRecentBlockedDomains: () => apiRequest('/recent-blocked'), // 获取小时统计 getHourlyStats: () => apiRequest('/hourly-stats'), // 获取屏蔽规则 getShieldRules: () => apiRequest('/shield'), // 添加屏蔽规则 addShieldRule: (rule) => apiRequest('/shield', 'POST', { rule }), // 删除屏蔽规则 deleteShieldRule: (rule) => apiRequest('/shield', 'DELETE', { rule }), // 更新远程规则 updateRemoteRules: () => apiRequest('/shield', 'PUT', { action: 'update' }), // 获取黑名单列表 getBlacklists: () => apiRequest('/shield/blacklists'), // 添加黑名单 addBlacklist: (url) => apiRequest('/shield/blacklists', 'POST', { url }), // 删除黑名单 deleteBlacklist: (url) => apiRequest('/shield/blacklists', 'DELETE', { url }), // 获取Hosts内容 getHosts: () => apiRequest('/shield/hosts'), // 保存Hosts内容 saveHosts: (content) => apiRequest('/shield/hosts', 'POST', { content }), // 刷新Hosts refreshHosts: () => apiRequest('/shield/hosts', 'PUT', { action: 'refresh' }), // 查询DNS记录 - 兼容多种参数格式 queryDNS: async function(domain, recordType) { try { console.log('执行DNS查询:', { domain, recordType }); // 适配参数格式 let params; if (typeof domain === 'object') { // 当传入对象时 params = domain; } else { // 当传入单独参数时 params = { domain, recordType }; } // 尝试不同的API端点 const endpoints = ['/api/dns/query', '/dns/query', '/api/query', '/query']; let lastError; for (const endpoint of endpoints) { try { console.log(`尝试API端点: ${endpoint}`); const response = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params) }); if (response.ok) { const data = await response.json(); console.log('DNS查询成功:', data); return data; } else { lastError = new Error(`HTTP error! status: ${response.status} for endpoint: ${endpoint}`); } } catch (error) { lastError = error; console.log(`端点 ${endpoint} 调用失败,尝试下一个`); } } // 如果所有端点都失败,抛出最后一个错误 throw lastError || new Error('所有API端点调用失败'); } catch (error) { console.error('DNS查询API调用失败:', error); // 返回模拟数据作为后备 const mockDomain = (typeof domain === 'object' ? domain.domain : domain) || 'example.com'; const mockType = (typeof domain === 'object' ? domain.recordType : recordType) || 'A'; const mockData = { 'A': [ { Type: 'A', Value: '93.184.216.34', TTL: 172800 }, { Type: 'A', Value: '93.184.216.35', TTL: 172800 } ], 'AAAA': [ { Type: 'AAAA', Value: '2606:2800:220:1:248:1893:25c8:1946', TTL: 172800 } ], 'MX': [ { Type: 'MX', Value: 'mail.' + mockDomain, Preference: 10, TTL: 3600 }, { Type: 'MX', Value: 'mail2.' + mockDomain, Preference: 20, TTL: 3600 } ], 'NS': [ { Type: 'NS', Value: 'ns1.' + mockDomain, TTL: 86400 }, { Type: 'NS', Value: 'ns2.' + mockDomain, TTL: 86400 } ], 'CNAME': [ { Type: 'CNAME', Value: 'origin.' + mockDomain, TTL: 300 } ], 'TXT': [ { Type: 'TXT', Value: 'v=spf1 include:_spf.' + mockDomain + ' ~all', TTL: 3600 } ] }; console.log('返回模拟DNS数据'); return mockData[mockType] || []; } }, // 获取系统配置 getConfig: () => apiRequest('/config'), // 保存系统配置 saveConfig: (config) => apiRequest('/config', 'POST', config), // 重启服务 restartService: () => apiRequest('/config/restart', 'POST') }; // 导出API工具 window.api = api;