fix empty exclude/nil proxy

pull/101/head
ianwoolf 2019-08-07 10:51:41 +08:00 committed by kun
parent 55a9e0a0c5
commit 9946403fec
2 changed files with 21 additions and 14 deletions

View File

@ -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

View File

@ -31,7 +31,15 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
srv: srv,
}
if opts != nil {
if remote, err := url.Parse(opts.Proxy); err == nil {
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) {
@ -43,7 +51,6 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
rt.proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
rt.pattern = opts.Pattern
}
return rt
@ -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