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 match the module path, pattern are matched to the full path specified, not only
to the host component. 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 ## Use docker image

View File

@ -31,18 +31,25 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
srv: srv, srv: srv,
} }
if opts != nil { if opts != nil {
if remote, err := url.Parse(opts.Proxy); err == nil { if opts.Proxy == "" {
proxy := httputil.NewSingleHostReverseProxy(remote) log.Printf("not set proxy, all direct.")
director := proxy.Director return rt
proxy.Director = func(r *http.Request) { }
director(r) remote, err := url.Parse(opts.Proxy)
r.Host = remote.Host if err != nil {
} log.Printf("parse proxy fail, all direct.")
rt.proxy = proxy 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{ rt.proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
} }
rt.pattern = opts.Pattern rt.pattern = opts.Pattern
} }
@ -51,13 +58,13 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
func (rt *Router) Direct(path string) bool { func (rt *Router) Direct(path string) bool {
if rt.pattern == "" { if rt.pattern == "" {
return true return false
} }
return GlobsMatchPath(rt.pattern, path) return GlobsMatchPath(rt.pattern, path)
} }
func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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) log.Printf("------ --- %s [direct]\n", r.URL)
rt.srv.ServeHTTP(w, r) rt.srv.ServeHTTP(w, r)
return return