-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathSingboxConfigBuilder.js
More file actions
487 lines (427 loc) · 19.2 KB
/
SingboxConfigBuilder.js
File metadata and controls
487 lines (427 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import { SING_BOX_CONFIG, generateRuleSets, generateRules, getOutbounds, PREDEFINED_RULE_SETS } from '../config/index.js';
import { BaseConfigBuilder } from './BaseConfigBuilder.js';
import { deepCopy, groupProxiesByCountry } from '../utils.js';
import { addProxyWithDedup } from './helpers/proxyHelpers.js';
import { buildSelectorMembers as buildSelectorMemberList, buildNodeSelectMembers, uniqueNames } from './helpers/groupBuilder.js';
import { normalizeGroupName } from './helpers/groupNameUtils.js';
import { IR_VERSION, downgradeByCaps, normalizeLegacyProxyToIR } from '../ir/index.js';
import { mapIRToSingboxOutbound } from '../ir/maps/singbox.js';
export class SingboxConfigBuilder extends BaseConfigBuilder {
usesIR = true;
constructor(inputString, selectedRules, customRules, baseConfig, lang, userAgent, groupByCountry = false, enableClashUI = false, externalController, externalUiDownloadUrl, singboxVersion = '1.12') {
const resolvedBaseConfig = baseConfig ?? SING_BOX_CONFIG;
super(inputString, resolvedBaseConfig, lang, userAgent, groupByCountry);
this.selectedRules = selectedRules;
this.customRules = customRules;
this.countryGroupNames = [];
this.manualGroupName = null;
this.enableClashUI = enableClashUI;
this.externalController = externalController;
this.externalUiDownloadUrl = externalUiDownloadUrl;
this.singboxVersion = singboxVersion; // '1.11' or '1.12'
if (this.config?.dns?.servers?.length > 0) {
this.config.dns.servers[0].detour = this.t('outboundNames.Node Select');
}
}
/**
* Check if subscription format is compatible for use as Sing-Box outbound_provider
* Only available in Sing-Box 1.12+
* @param {'clash'|'singbox'|'unknown'} format - Detected subscription format
* @returns {boolean} - True if format is Sing-Box JSON and version supports providers
*/
isCompatibleProviderFormat(format) {
// outbound_providers only supported in Sing-Box 1.12+
if (this.singboxVersion === '1.11') {
return false;
}
return format === 'singbox';
}
/**
* Generate outbound_providers configuration from collected URLs
* @returns {object[]} - Array of outbound provider objects
*/
generateOutboundProviders() {
return this.providerUrls.map((url, index) => ({
tag: `_auto_provider_${index + 1}`,
type: 'http',
download_url: url,
path: `./providers/_auto_provider_${index + 1}.json`,
download_interval: '24h',
health_check: {
enabled: true,
url: 'https://www.gstatic.com/generate_204',
interval: '5m'
}
}));
}
/**
* Get list of provider tags
* @returns {string[]} - Array of provider tags
*/
getProviderTags() {
return this.providerUrls.map((_, index) => `_auto_provider_${index + 1}`);
}
/**
* Get all provider tags (user-defined + auto-generated)
* @returns {string[]} - Array of provider tags
*/
getAllProviderTags() {
if (this.singboxVersion === '1.11') {
return [];
}
const existingTags = Array.isArray(this.config.outbound_providers)
? this.config.outbound_providers.map(p => p?.tag).filter(Boolean)
: [];
const autoTags = this.getProviderTags();
return [...new Set([...existingTags, ...autoTags])];
}
getProxies() {
return this.config.outbounds.filter(outbound => outbound?.server != undefined);
}
getProxyName(proxy) {
return proxy.tag;
}
convertProxy(proxy) {
const ir = proxy?.version === IR_VERSION ? proxy : normalizeLegacyProxyToIR(proxy);
if (!ir) return null;
const downgraded = downgradeByCaps(ir, 'singbox');
return mapIRToSingboxOutbound(downgraded);
}
addProxyToConfig(proxy) {
this.config.outbounds = this.config.outbounds || [];
addProxyWithDedup(this.config.outbounds, proxy, {
getName: (item) => item?.tag,
setName: (item, name) => {
if (item) item.tag = name;
},
isSame: (existing = {}, incoming = {}) => {
const { tag: _incomingTag, ...restIncoming } = incoming;
const { tag: _existingTag, ...restExisting } = existing;
return JSON.stringify(restIncoming) === JSON.stringify(restExisting);
}
});
}
hasOutboundTag(tag) {
const target = normalizeGroupName(tag);
return (this.config.outbounds || []).some(outbound => normalizeGroupName(outbound?.tag) === target);
}
addAutoSelectGroup(proxyList) {
this.config.outbounds = this.config.outbounds || [];
const tag = this.t('outboundNames.Auto Select');
if (this.hasOutboundTag(tag)) return;
const group = {
type: "urltest",
tag,
outbounds: deepCopy(uniqueNames(proxyList))
};
// Add 'providers' field if we have outbound_providers
const providerTags = this.getAllProviderTags();
if (providerTags.length > 0) {
group.providers = providerTags;
}
this.config.outbounds.unshift(group);
}
addNodeSelectGroup(proxyList) {
this.config.outbounds = this.config.outbounds || [];
const tag = this.t('outboundNames.Node Select');
if (this.hasOutboundTag(tag)) return;
const members = buildNodeSelectMembers({
proxyList,
translator: this.t,
groupByCountry: this.groupByCountry,
manualGroupName: this.manualGroupName,
countryGroupNames: this.countryGroupNames
});
const group = {
type: "selector",
tag,
outbounds: members
};
// Add 'providers' field if we have outbound_providers
const providerTags = this.getAllProviderTags();
if (providerTags.length > 0) {
group.providers = providerTags;
}
this.config.outbounds.unshift(group);
}
buildSelectorMembers(proxyList = []) {
return buildSelectorMemberList({
proxyList,
translator: this.t,
groupByCountry: this.groupByCountry,
manualGroupName: this.manualGroupName,
countryGroupNames: this.countryGroupNames
});
}
addOutboundGroups(outbounds, proxyList) {
outbounds.forEach(outbound => {
if (outbound !== this.t('outboundNames.Node Select')) {
const selectorMembers = this.buildSelectorMembers(proxyList);
const tag = this.t(`outboundNames.${outbound}`);
if (this.hasOutboundTag(tag)) {
return;
}
this.config.outbounds.push({
type: "selector",
tag,
outbounds: selectorMembers
});
}
});
}
addCustomRuleGroups(proxyList) {
if (Array.isArray(this.customRules)) {
this.customRules.forEach(rule => {
const selectorMembers = this.buildSelectorMembers(proxyList);
if (this.hasOutboundTag(rule.name)) return;
this.config.outbounds.push({
type: "selector",
tag: rule.name,
outbounds: selectorMembers
});
});
}
}
addFallBackGroup(proxyList) {
const selectorMembers = this.buildSelectorMembers(proxyList);
if (this.hasOutboundTag(this.t('outboundNames.Fall Back'))) return;
this.config.outbounds.push({
type: "selector",
tag: this.t('outboundNames.Fall Back'),
outbounds: selectorMembers
});
}
addCountryGroups() {
const proxies = this.getProxies();
const countryGroups = groupProxiesByCountry(proxies, {
getName: proxy => this.getProxyName(proxy)
});
const existingTags = new Set((this.config.outbounds || []).map(o => normalizeGroupName(o?.tag)).filter(Boolean));
const manualProxyNames = proxies.map(p => p?.tag).filter(Boolean);
const manualGroupName = manualProxyNames.length > 0 ? this.t('outboundNames.Manual Switch') : null;
if (manualGroupName) {
const manualNorm = normalizeGroupName(manualGroupName);
if (!existingTags.has(manualNorm)) {
this.config.outbounds.push({
type: 'selector',
tag: manualGroupName,
outbounds: manualProxyNames
});
existingTags.add(manualNorm);
}
}
const countries = Object.keys(countryGroups).sort((a, b) => a.localeCompare(b));
const countryGroupNames = [];
countries.forEach(country => {
const { emoji, name, proxies: countryProxies } = countryGroups[country];
if (!countryProxies || countryProxies.length === 0) {
return;
}
const groupName = `${emoji} ${name}`;
const norm = normalizeGroupName(groupName);
if (!existingTags.has(norm)) {
this.config.outbounds.push({
tag: groupName,
type: 'urltest',
outbounds: countryProxies
});
existingTags.add(norm);
}
countryGroupNames.push(groupName);
});
const nodeSelectTag = this.t('outboundNames.Node Select');
const nodeSelectGroup = this.config.outbounds.find(o => normalizeGroupName(o?.tag) === normalizeGroupName(nodeSelectTag));
if (nodeSelectGroup && Array.isArray(nodeSelectGroup.outbounds)) {
const rebuilt = buildNodeSelectMembers({
proxyList: [],
translator: this.t,
groupByCountry: true,
manualGroupName,
countryGroupNames
});
nodeSelectGroup.outbounds = rebuilt;
}
this.countryGroupNames = countryGroupNames;
this.manualGroupName = manualGroupName;
}
/**
* Merge user-defined proxy groups (selector/urltest outbounds) with system-generated ones
* Handles same-tag groups by merging outbounds/providers fields
* @param {Array} userGroups - User-defined proxy groups from input config (converted to Clash format)
*/
mergeUserProxyGroups(userGroups) {
if (!Array.isArray(userGroups)) return;
const proxyList = this.getProxyList();
const validProxyTags = new Set(proxyList);
const allProviderTags = new Set(this.getAllProviderTags());
// Build valid reference set (proxy tags, group tags, special names)
const groupTags = new Set(
(this.config.outbounds || [])
.filter(o => o.type === 'selector' || o.type === 'urltest')
.map(o => normalizeGroupName(o?.tag))
.filter(Boolean)
);
const validRefs = new Set(['DIRECT', 'REJECT', 'direct', 'block']);
proxyList.forEach(n => validRefs.add(n));
groupTags.forEach(n => validRefs.add(n));
userGroups.forEach(userGroup => {
if (!userGroup?.name) return;
// Find existing outbound by normalized tag/name
const existingIndex = (this.config.outbounds || []).findIndex(o =>
normalizeGroupName(o?.tag) === normalizeGroupName(userGroup.name)
);
if (existingIndex >= 0) {
// Merge with existing system group
const existing = this.config.outbounds[existingIndex];
// Merge 'providers' field (Sing-Box uses 'providers' not 'use')
if (Array.isArray(userGroup.use) && userGroup.use.length > 0) {
const validUserProviders = userGroup.use.filter(p => allProviderTags.has(p));
existing.providers = [...new Set([
...(existing.providers || []),
...validUserProviders
])];
}
// Merge 'outbounds' field (equivalent to Clash 'proxies')
if (Array.isArray(userGroup.proxies) && userGroup.proxies.length > 0) {
const validUserOutbounds = userGroup.proxies.filter(p => validRefs.has(p));
existing.outbounds = [...new Set([
...(existing.outbounds || []),
...validUserOutbounds
])];
}
// Preserve user's custom settings
if (userGroup.url) existing.url = userGroup.url;
if (typeof userGroup.interval === 'number') {
existing.interval = `${userGroup.interval}s`;
}
} else {
// New user-defined group - convert from Clash format and add
const newOutbound = {
type: userGroup.type === 'url-test' ? 'urltest' : 'selector',
tag: userGroup.name
};
// Validate outbounds references
if (Array.isArray(userGroup.proxies)) {
newOutbound.outbounds = userGroup.proxies.filter(p => validRefs.has(p));
}
// Validate providers references
if (Array.isArray(userGroup.use)) {
const validProviders = userGroup.use.filter(p => allProviderTags.has(p));
if (validProviders.length > 0) {
newOutbound.providers = validProviders;
}
}
// Only add if has valid outbounds or providers
if ((newOutbound.outbounds?.length > 0) || (newOutbound.providers?.length > 0)) {
this.config.outbounds.push(newOutbound);
}
}
});
}
/**
* Validate outbounds before final output
* Ensures urltest groups have outbounds, fills empty ones with all proxy tags
*/
validateOutbounds() {
const proxyList = this.getProxyList();
const providerTags = this.getAllProviderTags();
(this.config.outbounds || []).forEach(outbound => {
// For urltest groups, ensure they have outbounds or providers
if (outbound.type === 'urltest' &&
(!outbound.outbounds || outbound.outbounds.length === 0) &&
(!outbound.providers || outbound.providers.length === 0)) {
// Fill with all available proxy tags
outbound.outbounds = [...proxyList];
// Also use all providers if available
if (providerTags.length > 0) {
outbound.providers = [...providerTags];
}
}
});
}
formatConfig() {
const rules = generateRules(this.selectedRules, this.customRules);
const { site_rule_sets, ip_rule_sets } = generateRuleSets(this.selectedRules, this.customRules);
this.config.route.rule_set = [...site_rule_sets, ...ip_rule_sets];
// Add outbound_providers if we have any
if (this.providerUrls.length > 0) {
const existingProviders = Array.isArray(this.config.outbound_providers) ? this.config.outbound_providers : [];
const newProviders = this.generateOutboundProviders();
this.config.outbound_providers = [...existingProviders, ...newProviders];
}
// Validate outbounds: fill empty urltest groups with all proxies
this.validateOutbounds();
rules.filter(rule => !!rule.domain_suffix || !!rule.domain_keyword).map(rule => {
this.config.route.rules.push({
domain_suffix: rule.domain_suffix,
domain_keyword: rule.domain_keyword,
protocol: rule.protocol,
outbound: this.t(`outboundNames.${rule.outbound}`)
});
});
rules.filter(rule => !!rule.site_rules[0]).map(rule => {
this.config.route.rules.push({
rule_set: [
...(rule.site_rules.length > 0 && rule.site_rules[0] !== '' ? rule.site_rules : []),
],
protocol: rule.protocol,
outbound: this.t(`outboundNames.${rule.outbound}`)
});
});
rules.filter(rule => !!rule.ip_rules[0]).map(rule => {
this.config.route.rules.push({
rule_set: [
...(rule.ip_rules
.map(ip => ip.trim())
.filter(ip => ip !== '')
.map(ip => `${ip}-ip`))
],
protocol: rule.protocol,
outbound: this.t(`outboundNames.${rule.outbound}`)
});
});
rules.filter(rule => !!rule.ip_cidr).map(rule => {
this.config.route.rules.push({
ip_cidr: rule.ip_cidr,
protocol: rule.protocol,
outbound: this.t(`outboundNames.${rule.outbound}`)
});
});
this.config.route.rules.unshift(
{ clash_mode: 'direct', outbound: 'DIRECT' },
{ clash_mode: 'global', outbound: this.t('outboundNames.Node Select') },
{ action: 'sniff' },
{ protocol: 'dns', action: 'hijack-dns' }
);
this.config.route.auto_detect_interface = true;
this.config.route.final = this.t('outboundNames.Fall Back');
// 如果启用了 Clash UI,添加配置
// 如果启用 Clash UI 或传入了自定义参数,添加/覆盖 Clash API 配置
if (this.enableClashUI || this.externalController || this.externalUiDownloadUrl) {
const defaultExternalController = "0.0.0.0:9090";
const defaultExternalUiDownloadUrl = "https://gh-proxy.com/https://github.com/Zephyruso/zashboard/archive/refs/heads/gh-pages.zip";
const defaultExternalUi = "./ui";
const defaultSecret = "";
const defaultDownloadDetour = "DIRECT";
const defaultClashMode = "rule";
this.config.experimental = this.config.experimental || {};
const existingClashApi = this.config.experimental.clash_api || {};
const externalController = this.externalController || existingClashApi.external_controller || defaultExternalController;
const externalUiDownloadUrl = this.externalUiDownloadUrl || existingClashApi.external_ui_download_url || defaultExternalUiDownloadUrl;
const externalUi = existingClashApi.external_ui || defaultExternalUi;
const secret = existingClashApi.secret ?? defaultSecret;
const externalUiDownloadDetour = existingClashApi.external_ui_download_detour || defaultDownloadDetour;
const clashMode = existingClashApi.default_mode || defaultClashMode;
this.config.experimental.clash_api = {
...existingClashApi,
external_controller: externalController,
external_ui: externalUi,
external_ui_download_url: externalUiDownloadUrl,
external_ui_download_detour: externalUiDownloadDetour,
secret,
default_mode: clashMode
};
}
return this.config;
}
}