diff --git a/dns.go b/dns.go index f2ab29a..a25a827 100644 --- a/dns.go +++ b/dns.go @@ -32,9 +32,9 @@ type DNS struct { } // DNSForwarder returns a dns forwarder. client -> dns.udp -> glider -> forwarder -> remote dns addr -func DNSForwarder(addr, raddr string, upProxy Proxy) (*DNS, error) { +func NewDNS(addr, raddr string, upProxy Proxy) (*DNS, error) { s := &DNS{ - proxy: newProxy(addr, upProxy), + proxy: NewProxy(addr, upProxy), dnsServer: raddr, dnsServerMap: make(map[string]string), } diff --git a/dnstun.go b/dnstun.go index 2bfca59..44d1d6a 100644 --- a/dnstun.go +++ b/dnstun.go @@ -2,7 +2,7 @@ package main -type dnstun struct { +type DNSTun struct { *proxy raddr string @@ -10,21 +10,21 @@ type dnstun struct { tcp Proxy } -// DNSTun returns a dns forwarder. -func DNSTun(addr, raddr string, upProxy Proxy) (Proxy, error) { - s := &dnstun{ - proxy: newProxy(addr, upProxy), +// NewDNSTun returns a dns forwarder. +func NewDNSTun(addr, raddr string, upProxy Proxy) (*DNSTun, error) { + s := &DNSTun{ + proxy: NewProxy(addr, upProxy), raddr: raddr, } - s.udp, _ = DNSForwarder(addr, raddr, upProxy) - s.tcp, _ = TCPTun(addr, raddr, upProxy) + s.udp, _ = NewDNS(addr, raddr, upProxy) + s.tcp, _ = NewTCPTun(addr, raddr, upProxy) return s, nil } // ListenAndServe . -func (s *dnstun) ListenAndServe() { +func (s *DNSTun) ListenAndServe() { if s.udp != nil { go s.udp.ListenAndServe() } diff --git a/http.go b/http.go index 62cd8b0..f594626 100644 --- a/http.go +++ b/http.go @@ -17,21 +17,21 @@ import ( ) // httpproxy -type httpproxy struct { +type HTTPProxy struct { *proxy } // HTTPProxy returns a http proxy. -func HTTPProxy(addr string, upProxy Proxy) (Proxy, error) { - s := &httpproxy{ - proxy: newProxy(addr, upProxy), +func NewHTTPProxy(addr string, upProxy Proxy) (*HTTPProxy, error) { + s := &HTTPProxy{ + proxy: NewProxy(addr, upProxy), } return s, nil } // ListenAndServe . -func (s *httpproxy) ListenAndServe() { +func (s *HTTPProxy) ListenAndServe() { l, err := net.Listen("tcp", s.addr) if err != nil { logf("failed to listen on %s: %v", s.addr, err) @@ -53,7 +53,7 @@ func (s *httpproxy) ListenAndServe() { } // Serve . -func (s *httpproxy) Serve(c net.Conn) { +func (s *HTTPProxy) Serve(c net.Conn) { defer c.Close() if c, ok := c.(*net.TCPConn); ok { @@ -147,7 +147,7 @@ func (s *httpproxy) Serve(c net.Conn) { } -func (s *httpproxy) servHTTPS(method, requestURI, proto string, c net.Conn) { +func (s *HTTPProxy) servHTTPS(method, requestURI, proto string, c net.Conn) { rc, err := s.GetProxy(requestURI).Dial("tcp", requestURI) if err != nil { c.Write([]byte(proto)) @@ -170,7 +170,7 @@ func (s *httpproxy) servHTTPS(method, requestURI, proto string, c net.Conn) { } // Dial connects to the address addr on the network net via the proxy. -func (s *httpproxy) Dial(network, addr string) (net.Conn, error) { +func (s *HTTPProxy) Dial(network, addr string) (net.Conn, error) { rc, err := s.GetProxy(s.addr).Dial("tcp", s.addr) if err != nil { logf("dial to %s error: %s", s.addr, err) diff --git a/main.go b/main.go index 368c1f8..89dd018 100644 --- a/main.go +++ b/main.go @@ -156,12 +156,12 @@ func main() { } // combine forwarders to a singer strategy forwarder - forwarder := newStrategyForwarder(conf.Strategy, forwarders) + forwarder := NewStrategyForwarder(conf.Strategy, forwarders) // rule forwarders - var ruleForwarders []*ruleForwarder + var ruleForwarders []*RuleForwarder for _, ruleFile := range conf.RuleFile { - ruleForwarder, err := newRuleProxyFromFile(ruleFile) + ruleForwarder, err := NewRuleProxyFromFile(ruleFile) if err != nil { log.Fatal(err) } @@ -170,7 +170,7 @@ func main() { } // combine ruleforwarders and global strategy forwarder - forwarder = newRulesForwarder(ruleForwarders, forwarder) + forwarder = NewRulesForwarder(ruleForwarders, forwarder) for _, listen := range conf.Listen { local, err := ProxyFromURL(listen, forwarder) @@ -188,7 +188,7 @@ func main() { } if conf.DNS != "" { - dns, err := DNSForwarder(conf.DNS, conf.DNSServer[0], forwarder) + dns, err := NewDNS(conf.DNS, conf.DNSServer[0], forwarder) if err != nil { log.Fatal(err) } diff --git a/mixed.go b/mixed.go index ef99118..882b8bf 100644 --- a/mixed.go +++ b/mixed.go @@ -17,8 +17,8 @@ var httpMethods = [][]byte{ []byte("TRACE"), } -// mixedproxy -type mixedproxy struct { +// MixedProxy . +type MixedProxy struct { *proxy http Proxy socks5 Proxy @@ -26,23 +26,23 @@ type mixedproxy struct { } // MixedProxy returns a mixed proxy. -func MixedProxy(network, addr, user, pass string, upProxy Proxy) (Proxy, error) { - p := &mixedproxy{ - proxy: newProxy(addr, upProxy), +func NewMixedProxy(network, addr, user, pass string, upProxy Proxy) (*MixedProxy, error) { + p := &MixedProxy{ + proxy: NewProxy(addr, upProxy), } - p.http, _ = HTTPProxy(addr, upProxy) - p.socks5, _ = SOCKS5Proxy(network, addr, user, pass, upProxy) + p.http, _ = NewHTTPProxy(addr, upProxy) + p.socks5, _ = NewSOCKS5Proxy(network, addr, user, pass, upProxy) if user != "" && pass != "" { - p.ss, _ = SSProxy(addr, user, pass, upProxy) + p.ss, _ = NewSSProxy(addr, user, pass, upProxy) } return p, nil } // mixedproxy . -func (p *mixedproxy) ListenAndServe() { +func (p *MixedProxy) ListenAndServe() { l, err := net.Listen("tcp", p.addr) if err != nil { logf("failed to listen on %s: %v", p.addr, err) diff --git a/proxy.go b/proxy.go index 87520a8..eb741dc 100644 --- a/proxy.go +++ b/proxy.go @@ -47,8 +47,8 @@ type proxy struct { enabled bool } -// newProxy . -func newProxy(addr string, forward Proxy) *proxy { +// NewProxy . +func NewProxy(addr string, forward Proxy) *proxy { if forward == nil { forward = Direct } @@ -91,22 +91,22 @@ func ProxyFromURL(s string, forwarder Proxy) (Proxy, error) { switch u.Scheme { case "mixed": - return MixedProxy("tcp", addr, user, pass, forwarder) + return NewMixedProxy("tcp", addr, user, pass, forwarder) case "http": - return HTTPProxy(addr, forwarder) + return NewHTTPProxy(addr, forwarder) case "socks5": - return SOCKS5Proxy("tcp", addr, user, pass, forwarder) + return NewSOCKS5Proxy("tcp", addr, user, pass, forwarder) case "ss": - p, err := SSProxy(addr, user, pass, forwarder) + p, err := NewSSProxy(addr, user, pass, forwarder) return p, err case "redir": - return RedirProxy(addr, forwarder) + return NewRedirProxy(addr, forwarder) case "tcptun": d := strings.Split(addr, "=") - return TCPTun(d[0], d[1], forwarder) + return NewTCPTun(d[0], d[1], forwarder) case "dnstun": d := strings.Split(addr, "=") - return DNSTun(d[0], d[1], forwarder) + return NewDNSTun(d[0], d[1], forwarder) } return nil, errors.New("unknown schema '" + u.Scheme + "'") diff --git a/redir_linux.go b/redir_linux.go index 9d1a5ae..3edfb73 100644 --- a/redir_linux.go +++ b/redir_linux.go @@ -17,12 +17,12 @@ const ( IP6T_SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv6/ip6_tables.h ) -type redir struct { +type RedirProxy struct { *proxy } -// RedirProxy returns a redirect proxy. -func RedirProxy(addr string, upProxy Proxy) (Proxy, error) { +// NewRedirProxy returns a redirect proxy. +func NewRedirProxy(addr string, upProxy Proxy) (*RedirProxy, error) { s := &redir{ proxy: newProxy(addr, upProxy), } @@ -31,7 +31,7 @@ func RedirProxy(addr string, upProxy Proxy) (Proxy, error) { } // ListenAndServe redirected requests as a server. -func (s *redir) ListenAndServe() { +func (s *RedirProxy) ListenAndServe() { l, err := net.Listen("tcp", s.addr) if err != nil { logf("failed to listen on %s: %v", s.addr, err) diff --git a/redir_other.go b/redir_other.go index ce591c4..cbd92ab 100644 --- a/redir_other.go +++ b/redir_other.go @@ -4,14 +4,14 @@ package main import "log" -type redir struct{ *proxy } +type RedirProxy struct{ *proxy } -// RedirProxy returns a redirect proxy. -func RedirProxy(addr string, upProxy Proxy) (Proxy, error) { - return &redir{proxy: newProxy(addr, upProxy)}, nil +// NewRedirProxy returns a redirect proxy. +func NewRedirProxy(addr string, upProxy Proxy) (Proxy, error) { + return &RedirProxy{proxy: NewProxy(addr, upProxy)}, nil } // ListenAndServe redirected requests as a server. -func (s *redir) ListenAndServe() { +func (s *RedirProxy) ListenAndServe() { log.Fatal("redir not supported on this os") } diff --git a/rule.go b/rule.go index afd2081..db936d2 100644 --- a/rule.go +++ b/rule.go @@ -9,8 +9,8 @@ import ( "github.com/nadoo/conflag" ) -// ruleForwarder, every ruleForwarder points to a rule file -type ruleForwarder struct { +// RuleForwarder, every ruleForwarder points to a rule file +type RuleForwarder struct { Forward []string Strategy string CheckWebSite string @@ -27,9 +27,9 @@ type ruleForwarder struct { Proxy } -// newRuleProxyFromFile . -func newRuleProxyFromFile(ruleFile string) (*ruleForwarder, error) { - p := &ruleForwarder{name: ruleFile} +// NewRuleProxyFromFile . +func NewRuleProxyFromFile(ruleFile string) (*RuleForwarder, error) { + p := &RuleForwarder{name: ruleFile} f := conflag.NewFromFile("rule", ruleFile) f.StringSliceUniqVar(&p.Forward, "forward", nil, "forward url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT[,SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT]") @@ -63,7 +63,7 @@ func newRuleProxyFromFile(ruleFile string) (*ruleForwarder, error) { forwarders = append(forwarders, forward) } - forwarder := newStrategyForwarder(p.Strategy, forwarders) + forwarder := NewStrategyForwarder(p.Strategy, forwarders) for _, forward := range forwarders { go check(forward, p.CheckWebSite, p.CheckDuration) diff --git a/rules.go b/rules.go index c71dc43..fa5b794 100644 --- a/rules.go +++ b/rules.go @@ -5,7 +5,7 @@ import ( "strings" ) -type rulesForwarder struct { +type RulesForwarder struct { globalForwarder Proxy domainMap map[string]Proxy @@ -13,14 +13,14 @@ type rulesForwarder struct { cidrMap map[string]Proxy } -// newRulesForwarder . -func newRulesForwarder(ruleForwarders []*ruleForwarder, globalForwarder Proxy) Proxy { +// NewRulesForwarder . +func NewRulesForwarder(ruleForwarders []*RuleForwarder, globalForwarder Proxy) Proxy { if len(ruleForwarders) == 0 { return globalForwarder } - p := &rulesForwarder{globalForwarder: globalForwarder} + p := &RulesForwarder{globalForwarder: globalForwarder} for _, f := range ruleForwarders { p.domainMap = make(map[string]Proxy) @@ -42,12 +42,12 @@ func newRulesForwarder(ruleForwarders []*ruleForwarder, globalForwarder Proxy) P return p } -func (p *rulesForwarder) Addr() string { return "rules forwarder" } -func (p *rulesForwarder) ListenAndServe() {} -func (p *rulesForwarder) Serve(c net.Conn) {} -func (p *rulesForwarder) CurrentProxy() Proxy { return p.globalForwarder.CurrentProxy() } +func (p *RulesForwarder) Addr() string { return "rules forwarder" } +func (p *RulesForwarder) ListenAndServe() {} +func (p *RulesForwarder) Serve(c net.Conn) {} +func (p *RulesForwarder) CurrentProxy() Proxy { return p.globalForwarder.CurrentProxy() } -func (p *rulesForwarder) GetProxy(dstAddr string) Proxy { +func (p *RulesForwarder) GetProxy(dstAddr string) Proxy { // TODO: change to index finders host, _, err := net.SplitHostPort(dstAddr) @@ -89,13 +89,13 @@ func (p *rulesForwarder) GetProxy(dstAddr string) Proxy { return p.globalForwarder.GetProxy(dstAddr) } -func (p *rulesForwarder) NextProxy() Proxy { +func (p *RulesForwarder) NextProxy() Proxy { return p.globalForwarder.NextProxy() } -func (p *rulesForwarder) Enabled() bool { return true } -func (p *rulesForwarder) SetEnable(enable bool) {} +func (p *RulesForwarder) Enabled() bool { return true } +func (p *RulesForwarder) SetEnable(enable bool) {} -func (p *rulesForwarder) Dial(network, addr string) (net.Conn, error) { +func (p *RulesForwarder) Dial(network, addr string) (net.Conn, error) { return p.GetProxy(addr).Dial(network, addr) } diff --git a/socks5.go b/socks5.go index 0414f51..7c82391 100644 --- a/socks5.go +++ b/socks5.go @@ -55,18 +55,18 @@ var socks5Errors = []string{ "address type not supported", } -type socks5 struct { +type SOCKS5Proxy struct { *proxy network string user string password string } -// SOCKS5Proxy returns a Proxy that makes SOCKSv5 connections to the given address +// NewSOCKS5Proxy returns a Proxy that makes SOCKSv5 connections to the given address // with an optional username and password. See RFC 1928. -func SOCKS5Proxy(network, addr, user, pass string, upProxy Proxy) (Proxy, error) { - s := &socks5{ - proxy: newProxy(addr, upProxy), +func NewSOCKS5Proxy(network, addr, user, pass string, upProxy Proxy) (*SOCKS5Proxy, error) { + s := &SOCKS5Proxy{ + proxy: NewProxy(addr, upProxy), user: user, password: pass, } @@ -75,7 +75,7 @@ func SOCKS5Proxy(network, addr, user, pass string, upProxy Proxy) (Proxy, error) } // ListenAndServe connects to the address addr on the network net via the SOCKS5 proxy. -func (s *socks5) ListenAndServe() { +func (s *SOCKS5Proxy) ListenAndServe() { l, err := net.Listen("tcp", s.addr) if err != nil { logf("failed to listen on %s: %v", s.addr, err) @@ -95,7 +95,7 @@ func (s *socks5) ListenAndServe() { } } -func (s *socks5) Serve(c net.Conn) { +func (s *SOCKS5Proxy) Serve(c net.Conn) { defer c.Close() if c, ok := c.(*net.TCPConn); ok { @@ -127,7 +127,7 @@ func (s *socks5) Serve(c net.Conn) { } // Dial connects to the address addr on the network net via the SOCKS5 proxy. -func (s *socks5) Dial(network, addr string) (net.Conn, error) { +func (s *SOCKS5Proxy) Dial(network, addr string) (net.Conn, error) { switch network { case "tcp", "tcp6", "tcp4": default: @@ -155,7 +155,7 @@ func (s *socks5) Dial(network, addr string) (net.Conn, error) { // connect takes an existing connection to a socks5 proxy server, // and commands the server to extend that connection to target, // which must be a canonical address with a host and port. -func (s *socks5) connect(conn net.Conn, target string) error { +func (s *SOCKS5Proxy) connect(conn net.Conn, target string) error { host, portStr, err := net.SplitHostPort(target) if err != nil { return err @@ -286,7 +286,7 @@ func (s *socks5) connect(conn net.Conn, target string) error { } // Handshake fast-tracks SOCKS initialization to get target address to connect. -func (s *socks5) handshake(rw io.ReadWriter) (Addr, error) { +func (s *SOCKS5Proxy) handshake(rw io.ReadWriter) (Addr, error) { // Read RFC 1928 for request and reply structure and sizes. buf := make([]byte, MaxAddrLen) // read VER, NMETHODS, METHODS diff --git a/ss.go b/ss.go index df51774..66efb56 100644 --- a/ss.go +++ b/ss.go @@ -10,20 +10,20 @@ import ( ) // ss -type ss struct { +type SSProxy struct { *proxy core.StreamConnCipher } -// SSProxy returns a shadowsocks proxy. -func SSProxy(addr, method, pass string, upProxy Proxy) (Proxy, error) { +// NewSSProxy returns a shadowsocks proxy. +func NewSSProxy(addr, method, pass string, upProxy Proxy) (*SSProxy, error) { ciph, err := core.PickCipher(method, nil, pass) if err != nil { log.Fatalf("PickCipher for '%s', error: %s", method, err) } - s := &ss{ - proxy: newProxy(addr, upProxy), + s := &SSProxy{ + proxy: NewProxy(addr, upProxy), StreamConnCipher: ciph, } @@ -31,7 +31,7 @@ func SSProxy(addr, method, pass string, upProxy Proxy) (Proxy, error) { } // ListenAndServe shadowsocks requests as a server. -func (s *ss) ListenAndServe() { +func (s *SSProxy) ListenAndServe() { l, err := net.Listen("tcp", s.addr) if err != nil { logf("failed to listen on %s: %v", s.addr, err) @@ -50,7 +50,7 @@ func (s *ss) ListenAndServe() { } } -func (s *ss) Serve(c net.Conn) { +func (s *SSProxy) Serve(c net.Conn) { defer c.Close() if c, ok := c.(*net.TCPConn); ok { @@ -85,7 +85,7 @@ func (s *ss) Serve(c net.Conn) { } // Dial connects to the address addr on the network net via the proxy. -func (s *ss) Dial(network, addr string) (net.Conn, error) { +func (s *SSProxy) Dial(network, addr string) (net.Conn, error) { target := ParseAddr(addr) if target == nil { diff --git a/strategy.go b/strategy.go index d61487c..08036bf 100644 --- a/strategy.go +++ b/strategy.go @@ -2,8 +2,8 @@ package main import "net" -// newStrategyForwarder . -func newStrategyForwarder(strategy string, forwarders []Proxy) Proxy { +// NewStrategyForwarder . +func NewStrategyForwarder(strategy string, forwarders []Proxy) Proxy { var proxy Proxy if len(forwarders) == 0 { proxy = Direct @@ -37,7 +37,7 @@ func newRRProxy(addr string, forwarders []Proxy) Proxy { if len(forwarders) == 0 { return Direct } else if len(forwarders) == 1 { - return newProxy(addr, forwarders[0]) + return NewProxy(addr, forwarders[0]) } return &rrProxy{forwarders: forwarders} diff --git a/tcptun.go b/tcptun.go index 8ccbbe5..20cc2df 100644 --- a/tcptun.go +++ b/tcptun.go @@ -2,15 +2,15 @@ package main import "net" -type tcptun struct { +type TCPTun struct { *proxy raddr string } -// TCPTun returns a redirect proxy. -func TCPTun(addr, raddr string, upProxy Proxy) (Proxy, error) { - s := &tcptun{ - proxy: newProxy(addr, upProxy), +// NewTCPTun returns a redirect proxy. +func NewTCPTun(addr, raddr string, upProxy Proxy) (*TCPTun, error) { + s := &TCPTun{ + proxy: NewProxy(addr, upProxy), raddr: raddr, } @@ -18,7 +18,7 @@ func TCPTun(addr, raddr string, upProxy Proxy) (Proxy, error) { } // ListenAndServe redirected requests as a server. -func (s *tcptun) ListenAndServe() { +func (s *TCPTun) ListenAndServe() { l, err := net.Listen("tcp", s.addr) if err != nil { logf("failed to listen on %s: %v", s.addr, err)