Skip to content

Commit

Permalink
Fix windows refresh hook cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Mar 28, 2019
1 parent d31bbf9 commit d6dbfd4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
23 changes: 4 additions & 19 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
},
Expand Down
12 changes: 12 additions & 0 deletions conf/conf_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
29 changes: 29 additions & 0 deletions conf/conf_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions conf/sower.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"net"

Expand All @@ -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)
Expand Down

0 comments on commit d6dbfd4

Please sign in to comment.