diff --git a/README.md b/README.md index 29fc83c..5cb7d29 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ In Router mode, use the -exclude flag set pattern , direct to the repo which match the module path, pattern are matched to the full path specified, not only to the host component. - ./bin/goproxy -listen=0.0.0.0:80 -cacheDir=/tmp/test -proxy https://goproxy.io -exclude "git.private.domain/[abc]" + ./bin/goproxy -listen=0.0.0.0:80 -cacheDir=/tmp/test -proxy https://goproxy.io -exclude "*.corp.example.com,rsc.io/private" ## Use docker image diff --git a/proxy/router.go b/proxy/router.go index e69daf1..048adce 100644 --- a/proxy/router.go +++ b/proxy/router.go @@ -31,18 +31,25 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router { srv: srv, } if opts != nil { - if remote, err := url.Parse(opts.Proxy); err == nil { - proxy := httputil.NewSingleHostReverseProxy(remote) - director := proxy.Director - proxy.Director = func(r *http.Request) { - director(r) - r.Host = remote.Host - } - rt.proxy = proxy + if opts.Proxy == "" { + log.Printf("not set proxy, all direct.") + return rt + } + remote, err := url.Parse(opts.Proxy) + if err != nil { + log.Printf("parse proxy fail, all direct.") + return rt + } + proxy := httputil.NewSingleHostReverseProxy(remote) + director := proxy.Director + proxy.Director = func(r *http.Request) { + director(r) + r.Host = remote.Host + } + rt.proxy = proxy - rt.proxy.Transport = &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } + rt.proxy.Transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } rt.pattern = opts.Pattern } @@ -51,13 +58,13 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router { func (rt *Router) Direct(path string) bool { if rt.pattern == "" { - return true + return false } return GlobsMatchPath(rt.pattern, path) } func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if rt.proxy != nil && rt.Direct(r.URL.Path) { + if rt.proxy == nil || rt.Direct(strings.TrimPrefix(r.URL.Path, "/")) { log.Printf("------ --- %s [direct]\n", r.URL) rt.srv.ServeHTTP(w, r) return