@@ -35,8 +35,8 @@ func (a *App) GetSystemProxy() FlagResult {
3535 return FlagResult {true , proxy }
3636}
3737
38- func (a * App ) SetSystemProxy (enable bool , server string , proxyType string , bypass string , darwinServices []string ) FlagResult {
39- log .Printf ("SetSystemProxy: %t %s %s %s %v" , enable , server , proxyType , bypass , darwinServices )
38+ func (a * App ) SetSystemProxy (enable bool , server string , proxyType string , bypass string , services []string ) FlagResult {
39+ log .Printf ("SetSystemProxy: %t %s %s %s %v" , enable , server , proxyType , bypass , services )
4040
4141 if proxyType == "" {
4242 proxyType = "mixed"
@@ -48,7 +48,7 @@ func (a *App) SetSystemProxy(enable bool, server string, proxyType string, bypas
4848 case "windows" :
4949 err = setWindowsSystemProxy (server , enable , proxyType , bypass )
5050 case "darwin" :
51- err = setDarwinSystemProxy (server , enable , proxyType , bypass , darwinServices )
51+ err = setDarwinSystemProxy (server , enable , proxyType , bypass , services )
5252 case "linux" :
5353 err = setLinuxSystemProxy (server , enable , proxyType , bypass )
5454 }
@@ -60,6 +60,25 @@ func (a *App) SetSystemProxy(enable bool, server string, proxyType string, bypas
6060 return FlagResult {true , "Success" }
6161}
6262
63+ func (a * App ) SetSystemDNS (servers string , services []string ) FlagResult {
64+ log .Printf ("SetSystemDNS: %s %v" , servers , services )
65+
66+ var err error
67+ switch Env .OS {
68+ case "darwin" :
69+ err = setDarwinSystemDNS (servers , services )
70+ case "linux" :
71+ err = setLinuxSystemDNS (servers , services )
72+ default :
73+ return FlagResult {true , "Success" }
74+ }
75+
76+ if err != nil {
77+ return FlagResult {false , err .Error ()}
78+ }
79+ return FlagResult {true , "Success" }
80+ }
81+
6382func (a * App ) GetSystemProxyBypass () FlagResult {
6483 log .Printf ("GetSystemProxyBypass" )
6584
@@ -347,6 +366,59 @@ func setLinuxSystemProxy(server string, enabled bool, proxyType string, bypass s
347366 }
348367}
349368
369+ func setDarwinSystemDNS (servers string , services []string ) error {
370+ dnsServers := splitCommaSeparated (servers )
371+ if len (dnsServers ) == 0 {
372+ dnsServers = []string {"Empty" }
373+ }
374+
375+ commands := make ([][]string , 0 , len (services ))
376+ for _ , service := range services {
377+ service = strings .TrimSpace (service )
378+ if service == "" {
379+ continue
380+ }
381+ commands = append (commands , append ([]string {"networksetup" , "-setdnsservers" , service }, dnsServers ... ))
382+ }
383+ return runSystemProxyCommands (commands ... )
384+ }
385+
386+ func setLinuxSystemDNS (servers string , services []string ) error {
387+ dnsServers := splitCommaSeparated (servers )
388+ ipv4Servers := make ([]string , 0 , len (dnsServers ))
389+ ipv6Servers := make ([]string , 0 , len (dnsServers ))
390+ for _ , server := range dnsServers {
391+ if strings .Contains (server , ":" ) {
392+ ipv6Servers = append (ipv6Servers , server )
393+ } else {
394+ ipv4Servers = append (ipv4Servers , server )
395+ }
396+ }
397+ ignoreAutoDNS := "yes"
398+ if len (dnsServers ) == 0 {
399+ ignoreAutoDNS = "no"
400+ }
401+
402+ for _ , service := range services {
403+ service = strings .TrimSpace (service )
404+ if service == "" {
405+ continue
406+ }
407+ _ , err := runSystemProxyCommand (
408+ "nmcli" , "connection" , "modify" , service ,
409+ "ipv4.ignore-auto-dns" , ignoreAutoDNS , "ipv4.dns" , strings .Join (ipv4Servers , "," ),
410+ "ipv6.ignore-auto-dns" , ignoreAutoDNS , "ipv6.dns" , strings .Join (ipv6Servers , "," ),
411+ )
412+ if err != nil {
413+ return err
414+ }
415+ if _ , err := runSystemProxyCommand ("nmcli" , "connection" , "up" , service ); err != nil {
416+ return err
417+ }
418+ }
419+ return nil
420+ }
421+
350422func getWindowsSystemProxyBypass () (string , error ) {
351423 out , err := runSystemProxyCommand ("reg" , "query" , `HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings` , "/v" , "ProxyOverride" )
352424 if err != nil {
@@ -483,6 +555,17 @@ func splitBypass(bypass string) []string {
483555 return result
484556}
485557
558+ func splitCommaSeparated (value string ) []string {
559+ result := []string {}
560+ for item := range strings .SplitSeq (value , "," ) {
561+ item = strings .TrimSpace (item )
562+ if item != "" {
563+ result = append (result , item )
564+ }
565+ }
566+ return result
567+ }
568+
486569func normalizeBypass (bypass string , separator string ) string {
487570 return strings .Join (splitBypass (bypass ), separator )
488571}
0 commit comments