mirror of https://github.com/goproxyio/goproxy
cache files fetched from upstream proxy in -CacheDir under router mode
parent
d976bbfa43
commit
a7bbe9e74f
3
main.go
3
main.go
|
@ -38,7 +38,7 @@ import (
|
||||||
|
|
||||||
var downloadRoot string
|
var downloadRoot string
|
||||||
|
|
||||||
const listExpire = 5 * time.Minute
|
const listExpire = proxy.ListExpire
|
||||||
|
|
||||||
var listen string
|
var listen string
|
||||||
var cacheDir string
|
var cacheDir string
|
||||||
|
@ -94,6 +94,7 @@ func main() {
|
||||||
handle = &logger{proxy.NewRouter(proxy.NewServer(new(ops)), &proxy.RouterOptions{
|
handle = &logger{proxy.NewRouter(proxy.NewServer(new(ops)), &proxy.RouterOptions{
|
||||||
Pattern: excludeHost,
|
Pattern: excludeHost,
|
||||||
Proxy: proxyHost,
|
Proxy: proxyHost,
|
||||||
|
DownloadRoot: downloadRoot,
|
||||||
})}
|
})}
|
||||||
} else {
|
} else {
|
||||||
handle = &logger{proxy.NewServer(new(ops))}
|
handle = &logger{proxy.NewServer(new(ops))}
|
||||||
|
|
|
@ -1,19 +1,28 @@
|
||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const ListExpire = 5 * time.Minute
|
||||||
|
|
||||||
// A RouterOps provides the proxy host and the external pattern
|
// A RouterOps provides the proxy host and the external pattern
|
||||||
type RouterOptions struct {
|
type RouterOptions struct {
|
||||||
Pattern string
|
Pattern string
|
||||||
Proxy string
|
Proxy string
|
||||||
|
DownloadRoot string
|
||||||
}
|
}
|
||||||
|
|
||||||
// A Router is the proxy HTTP server,
|
// A Router is the proxy HTTP server,
|
||||||
|
@ -23,6 +32,7 @@ type Router struct {
|
||||||
srv *Server
|
srv *Server
|
||||||
proxy *httputil.ReverseProxy
|
proxy *httputil.ReverseProxy
|
||||||
pattern string
|
pattern string
|
||||||
|
downloadRoot string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRouter returns a new Router using the given operations.
|
// NewRouter returns a new Router using the given operations.
|
||||||
|
@ -51,7 +61,29 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
|
||||||
rt.proxy.Transport = &http.Transport{
|
rt.proxy.Transport = &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
}
|
}
|
||||||
|
rt.proxy.ModifyResponse = func(r *http.Response) error {
|
||||||
|
if r.StatusCode == http.StatusOK {
|
||||||
|
var buf []byte
|
||||||
|
if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
|
||||||
|
if gr, err := gzip.NewReader(r.Body); err == nil {
|
||||||
|
defer gr.Close()
|
||||||
|
buf, _ = ioutil.ReadAll(gr)
|
||||||
|
r.Header.Del("Content-Encoding")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buf, _ = ioutil.ReadAll(r.Body)
|
||||||
|
}
|
||||||
|
r.Body = ioutil.NopCloser(bytes.NewReader(buf))
|
||||||
|
if buf != nil {
|
||||||
|
file := filepath.Join(opts.DownloadRoot, r.Request.URL.Path)
|
||||||
|
os.MkdirAll(path.Dir(file), 755)
|
||||||
|
ioutil.WriteFile(file, buf, 0666)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
rt.pattern = opts.Pattern
|
rt.pattern = opts.Pattern
|
||||||
|
rt.downloadRoot = opts.DownloadRoot
|
||||||
}
|
}
|
||||||
return rt
|
return rt
|
||||||
}
|
}
|
||||||
|
@ -69,6 +101,41 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
rt.srv.ServeHTTP(w, r)
|
rt.srv.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
file := filepath.Join(rt.downloadRoot, r.URL.Path)
|
||||||
|
if info, err := os.Stat(file); err == nil {
|
||||||
|
if f, err := os.Open(file); err == nil {
|
||||||
|
var ctype string
|
||||||
|
defer f.Close()
|
||||||
|
i := strings.Index(r.URL.Path, "/@v/")
|
||||||
|
what := r.URL.Path[i+len("/@v/"):]
|
||||||
|
if what == "list" {
|
||||||
|
if time.Since(info.ModTime()) >= ListExpire {
|
||||||
|
log.Printf("------ --- %s [proxy]\n", r.URL)
|
||||||
|
rt.proxy.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
ctype = "text/plain; charset=UTF-8"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ext := path.Ext(what)
|
||||||
|
switch ext {
|
||||||
|
case ".info":
|
||||||
|
ctype = "application/json"
|
||||||
|
case ".mod":
|
||||||
|
ctype = "text/plain; charset=UTF-8"
|
||||||
|
case ".zip":
|
||||||
|
ctype = "application/octet-stream"
|
||||||
|
default:
|
||||||
|
http.Error(w, "request not recognized", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", ctype)
|
||||||
|
log.Printf("------ --- %s [cached]\n", r.URL)
|
||||||
|
http.ServeContent(w, r, "", info.ModTime(), f)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
log.Printf("------ --- %s [proxy]\n", r.URL)
|
log.Printf("------ --- %s [proxy]\n", r.URL)
|
||||||
rt.proxy.ServeHTTP(w, r)
|
rt.proxy.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue