-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathworker.ts
executable file
·193 lines (176 loc) · 7.57 KB
/
worker.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
/*!
* Clash Subscription Worker (v2ray) v1.0
* Copyright 2023 Vahid Farid (https://twitter.com/vahidfarid)
* Licensed under GPLv3 (https://github.com/vfarid/clash-worker/blob/main/Licence.md)
*/
const MAX_CONFIGS: number = 1000
const INCLUDE_ORIGINAL: boolean = true
const ONLY_ORIGINAL: boolean = false
const SELECTED_TYPES: Array<string> = ["vmess", "ss", "ssr", "trojan", "snell", "http", "socks5"]
const SELECTED_PROVIDERS: Array<string> = []
const configProviders: any = {
"mahdibland": "https://raw.githubusercontent.com/mahdibland/SSAggregator/master/sub/sub_merge_yaml.yml",
"mfuu": "https://raw.githubusercontent.com/mfuu/v2ray/master/clash.yaml",
"peasoft": "https://raw.githubusercontent.com/peasoft/NoMoreWalls/master/list.yml",
"getnode": "https://raw.githubusercontent.com/a2470982985/getNode/main/clash.yaml",
"nodefree": "https://raw.githubusercontent.com/mlabalabala/v2ray-node/main/nodefree4clash.txt",
"clashnode": "https://raw.githubusercontent.com/mlabalabala/v2ray-node/main/clashnode4clash.txt",
}
const ipProviderLink: string = "https://raw.githubusercontent.com/vfarid/cf-clean-ips/main/list.json"
var selectedTypes: Array<string> = SELECTED_TYPES
var selectedProviders: Array<string> = SELECTED_PROVIDERS
var operators: Array<string> = []
var cleanIPs: Array<any> = []
var maxConfigs: number = MAX_CONFIGS
var includeOriginalConfigs: boolean = INCLUDE_ORIGINAL
var onlyOriginalConfigs: boolean = ONLY_ORIGINAL
import yaml from 'js-yaml'
import { landingPage } from "./landing"
import { mixConfig, validateConfig } from "./config"
import { getMultipleRandomElements } from "./helpers"
import { toClash } from "./clash"
export default {
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url)
const path = url.pathname.replace(/^\/|\/$/g, "")
const parts = path.split("/")
const type = parts[0].toLowerCase()
if (type === "clash") {
// var link = 'https://raw.githubusercontent.com/mfuu/v2ray/master/clash.yaml'
// var json = yaml.load(await fetch(link).then(r => r.text()))
// return new Response(JSON.stringify(json))
if (parts[1] !== undefined) {
if (parts[1].includes(".") || parts[1].includes(":")) { // Subdomain or IP
cleanIPs = parts[1].toLowerCase().trim().split(",").map((ip: string) => {return {ip: ip, operator: "IP"}})
operators = ["IP"]
} else { // Operator code
try {
operators = parts[1].toUpperCase().trim().split(",")
cleanIPs = await fetch(ipProviderLink)
.then((r: Response) => r.json())
.then((j: any) => j.ipv4)
cleanIPs = cleanIPs.filter((el: any) => operators.includes(el.operator))
} catch (e) { }
}
}
if (url.searchParams.has("max")) {
maxConfigs = parseInt(url.searchParams.get("max") as string)
if (!maxConfigs) {
maxConfigs = MAX_CONFIGS
}
}
if (url.searchParams.has("original")) {
const original = url.searchParams.get("original") as string
includeOriginalConfigs = ["1", "true", "yes", "y"].includes(original.toLowerCase())
}
if (includeOriginalConfigs && url.searchParams.has("merge")) {
const merge = url.searchParams.get("merge") as string
onlyOriginalConfigs = !["1", "true", "yes", "y"].includes(merge.toLowerCase())
}
if (url.searchParams.has("type")) {
selectedTypes = (url.searchParams.get("type") as string).toLocaleLowerCase().split(",").map((s: string) => s.trim())
}
if (url.searchParams.has("provider")) {
selectedProviders = (url.searchParams.get("provider") as string).toLocaleLowerCase().split(",").map((s: string) => s.trim())
}
if (includeOriginalConfigs && !onlyOriginalConfigs) {
maxConfigs = Math.floor(maxConfigs / 2)
}
var configList: Array<any> = []
var acceptableConfigList: Array<any> = []
var finalConfigList: Array<any> = []
var newConfigs: any
const configPerList: number = Math.floor(maxConfigs / Object.keys(configProviders).length)
for (const provider in configProviders) {
try {
if (selectedProviders.length > 0 && !selectedProviders.includes(provider)) {
continue
}
const content: string = await fetch(configProviders[provider]).then(r => r.text())
const json: any = yaml.load(content)
newConfigs = json.proxies;
if (!onlyOriginalConfigs) {
acceptableConfigList.push({
name: provider,
count: configPerList,
configs: newConfigs.filter((cnf: any) => cnf.type == "vmess"),
mergedConfigs: null,
})
}
if (includeOriginalConfigs) {
configList.push({
name: provider,
count: configPerList,
configs: newConfigs.filter((cnf: any) => selectedTypes.includes(cnf.type)),
renamedConfigs: null,
})
}
} catch (e) { }
}
var ipList = []
if (!cleanIPs.length) {
operators = ["General"]
cleanIPs = [{ip: "", operator: "General"}]
}
for (const operator of operators) {
var ipList = cleanIPs.filter(el => el.operator == operator).slice(0, 5)
var ip = ipList[Math.floor(Math.random() * ipList.length)].ip
for (const i in acceptableConfigList) {
const el = acceptableConfigList[i]
acceptableConfigList[i].mergedConfigs = el.configs
.map((cnf: any) => mixConfig(cnf, url, ip, operator, el.name))
.filter((cnf: any) => cnf.merged && cnf.name)
}
var remaining = 0
for (var i = 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) {
for (const i in configList) {
const el = configList[i]
configList[i].renamedConfigs = el.configs
.map((cnf: any) => validateConfig(cnf, el.name))
.filter((cnf: any) => cnf.name)
}
var remaining = 0
for (var i = 0; i < 5; i++) {
for (const el of configList) {
if (el.count > el.renamedConfigs.length) {
remaining = remaining + el.count - el.renamedConfigs.length
el.count = el.renamedConfigs.length
} else if (el.count < el.renamedConfigs.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.renamedConfigs, el.count)
)
}
}
return new Response(toClash(finalConfigList))
} else if (path) {
const addrPath = url.pathname.replace(/^\/|\/$/g, "")
const newUrl = new URL("https://" + addrPath)
return fetch(new Request(newUrl, request))
} else {
return landingPage(url)
}
}
}