diff --git a/src/collector.ts b/src/collector.ts index d7de4cd4..f0820e64 100644 --- a/src/collector.ts +++ b/src/collector.ts @@ -1,15 +1,16 @@ import yaml from 'js-yaml' import { Buffer } from 'buffer' import { GetVlessConfigList } from './vless' +import { GetTrojanConfigList } from './trojan' import { MixConfig, ValidateConfig, DecodeConfig } from "./config" import { GetMultipleRandomElements, RemoveDuplicateConfigs, AddNumberToConfigs, IsBase64, MuddleDomain } from "./helpers" -import { version, defaultProtocols, defaultALPNList, defaultPFList, fragmentsLengthList, fragmentsIntervalList } from "./variables" +import { version, providersUri, defaultProtocols, defaultALPNList, defaultPFList, fragmentsLengthList, fragmentsIntervalList } from "./variables" import { Env, Config } from "./interfaces" export async function GetConfigList(url: URL, env: Env): Promise> { let maxConfigs: number = 200 - const maxVlessConfigs: number = 20 + const maxBuiltInConfigsPerType: number = 20 let protocols: Array = [] let providers: Array = [] let alpnList: Array = [] @@ -28,11 +29,14 @@ export async function GetConfigList(url: URL, env: Env): Promise> protocols = await env.settings.get("Protocols").then(val => {return val ? val.split("\n") : []}) } const blockPorn = await env.settings.get("BlockPorn") == "yes" - const limitCountries = ((await env.settings.get("Countries"))?.split(",") || []).length > 0 + const limitCountries = ((await env.settings.get("Countries")) || "").trim().length > 0 - if (blockPorn || limitCountries) { + if (blockPorn) { protocols = ["built-in-vless"] - maxConfigs = 0 + maxConfigs = maxBuiltInConfigsPerType + } else if (limitCountries) { + protocols = ["built-in-vless", "built-in-trojan"] + maxConfigs = maxBuiltInConfigsPerType * 2 } providers = (await env.settings.get("Providers"))?.split("\n").filter(t => t.trim().length > 0) || [] @@ -44,21 +48,18 @@ export async function GetConfigList(url: URL, env: Env): Promise> settingsNotAvailable = (await env.settings.get("MaxConfigs")) === null myConfigs = (await env.settings.get("Configs"))?.split("\n").filter(t => t.trim().length > 0) || [] enableFragments = await env.settings.get("EnableFragments") == "yes" - - let proxies = (await env.settings.get("ManualProxies"))?.split("\n").filter(t => t.trim().length > 0) || [] - if (!proxies.length) { - proxies = await fetch("https://raw.githubusercontent.com/vfarid/v2ray-worker/main/resources/proxy-list.txt").then(r => r.text()).then(t => t.trim().split("\n").filter(t => t.trim().length > 0)) - } - await env.settings.put("Proxies", proxies.join("\n")) } catch { } - + protocols = protocols.length ? protocols : defaultProtocols alpnList = alpnList.length ? alpnList : defaultALPNList fingerPrints = fingerPrints.length ? fingerPrints : defaultPFList cleanDomainIPs = cleanDomainIPs.length ? cleanDomainIPs : [MuddleDomain(url.hostname)] if (protocols.includes("built-in-vless")) { - maxConfigs = maxConfigs - maxVlessConfigs + maxConfigs = maxConfigs - maxBuiltInConfigsPerType + } + if (protocols.includes("built-in-trojan")) { + maxConfigs = maxConfigs - maxBuiltInConfigsPerType } if (settingsNotAvailable) { @@ -66,7 +67,7 @@ export async function GetConfigList(url: URL, env: Env): Promise> includeMergedConfigs = true } if (!providers.length) { - providers = await fetch("https://raw.githubusercontent.com/vfarid/v2ray-worker/main/resources/provider-list.txt").then(r => r.text()).then(t => t.trim().split("\n").filter(t => t.trim().length > 0)) + providers = await fetch(providersUri).then(r => r.text()).then(t => t.trim().split("\n").filter(t => t.trim().length > 0)) } if (includeOriginalConfigs && includeMergedConfigs) { @@ -81,7 +82,7 @@ export async function GetConfigList(url: URL, env: Env): Promise> for (const providerUrl of providers) { try { - var content: string = await fetch(providerUrl.trim()).then(r => r.text()) + var content: string = await fetch(providerUrl).then(r => r.text()) try { const json: any = yaml.load(content) newConfigs = json.proxies; @@ -115,6 +116,7 @@ export async function GetConfigList(url: URL, env: Env): Promise> } } catch (e) { } } + if (!cleanDomainIPs.length) { cleanDomainIPs = [MuddleDomain(url.hostname)] } @@ -126,6 +128,7 @@ export async function GetConfigList(url: URL, env: Env): Promise> .map((cnf: any) => MixConfig(cnf, url, address, el.name)) .filter((cnf: any) => cnf?.merged && cnf?.remarks) } + let remaining: number = 0 for (let i: number = 0; i < 5; i++) { for (const el of acceptableConfigList) { @@ -138,6 +141,7 @@ export async function GetConfigList(url: URL, env: Env): Promise> } } } + for (const el of acceptableConfigList) { finalConfigList = finalConfigList.concat( GetMultipleRandomElements(el.mergedConfigs, el.count) @@ -171,14 +175,21 @@ export async function GetConfigList(url: URL, env: Env): Promise> } finalConfigList = RemoveDuplicateConfigs(finalConfigList.filter(ValidateConfig)) + + let vlessConfigList: Array = [] + let trojanConfigList: Array = [] + let startNo = 1 if (protocols.includes("built-in-vless")) { - finalConfigList = AddNumberToConfigs(finalConfigList, maxVlessConfigs + 1) - finalConfigList = (await GetVlessConfigList(url.hostname, cleanDomainIPs, 1, maxVlessConfigs, env)).concat(finalConfigList) - } else { - finalConfigList = AddNumberToConfigs(finalConfigList, 1) + vlessConfigList = await GetVlessConfigList(url.hostname, cleanDomainIPs, startNo, maxBuiltInConfigsPerType, env) + startNo += maxBuiltInConfigsPerType } - + if (protocols.includes("built-in-trojan")) { + trojanConfigList = await GetTrojanConfigList(url.hostname, cleanDomainIPs, startNo, maxBuiltInConfigsPerType, env) + startNo += maxBuiltInConfigsPerType + } + finalConfigList = vlessConfigList.concat(trojanConfigList).concat(AddNumberToConfigs(finalConfigList, startNo)) + finalConfigList = finalConfigList.map((conf: Config) => { conf.fp = fingerPrints[Math.floor(Math.random() * fingerPrints.length)] conf.alpn = alpnList[Math.floor(Math.random() * alpnList.length)] @@ -187,6 +198,7 @@ export async function GetConfigList(url: URL, env: Env): Promise> } return conf }) + console.log(finalConfigList) return finalConfigList } \ No newline at end of file