From 0e03605c37c84d89b825c095129da2e3076e6f65 Mon Sep 17 00:00:00 2001 From: wweir Date: Sun, 9 Dec 2018 10:34:53 +0800 Subject: [PATCH] Support clear dns cache hook --- conf/conf.go | 40 ++++++++++++++++++++++++++++++---------- conf/sower.toml | 1 + proxy/server.go | 2 +- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/conf/conf.go b/conf/conf.go index f47d045..cedccd0 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -1,9 +1,13 @@ package conf import ( + "context" "flag" "net" + "os/exec" + "path/filepath" "strconv" + "time" "github.com/BurntSushi/toml" "github.com/golang/glog" @@ -11,14 +15,16 @@ import ( ) var Conf = struct { - ConfigFile string - ServerPort string `toml:"server_port"` - ServerAddr string `toml:"server_addr"` - DnsServer string `toml:"dns_server"` - ClientIP string `toml:"client_ip"` - ClientIPNet net.IP `toml:"-"` - BlockList []string `toml:"blocklist"` - Verbose int `toml:"verbose"` + ConfigFile string + ServerPort string `toml:"server_port"` + ServerAddr string `toml:"server_addr"` + DnsServer string `toml:"dns_server"` + ClientIP string `toml:"client_ip"` + ClientIPNet net.IP `toml:"-"` + ClearDnsCache string `toml:"clear_dns_cache"` + BlockList []string `toml:"blocklist"` + Suggestions []string `toml:"suggestions"` + Verbose int `toml:"verbose"` }{} func init() { @@ -46,6 +52,15 @@ var OnRefreash = []func() error{func() error { return err } Conf.ClientIPNet = net.ParseIP(Conf.ClientIP) + + // clear dns cache + if Conf.ClearDnsCache != "" { + ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second) + if err := exec.CommandContext(ctx, "sh", "-c", Conf.ClearDnsCache).Run(); err != nil { + glog.Errorln(err) + } + } + // for glog if err := flag.Set("v", strconv.Itoa(Conf.Verbose)); err != nil { return err @@ -53,13 +68,12 @@ var OnRefreash = []func() error{func() error { return nil }} -// watchConfigFile changes, fsnotify take too much cpu time, DIY func watchConfigFile() { watcher, err := fsnotify.NewWatcher() if err != nil { glog.Fatalln(err) } - if err := watcher.Add(Conf.ConfigFile); err != nil { + if err := watcher.Add(filepath.Dir(Conf.ConfigFile)); err != nil { glog.Fatalln(err) } @@ -67,6 +81,12 @@ func watchConfigFile() { for { select { case event := <-watcher.Events: + if event.Op == fsnotify.Rename { + if err := watcher.Add(Conf.ConfigFile); err != nil { + glog.Errorln(err) + } + } + glog.Infof("watch %s event: %v", Conf.ConfigFile, event) for i := range OnRefreash { if err := OnRefreash[i](); err != nil { diff --git a/conf/sower.toml b/conf/sower.toml index 419d3ba..5894acf 100644 --- a/conf/sower.toml +++ b/conf/sower.toml @@ -2,6 +2,7 @@ server_port="5533" server_addr="remote-server:5533" # replce it to remote server dns_server="114.114.114.114" # client_ip="192.168.0.1" +# clear_dns_cache="sudo killall -HUP mDNSResponder" blocklist=[ "*.google.com", # google "ocsp.pki.goog", diff --git a/proxy/server.go b/proxy/server.go index 3da855b..8989b4c 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -9,7 +9,7 @@ import ( ) func StartServer(port string) { - ln, err := quic.ListenAddr(":"+port, mockTlsPem(), nil) + ln, err := quic.ListenAddr(":"+port, mockTlsPem(), dialConf) if err != nil { glog.Fatalln(err) }