forked from Keywos/rule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pname.js_old
196 lines (187 loc) · 5.49 KB
/
pname.js_old
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
/**
* 日期:2023-08-06 12:15:05 仅支持Surge、Loon
* 注意: Surge 必须使用带 有参数 [ability=http-client-policy] 走指定节点功能的substore否则脚本无效
* 用法:Sub-Store 脚本操作里添加 此脚本链接 https://github.com/Keywos/rule/raw/main/pname.js#timeout=1000&bs=30
* 作者:@Key
* 功能:去除无效节点
*
* 参数:
* [bs=] 批处理节点数
* [timeout=] 超时时间 单位 ms
* [flag] 加国旗
* [px] 根据 [Https Ping cloudflare] 延时排序
*/
const $ = $substore;
const iar = $arguments;
let timeout = iar.timeout || 2000,
flag = iar.flag,
debug = iar.debug,
Sort = iar.px,
bs = iar.bs || 20;
const { isLoon: isLoon, isSurge: isSurge } = $substore.env,
target = isLoon ? "Loon" : isSurge ? "Surge" : undefined;
async function operator(e = [], targetPlatform, env) {
let tzname = "", subcoll = "", x = false, xy = false;
if (env?.source?.[e?.[0]?.subName]) x = true;
if (env?.source?._collection?.name) xy = true;
if (x && xy) {
tzname =
env.source._collection.name + ": [" + env.source._collection.subscriptions + "]";
subcoll = "组合订阅内单条订阅加了脚本, 输出组合订阅";
} else if (x) {
tzname = env.source[e[0].subName].name;
subcoll = "单条订阅脚本";
} else {
tzname = env.source._collection.name;
subcoll = "组合订阅脚本";
}
const startTime = new Date();
const support = isLoon || isSurge;
if (!support) {
$.notify("No Loon or Surge")
$.error(`No Loon or Surge`);
return e;
}
if (e.length < 1) {
$notification.post("PNAME:"+subcoll+tzname, "订阅无节点", "");
return e;
}
function klog(...arg) {
console.log("[PNAME] "+subcoll+tzname+ " " + arg);
}
const ein = e.length;
klog(`开始处理节点: ${ein} 个`);
klog(`批处理节点数: ${bs} 个`);
let i = 0, newnode = [];
while (i < e.length) {
const batch = e.slice(i, i + bs);
await Promise.all(
batch.map(async (pk) => {
try {
const OUTK = await OUTIA(pk);
const qcip = pk.server + OUTK.ip;
flag && (pk.name = getflag(OUTK.loc) + " " + pk.name);
newnode.push(OUTK.ip)
pk.Key = OUTK;
pk.qc = qcip
} catch (err) {
delog(err.message)
}
})
);
i += bs;
}
e = removels(e);
let eout = e.length;
if (eout > 2 && isSurge){
delog(newnode)
const allsame = newnode.every((value, index, arr) => value === arr[0]);
if(allsame){
klog(`未使用带指定节点功能的 SubStore`);
$notification.post('PNAME:点击以安装对应版本'+subcoll+tzname,'未使用带指定节点功能的 SubStore,或所有节点落地IP相同','',{url: "https://raw.githubusercontent.com/sub-store-org/Sub-Store/master/config/Surge-ability.sgmodule",})
return e;
}
}
Sort && (e.sort((a, b) => a.Key.tk - b.Key.tk));
const endTime = new Date();
const timeDiff = endTime.getTime() - startTime.getTime();
klog(`处理完后剩余: ${eout} 个`);
klog(`此方法总用时: ${zhTime(timeDiff)}`);
return e;
}
function getflag(e) {
const t = e
.toUpperCase()
.split("")
.map((e) => 127397 + e.charCodeAt());
return String.fromCodePoint(...t).replace(/🇹🇼/g, "🇨🇳");
}
function sleep(e) {
return new Promise((t) => setTimeout(t, e));
}
let apiRead = 0, apiw = 0;
async function OUTIA(e) {
const maxRE = 2;
//https://cloudflare.com/cdn-cgi/trace
const url = `https://cloudflare.com/cdn-cgi/trace`;
const getHttp = async (reTry) => {
try {
let r = ProxyUtils.produce([e], target);
let time = Date.now();
const response = await Promise.race([
$.http.get({ url: url, node: r, "policy-descriptor": r }),
new Promise((_, reject) =>
setTimeout(() => reject(new Error("timeout")), timeout)
),
]);
const data = response.body;
if (data.length > 0) {
let endtime = Date.now() - time;
let lines = data.split("\n");
let key = lines.reduce((acc, line) => {
const [name, value] = line.split("=").map((item) => item.trim());
if (["ip", "loc", "warp"].includes(name)) {
acc[name] = value;
acc["tk"] = endtime;
}
return acc;
}, {});
return key;
} else {
throw new Error(resdata.message);
}
} catch (error) {
if (reTry < maxRE) {
await sleep(getRandom());
delog(e.name + "-> [OUTKApi超时查询次数] " + reTry);
return getHttp(reTry + 1);
} else {
throw error;
}
}
};
const resGet = new Promise((resolve, reject) => {
getHttp(1)
.then((data) => {
apiw++;
resolve(data);
})
.catch(reject);
});
return resGet;
}
function getRandom() {
return Math.floor(Math.random() * (200 - 20 + 1) + 20);
}
function delog(...arg) {
if (debug) {
console.log("[PNAME] " + arg);
}
}
function removels(e) {
const t = new Set();
const n = [];
for (const s of e) {
if (s.qc && !t.has(s.qc)) {
t.add(s.qc);
n.push(s);
}
}
return n;
}
function zhTime(e) {
e = e.toString().replace(/-/g, "");
if (e < 1e3) {
return `${Math.round(e)}毫秒`;
} else if (e < 6e4) {
return `${Math.round(e / 1e3)}秒`;
} else if (e < 36e5) {
return `${Math.round(e / 6e4)}分钟`;
} else if (e >= 36e5) {
return `${Math.round(e / 36e5)}小时`;
}
}
function getid(e) {
let t = "ld";
return `${t}-${e.server}-${e.port}`;
}