2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 10:17:17 +08:00

feat(dns): 优化系统工具箱DNS设置以适配现代Linux系统 (#1202)

* Initial plan

* feat(dns): 优化系统工具箱DNS设置以适配现代Linux系统

- 创建 pkg/dns 包实现多种DNS管理方式
- 支持 NetworkManager (RHEL 9.x/10.x)
- 支持 netplan (Debian 12+/Ubuntu 22+)
- 回退到直接修改 /etc/resolv.conf
- 更新前端显示当前DNS管理方式
- 添加单元测试

Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com>

* fix: 修复代码审查发现的问题

- 提取 shell 参数转义逻辑到独立函数
- 修正 netplan 配置文件选择的注释说明
- 使用常量替代硬编码的缩进值
- 添加前端空数组安全检查

Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com>

* fix: 优化

* fix: 优化

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com>
Co-authored-by: 耗子 <haozi@loli.email>
This commit is contained in:
Copilot
2026-01-09 06:33:23 +08:00
committed by GitHub
parent f2d3911266
commit 47537e282b
8 changed files with 845 additions and 16 deletions

View File

@@ -13,6 +13,7 @@ import (
"github.com/acepanel/panel/internal/app"
"github.com/acepanel/panel/internal/http/request"
"github.com/acepanel/panel/pkg/dns"
"github.com/acepanel/panel/pkg/io"
"github.com/acepanel/panel/pkg/ntp"
"github.com/acepanel/panel/pkg/shell"
@@ -32,19 +33,16 @@ func NewToolboxSystemService(t *gotext.Locale) *ToolboxSystemService {
// GetDNS 获取 DNS 信息
func (s *ToolboxSystemService) GetDNS(w http.ResponseWriter, r *http.Request) {
raw, err := io.Read("/etc/resolv.conf")
dnsServers, manager, err := dns.GetDNS()
if err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}
match := regexp.MustCompile(`nameserver\s+(\S+)`).FindAllStringSubmatch(raw, -1)
dns := make([]string, 0)
for _, m := range match {
dns = append(dns, m[1])
}
Success(w, dns)
Success(w, chix.M{
"dns": dnsServers,
"manager": manager.String(),
})
}
// UpdateDNS 设置 DNS 信息
@@ -55,11 +53,7 @@ func (s *ToolboxSystemService) UpdateDNS(w http.ResponseWriter, r *http.Request)
return
}
var dns string
dns += "nameserver " + req.DNS1 + "\n"
dns += "nameserver " + req.DNS2 + "\n"
if err := io.Write("/etc/resolv.conf", dns, 0644); err != nil {
if err := dns.SetDNS(req.DNS1, req.DNS2); err != nil {
Error(w, http.StatusInternalServerError, s.t.Get("failed to update DNS: %v", err))
return
}