-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathswitch-check-ytb.js
230 lines (203 loc) · 9.73 KB
/
switch-check-ytb.js
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/***
For Quantumult-X 598+ ONLY!!
[task_local]
// UI 查询版本
event-interaction https://raw.githubusercontent.com/KOP-XIAO/QuantumultX/master/Scripts/switch-check-ytb.js, tag=YouTube 切换, img-url=https://raw.githubusercontent.com/Koolson/Qure/master/IconSet/Color/YouTube_Letter.png, enabled=true
// cron task 版本
0 8 * * * https://raw.githubusercontent.com/KOP-XIAO/QuantumultX/master/Scripts/switch-check-ytb.js#policy=你的策略组, tag=YouTube 定时切换, img-url=https://raw.githubusercontent.com/Koolson/Qure/master/IconSet/Color/YouTube_Letter.png, enabled=true
ps. 简单粗暴的 UI-Interaction 版本。无数据持久化、粗暴延迟等待。完美主义建议使用 Helge大佬的boxjs版本 https://t.me/QuanXNews/193
@XIAO_KOP
2022-07-04
**/
const BASE_URL = 'https://www.youtube.com/premium'
const link = { "media-url": "https://raw.githubusercontent.com/KOP-XIAO/QuantumultX/master/img/southpark/7.png" }
var cronsign = $environment.executeType == 0 || $environment.executeType == "0" || $environment.executeType == "-1"? "Y" : "N"
var policy = $environment.executeType == 0 || $environment.executeType == "0" || $environment.executeType == "-1"? GetPolicy($environment.sourcePath) : $environment.params
console.log(JSON.stringify($environment))
console.log("策略组:"+policy)
function GetPolicy(cnt) {
if (cnt && cnt.indexOf("#policy=") !=-1) {
return decodeURIComponent(cnt.split("#policy=")[1].trim())
}else {
return ""
}
}
const message = {
action: "get_customized_policy",
content: policy
};
var output=[]
var OKList=[]
var NoList=["不支持节点 ➟ "]
var ErrorList=["检测出错节点 ➟ "]
var pflag=1 //是否是策略,或者简单节点
var sign=0
$configuration.sendMessage(message).then(resolve => {
if (resolve.error) {
console.log(resolve.error);
$done()
}
if (resolve.ret) {
//$notify(JSON.stringify(resolve.ret))
output=JSON.stringify(resolve.ret[message.content])? JSON.parse(JSON.stringify(resolve.ret[message.content]["candidates"])) : [policy]
pflag = JSON.stringify(resolve.ret[message.content])? pflag:0
console.log("YouTube Premium 检测")
console.log("节点or策略组:"+pflag)
if (pflag==1) {
console.log("节点数量:"+resolve.ret[policy]["candidates"].length)
if(resolve.ret[policy]["candidates"].length==0) {
$done({"title":"YouTube Premium 检测","htmlMessage":`<p style="text-align: center; font-family: -apple-system; font-size: large; font-weight: thin"><br><b>😭 无有效节点</b>`});
}
}
//$notify(typeof(output),output)
Check()
//$done({"title":"策略内容","message":output})
}
//$done();|
}, reject => {
// Normally will never happen.
$done();
});
function Len(cnt) {
return cnt.length-1
}
function Check() {
var relay = 2000;
for ( var i=0;i < output.length;i++) {
testYTB(output[i])
}
if (output.length<=5) {
relay = 2000
} else if (output.length<10) {
relay =4000
} else if (output.length<15) {
relay =6000
} else if (output.length<20) {
relay =8000
} else {
relay =10000
}
console.log(output.length+":"+relay)
setTimeout(() => {
console.log("⛳️ 共计 "+OKList.length+" 个:支持节点 ➟ "+ OKList)
console.log("🏠 共计 "+Len(NoList)+" 个:"+NoList)
console.log("🕹 共计 "+Len(ErrorList)+" 个:"+ErrorList)
sign = 1
if (OKList[0] && pflag==1) { //有支持节点、且为策略组才操作
console.log("开始排序")
ReOrder(OKList)
} else if (!OKList[0]){ //不支持
content =pflag==0 ? `<p style="text-align: center; font-family: -apple-system; font-size: large; font-weight: thin"><br><b>😭 该节点不支持 YouTube Premium </b><br><br>👇<br><br><font color=#FF5733>-------------------------<br><b>⟦ `+policy+` ⟧ </b><br>-------------------------</font>`: `<p style="text-align: center; font-family: -apple-system; font-size: large; font-weight: thin">` + "<br>❌ <b>⟦ "+policy+ " ⟧ </b>⚠️ 切换失败<br><br><b>该策略组内未找到支持 YouTube Premium 的节点" + "<br><br><font color=#FF5733>-----------------------------<br><b>检测详情请查看JS脚本记录</b><br>-----------------------------</font>"+`</p>`
$done({"title":"YouTube Premium 检测&切换", "htmlMessage": content})
} else if (OKList[0]){ //支持, 但为节点
content =`<p style="text-align: center; font-family: -apple-system; font-size: large; font-weight: thin"><br><b> 🎉 该节点支持 YouTube Premium </b><br><br>👇<br><br><font color=#FF5733>-------------------------<br><b>⟦ `+policy+` ⟧ </b><br>-------------------------</font>`
$done({"title":"YouTube Premium 检测&切换", "htmlMessage": content})
}
}, relay)
}
//选择最优延迟节点
function ReOrder(cnt) {
const array = cnt;
const messageURL = {
action: "url_latency_benchmark",
content: array
};
$configuration.sendMessage(messageURL).then(resolve => {
if (resolve.error) {
console.log(resolve.error);
}
if (resolve.ret) {
let output=JSON.stringify(resolve.ret);
console.log("节点延迟:"+output);
//排序
console.log("排序前: "+ array)
if(array){
try {
array.sort(function (a,b) {
//console.log(a+" VS "+b)
return (resolve.ret[a][1]!=-1 && resolve.ret[b][1] !=-1)? resolve.ret[a][1]-resolve.ret[b][1] : resolve.ret[b][1]
})
} catch (err) {
console.log(err)
}
}
console.log("排序后: "+array)
let Ping =resolve.ret[array[0]]
const dict = { [policy] : array[0]};
if(array[0]) {
console.log("选定支持YouTube Premium:"+array[0]+"延迟数据为 👉"+Ping)
Ping = " ⚡️ 节点延迟 ➟ 「 "+Ping + " 」 "
}
const mes1 = {
action: "set_policy_state",
content: dict
};
$configuration.sendMessage(mes1).then(resolve => {
if (resolve.error) {
console.log(resolve.error);
content =pflag==0 && array[0]? `<p style="text-align: center; font-family: -apple-system; font-size: large; font-weight: thin">` + "<br><b>⟦ "+policy+ " ⟧ </b><br><br>🎉 该节点支持 <b>YouTube Premium</b>" + `</p>` : `<p style="text-align: center; font-family: -apple-system; font-size: large; font-weight: thin">` + "<br><b>⟦ "+policy+ " ⟧ </b><br><br>⚠️ 该节点不支持 <b>YouTube Premium</b>" + `</p>`
content = pflag!=0 && !array[0]? `<p style="text-align: center; font-family: -apple-system; font-size: large; font-weight: thin">` + "<br>❌ <b>⟦ "+policy+ " ⟧ </b>⚠️ 切换失败<br><br>该策略组内未找到支持 <b>YouTube Premium</b> 的节点" + "<br><br>-----------------------------<br><b><font color=#FF5733>检测详情请查看JS脚本记录</font></b><br>-----------------------------"+`</p>` : content
$done({"title":"YouTube 检测&切换", "htmlMessage": content})
}
if (resolve.ret) {
console.log("已经切换至支持 <b>Premium</b> 的路线 ➟ "+array[0])
if (cronsign == "Y") { $notify("📺 YouTube Premium 定时检测&切换", "🎉 已经切换至支持 Premium 的最优延迟线路👇", array[0] +"\n 👉 "+Ping)}
content = `<p style="text-align: center; font-family: -apple-system; font-size: large; font-weight: thin">` + "<br><b>⟦ "+policy+ " ⟧ </b>已切换至支持<b>Premium</b> 的路线中延迟最优节点<br><br> 👇<br><br> ⟦ "+array[0]+ " ⟧" + "<br><br><font color=#16A085>"+Ping+"</font><br>-----------------------------<br><b><font color=#FF5733>检测详情请查看JS脚本记录</font></b><br>-----------------------------"+`</p>`
$done({"title":"YouTube 检测&切换", "htmlMessage": content })
}
}, reject => {
$done();
});
}
//$done();
}, reject => {
// Normally will never happen.
$done();
});
}
function testYTB(pname) {
return new Promise((resolve, reject) => {
let opts = { policy : pname }
let option = {
url: BASE_URL,
opts: opts,
headers: {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36',
'Accept-Language': 'en',
},
}
$task.fetch(option).then(response=> {
let data = response.body
//console.log(response.statusCode)
if (sign==0) {
if (response.statusCode !== 200) {
console.log(pname+":检测出错")
ErrorList.push(pname)
reject('Error')
return
}
if (data.indexOf('Premium is not available in your country') !== -1) {
console.log(pname+":未支持")
NoList.push(pname)
resolve('Not Available')
return
}
let region = ''
let re = new RegExp('"GL":"(.*?)"', 'gm')
let result = re.exec(data)
if (result != null && result.length === 2) {
region = result[1]
} else if (data.indexOf('www.google.cn') !== -1) {
region = 'CN'
} else {
region = 'US'
}
console.log(pname+":支持"+region)
OKList.push(pname)
resolve(region)
}
reject('Error')
})
})
}