125 lines
4.0 KiB
JavaScript
125 lines
4.0 KiB
JavaScript
// 初始化配置管理面板
|
|
function initConfigPanel() {
|
|
// 加载当前配置
|
|
loadConfig();
|
|
|
|
// 初始化事件监听器
|
|
initConfigEventListeners();
|
|
}
|
|
|
|
// 初始化事件监听器
|
|
function initConfigEventListeners() {
|
|
// 保存配置按钮
|
|
document.getElementById('save-config').addEventListener('click', saveConfig);
|
|
|
|
// 屏蔽方法变更
|
|
document.getElementById('block-method').addEventListener('change', updateCustomBlockIpVisibility);
|
|
}
|
|
|
|
// 加载当前配置
|
|
function loadConfig() {
|
|
apiRequest('/config')
|
|
.then(config => {
|
|
renderConfig(config);
|
|
})
|
|
.catch(error => {
|
|
console.error('获取配置失败:', error);
|
|
window.showNotification('获取配置失败', 'error');
|
|
});
|
|
}
|
|
|
|
// 渲染配置表单
|
|
function renderConfig(config) {
|
|
if (!config) return;
|
|
|
|
// 设置屏蔽方法
|
|
const blockMethodSelect = document.getElementById('block-method');
|
|
if (config.shield && config.shield.blockMethod) {
|
|
blockMethodSelect.value = config.shield.blockMethod;
|
|
}
|
|
|
|
// 设置自定义屏蔽IP
|
|
const customBlockIpInput = document.getElementById('custom-block-ip');
|
|
if (config.shield && config.shield.customBlockIP) {
|
|
customBlockIpInput.value = config.shield.customBlockIP;
|
|
}
|
|
|
|
// 设置远程规则更新间隔
|
|
const updateIntervalInput = document.getElementById('update-interval');
|
|
if (config.shield && config.shield.updateInterval) {
|
|
updateIntervalInput.value = config.shield.updateInterval;
|
|
}
|
|
|
|
// 更新自定义屏蔽IP的可见性
|
|
updateCustomBlockIpVisibility();
|
|
}
|
|
|
|
// 更新自定义屏蔽IP输入框的可见性
|
|
function updateCustomBlockIpVisibility() {
|
|
const blockMethod = document.getElementById('block-method').value;
|
|
const customBlockIpContainer = document.getElementById('custom-block-ip').closest('.form-group');
|
|
|
|
if (blockMethod === 'customIP') {
|
|
customBlockIpContainer.style.display = 'block';
|
|
} else {
|
|
customBlockIpContainer.style.display = 'none';
|
|
}
|
|
}
|
|
|
|
// 保存配置
|
|
function saveConfig() {
|
|
// 收集表单数据
|
|
const configData = {
|
|
shield: {
|
|
blockMethod: document.getElementById('block-method').value,
|
|
updateInterval: parseInt(document.getElementById('update-interval').value)
|
|
}
|
|
};
|
|
|
|
// 如果选择了自定义IP,添加到配置中
|
|
if (configData.shield.blockMethod === 'customIP') {
|
|
const customBlockIp = document.getElementById('custom-block-ip').value.trim();
|
|
|
|
// 验证自定义IP格式
|
|
if (!isValidIp(customBlockIp)) {
|
|
window.showNotification('请输入有效的自定义屏蔽IP', 'warning');
|
|
return;
|
|
}
|
|
|
|
configData.shield.customBlockIP = customBlockIp;
|
|
}
|
|
|
|
// 验证更新间隔
|
|
if (isNaN(configData.shield.updateInterval) || configData.shield.updateInterval < 60) {
|
|
window.showNotification('更新间隔必须大于等于60秒', 'warning');
|
|
return;
|
|
}
|
|
|
|
// 保存配置
|
|
apiRequest('/config', 'PUT', configData)
|
|
.then(response => {
|
|
if (response.success) {
|
|
window.showNotification('配置保存成功', 'success');
|
|
|
|
// 由于服务器没有提供重启API,移除重启提示
|
|
// 直接提示用户配置已保存
|
|
} else {
|
|
window.showNotification(`保存失败: ${response.message || '未知错误'}`, 'error');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('保存配置失败:', error);
|
|
window.showNotification('保存配置失败', 'error');
|
|
});
|
|
}
|
|
|
|
// 服务重启功能已移除,因为服务器没有提供对应的API端点
|
|
|
|
// 验证IP地址格式
|
|
function isValidIp(ip) {
|
|
// 支持IPv4和IPv6简单验证
|
|
const ipv4Regex = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/;
|
|
const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}$/;
|
|
|
|
return ipv4Regex.test(ip) || ipv6Regex.test(ip);
|
|
} |