Skip to content

feat: resolve bare domains into hosts entries via DoH - #1040

Merged
oldj merged 18 commits into
oldj:developfrom
ziji-wu:feat/domain-resolve-hosts
Sep 3, 2026
Merged

oldj merged 18 commits into
oldj:developfrom
ziji-wu:feat/domain-resolve-hosts

Conversation

@ziji-wu

@ziji-wu ziji-wu commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

功能

远程 hosts 条目新增第二种来源:域名解析。除了订阅 URL,现在可以直接输入裸域名(如 github.com),SwitchHosts 会通过可配置的 DoH(DNS over HTTPS)服务解析出 IP,生成 hosts 行(<ip> <domain>),并复用现有的自动刷新机制定时更新——IP 变化时内容自动跟随。直接粘贴完整 URL(如 https://github.com/)也可以,保存时会自动提取出裸域名。

image image image

动机

目前想把某个域名固定到真实 IP 的用户,只能借助外部查询网站或社区维护的定时解析仓库(如 #612、#803 提到的用 GitHub Actions 定时生成 hosts 再订阅的方案)。内置域名解析来源可以省去这个中转,而且更新时效远快于定时任务。

改动

  • 远程条目新增可选字段 source: 'url' | 'domain'(缺省视为 'url';条目以无 schema 的 JSON 存储,旧数据零迁移、行为完全不变)
  • 新增 src-tauri/src/dns.rs:DoH 服务注册表(阿里 / DNSPod / Cloudflare / Google / 自定义模板)、域名校验、Google 风格 JSON 响应解析、hosts 内容生成
  • refresh.rs:source=domain 的节点走 DoH 解析而非 HTTP 拉取;下游(写入、last_refresh、事件广播)与 URL 路径完全共享;后台定时扫描兼容裸域名条目
  • 无新增 Tauri 命令、无新增 Rust 依赖
  • 设置页新增"域名解析 DNS"配置项(12 个 i18n key,9 种语言全部补齐)
  • 保存时自动把粘贴的 URL 归一化为裸域名

兼容性

  • 无 source 字段的旧数据行为与之前完全一致
  • 订阅 URL 类型的拉取行为无任何变化
  • 解析失败时保留条目上一次的成功内容,不静默回退系统 DNS

测试

  • cargo test:服务注册表、域名校验、DoH JSON 解析、内容黄金输出、本地 axum stub 端到端、定时扫描包含域名条目
  • vitest:isValidDomain/extractDomain 与 Rust 侧规则一致性、表单行为(hint 显示、非法域名拦截、URL 归一化、source 字段往返)
  • 已在 Windows 上手动冒烟:添加 → 解析生成 → 手动/自动刷新 → 切换 DNS 服务 → 断网失败保留旧内容

@ziji-wu
ziji-wu marked this pull request as ready for review September 2, 2026 12:43
oldj added 5 commits September 3, 2026 10:29
refresh_one_inner decides "changed" by comparing the whole entry file,
so a per-refresh timestamp line made every refresh look like an update:
the entry was rewritten, hosts_content_changed fired, and the renderer
rewrote the system hosts even when the IP had not moved. The refresh
time is already recorded on the node as last_refresh (in local time,
which the removed line also disagreed with by using UTC).
…o panel

A bare domain has no scheme, so open_url rejected the BrowserLink
silently; the row also still said "URL".
… domain

Only the add path kicked off an immediate refresh, so editing an item
into a domain source (or changing its domain) kept the old content on
disk until the next scheduled tick.
It only matters for domain-sourced remote entries, which is a niche
use; keep the General tab to the everyday options.
@oldj
oldj changed the base branch from master to develop September 3, 2026 03:18
@oldj
oldj merged commit 54c1cf8 into oldj:develop Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants