Skip to content

Commit

Permalink
Update collector.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarid committed May 31, 2024
1 parent 68db5b9 commit 1dd4911
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/collector.ts
Original file line number Diff line number Diff line change
@@ -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<Array<Config>> {
let maxConfigs: number = 200
const maxVlessConfigs: number = 20
const maxBuiltInConfigsPerType: number = 20
let protocols: Array<string> = []
let providers: Array<string> = []
let alpnList: Array<string> = []
Expand All @@ -28,11 +29,14 @@ export async function GetConfigList(url: URL, env: Env): Promise<Array<Config>>
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) || []
Expand All @@ -44,29 +48,26 @@ export async function GetConfigList(url: URL, env: Env): Promise<Array<Config>>
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) {
includeOriginalConfigs = true
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) {
Expand All @@ -81,7 +82,7 @@ export async function GetConfigList(url: URL, env: Env): Promise<Array<Config>>

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;
Expand Down Expand Up @@ -115,6 +116,7 @@ export async function GetConfigList(url: URL, env: Env): Promise<Array<Config>>
}
} catch (e) { }
}

if (!cleanDomainIPs.length) {
cleanDomainIPs = [MuddleDomain(url.hostname)]
}
Expand All @@ -126,6 +128,7 @@ export async function GetConfigList(url: URL, env: Env): Promise<Array<Config>>
.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) {
Expand All @@ -138,6 +141,7 @@ export async function GetConfigList(url: URL, env: Env): Promise<Array<Config>>
}
}
}

for (const el of acceptableConfigList) {
finalConfigList = finalConfigList.concat(
GetMultipleRandomElements(el.mergedConfigs, el.count)
Expand Down Expand Up @@ -171,14 +175,21 @@ export async function GetConfigList(url: URL, env: Env): Promise<Array<Config>>
}

finalConfigList = RemoveDuplicateConfigs(finalConfigList.filter(ValidateConfig))

let vlessConfigList: Array<Config> = []
let trojanConfigList: Array<Config> = []
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)]
Expand All @@ -187,6 +198,7 @@ export async function GetConfigList(url: URL, env: Env): Promise<Array<Config>>
}
return conf
})
console.log(finalConfigList)

return finalConfigList
}

0 comments on commit 1dd4911

Please sign in to comment.