-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add BaseConfigBuilder class for building configuration
- Loading branch information
Showing
5 changed files
with
376 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"wrangler": "^3.60.3" | ||
}, | ||
"dependencies": { | ||
"js-yaml": "^4.1.0" | ||
"js-yaml": "^4.1.0", | ||
"sublink-plus": "file:" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { ProxyParser } from './ProxyParsers.js'; | ||
import { DeepCopy } from './utils.js'; | ||
|
||
export class BaseConfigBuilder { | ||
constructor(inputString, baseConfig) { | ||
this.inputString = inputString; | ||
this.config = DeepCopy(baseConfig); | ||
} | ||
|
||
async build() { | ||
const customItems = await this.parseCustomItems(); | ||
this.addCustomItems(customItems); | ||
this.addSelectors(); | ||
return this.formatConfig(); | ||
} | ||
|
||
async parseCustomItems() { | ||
const urls = this.inputString.split('\n').filter(url => url.trim() !== ''); | ||
const parsedItems = []; | ||
|
||
for (const url of urls) { | ||
const result = await ProxyParser.parse(url); | ||
if (Array.isArray(result)) { | ||
for (const subUrl of result) { | ||
const subResult = await ProxyParser.parse(subUrl); | ||
if (subResult) { | ||
parsedItems.push(subResult); | ||
} | ||
} | ||
} else if (result) { | ||
parsedItems.push(result); | ||
} | ||
} | ||
|
||
return parsedItems; | ||
} | ||
|
||
addCustomItems(customItems) { | ||
// This method should be implemented in child classes | ||
throw new Error('addCustomItems must be implemented in child class'); | ||
} | ||
|
||
addSelectors() { | ||
// This method should be implemented in child classes | ||
throw new Error('addSelectors must be implemented in child class'); | ||
} | ||
|
||
formatConfig() { | ||
// This method should be implemented in child classes | ||
throw new Error('formatConfig must be implemented in child class'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,35 @@ | ||
import { SING_BOX_CONFIG, SELECTORS_LIST } from './config.js'; | ||
import { ProxyParser } from './ProxyParsers.js'; | ||
import { BaseConfigBuilder } from './BaseConfigBuilder.js'; | ||
import { DeepCopy } from './utils.js'; | ||
|
||
export class ConfigBuilder { | ||
export class ConfigBuilder extends BaseConfigBuilder { | ||
constructor(inputString) { | ||
this.inputString = inputString; | ||
this.config = DeepCopy(SING_BOX_CONFIG); | ||
super(inputString, SING_BOX_CONFIG); | ||
} | ||
|
||
async build() { | ||
const customOutbounds = await this.parseCustomOutbounds(); | ||
this.addCustomOutbounds(customOutbounds); | ||
this.addSelectors(); | ||
return this.config; | ||
} | ||
|
||
async parseCustomOutbounds() { | ||
const urls = this.inputString.split('\n').filter(url => url.trim() !== ''); | ||
const parsedOutbounds = []; | ||
|
||
for (const url of urls) { | ||
const result = await ProxyParser.parse(url); | ||
if (Array.isArray(result)) { | ||
// If the result is an array, it's from an HTTP(S) source | ||
for (const subUrl of result) { | ||
const subResult = await ProxyParser.parse(subUrl); | ||
if (subResult) { | ||
parsedOutbounds.push(subResult); | ||
} | ||
} | ||
} else if (result) { | ||
parsedOutbounds.push(result); | ||
} | ||
} | ||
|
||
return parsedOutbounds; | ||
} | ||
|
||
addCustomOutbounds(customOutbounds) { | ||
// Filter out null values before adding to config.outbounds | ||
const validOutbounds = customOutbounds.filter(outbound => outbound != null); | ||
this.config.outbounds.push(...validOutbounds); | ||
addCustomItems(customItems) { | ||
const validItems = customItems.filter(item => item != null); | ||
this.config.outbounds.push(...validItems); | ||
} | ||
|
||
addSelectors() { | ||
const tagList = this.config.outbounds.filter(outbound => outbound?.server != undefined).map(outbound => outbound.tag); | ||
addSelectors() { | ||
const tagList = this.config.outbounds.filter(outbound => outbound?.server != undefined).map(outbound => outbound.tag); | ||
this.config.outbounds.push({ | ||
type: "urltest", | ||
tag: "⚡ 自动选择", | ||
outbounds: DeepCopy(tagList), | ||
}); | ||
tagList.unshift('DIRECT', 'REJECT', '⚡ 自动选择'); | ||
SELECTORS_LIST.forEach(selector => { | ||
this.config.outbounds.push({ | ||
type: "selector", | ||
tag: selector, | ||
outbounds: selector !== '🚀 节点选择' ? ['🚀 节点选择', ...tagList] : tagList | ||
}); | ||
}); | ||
} | ||
SELECTORS_LIST.forEach(selector => { | ||
this.config.outbounds.push({ | ||
type: "selector", | ||
tag: selector, | ||
outbounds: selector !== '🚀 节点选择' ? ['🚀 节点选择', ...tagList] : tagList | ||
}); | ||
}); | ||
} | ||
|
||
formatConfig() { | ||
return this.config; | ||
} | ||
} |
Oops, something went wrong.