Skip to content

Commit

Permalink
fix(dns): fail to init DHCP DNS servers
Browse files Browse the repository at this point in the history
  • Loading branch information
igoogolx committed Jan 8, 2025
1 parent adcde5c commit ae50588
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
19 changes: 19 additions & 0 deletions internal/cfg/distribution/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/igoogolx/itun2socks/internal/constants"
"github.com/igoogolx/itun2socks/internal/dns"
"github.com/igoogolx/itun2socks/internal/matcher"
"github.com/igoogolx/itun2socks/pkg/log"
)

type Config struct {
Expand Down Expand Up @@ -45,15 +46,33 @@ func NewTun(
localDns []string,
defaultInterfaceName string,
disableCache bool,
networkService string,
) (Config, error) {
if len(boostDns) == 0 || len(remoteDns) == 0 || len(localDns) == 0 {
return Config{}, fmt.Errorf("dns can't be empty")
}
originalDnsServers, err := dns.Hijack(networkService, "empty")
if err != nil {
return Config{}, err
}
if len(originalDnsServers) != 0 {
log.Infoln(log.FormatLog(log.ExecutorPrefix, "current dns servers %v: %v"), networkService, originalDnsServers)
defer func(networkService string) {
err := dns.Resume(networkService)
if err != nil {
log.Warnln(log.FormatLog(log.ExecutorPrefix, "fail to resume dns: %v"), err)
} else {
log.Infoln(log.FormatLog(log.ExecutorPrefix, "resume dns to: %v"), originalDnsServers)
}
}(networkService)
}

dns.ResetCache()
dnsConfig, err := NewDnsDistribution(boostDns, remoteDns, localDns, defaultInterfaceName, disableCache)
if err != nil {
return Config{}, err
}

return Config{
dnsConfig,
}, nil
Expand Down
1 change: 1 addition & 0 deletions internal/cfg/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewTun(defaultInterfaceName string) (*Config, error) {
rawConfig.Setting.Dns.Server.Local,
defaultInterfaceName,
disableDnsCache,
rawConfig.Setting.NetworkService,
)

if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions internal/dns/hijack_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package dns

import (
"fmt"
"github.com/igoogolx/itun2socks/internal/constants"
"net"
"os/exec"
"strings"
Expand All @@ -29,26 +28,26 @@ func getOriginalDnsServers(networkService string) ([]string, error) {
return dnsServers, nil
}

func Hijack(networkService string) error {
func Hijack(networkService string, dnsServer string) ([]string, error) {
var err error
originalDnsServers, err = getOriginalDnsServers(networkService)
if err != nil {
return err
return nil, err
}
cmd := exec.Command("networksetup", "-setdnsservers", networkService, constants.HijackedDns)
cmd := exec.Command("networksetup", "-setdnsservers", networkService, dnsServer)
_, err = cmd.Output()
if err != nil {
return fmt.Errorf("failed to hajack DNS servers: %v", err)
return nil, fmt.Errorf("failed to hajack DNS servers: %v", err)
}
return nil
return originalDnsServers, nil
}

func Resume(networkService string) error {
var err error
defer func() {
originalDnsServers = []string{}
}()
dnsServer := "Empty"
dnsServer := "empty"
if len(originalDnsServers) != 0 {
dnsServer = strings.Join(originalDnsServers, " ")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/dns/hijack_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

package dns

func Hijack(_ string) error {
return nil
func Hijack(_ string, server string) ([]string, error) {
return []string{}, nil
}

func Resume(_ string) error {
Expand Down
3 changes: 2 additions & 1 deletion internal/executor/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/Dreamacro/clash/component/iface"
"github.com/igoogolx/itun2socks/internal/cfg"
"github.com/igoogolx/itun2socks/internal/constants"
"github.com/igoogolx/itun2socks/internal/dns"
localserver "github.com/igoogolx/itun2socks/internal/local_server"
"github.com/igoogolx/itun2socks/internal/tunnel/statistic"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (c *TunClient) Start() error {
}
if c.config.HijackDns.Enabled {

err := dns.Hijack(c.config.HijackDns.NetworkService)
_, err := dns.Hijack(c.config.HijackDns.NetworkService, constants.HijackedDns)
if err != nil {
return err
}
Expand Down

0 comments on commit ae50588

Please sign in to comment.