mirror of https://github.com/goproxyio/goproxy
fix empty exclude/nil proxy
parent
55a9e0a0c5
commit
9946403fec
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue