Skip to content

Commit

Permalink
Add suggestion for manual rule
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Dec 8, 2018
1 parent cb88395 commit b83f202
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
15 changes: 10 additions & 5 deletions conf/sower.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ server_addr="remote-server:5533" # replce it to remote server
dns_server="114.114.114.114"
# client_ip="192.168.0.1"
blocklist=[
"*.*.google.com", # google
"*.google.com", # google
"ocsp.pki.goog",
"g.co",
"*.*.goo.gl",
"*.goo.gl",
"*.googleusercontent.com",
"*.googleapis.com",
"gmail.com",
Expand All @@ -24,19 +25,21 @@ blocklist=[
"www.linkedin.com",
"www.reddit.com", # reddit
"www.redditmedia.com",
"*.*.blogspot.com", # blogspot
"*.blogspot.com", # blogspot
"www.blogger.com",
"www.feedburner.com",
"img1.blogblog.com",
"*.*.aws.amazon.com", # amazon
"*.aws.amazon.com", # amazon
"m.media-amazon.com",
"*.awsstatic.com",
"*.*.*.cloudfront.net", # atlassian
"*.cloudfront.net", # atlassian
"*.medium.com",
"accounts-static.cdn.mozilla.net", # firefox
"*.services.mozilla.com",
"pocket-image-cache.com", # pocket
"*.golang.org", # golang
"go.googlesource.com",
"godoc.org",
"gist.github.com", # github
"*.k8s.io", # k8s
"k8s.gcr.io",
Expand All @@ -61,5 +64,7 @@ blocklist=[
"*.nyt.com",
"bandwagonhost.com", # bandwagonhost
"www.bwh1.net",
"*.akadns.net", # suggestions
"*.haxx.se",
]
verbose=1
22 changes: 18 additions & 4 deletions dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func StartDNS(dnsServer string) {

func bestTry(w dns.ResponseWriter, r *dns.Msg, domain, dnsServer string) {
msg, _ := dns.Exchange(r, dnsServer+":53")
if msg == nil {
return
}
if len(msg.Answer) == 0 { // expose any response
w.WriteMsg(msg)
return
Expand Down Expand Up @@ -65,18 +68,29 @@ func manual(w dns.ResponseWriter, r *dns.Msg, domain, dnsServer string) {
w.WriteMsg(localA(r, domain))
return
}
glog.V(2).Infof("match %s fail", domain)

// expose any response
msg, _ := dns.Exchange(r, dnsServer+":53")
msg, _ := dns.Exchange(r, dnsServer+":53") // expose any response
if msg == nil {
glog.V(1).Infof("get dns of %s fail", domain)
return
}
w.WriteMsg(msg)

if conf.Conf.Verbose != 0 && len(msg.Answer) != 0 {
go func() {
_, err := net.DialTimeout("tcp", domain+":http", 3*time.Second)
if err != nil && strings.Contains(err.Error(), "timeout") {
glog.V(1).Infof("SUGGEST check (%s) http(s) service: %s", domain, err)
}
}()
}
}

func localA(r *dns.Msg, domain string) *dns.Msg {
m := new(dns.Msg)
m.SetReply(r)
m.Answer = []dns.RR{&dns.A{
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 720},
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 20},
A: conf.Conf.ClientIPNet,
}}
return m
Expand Down
7 changes: 2 additions & 5 deletions dns/suffixTree.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ func (n *Node) matchSecs(secs []string) bool {
return n.matchSecs(secs[:length-1])
}

if n, ok := n.Node["*"]; ok {
return n.matchSecs(secs[:length-1])
}

return false
_, ok := n.Node["*"]
return ok
}
12 changes: 7 additions & 5 deletions proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func StartClient(server string) {
continue
}
}
glog.Infoln("new session to", sess.RemoteAddr())
glog.Infof("new session from (%s) to (%s)", sess.LocalAddr(), sess.RemoteAddr())

for { // session rotate logic
select {
Expand All @@ -41,13 +41,13 @@ func StartClient(server string) {
}

func openStream(conn net.Conn, sess quic.Session, reDialCh chan<- net.Conn) bool {
glog.V(1).Infoln("new request from", conn.RemoteAddr())
glog.V(2).Infoln("new request from", conn.RemoteAddr())

okCh := make(chan struct{})
go func() {
stream, err := sess.OpenStream()
if err != nil {
glog.Warningf("connect to remote(%s) fail:%s\n", sess.RemoteAddr(), err)
glog.Warningf("start stream to (%s) fail:%s\n", sess.RemoteAddr(), err)
reDialCh <- conn
close(okCh)
return
Expand All @@ -62,15 +62,17 @@ func openStream(conn net.Conn, sess quic.Session, reDialCh chan<- net.Conn) bool
}
close(okCh)

conn.(*net.TCPConn).SetKeepAlive(true)
if err := conn.(*net.TCPConn).SetKeepAlive(true); err != nil {
glog.Warningln(err)
}
relay(&streamConn{stream, sess}, conn)
conn.Close()
}()

select {
case _, ok := <-okCh: // false means close on error
return ok
case <-time.After(time.Second):
case <-time.After(500 * time.Millisecond):
return false
}
}
Expand Down
5 changes: 3 additions & 2 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func acceptStream(stream quic.Stream, sess quic.Session) {
return
}
defer rc.Close()
rc.(*net.TCPConn).SetKeepAlive(true)

if err := rc.(*net.TCPConn).SetKeepAlive(true); err != nil {
glog.Warningln(err)
}
relay(rc, conn)
}

0 comments on commit b83f202

Please sign in to comment.