Skip to content

Commit 04f57b1

Browse files
committed
feat: add custom rule sets supported
1 parent d19a865 commit 04f57b1

7 files changed

+358
-156
lines changed

README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
- Sing-Box
1717
- Clash
1818
- Xray/V2Ray
19-
- 提供快速上手的Web界面
19+
- 提供快速上手的Web界面,支持自定义路由规则
2020
- 支持短链接生成(基于R2)
2121
- 浅色/深色主题切换
2222

2323
## 最近更新
2424

25+
### 5/8
26+
27+
- 支持自定义规则集
28+
2529
### 4/8
2630

2731
- 重构配置文件生成逻辑
@@ -34,13 +38,6 @@
3438

3539
---
3640

37-
### 30/7
38-
39-
- 简化路由逻辑
40-
- 修复了一些小问题
41-
42-
---
43-
4441
## 部署
4542

4643
[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/7Sageer/sublink-worker)
@@ -50,6 +47,7 @@
5047
## 项目结构
5148

5249
- `index.js`: 主要的服务器逻辑,处理请求路由
50+
- `BaseConfigBuilder.js`: 构建基础配置
5351
- `SingboxConfigBuilder.js`: 构建Sing-Box配置
5452
- `ClashConfigBuilder.js`: 构建Clash配置
5553
- `ProxyParsers.js`: 解析各种代理协议的URL

doc/image.png

-75.8 KB
Loading

src/ClashConfigBuilder.js

+35-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import yaml from 'js-yaml';
2-
import { CLASH_CONFIG, SELECTORS_LIST } from './config.js';
2+
import { CLASH_CONFIG, generateRuleSets, generateRules, getOutbounds} from './config.js';
33
import { BaseConfigBuilder } from './BaseConfigBuilder.js';
44
import { DeepCopy } from './utils.js';
55

