Skip to content

Commit

Permalink
1. add comments, 2. optimize text message
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Aug 10, 2021
1 parent 65234b1 commit 38c7391
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
15 changes: 7 additions & 8 deletions cmd/sower/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (

conf = struct {
Remote struct {
Type string `default:"sower" required:"true" usage:"optional: sower/trojan/socks5"`
Type string `default:"sower" required:"true" usage:"option: sower/trojan/socks5"`
Addr string `required:"true" usage:"proxy address, eg: proxy.com/127.0.0.1:7890"`
Password string `usage:"remote proxy password"`
}
Expand All @@ -44,24 +44,24 @@ var (
Router struct {
Block struct {
File string `usage:"block list file, local file or remote"`
FilePrefix string `default:"**." usage:"parsed as 'prefix.line_text'"`
FilePrefix string `default:"**." usage:"parsed as '<prefix>line_text'"`
Rules []string `usage:"block list rules"`
}
Direct struct {
File string `usage:"direct list file, local file or remote"`
FilePrefix string `default:"**." usage:"parsed as 'prefix.line_text'"`
FilePrefix string `default:"**." usage:"parsed as '<prefix>line_text'"`
Rules []string `usage:"direct list rules"`
}
Proxy struct {
File string `usage:"proxy list file, local file or remote"`
FilePrefix string `default:"**." usage:"parsed as 'prefix.line_text'"`
FilePrefix string `default:"**." usage:"parsed as '<prefix>line_text'"`
Rules []string `usage:"proxy list rules"`
}

Country struct {
MMDB string `usage:"mmdb file"`
File string `usage:"CIDR block list file, local file or remote"`
FilePrefix string `default:"" usage:"parsed as 'prefix.line_text'"`
FilePrefix string `default:"" usage:"parsed as '<prefix>line_text'"`
Rules []string `usage:"CIDR list rules"`
}
}
Expand Down Expand Up @@ -117,11 +117,10 @@ func main() {
}
go ServeHTTPS(lnHTTPS, r)

addr := net.JoinHostPort(conf.DNS.Serve, "53")
log.Info().
Str("listen_on", addr).
Str("listen_on", conf.DNS.Serve).
Msg("DNS proxy started")
if err := dns.ListenAndServe(addr, "udp", r); err != nil {
if err := dns.ListenAndServe(net.JoinHostPort(conf.DNS.Serve, "53"), "udp", r); err != nil {
log.Fatal().Err(err).Msg("serve dns")
}
}()
Expand Down
3 changes: 3 additions & 0 deletions cmd/sowerd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func serve443(ln net.Listener, fakeSite string, sower *sower.Sower, trojan *troj
Msgf("relay conn to %s", addr)
}()

// 1. detect if it's a sower underlaying connection
teeconn.Reread()
if addr, err = sower.Unwrap(teeconn); err == nil {
teeconn.Stop()
Expand All @@ -127,6 +128,7 @@ func serve443(ln net.Listener, fakeSite string, sower *sower.Sower, trojan *troj
return
}

// 2. detect if it's a trojan underlaying connection
teeconn.Reread()
if addr, err = trojan.Unwrap(teeconn); err == nil {
teeconn.Stop()
Expand All @@ -135,6 +137,7 @@ func serve443(ln net.Listener, fakeSite string, sower *sower.Sower, trojan *troj
return
}

// 3. fallback to fake site
teeconn.Stop().Reread()
dur, err = util.RelayTo(teeconn, fakeSite)
}
2 changes: 2 additions & 0 deletions router/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (r *Router) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
}

domain := req.Question[0].Name
// 1. rule_based( block > direct > proxy )
switch {
case r.blockRule.Match(domain):
_ = w.WriteMsg(r.dnsFail(req, dns.RcodeNameError))
Expand All @@ -28,6 +29,7 @@ func (r *Router) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
return
}

// 2. direct with cache, do not fallback to proxy to avoid side-effect
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))
Expand Down
1 change: 1 addition & 0 deletions router/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ping struct {
}

func (p *ping) Fulfill(key string) error {
// just like `curl -I http://domain.com:80`
_, err := pingClient.Head(net.JoinHostPort(key, "80"))
deferlog.Std.DebugWarn(err).
Str("domain", key).
Expand Down
7 changes: 4 additions & 3 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func (r *Router) RouteHandle(conn net.Conn, domain string, port uint16) (err err

addr := net.JoinHostPort(domain, strconv.FormatUint(uint64(port), 10))

// 1. rule_based( block > direct > proxy )
// 2. detect_based( CN IP || access site )
// 3. fallback( proxy )
switch {
case r.blockRule.Match(domain):
return nil
Expand All @@ -117,9 +120,7 @@ func (r *Router) RouteHandle(conn net.Conn, domain string, port uint16) (err err
case r.proxyRule.Match(domain):
return r.ProxyHandle(conn, domain, port)

case r.localSite(domain):
return r.DirectHandle(conn, addr)
case r.isAccess(domain, port):
case r.localSite(domain), r.isAccess(domain, port):
return r.DirectHandle(conn, addr)
default:
return r.ProxyHandle(conn, domain, port)
Expand Down

0 comments on commit 38c7391

Please sign in to comment.