forked from vfarid/v2ray-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.ts
214 lines (191 loc) · 8.52 KB
/
collector.ts
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
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, 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 maxBuiltInConfigsPerType: number = 20
let protocols: Array<string> = []
let providers: Array<string> = []
let alpnList: Array<string> = []
let fingerPrints: Array<string> = []
let includeOriginalConfigs: boolean = true
let includeMergedConfigs: boolean = true
let cleanDomainIPs: Array<string> = []
let myConfigs: Array<string> = []
let settingsNotAvailable: boolean = true
let enableFragments = false
try {
maxConfigs = parseInt(await env.settings.get("MaxConfigs") || "200")
const settingsVersion = await env.settings.get("Version") || "2.0"
if (settingsVersion == version) {
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")) || "").trim().length > 0
if (blockPorn) {
protocols = ["built-in-vless"]
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) || []
myConfigs = (await env.settings.get("Configs"))?.split("\n").filter(t => t.trim().length > 0) || []
alpnList = (await env.settings.get("ALPNs"))?.split("\n").filter(t => t.trim().length > 0) || []
fingerPrints = (await env.settings.get("FingerPrints"))?.split("\n").filter(t => t.trim().length > 0) || []
includeOriginalConfigs = (await env.settings.get("IncludeOriginalConfigs") || "yes") == "yes"
includeMergedConfigs = ((await env.settings.get("IncludeMergedConfigs") || "yes") == "yes") && (protocols.includes("vmess") || protocols.includes("vless") || myConfigs.length > 0)
cleanDomainIPs = (await env.settings.get("CleanDomainIPs"))?.split("\n").filter(t => t.trim().length > 0) || []
settingsNotAvailable = (await env.settings.get("MaxConfigs")) === null
enableFragments = await env.settings.get("EnableFragments") == "yes"
} catch { }
if (!protocols.length && !myConfigs) {
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 - maxBuiltInConfigsPerType
}
if (protocols.includes("built-in-trojan")) {
maxConfigs = maxConfigs - maxBuiltInConfigsPerType
}
if (settingsNotAvailable) {
includeOriginalConfigs = true
includeMergedConfigs = true
}
if (!providers.length) {
providers = await fetch(providersUri).then(r => r.text()).then(t => t.trim().split("\n").filter(t => t.trim().length > 0))
}
if (includeOriginalConfigs && includeMergedConfigs) {
maxConfigs = Math.floor(maxConfigs / 2)
}
let configList: Array<any> = []
let acceptableConfigList: Array<any> = []
let finalConfigList: Array<Config> = []
let newConfigs: Array<any> = []
const configPerList: number = Math.floor(maxConfigs / Object.keys(providers).length)
for (const providerUrl of providers) {
try {
var content: string = await fetch(providerUrl).then(r => r.text())
try {
const json: any = yaml.load(content)
newConfigs = json.proxies;
if (!newConfigs.length) {
throw "no-yaml"
}
newConfigs = newConfigs.filter((cnf: any) => protocols.includes(cnf.type)).filter(ValidateConfig)
} catch (e) {
if (IsBase64(content)) {
content = Buffer.from(content, "base64").toString("utf-8")
}
newConfigs = content.split("\n").filter((cnf: string) => cnf.match(new RegExp(`^(${protocols.join("|")}):\/\/`, "i")))
if (newConfigs.length) {
newConfigs = newConfigs.map(DecodeConfig).filter(ValidateConfig)
}
}
if (includeMergedConfigs) {
acceptableConfigList.push({
url: providerUrl,
count: configPerList,
configs: newConfigs.filter((cnf: any) => ["vmess", "vless"].includes(cnf.configType)),
mergedConfigs: null,
})
}
if (includeOriginalConfigs) {
configList.push({
url: providerUrl,
count: configPerList,
configs: newConfigs,
})
}
} catch (e) { }
}
if (!cleanDomainIPs.length) {
cleanDomainIPs = [MuddleDomain(url.hostname)]
}
let address: string = cleanDomainIPs[Math.floor(Math.random() * cleanDomainIPs.length)]
for (const i in acceptableConfigList) {
const el: any = acceptableConfigList[i]
acceptableConfigList[i].mergedConfigs = el.configs
.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) {
if (el.count > el.mergedConfigs.length) {
remaining = remaining + el.count - el.mergedConfigs.length
el.count = el.mergedConfigs.length
} else if (el.count < el.mergedConfigs.length && remaining > 0) {
el.count = el.count + Math.ceil(remaining / 3)
remaining = remaining - Math.ceil(remaining / 3)
}
}
}
for (const el of acceptableConfigList) {
finalConfigList = finalConfigList.concat(
GetMultipleRandomElements(el.mergedConfigs, el.count)
)
}
if (includeOriginalConfigs) {
let remaining = 0
for (let i = 0; i < 5; i++) {
for (const el of configList) {
if (el.count > el.configs.length) {
remaining = remaining + el.count - el.configs.length
el.count = el.configs.length
} else if (el.count < el.configs.length && remaining > 0) {
el.count = el.count + Math.ceil(remaining / 3)
remaining = remaining - Math.ceil(remaining / 3)
}
}
}
for (const el of configList) {
finalConfigList = finalConfigList.concat(
GetMultipleRandomElements(el.configs, el.count)
)
}
}
if (myConfigs.length) {
let myValidConfigs: Array<any> = myConfigs.map(DecodeConfig).filter(ValidateConfig)
if (includeOriginalConfigs || !includeMergedConfigs) {
finalConfigList = finalConfigList.concat(myValidConfigs)
}
if (includeMergedConfigs) {
let myMergedConfigs: Array<any> = myValidConfigs.map((cnf: any) => MixConfig(cnf, url, address, "my"))
console.log(myValidConfigs, myMergedConfigs)
myMergedConfigs = myMergedConfigs.filter((cnf: any) => cnf?.merged && cnf?.remarks)
console.log(myValidConfigs, myMergedConfigs)
finalConfigList = finalConfigList.concat(myMergedConfigs)
}
}
finalConfigList = RemoveDuplicateConfigs(finalConfigList.filter(ValidateConfig))
let vlessConfigList: Array<Config> = []
let trojanConfigList: Array<Config> = []
let startNo = 1
if (protocols.includes("built-in-vless")) {
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)]
if (enableFragments && conf.tls == "tls") {
conf.fragment = `tlshello,${fragmentsLengthList[Math.floor(Math.random() * fragmentsLengthList.length)]},${fragmentsIntervalList[Math.floor(Math.random() * fragmentsIntervalList.length)]}`
}
return conf
})
return finalConfigList
}