66
export class ClashConfigBuilder extends BaseConfigBuilder {
7-
constructor(inputString) {
7+
constructor(inputString, selectedRules) {
88
super(inputString, CLASH_CONFIG);
9+
this.selectedRules = selectedRules;
910
}
1011

1112
addCustomItems(customItems) {
@@ -17,7 +18,9 @@ export class ClashConfigBuilder extends BaseConfigBuilder {
1718
}
1819

1920
addSelectors() {
21+
const outbounds = getOutbounds(this.selectedRules);
2022
const proxyList = this.config.proxies.map(proxy => proxy.name);
23+
2124
this.config['proxy-groups'].push({
2225
name: '⚡ 自动选择',
2326
type: 'url-test',
@@ -26,21 +29,46 @@ export class ClashConfigBuilder extends BaseConfigBuilder {
2629
interval: 300,
2730
lazy: false
2831
});
32+
2933
proxyList.unshift('DIRECT', 'REJECT', '⚡ 自动选择');
30-
SELECTORS_LIST.forEach(selector => {
31-
if (!this.config['proxy-groups'].some(g => g.name === selector)) {
34+
outbounds.unshift('🚀 节点选择', 'GLOBAL');
35+
36+
outbounds.forEach(outbound => {
37+
if (outbound !== '🚀 节点选择') {
38+
this.config['proxy-groups'].push({
39+
type: "select",
40+
name: outbound,
41+
proxies: ['🚀 节点选择', ...proxyList]
42+
});
43+
} else {
3244
this.config['proxy-groups'].push({
3345
type: "select",
34-
name: selector,
35-
proxies: selector !== '🚀 节点选择' ? ['🚀 节点选择', ...proxyList] : proxyList
46+
name: outbound,
47+
proxies: proxyList
3648
});
3749
}
3850
});
39-
}
4051

52+
this.config['proxy-groups'].push({
53+
type: "select",
54+
name: "🐟 漏网之鱼",
55+
proxies: ['🚀 节点选择', ...proxyList]
56+
});
57+
}
4158
formatConfig() {
59+
const rules = generateRules(this.selectedRules);
60+
61+
this.config.rules = rules.flatMap(rule => [
62+
...rule.site_rules.map(site => `GEOSITE,${site},${rule.outbound}`),
63+
...rule.ip_rules.map(ip => `GEOIP,${ip},${rule.outbound}`)
64+
]);
65+
66+
// Add the final catch-all rule
67+
this.config.rules.push('MATCH,🐟 漏网之鱼');
68+
4269
return yaml.dump(this.config);
4370
}
71+
4472
convertToClashProxy(proxy) {
4573
switch(proxy.type) {
4674
case 'shadowsocks':

src/SingboxConfigBuilder.js

+47-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { SING_BOX_CONFIG, SELECTORS_LIST } from './config.js';
1+
import { SING_BOX_CONFIG, generateRuleSets, generateRules, getOutbounds} from './config.js';
22
import { BaseConfigBuilder } from './BaseConfigBuilder.js';
33
import { DeepCopy } from './utils.js';
44

55
export class ConfigBuilder extends BaseConfigBuilder {
6-
constructor(inputString) {
6+
constructor(inputString, selectedRules) {
77
super(inputString, SING_BOX_CONFIG);
8+
this.selectedRules = selectedRules;
89
}
910

1011
addCustomItems(customItems) {
@@ -13,23 +14,58 @@ export class ConfigBuilder extends BaseConfigBuilder {
1314
}
1415

1516
addSelectors() {
16-
const tagList = this.config.outbounds.filter(outbound => outbound?.server != undefined).map(outbound => outbound.tag);
17+
const outbounds = getOutbounds(this.selectedRules);
18+
const proxyList = this.config.outbounds.filter(outbound => outbound?.server != undefined).map(outbound => outbound.tag);
19+
1720
this.config.outbounds.push({
1821
type: "urltest",
1922
tag: "⚡ 自动选择",
20-
outbounds: DeepCopy(tagList),
23+
outbounds: DeepCopy(proxyList),
2124
});
22-
tagList.unshift('DIRECT', 'REJECT', '⚡ 自动选择');
23-
SELECTORS_LIST.forEach(selector => {
24-
this.config.outbounds.push({
25-
type: "selector",
26-
tag: selector,
27-
outbounds: selector !== '🚀 节点选择' ? ['🚀 节点选择', ...tagList] : tagList
28-
});
25+
26+
proxyList.unshift('DIRECT', 'REJECT', '⚡ 自动选择');
27+
outbounds.unshift('🚀 节点选择', 'GLOBAL');
28+
outbounds.forEach(outbound => {
29+
if (outbound !== '🚀 节点选择') {
30+
this.config.outbounds.push({
31+
type: "selector",
32+
tag: outbound,
33+
outbounds: ['🚀 节点选择', ...proxyList]
34+
});
35+
} else {
36+
this.config.outbounds.push({
37+
type: "selector",
38+
tag: outbound,
39+
outbounds: proxyList
40+
});
41+
}
42+
});
43+
44+
this.config.outbounds.push({
45+
type: "selector",
46+
tag: "🐟 漏网之鱼",
47+
outbounds: ['🚀 节点选择', ...proxyList]
2948
});
3049
}
3150

3251
formatConfig() {
52+
const rules = generateRules(this.selectedRules);
53+
const { site_rule_sets, ip_rule_sets } = generateRuleSets(this.selectedRules);
54+
55+
this.config.route.rule_set = [...site_rule_sets, ...ip_rule_sets];
56+
57+
this.config.route.rules = rules.map(rule => ({
58+
rule_set: [...rule.site_rules, ...rule.ip_rules.map(ip => `${ip}-ip`)],
59+
outbound: rule.outbound
60+
}));
61+
62+
// Add any default rules that should always be present
63+
this.config.route.rules.push(
64+
{ protocol: 'dns', port: 53, outbound: 'dns-out' },
65+
{ clash_mode: 'direct', outbound: 'DIRECT' },
66+
{ clash_mode: 'global', outbound: 'GLOBAL' }
67+
);
68+
3369
return this.config;
3470
}
3571
}

0 commit comments

Comments
 (0)