forked from sousuke0422/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getInstancesInfos.js
446 lines (387 loc) · 12.6 KB
/
getInstancesInfos.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import glog from 'fancy-log'
import semver from 'semver'
import AbortController from 'abort-controller'
import extend from 'extend'
import loadyaml from './loadyaml.js'
import Queue from 'promise-queue';
import { performance } from 'perf_hooks';
import fetch from 'node-fetch';
const instances = loadyaml("./data/instances.yml")
const pqueue = new Queue(32)
const ua = "JoinMisskey/0.1.0; +https://join.misskey.page/instances";
function safeFetch(method, url, options)/*: Promise<Response | null | false | undefined>*/ {
const controller = new AbortController()
const timeout = setTimeout(
() => { controller.abort() },
30000
)
const start = performance.now();
// glog("POST start", url)
return fetch(url, extend(true, options, { method, signal: controller.signal })).then(
res => {
if (res && res.ok) {
const end = performance.now();
if (end - start > 1000) {
glog.warn("POST slow", url, (end - start) / 1000)
}
return res;
}
glog("POST finish", url, res.status, res.ok)
if (res.status >= 500 && res.status < 600) return null;
},
async e => {
glog("POST failed...", url, e.errno, e.type)
if (e.errno?.toLowerCase().includes('timeout') || e.type === 'aborted') return null;
return false;
}
).finally(() => {
clearTimeout(timeout)
})
}
async function fetchJson(method, url, json) {
const option = {
body: JSON.stringify(json ? json : {}),
headers: {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0"
},
redirect: "error"
};
let retryCount = 0;
while (retryCount < 2) {
if (retryCount > 0) glog('retry', url, retryCount);
await new Promise(resolve => retryCount > 0 ? setTimeout(resolve, 20000) : resolve());
const res = await safeFetch(method, url, option)
.then(res => {
if (res === null) return null;
if (!res) return false;
return res.json();
})
.catch(e => {
glog.error(url, e)
return false
});
if (res === false) return false;
if (res !== null) return res;
retryCount += 1;
}
return false;
}
async function getNodeinfo(base)/*: Promise<Response | null | false | undefined>*/ {
const controller = new AbortController()
const timeout = setTimeout(
() => { controller.abort() },
30000
)
const wellnownUrl = `https://${base}/.well-known/nodeinfo`;
const wellknown = await fetch(wellnownUrl, {
method: "GET",
headers: {
"User-Agent": ua,
},
redirect: "error",
signal: controller.signal
}).then(res => {
if (res && res.ok) {
glog("Get WellKnown Nodeinfo finish", wellnownUrl, res.status, res.ok)
return res.json();
}
return;
}).catch(async e => {
glog("Get WellKnown Nodeinfo failed...", wellnownUrl, e.errno, e.type)
return;
}).finally(() => {
clearTimeout(timeout);
});
if (wellknown.links == null || !Array.isArray(wellknown.links)) {
glog("WellKnown Nodeinfo was Not Array", wellnownUrl, wellknown);
return null;
}
const links = wellknown.links;
const lnik1_0 = links.find(link => link.rel === 'http://nodeinfo.diaspora.software/ns/schema/1.0');
const lnik2_0 = links.find(link => link.rel === 'http://nodeinfo.diaspora.software/ns/schema/2.0');
const lnik2_1 = links.find(link => link.rel === 'http://nodeinfo.diaspora.software/ns/schema/2.1');
const link = lnik2_1 ?? lnik2_0 ?? lnik1_0;
if (link == null || typeof link !== 'object') {
glog("Nodeinfo Link was Null", wellnownUrl);
return null;
}
const controller2 = new AbortController()
const timeout2 = setTimeout(
() => { controller2.abort() },
30000
)
const info = await fetch(link.href, {
method: "GET",
headers: {
"User-Agent": ua,
},
redirect: "error",
signal: controller2.signal
}).then(res => {
if (res && res.ok) {
glog("Get Nodeinfo finish", link.href, res.status, res.ok)
return res.json();
}
return;
}).catch(async e => {
glog("Get Nodeinfo failed...", link.href, e.errno, e.type)
if (e.errno?.toLowerCase().includes('timeout') || e.type === 'aborted') return null;
return;
}).finally(() => {
clearTimeout(timeout2);
})
return info;
}
async function safeGetNodeInfo(base) {
const retry = (timeout) => new Promise((res, rej) => {
setTimeout(() => {
getNodeinfo(base).then(res, rej)
}, timeout)
});
return getNodeinfo(base)
.then(res => res === undefined ? retry(10000) : res)
.catch(e => retry(10000))
.catch(() => null);
}
// misskey-dev/misskeyを最後に持っていくべし
export const ghRepos = [
//"mei23/misskey",
//"mei23/misskey-v11",
//"kokonect-link/cherrypick",
"misskey-dev/misskey"
];
export const gtRepos = [
//"codeberg.org/thatonecalculator/calckey",
//"akkoma.dev/FoundKeyGang/FoundKey",
]
function hasVulnerability(repo, version) {
switch (repo) {
case 'misskey-dev/misskey':
return (
//semver.satisfies(version, '< 12.119.2') ||
//semver.satisfies(version, '< 12.90.0') ||
//semver.satisfies(version, '< 12.51.0') ||
//semver.satisfies(version, '>= 10.46.0 < 10.102.4 || >= 11.0.0-alpha.1 < 11.20.2') ||
semver.satisfies(version, '<= 2023.11.0') // https://github.com/misskey-dev/misskey/security/advisories/GHSA-3f39-6537-3cgc
);
/*
case 'mei23/misskey':
return (
semver.satisfies(version, '< 10.102.608-m544') ||
semver.satisfies(version, '< 10.102.338-m544')
);
case 'mei23/misskey-v11':
return (
semver.satisfies(version, '< 11.37.1-20221202185541') ||
semver.satisfies(version, '< 11.37.1-20210825162615')
);
case 'FoundKeyGang/FoundKey':
return (
semver.satisfies(version, '< v13.0.0-preview3')
);
*/
default:
return false;
}
}
async function getVersions() {
glog("Getting Misskey Versions")
const maxRegExp = /<https:\/\/.*?>; rel="next", <https:\/\/.*?\?page=(\d+)>; rel="last"/;
const versions = new Map();
const versionOutput = {};
const vqueue = new Queue(3)
for (const repo of gtRepos) {
glog(repo, "Start")
const repoSplit = repo.split('/');
const res = await fetch(`https://${repoSplit[0]}/api/v1/repos/${repoSplit[1]}/${repoSplit[2]}/tags`, { "User-Agent": ua, }).catch(() => null);
if (!res || !res.ok) {
glog.error(`Failed to get tags from ${repo} (response is not ok)`);
continue;
};
const json = await res.json();
if (!Array.isArray(json)) {
glog.error(`Failed to get tags from ${repo} (body is not array)`);
continue;
}
const gtVersions = json.slice(0, 40);
for (let i = 0; i < gtVersions.length; i++) {
const version = semver.clean(gtVersions[i].name, { loose: true });
versions.set(version, {
repo: `${repoSplit[1]}/${repoSplit[2]}`,
count: i,
hasVulnerability: hasVulnerability(repo, version),
});
}
versionOutput[repo] = gtVersions.map(tag => tag.name);
glog(repo, "Finish", json.length);
}
const ghHeaders = {
"User-Agent": ua,
Authorization: `bearer ${process.env.LB_TOKEN}`
};
for (const repo of ghRepos) {
glog("GitHub", repo, "Start")
const res1 = await fetch(`https://api.github.com/repos/${repo}/releases`, { headers: ghHeaders })
const link = res1.headers.get("link")
const max = link && Math.min(Number(maxRegExp.exec(link)[1]), repo === "misskey-dev/misskey" ? 99999 : 4)
/**
* バージョンカウント
*/
let versionCount = 0;
/**
* バリュー算出用のバージョンカウント
* これを上げるとインスタンスバリューは下がる
*/
let valueCount = 0;
const resp = (await Promise.all([Promise.resolve(res1), ...(!link ? []
: Array(max - 1).fill()
.map((v, i) => `https://api.github.com/repos/${repo}/releases?page=${i + 2}`)
.map(url => vqueue.add(() => fetch(url, { headers: ghHeaders }))))]
.map((resa, i) => resa.then(
res => res.json(),
e => {
glog(repo, "Error(fetch)", e)
return Promise.resolve([])
}
).then(
json => {
for (const release of json) {
// glog("Misskey Version", release.tag_name)
const version = semver.clean(release.tag_name, { loose: true });
if (!version) {
glog.error("Invalid version", release.tag_name)
continue;
}
versions.set(version, {
repo,
count: versionCount,
valueCount,
hasVulnerability: hasVulnerability(repo, version),
});
// バージョンカウントアップ
versionCount++;
if (repo === 'misskey-dev/misskey' && version.indexOf('-') >= 0) {
// misskey-dev/misskeyかつプレリリースっぽければカウントアップしない
} else {
valueCount++;
}
}
// used for versionOutput
return json.map(release => release.tag_name);
},
e => {
glog(repo, "Error(json)", e)
return Promise.resolve([])
}
).catch(e => { throw Error(e) })
))).flat(1)
versionOutput[repo] = resp;
glog(repo, "Finish", resp.length)
}
glog("Got Misskey Versions")
return { versions, versionOutput }
}
export const getInstancesInfos = async function () {
glog("Getting Instances' Infos")
const promises = [];
const alives = [], deads = [], notMisskey = [], outdated = [];
const { versions, versionOutput } = await getVersions()
// eslint-disable-next-line no-restricted-syntax
for (let t = 0; t < instances.length; t += 1) {
const instance = instances[t]
promises.push(pqueue.add(async () => {
const nodeinfo = (await safeGetNodeInfo(instance.url)) || null;
if (!nodeinfo) {
deads.push(extend(true, { isAlive: false, value: 0 }, instance));
return;
}
if (nodeinfo.software.name !== "misskey") {
notMisskey.push({
nodeinfo,
...instance
});
return;
}
const versionInfo = (() => {
const sem1 = semver.clean(nodeinfo.software.version, { loose: true })
if (versions.has(sem1)) return { just: true, ...versions.get(sem1) };
const sem2 = semver.valid(semver.coerce(nodeinfo.software.version))
let current = { repo: 'misskey-dev/misskey', count: 999999, valueCount: 999999 };
for (const [key, value] of versions.entries()) {
if (sem1 && sem1.startsWith(key)) {
if (value.valueCount === 0) return { just: false, ...value };
else if (current.valueCount >= value.valueCount) current = { just: false, ...value };
} else if (sem2 && value.repo == 'misskey-dev/misskey' && sem2.startsWith(key)) {
if (value.valueCount === 0) return { just: false, ...value };
else if (current.valueCount >= value.valueCount) current = { just: false, ...value };
}
}
return current
})()
if (versionInfo.just && versionInfo.hasVulnerability) {
outdated.push({
nodeinfo,
...instance,
});
return;
};
const meta = (await fetchJson('POST', `https://${instance.url}/api/meta`)) || null;
const stats = (await fetchJson('POST', `https://${instance.url}/api/stats`)) || null;
const noteChart = (await fetchJson('POST', `https://${instance.url}/api/charts/notes`, { span: "day", limit: 15 })) || null;
if (nodeinfo && meta && stats && noteChart) {
if (meta) {
delete meta.emojis;
delete meta.announcements;
}
/* インスタンスバリューの算出 */
let value = 0
/* ノート増加数の15日間の平均 */
let npd15 = 0
// 1. バージョンのリリース順をもとに並び替え
value += 100000 - (versionInfo.valueCount - 30) * 7200
// (基準値に影響があるかないか程度に色々な値を考慮する)
if (noteChart && Array.isArray(noteChart.local?.inc)) {
// 2.
const nli15 = noteChart.local?.inc.filter(e => e !== 0)
if (nli15.length > 0) {
npd15 = nli15.reduce((prev, current) => prev + current) / nli15.length;
// 2. ノート増加数の15日間の平均 * 1
// eslint-disable-next-line no-mixed-operators
value += npd15 * 1;
}
// もし統計の数が15日に満たない場合、新規インスタンス特典を付与
// value += (15 - arr.length) * 360
}
alives.push(extend(true, instance, {
value,
meta,
nodeinfo,
stats,
npd15,
name: instance.name || nodeinfo.metadata.nodeName || meta.name || instance.url,
description: nodeinfo.metadata.nodeDescription || meta.description || (instance.description || null),
langs: instance.langs || ['ja', 'en', 'de', 'fr', 'zh', 'ko', 'ru', 'th', 'es'],
isAlive: true,
repo: versionInfo?.repo
}))
} else {
deads.push(extend(true, { isAlive: false, value: 0 }, instance))
}
}));
}
const interval = setInterval(() => {
glog(`${pqueue.getQueueLength()} requests remain and ${pqueue.getPendingLength()} requests processing.`)
}, 1000)
await Promise.all(promises);
clearInterval(interval)
glog("Got Instances' Infos")
return {
alives: alives.sort((a, b) => (b.value || 0) - (a.value || 0)),
deads,
notMisskey,
outdated,
versions,
versionOutput,
}
}