mirror of
https://github.com/wweir/sower.git
synced 2026-10-19 16:14:33 +00:00
optimize code, comment / log / flag
This commit is contained in:
+3
-1
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func (r *Router) localSite(domain string) bool {
|
||||
|
||||
// parse domain to IP
|
||||
ip := net.ParseIP(domain)
|
||||
if ip == nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
@@ -27,12 +27,14 @@ func (r *Router) localSite(domain string) bool {
|
||||
ip = ips[0]
|
||||
}
|
||||
|
||||
// CIDR match
|
||||
for _, cidr := range r.country.cidrs {
|
||||
if cidr.Contains(ip) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// MMDB match CN
|
||||
if r.country.Reader != nil {
|
||||
city, err := r.country.City(ip)
|
||||
if err != nil {
|
||||
|
||||
+5
-5
@@ -11,32 +11,32 @@ import (
|
||||
func (r *Router) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
|
||||
// https://stackoverflow.com/questions/4082081/requesting-a-and-aaaa-records-in-single-dns-query/4083071#4083071
|
||||
if len(req.Question) == 0 {
|
||||
w.WriteMsg(r.dnsFail(req, dns.RcodeFormatError))
|
||||
_ = w.WriteMsg(r.dnsFail(req, dns.RcodeFormatError))
|
||||
return
|
||||
}
|
||||
|
||||
domain := req.Question[0].Name
|
||||
switch {
|
||||
case r.blockRule.Match(domain):
|
||||
w.WriteMsg(r.dnsFail(req, dns.RcodeNameError))
|
||||
_ = w.WriteMsg(r.dnsFail(req, dns.RcodeNameError))
|
||||
return
|
||||
|
||||
case r.directRule.Match(domain):
|
||||
|
||||
case r.proxyRule.Match(domain):
|
||||
w.WriteMsg(r.dnsProxyA(domain, r.dns.serveIP, req))
|
||||
_ = w.WriteMsg(r.dnsProxyA(domain, r.dns.serveIP, req))
|
||||
return
|
||||
}
|
||||
|
||||
c := &dnsCache{Router: r, Req: req}
|
||||
if err := r.dns.cache.Remember(c, req.Question[0].String()); err != nil {
|
||||
w.WriteMsg(r.dnsFail(req, dns.RcodeServerFailure))
|
||||
_ = w.WriteMsg(r.dnsFail(req, dns.RcodeServerFailure))
|
||||
return
|
||||
}
|
||||
|
||||
c.Resp.SetReply(req)
|
||||
c.Resp.Compress = true
|
||||
w.WriteMsg(c.Resp)
|
||||
_ = w.WriteMsg(c.Resp)
|
||||
}
|
||||
|
||||
func (r *Router) dnsFail(req *dns.Msg, rcode int) *dns.Msg {
|
||||
|
||||
+7
-2
@@ -4,6 +4,8 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/wweir/sower/pkg/deferlog"
|
||||
)
|
||||
|
||||
var pingClient = http.Client{
|
||||
@@ -19,7 +21,7 @@ func (r *Router) isAccess(domain string, port uint16) bool {
|
||||
}
|
||||
|
||||
p := &ping{}
|
||||
r.accessCache.Remember(p, domain)
|
||||
_ = r.accessCache.Remember(p, domain)
|
||||
return p.isAccess
|
||||
}
|
||||
|
||||
@@ -28,7 +30,10 @@ type ping struct {
|
||||
}
|
||||
|
||||
func (p *ping) Fulfill(key string) error {
|
||||
_, err := http.Head(net.JoinHostPort(key, "80"))
|
||||
_, err := pingClient.Head(net.JoinHostPort(key, "80"))
|
||||
deferlog.Std.DebugWarn(err).
|
||||
Str("domain", key).
|
||||
Msg("detect if site is accessible")
|
||||
p.isAccess = (err == nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
+1
-1
@@ -130,7 +130,7 @@ func (r *Router) ProxyHandle(conn net.Conn, domain string, port uint16) error {
|
||||
start := time.Now()
|
||||
rc, err := r.ProxyDial("tcp", domain, port)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "dial proxy to (%s:%d), spend (%s)", domain, port, time.Since(start))
|
||||
return errors.Wrapf(err, "proxy dial (%s:%d), spend (%s)", domain, port, time.Since(start))
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user