build-speedtest-domainset.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import path from 'node:path';
  2. import tldts from 'tldts-experimental';
  3. import { task } from './trace';
  4. import { SHARED_DESCRIPTION } from './constants/description';
  5. import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
  6. import { DomainsetOutput } from './lib/rules/domainset';
  7. import { OUTPUT_SURGE_DIR, SOURCE_DIR } from './constants/dir';
  8. import { $$fetch } from './lib/fetch-retry';
  9. import { fastUri } from 'fast-uri';
  10. interface SpeedTestServer {
  11. url: string,
  12. lat: string,
  13. lon: string,
  14. distance: number,
  15. name: string,
  16. country: string,
  17. cc: string,
  18. sponsor: string,
  19. id: string,
  20. preferred: number,
  21. https_functional: number,
  22. host: string
  23. }
  24. const getSpeedtestHostsGroupsPromise = $$fetch('https://speedtest-net-servers.cdn.skk.moe/servers.json')
  25. .then(res => res.json() as Promise<SpeedTestServer[]>)
  26. .then((data) => data.reduce<string[]>((prev, cur) => {
  27. let hn: string | null | undefined = null;
  28. if (cur.host) {
  29. hn = tldts.getHostname(cur.host, { detectIp: false, validateHostname: true });
  30. if (hn) {
  31. prev.push(hn);
  32. }
  33. }
  34. if (cur.url) {
  35. hn = fastUri.parse(cur.url).host;
  36. if (hn) {
  37. prev.push(hn);
  38. }
  39. }
  40. return prev;
  41. }, []));
  42. export const buildSpeedtestDomainSet = task(require.main === module, __filename)(
  43. async (span) => new DomainsetOutput(span, 'speedtest')
  44. .withTitle('Sukka\'s Ruleset - Speedtest Domains')
  45. .withDescription([
  46. ...SHARED_DESCRIPTION,
  47. '',
  48. 'This file contains common speedtest endpoints.'
  49. ])
  50. .addFromDomainset(readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'domainset/speedtest.conf')))
  51. .addFromDomainset(readFileIntoProcessedArray(path.resolve(OUTPUT_SURGE_DIR, 'domainset/speedtest.conf')))
  52. .bulkAddDomain(await span.traceChildPromise('get speedtest hosts groups', getSpeedtestHostsGroupsPromise))
  53. .write()
  54. );