From 5a3edf229f7cc3efc448eda6bd66e97d24286610 Mon Sep 17 00:00:00 2001 From: Xuzheng Chen <1092889706@qq.com> Date: Sun, 12 Nov 2023 02:47:51 +0800 Subject: [PATCH] chore: use file operation instead of cmd --- stack/tun/stack_darwin.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/stack/tun/stack_darwin.go b/stack/tun/stack_darwin.go index 2ad17f5..9483757 100644 --- a/stack/tun/stack_darwin.go +++ b/stack/tun/stack_darwin.go @@ -10,6 +10,7 @@ import ( "golang.org/x/sys/unix" "net" "net/netip" + "os" "os/exec" "sync" "syscall" @@ -53,11 +54,15 @@ func (s *Stack) AddRoute(target string) error { } func (s *Stack) AddDnsServer(dnsServer string, targetHost string) error { - command := exec.Command("echo", "nameserver", dnsServer, ">", fmt.Sprintf("/etc/resolver/%s", targetHost)) - err := command.Run() + fileName := fmt.Sprintf("/etc/resolver/%s", targetHost) + file, err := os.Create(fileName) if err != nil { return err } + defer file.Close() + + file.WriteString(fmt.Sprintf("nameserver %s\n", dnsServer)) + terminal_func.RegisterTerminalFunc("DelDnsServer_"+targetHost, func(ctx context.Context) error { delCommand := exec.Command("rm", fmt.Sprintf("/etc/resolver/%s", targetHost)) delErr := delCommand.Run()