Skip to content

Commit

Permalink
feat: make resolveHostname a global option
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed May 5, 2024
1 parent c0349a0 commit 3b6f3a7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 27 deletions.
9 changes: 3 additions & 6 deletions docs/guide/advance/surge-advance.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,25 @@ sidebarDepth: 2
- 如果你已经订阅了 Surge 4,推荐使用 [原生](/guide/custom-config.md#surgeconfig-v2ray) 的 Vmess 支持
:::

Surge 没有原生提供对 V2Ray 和 SSR 的支持 ~~(将来也不太可能)~~ ,但是提供了一个叫做 [External Proxy Provider](https://medium.com/@Blankwonder/surge-mac-new-features-external-proxy-provider-375e0e9ea660) 的功能,能够满足我们连接 V2Ray 和 SSR 服务器。
Surge 没有原生提供对 SSR 的支持 ~~(将来也不太可能)~~ ,但是提供了一个叫做 [External Proxy Provider](https://medium.com/@Blankwonder/surge-mac-new-features-external-proxy-provider-375e0e9ea660) 的功能,能够满足我们连接 SSR 服务器。

## 开始之前

在一切开始之前,你需要确保本地已经安装了 V2Ray 和 SSR 的可执行文件。

- [安装 V2Ray](https://github.com/v2ray/homebrew-v2ray)
- [安装 SSR](/guide/install-ssr-local.md)

## 修改 Surgio 配置

找到 `surgio.conf.js`,补充如下字段:

```js{3-9}
```js{3-6}
module.exports = {
// ...
binPath: {
shadowsocksr: '/usr/local/bin/ssr-local',
},
surgeConfig: {
resolveHostname: true,
},
resolveHostname: true,
}
```

Expand Down
13 changes: 12 additions & 1 deletion docs/guide/custom-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ module.exports = {
- 类型: `boolean`
- 默认值: `false`

在 Surge 官方对 External Provider 的 [解释](https://medium.com/@Blankwonder/surge-mac-new-features-external-proxy-provider-375e0e9ea660) 中提到,为了不让代理进程(如 ssr-local)的流量经过 Surge 的 TUN 模块,需要额外指定 `addresses` 参数。在之前版本的 Surgio 里,生成的配置不会对域名进行解析,导致实际使用中仍然会造成额外的性能损耗。
如果你已经开启了全局的 `resolveHostname`,可以不开启此项。

为了不让代理进程(如 ssr-local)的流量经过 Surge 的 TUN 模块,需要额外指定 `addresses` 参数。在之前版本的 Surgio 里,生成的配置不会对域名进行解析,导致实际使用中仍然会造成额外的性能损耗。

打开这个选项后,Surgio 会在生成配置的时候解析域名。不过,这必然会造成生成时间延长,所以请按照个人的需要进行选择。

Expand Down Expand Up @@ -422,6 +424,15 @@ module.exports = {

某些机场的节点域名 TTL 非常小,在某些情况下可能会导致 DNS 回溯解析超时,这样会导致节点本身可用但是被抛弃,所以建议谨慎开启该选项。

## resolveHostname

- 类型: `boolean`
- 默认值: `false`

是否解析节点的域名。开启此功能后 Surgio 会将节点的域名解析为 IP 地址,这样可能可以加速节点的连接速度。请注意,这个选项和 `surgeConfig.resolveHostname` 不同,前者是全局配置,后者供生成 Surge 使用的 SSR 配置。

打开这个选项后,Surgio 会在生成配置的时候解析域名,这必然会造成生成时间延长,所以请按照个人的需要进行选择。另外,开启此选项后将失去节点的负载均衡功能(如有)和 DNS 解析的灵活性,所以请**谨慎使用**

## flags

- 类型: `object`
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const normalizeConfig = (
internetTestUrl: INTERNET_TEST_URL,
internetTestInterval: INTERNET_TEST_INTERVAL,
checkHostname: false,
resolveHostname: false,
cache: {
type: 'default',
},
Expand Down
29 changes: 18 additions & 11 deletions src/generator/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,15 @@ export class Artifact extends EventEmitter {
) {
try {
const domains = await resolveDomain(nodeConfig.hostname)

/* istanbul ignore next */
if (domains.length < 1) {
logger.warn(
`DNS 解析结果中 ${nodeConfig.hostname} 未有对应 IP 地址,将忽略该节点`,
)
return undefined
} /* istanbul ignore next */ else {
nodeConfig.hostnameIp = domains
}
} catch (err) /* istanbul ignore next */ {
logger.warn(`${nodeConfig.hostname} 无法解析,将忽略该节点`)
Expand All @@ -483,19 +487,22 @@ export class Artifact extends EventEmitter {
}

if (
config?.surgeConfig?.resolveHostname &&
config?.resolveHostname &&
'hostname' in nodeConfig &&
!isIp(nodeConfig.hostname) &&
[NodeTypeEnum.Vmess, NodeTypeEnum.Shadowsocksr].includes(
nodeConfig.type,
)
!isIp(nodeConfig.hostname)
) {
try {
nodeConfig.hostnameIp = await resolveDomain(nodeConfig.hostname)
} catch (err) /* istanbul ignore next */ {
logger.warn(
`${nodeConfig.hostname} 无法解析,将忽略该域名的解析结果`,
)
/* istanbul ignore next */
if (nodeConfig.hostnameIp) {
nodeConfig.hostname = nodeConfig.hostnameIp[0]
} /* istanbul ignore next */ else {
try {
nodeConfig.hostnameIp = await resolveDomain(nodeConfig.hostname)
nodeConfig.hostname = nodeConfig.hostnameIp[0]
} catch (err) {
logger.warn(
`${nodeConfig.hostname} 无法解析,将忽略该域名的解析结果`,
)
}
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,6 @@ export type Hysteria2NodeConfig = z.infer<typeof Hysteria2NodeConfigValidator> &
SurgioInternals

export interface SurgioInternals {
binPath?: string
localPort?: number
surgeConfig?: CommandConfig['surgeConfig']
clashConfig?: CommandConfig['clashConfig']
quantumultXConfig?: CommandConfig['quantumultXConfig']
surfboardConfig?: CommandConfig['surfboardConfig']
hostnameIp?: ReadonlyArray<string>
provider?: Provider
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getNetworkResolveTimeout } from './env-flag'

const domainCache = caching('memory', {
ttl: ms('1d'),
max: 1000,
max: 5000,
})
const logger = createLogger({ service: 'surgio:utils:dns' })

Expand Down
6 changes: 5 additions & 1 deletion src/utils/surge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ function nodeListMapper(
)
}

if (nodeConfig.hostnameIp && nodeConfig.hostnameIp.length) {
if (
nodeConfig?.surgeConfig?.resolveHostname &&
nodeConfig.hostnameIp &&
nodeConfig.hostnameIp.length
) {
nodeConfigString.push(
...nodeConfig.hostnameIp.map((item) => `addresses = ${item}`),
)
Expand Down
3 changes: 3 additions & 0 deletions src/validators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export const SimpleNodeConfigValidator = z.object({
surfboardConfig: SurfboardConfigValidator.optional(),
quantumultXConfig: QuantumultXConfigValidator.optional(),
clashConfig: ClashConfigValidator.optional(),
hostnameIp: z.array(z.string()).readonly().optional(),
binPath: z.string().optional(),
localPort: z.number().optional(),
})

export const TlsNodeConfigValidator = SimpleNodeConfigValidator.extend({
Expand Down
1 change: 1 addition & 0 deletions src/validators/surgio-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const SurgioConfigValidator = z.object({
})
.optional(),
checkHostname: z.oboolean(),
resolveHostname: z.oboolean(),
proxyTestUrl: z.string().url().optional(),
proxyTestInterval: z.onumber(),
internetTestUrl: z.string().url().optional(),
Expand Down

0 comments on commit 3b6f3a7

Please sign in to comment.