From 989d3c97873ddfd6b20510020af45b7275479965 Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Sun, 20 Aug 2017 21:44:18 +0800 Subject: [PATCH] rules: add rules-dir folder config support --- .gitignore | 3 +++ direct.go | 4 ++-- dns.go | 5 +++-- dnstun.go | 1 + http.go | 4 ++-- main.go | 19 ++++++++++++++++--- mixed.go | 6 +++--- redir_other.go | 1 + rule.go | 6 +++--- rules.go | 1 + socks5.go | 2 ++ ss.go | 3 ++- tcptun.go | 1 + utils.go | 26 ++++++++++++++++++++++++++ 14 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 utils.go diff --git a/.gitignore b/.gitignore index dafd4ed..3709d49 100644 --- a/.gitignore +++ b/.gitignore @@ -17,5 +17,8 @@ *.zip /*.conf /*.rule + +rules.d/*.rule + glider bak/ diff --git a/direct.go b/direct.go index a2deb56..84da69a 100644 --- a/direct.go +++ b/direct.go @@ -10,8 +10,8 @@ type direct struct { var Direct = &direct{} func (d *direct) Addr() string { return "127.0.0.1" } -func (d *direct) ListenAndServe() { logf("base proxy ListenAndServe") } -func (d *direct) Serve(c net.Conn) { logf("base proxy Serve") } +func (d *direct) ListenAndServe() { logf("direct proxy ListenAndServe") } +func (d *direct) Serve(c net.Conn) { logf("direct proxy Serve") } func (d *direct) CurrentProxy() Proxy { return d } func (d *direct) GetProxy(dstAddr string) Proxy { return d } func (d *direct) NextProxy() Proxy { return d } diff --git a/dns.go b/dns.go index a25a827..1d689f9 100644 --- a/dns.go +++ b/dns.go @@ -24,6 +24,7 @@ const TCPDNSHEADERLen = 2 + UDPDNSHeaderLen // so we should also serve tcp requests. const MaxUDPDNSLen = 512 +// DNS . type DNS struct { *proxy dnsServer string @@ -31,7 +32,7 @@ type DNS struct { dnsServerMap map[string]string } -// DNSForwarder returns a dns forwarder. client -> dns.udp -> glider -> forwarder -> remote dns addr +// NewDNS returns a dns forwarder. client -> dns.udp -> glider -> forwarder -> remote dns addr func NewDNS(addr, raddr string, upProxy Proxy) (*DNS, error) { s := &DNS{ proxy: NewProxy(addr, upProxy), @@ -70,7 +71,7 @@ func (s *DNS) ListenAndServe() { dnsServer := s.GetServer(domain) // TODO: check here - rc, err := s.GetProxy(domain+":53").GetProxy(domain+":53").Dial("tcp", dnsServer) + rc, err := s.GetProxy(domain+":53").Dial("tcp", dnsServer) if err != nil { logf("failed to connect to server %v: %v", dnsServer, err) return diff --git a/dnstun.go b/dnstun.go index 44d1d6a..0055204 100644 --- a/dnstun.go +++ b/dnstun.go @@ -2,6 +2,7 @@ package main +// DNSTun . type DNSTun struct { *proxy raddr string diff --git a/http.go b/http.go index f594626..811c14c 100644 --- a/http.go +++ b/http.go @@ -16,12 +16,12 @@ import ( "time" ) -// httpproxy +// HTTPProxy . type HTTPProxy struct { *proxy } -// HTTPProxy returns a http proxy. +// NewHTTPProxy returns a http proxy. func NewHTTPProxy(addr string, upProxy Proxy) (*HTTPProxy, error) { s := &HTTPProxy{ proxy: NewProxy(addr, upProxy), diff --git a/main.go b/main.go index 89dd018..ae9f47b 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ import ( ) // VERSION . -const VERSION = "0.3.2" +const VERSION = "0.4.0" var conf struct { Verbose bool @@ -22,6 +22,7 @@ var conf struct { Listen []string Forward []string RuleFile []string + RulesDir string DNS string DNSServer []string @@ -122,6 +123,7 @@ func main() { flag.StringSliceUniqVar(&conf.Listen, "listen", nil, "listen url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT") flag.StringSliceUniqVar(&conf.Forward, "forward", nil, "forward url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT[,SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT]") flag.StringSliceUniqVar(&conf.RuleFile, "rulefile", nil, "rule file path") + flag.StringVar(&conf.RulesDir, "rules-dir", "rules.d", "rule file folder") flag.StringVar(&conf.DNS, "dns", "", "dns listen address") flag.StringSliceUniqVar(&conf.DNSServer, "dnsserver", []string{"8.8.8.8:53"}, "remote dns server") @@ -155,13 +157,24 @@ func main() { forwarders = append(forwarders, forward) } - // combine forwarders to a singer strategy forwarder + // combine forwarders to a single strategy forwarder forwarder := NewStrategyForwarder(conf.Strategy, forwarders) // rule forwarders var ruleForwarders []*RuleForwarder for _, ruleFile := range conf.RuleFile { - ruleForwarder, err := NewRuleProxyFromFile(ruleFile) + ruleForwarder, err := NewRuleForwarderFromFile(ruleFile) + if err != nil { + log.Fatal(err) + } + + ruleForwarders = append(ruleForwarders, ruleForwarder) + } + + // rules folder + ruleFolderFiles, _ := listDir(conf.RulesDir, ".rule") + for _, ruleFile := range ruleFolderFiles { + ruleForwarder, err := NewRuleForwarderFromFile(ruleFile) if err != nil { log.Fatal(err) } diff --git a/mixed.go b/mixed.go index 882b8bf..5845aac 100644 --- a/mixed.go +++ b/mixed.go @@ -6,7 +6,7 @@ import ( ) // https://www.ietf.org/rfc/rfc2616.txt, http methods must be uppercase. -var httpMethods = [][]byte{ +var httpMethods = [...][]byte{ []byte("GET"), []byte("POST"), []byte("PUT"), @@ -25,7 +25,7 @@ type MixedProxy struct { ss Proxy } -// MixedProxy returns a mixed proxy. +// NewMixedProxy returns a mixed proxy. func NewMixedProxy(network, addr, user, pass string, upProxy Proxy) (*MixedProxy, error) { p := &MixedProxy{ proxy: NewProxy(addr, upProxy), @@ -41,7 +41,7 @@ func NewMixedProxy(network, addr, user, pass string, upProxy Proxy) (*MixedProxy return p, nil } -// mixedproxy . +// ListenAndServe . func (p *MixedProxy) ListenAndServe() { l, err := net.Listen("tcp", p.addr) if err != nil { diff --git a/redir_other.go b/redir_other.go index cbd92ab..44e72fc 100644 --- a/redir_other.go +++ b/redir_other.go @@ -4,6 +4,7 @@ package main import "log" +// RedirProxy . type RedirProxy struct{ *proxy } // NewRedirProxy returns a redirect proxy. diff --git a/rule.go b/rule.go index db936d2..9946ad9 100644 --- a/rule.go +++ b/rule.go @@ -9,7 +9,7 @@ import ( "github.com/nadoo/conflag" ) -// RuleForwarder, every ruleForwarder points to a rule file +// RuleForwarder , every ruleForwarder points to a rule file type RuleForwarder struct { Forward []string Strategy string @@ -27,8 +27,8 @@ type RuleForwarder struct { Proxy } -// NewRuleProxyFromFile . -func NewRuleProxyFromFile(ruleFile string) (*RuleForwarder, error) { +// NewRuleForwarderFromFile . +func NewRuleForwarderFromFile(ruleFile string) (*RuleForwarder, error) { p := &RuleForwarder{name: ruleFile} f := conflag.NewFromFile("rule", ruleFile) diff --git a/rules.go b/rules.go index fa5b794..a5b751c 100644 --- a/rules.go +++ b/rules.go @@ -5,6 +5,7 @@ import ( "strings" ) +// RulesForwarder . type RulesForwarder struct { globalForwarder Proxy diff --git a/socks5.go b/socks5.go index 7c82391..e266563 100644 --- a/socks5.go +++ b/socks5.go @@ -55,6 +55,7 @@ var socks5Errors = []string{ "address type not supported", } +// SOCKS5Proxy . type SOCKS5Proxy struct { *proxy network string @@ -95,6 +96,7 @@ func (s *SOCKS5Proxy) ListenAndServe() { } } +// Serve . func (s *SOCKS5Proxy) Serve(c net.Conn) { defer c.Close() diff --git a/ss.go b/ss.go index 66efb56..39c8347 100644 --- a/ss.go +++ b/ss.go @@ -9,7 +9,7 @@ import ( "github.com/shadowsocks/go-shadowsocks2/core" ) -// ss +// SSProxy . type SSProxy struct { *proxy core.StreamConnCipher @@ -50,6 +50,7 @@ func (s *SSProxy) ListenAndServe() { } } +// Serve . func (s *SSProxy) Serve(c net.Conn) { defer c.Close() diff --git a/tcptun.go b/tcptun.go index 20cc2df..49d9a92 100644 --- a/tcptun.go +++ b/tcptun.go @@ -2,6 +2,7 @@ package main import "net" +// TCPTun . type TCPTun struct { *proxy raddr string diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..0274232 --- /dev/null +++ b/utils.go @@ -0,0 +1,26 @@ +package main + +import ( + "io/ioutil" + "os" + "strings" +) + +func listDir(dirPth string, suffix string) (files []string, err error) { + files = make([]string, 0, 10) + dir, err := ioutil.ReadDir(dirPth) + if err != nil { + return nil, err + } + PthSep := string(os.PathSeparator) + suffix = strings.ToUpper(suffix) + for _, fi := range dir { + if fi.IsDir() { + continue + } + if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) { + files = append(files, dirPth+PthSep+fi.Name()) + } + } + return files, nil +}