From 0ebe3c4693586d19890c49da3cabd93b087c66c6 Mon Sep 17 00:00:00 2001 From: 7Sageer <125936732+7Sageer@users.noreply.github.com> Date: Sun, 25 Aug 2024 14:55:07 +0800 Subject: [PATCH] fix: wrong custom rules --- src/BaseConfigBuilder.js | 1 + src/ClashConfigBuilder.js | 14 ++++++++------ src/SingboxConfigBuilder.js | 14 ++++++++------ src/config.js | 22 ++++++++++++---------- src/htmlBuilder.js | 8 ++++++++ 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/BaseConfigBuilder.js b/src/BaseConfigBuilder.js index b08e1aac..0c087e14 100644 --- a/src/BaseConfigBuilder.js +++ b/src/BaseConfigBuilder.js @@ -5,6 +5,7 @@ export class BaseConfigBuilder { constructor(inputString, baseConfig) { this.inputString = inputString; this.config = DeepCopy(baseConfig); + this.customRules = []; } async build() { diff --git a/src/ClashConfigBuilder.js b/src/ClashConfigBuilder.js index 7dd0d130..21b30bf7 100644 --- a/src/ClashConfigBuilder.js +++ b/src/ClashConfigBuilder.js @@ -58,13 +58,15 @@ export class ClashConfigBuilder extends BaseConfigBuilder { } }); - this.customRules.forEach(rule => { - this.config['proxy-groups'].push({ - type: "select", - name: rule.outbound, - proxies: ['🚀 节点选择', ...proxyList] + if (Array.isArray(this.customRules)) { + this.customRules.forEach(rule => { + this.config['proxy-groups'].push({ + type: "select", + name: rule.name, + proxies: ['🚀 节点选择', ...proxyList] + }); }); - }); + } this.config['proxy-groups'].push({ type: "select", diff --git a/src/SingboxConfigBuilder.js b/src/SingboxConfigBuilder.js index d1742af8..f60069e1 100644 --- a/src/SingboxConfigBuilder.js +++ b/src/SingboxConfigBuilder.js @@ -51,13 +51,15 @@ export class ConfigBuilder extends BaseConfigBuilder { } }); - this.customRules.forEach(rule => { - this.config.outbounds.push({ - type: "selector", - tag: rule.outbound, - outbounds: ['🚀 节点选择', ...proxyList] + if (Array.isArray(this.customRules)) { + this.customRules.forEach(rule => { + this.config.outbounds.push({ + type: "selector", + tag: rule.name, + outbounds: ['🚀 节点选择', ...proxyList] + }); }); - }); + } this.config.outbounds.push({ type: "selector", diff --git a/src/config.js b/src/config.js index 09f6ffe0..7f578592 100644 --- a/src/config.js +++ b/src/config.js @@ -172,13 +172,15 @@ export function generateRules(selectedRules = [], customRules = []) { } }); - customRules.forEach(rule => { - rules.push({ - site_rules: rule.sites, - ip_rules: rule.ips, - outbound: rule.outbound - }); - }); + if (customRules.length > 0) { + customRules.forEach((rule) => { + rules.push({ + site_rules: rule.site.split(','), + ip_rules: rule.ip.split(','), + outbound: rule.name + }); + }); + } return rules; } @@ -206,10 +208,10 @@ export function generateRuleSets(selectedRules = [], customRules = []) { rule.ip_rules.forEach(ipRule => ipRuleSets.add(ipRule)); } }); - + customRules.forEach(rule => { - rule.sites.forEach(site => siteRuleSets.add(site)); - rule.ips.forEach(ip => ipRuleSets.add(ip)); + rule.site.split(',').forEach(site => siteRuleSets.add(site.trim())); + siteRuleSets.add(rule.ip.trim()); }); const site_rule_sets = Array.from(siteRuleSets).map(rule => ({ diff --git a/src/htmlBuilder.js b/src/htmlBuilder.js index dad3ceb4..716dadce 100644 --- a/src/htmlBuilder.js +++ b/src/htmlBuilder.js @@ -632,6 +632,13 @@ const submitFormFunction = () => ` .map(checkbox => checkbox.value); } + // 收集自定义规则数据 + const customRules = Array.from(document.querySelectorAll('.custom-rule')).map(rule => ({ + site: rule.querySelector('input[name="customRuleSite[]"]').value, + ip: rule.querySelector('input[name="customRuleIP[]"]').value, + name: rule.querySelector('input[name="customRuleName[]"]').value + })); + const xrayUrl = \`\${window.location.origin}/xray?config=\${encodeURIComponent(inputString)}\`; const singboxUrl = \`\${window.location.origin}/singbox?config=\${encodeURIComponent(inputString)}&selectedRules=\${encodeURIComponent(JSON.stringify(selectedRules))}&customRules=\${encodeURIComponent(JSON.stringify(customRules))}\`; const clashUrl = \`\${window.location.origin}/clash?config=\${encodeURIComponent(inputString)}&selectedRules=\${encodeURIComponent(JSON.stringify(selectedRules))}&customRules=\${encodeURIComponent(JSON.stringify(customRules))}\`; @@ -709,6 +716,7 @@ const customRuleFunctions = ` const customRulesDiv = document.getElementById('customRules'); const newRuleDiv = document.createElement('div'); newRuleDiv.className = 'custom-rule mb-3 p-3 border rounded'; + newRuleDiv.dataset.ruleId = customRuleCount++; newRuleDiv.innerHTML = \`