parse-dnsmasq.ts 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import { createReadlineInterfaceFromResponse } from './fetch-text-by-line';
  2. import { parse as tldtsParse } from 'tldts';
  3. import type { NodeFetchResponse } from './make-fetch-happen';
  4. import type { UndiciResponseData } from './fetch-retry';
  5. import type { Response } from 'undici';
  6. function isDomainLoose(domain: string): boolean {
  7. const { isIcann, isPrivate, isIp } = tldtsParse(domain);
  8. return !!(!isIp && (isIcann || isPrivate));
  9. }
  10. export function extractDomainsFromFelixDnsmasq(line: string): string | null {
  11. if (line.startsWith('server=/') && line.endsWith('/114.114.114.114')) {
  12. return line.slice(8, -16);
  13. }
  14. return null;
  15. }
  16. export async function parseFelixDnsmasqFromResp(resp: NodeFetchResponse | UndiciResponseData | Response): Promise<string[]> {
  17. const results: string[] = [];
  18. for await (const line of createReadlineInterfaceFromResponse(resp, true)) {
  19. const domain = extractDomainsFromFelixDnsmasq(line);
  20. if (domain && isDomainLoose(domain)) {
  21. results.push(domain);
  22. }
  23. }
  24. return results;
  25. }