From 71a26a820ad8cd9159b72668cc8a3b1e7a727616 Mon Sep 17 00:00:00 2001 From: proletarius101 Date: Sat, 1 Aug 2020 16:49:11 +0800 Subject: [PATCH] feat: discard domains not resolvable --- docs/guide/custom-config.md | 7 +++++++ lib/generator/artifact.ts | 13 +++++++++++++ lib/types.ts | 1 + lib/utils/config.ts | 1 + 4 files changed, 22 insertions(+) diff --git a/docs/guide/custom-config.md b/docs/guide/custom-config.md index 4ee7b6a08..6ed834347 100644 --- a/docs/guide/custom-config.md +++ b/docs/guide/custom-config.md @@ -271,3 +271,10 @@ Clash 规则中的 `interval`。 1. 全局模板变量的用法和 Artifact 中定义的模板变量相同,相关文档请查阅 [这里](/guide/custom-artifact.md#customparams); 2. 在合并全局、局部模板变量和面板 URL 参数时的优先级为:URL 参数 > 局部 > 全局; ::: + +### checkHostname + +- 类型: `boolean` +- 默认值: `false` + +是否丢弃无法解析出域名 IP 地址的节点。无法解析出域名的节点有可能会导致 Clash 的 `url-test` 模式抛出异常而中止,丢弃这些节点可以避免这个问题。如果不是用公共 DNS 解析节点域名,或者有其它机制,可以关闭此项检测。 \ No newline at end of file diff --git a/lib/generator/artifact.ts b/lib/generator/artifact.ts index baa3805d2..340f65487 100644 --- a/lib/generator/artifact.ts +++ b/lib/generator/artifact.ts @@ -374,6 +374,19 @@ export class Artifact extends EventEmitter { nodeConfig.mptcp = provider.mptcp; } + // check whether the hostname resolves in case of blocking clash's node heurestic + if ( + config?.checkHostname && + !isIp(nodeConfig.hostname) + ) { + try { + await resolveDomain(nodeConfig.hostname); + } catch (err) /* istanbul ignore next */ { + logger.warn(`${nodeConfig.hostname} 无法解析,将忽略该节点`); + return undefined; + } + } + if ( config?.surgeConfig?.resolveHostname && !isIp(nodeConfig.hostname) && diff --git a/lib/types.ts b/lib/types.ts index 12aba4f3f..377b281d6 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -32,6 +32,7 @@ export interface CommandConfig { readonly templateDir: string; readonly configDir: string; readonly analytics?: boolean; + readonly checkHostname?: boolean; readonly upload?: { readonly prefix: string; readonly region: string; diff --git a/lib/utils/config.ts b/lib/utils/config.ts index 0a2484db2..9055eebc1 100644 --- a/lib/utils/config.ts +++ b/lib/utils/config.ts @@ -52,6 +52,7 @@ export const normalizeConfig = (cwd: string, userConfig: Partial) }, proxyTestUrl: PROXY_TEST_URL, proxyTestInterval: PROXY_TEST_INTERVAL, + checkHostname: false, }; const config: CommandConfig = _.defaultsDeep(userConfig, defaultConfig);