diff --git a/conf/conf.go b/conf/conf.go index 5cf08b9..97588ce 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -1,15 +1,9 @@ package conf import ( - "context" "flag" "os" - "os/exec" - "runtime" "strconv" - "time" - - "github.com/pkg/errors" "github.com/golang/glog" toml "github.com/pelletier/go-toml" @@ -41,6 +35,9 @@ var Conf = struct { func init() { initArgs() + if Conf.VersionOnly { + return + } if _, err := os.Stat(Conf.ConfigFile); os.IsNotExist(err) { glog.Warningln("no config file has been load:", Conf.ConfigFile) @@ -77,19 +74,7 @@ var refreshFns = []func() (string, error){ func() (string, error) { action := "clear dns cache" if Conf.ClearDNSCache != "" { - ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) - defer cancel() - - switch runtime.GOOS { - case "windows": - if out, err := exec.CommandContext(ctx, "cmd", "/c", Conf.ClearDNSCache).CombinedOutput(); err != nil { - return action, errors.Wrapf(err, "cmd: %s, output: %s, error", Conf.ClearDNSCache, out) - } - default: - if out, err := exec.CommandContext(ctx, "sh", "-c", Conf.ClearDNSCache).CombinedOutput(); err != nil { - return action, errors.Wrapf(err, "cmd: %s, output: %s, error", Conf.ClearDNSCache, out) - } - } + return action, execute(Conf.ClearDNSCache) } return action, nil }, diff --git a/conf/conf_other.go b/conf/conf_other.go index 43eb08d..001cfd6 100644 --- a/conf/conf_other.go +++ b/conf/conf_other.go @@ -3,11 +3,15 @@ package conf import ( + "context" "flag" "os" + "os/exec" "path/filepath" "strings" + "time" + "github.com/pkg/errors" "github.com/wweir/sower/dns" "github.com/wweir/sower/proxy/shadow" "github.com/wweir/sower/proxy/transport" @@ -31,3 +35,11 @@ func initArgs() { flag.Parse() } } + +func execute(cmd string) error { + ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) + defer cancel() + + out, err := exec.CommandContext(ctx, "sh", "-c", Conf.ClearDNSCache).CombinedOutput() + return errors.Wrapf(err, "cmd: %s, output: %s, error", Conf.ClearDNSCache, out) +} diff --git a/conf/conf_windows.go b/conf/conf_windows.go index f9c08c2..d7cba50 100644 --- a/conf/conf_windows.go +++ b/conf/conf_windows.go @@ -3,14 +3,18 @@ package conf import ( + "context" "flag" "fmt" "os" + "os/exec" "path/filepath" "strings" + "syscall" "time" "github.com/golang/glog" + "github.com/pkg/errors" "golang.org/x/sys/windows" "golang.org/x/sys/windows/svc" "golang.org/x/sys/windows/svc/eventlog" @@ -146,3 +150,28 @@ func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes c } } } + +func execute(cmd string) error { + ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) + defer cancel() + + var cmds []string + for _, cmd := range strings.Split(Conf.ClearDNSCache, " ") { + if cmd == "" { + continue + } + if strings.HasPrefix(cmd, "/") { + cmd = strings.Replace(cmd, "/", "-", 1) + } + cmds = append(cmds, cmd) + } + + if len(cmds) != 0 { + return nil + } + + command := exec.CommandContext(ctx, cmds[0], cmds[1:]...) + command.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + out, err := command.CombinedOutput() + return errors.Wrapf(err, "cmd: %s, output: %s, error", Conf.ClearDNSCache, out) +} diff --git a/conf/sower.toml b/conf/sower.toml index 9361884..39f9ab7 100644 --- a/conf/sower.toml +++ b/conf/sower.toml @@ -4,9 +4,9 @@ password="12345678" server_port="5533" # server_addr="remote-server:5533" # replce it to remote server http_proxy=":8080" # eg: 192.168.0.2:8080 -dns_server="114.114.114.114" # Keep empty for dynamic setting from net env +dns_server="" # Keep empty for dynamic setting from net env client_ip="127.0.0.1" # listen the IP, dns target is the IP -# clear_dns_cache="pkill mDNSResponder" # Windows: "ipconfig /flushdnss" +# clear_dns_cache="pkill mDNSResponder || true" # Windows: "ipconfig /flushdnss" suggest_level="SPEEDUP" # DISABLE, BLOCK, SPEEDUP blocklist=[ "**.google.*", # google diff --git a/main.go b/main.go index 156ad80..d1120d7 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "net" @@ -16,7 +17,8 @@ var version, date string func main() { cfg := &conf.Conf if cfg.VersionOnly { - fmt.Printf("Version: %s %s config: %v", version, date, cfg) + config, _ := json.MarshalIndent(cfg, "", "\t") + fmt.Printf("Version:\n\t%s %s\nConfig:\n%s", version, date, config) return } glog.Infof("Starting sower(%s %s): %v", version, date, cfg)