Skip to content

Commit

Permalink
Support clear dns cache hook
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Dec 9, 2018
1 parent 9ca5898 commit 0e03605
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
40 changes: 30 additions & 10 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package conf

import (
"context"
"flag"
"net"
"os/exec"
"path/filepath"
"strconv"
"time"

"github.com/BurntSushi/toml"
"github.com/golang/glog"
"github.com/wweir/fsnotify"
)

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() {
Expand Down Expand Up @@ -46,27 +52,41 @@ 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
}
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)
}

go func() {
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 {
Expand Down
1 change: 1 addition & 0 deletions conf/sower.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 0e03605

Please sign in to comment.