build-speedtest-domainset.ts 1.9 KB

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