mirror of https://github.com/goproxyio/goproxy
golint
parent
914cff2342
commit
7472c4efb3
14
main.go
14
main.go
|
@ -158,10 +158,13 @@ type responseLogger struct {
|
|||
http.ResponseWriter
|
||||
}
|
||||
|
||||
// WriteHeader writes header code into responser writer.
|
||||
func (r *responseLogger) WriteHeader(code int) {
|
||||
r.code = code
|
||||
r.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
// ServeHTTP implements http handler.
|
||||
func (l *logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
rl := &responseLogger{code: 200, ResponseWriter: w}
|
||||
|
@ -172,9 +175,12 @@ func (l *logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
// An ops is a proxy.ServerOps implementation.
|
||||
type ops struct{}
|
||||
|
||||
// NewContext crates a context.
|
||||
func (*ops) NewContext(r *http.Request) (context.Context, error) {
|
||||
return context.Background(), nil
|
||||
}
|
||||
|
||||
// List lists proxy files.
|
||||
func (*ops) List(ctx context.Context, mpath string) (proxy.File, error) {
|
||||
escMod, err := module.EscapePath(mpath)
|
||||
if err != nil {
|
||||
|
@ -209,6 +215,8 @@ func (*ops) List(ctx context.Context, mpath string) (proxy.File, error) {
|
|||
|
||||
return os.Open(file)
|
||||
}
|
||||
|
||||
// Latest fetch latest file.
|
||||
func (*ops) Latest(ctx context.Context, path string) (proxy.File, error) {
|
||||
d, err := download(module.Version{Path: path, Version: "latest"})
|
||||
if err != nil {
|
||||
|
@ -216,6 +224,8 @@ func (*ops) Latest(ctx context.Context, path string) (proxy.File, error) {
|
|||
}
|
||||
return os.Open(d.Info)
|
||||
}
|
||||
|
||||
// Info fetch info file.
|
||||
func (*ops) Info(ctx context.Context, m module.Version) (proxy.File, error) {
|
||||
d, err := download(m)
|
||||
if err != nil {
|
||||
|
@ -223,6 +233,8 @@ func (*ops) Info(ctx context.Context, m module.Version) (proxy.File, error) {
|
|||
}
|
||||
return os.Open(d.Info)
|
||||
}
|
||||
|
||||
// GoMod fetch go mod file.
|
||||
func (*ops) GoMod(ctx context.Context, m module.Version) (proxy.File, error) {
|
||||
d, err := download(m)
|
||||
if err != nil {
|
||||
|
@ -230,6 +242,8 @@ func (*ops) GoMod(ctx context.Context, m module.Version) (proxy.File, error) {
|
|||
}
|
||||
return os.Open(d.GoMod)
|
||||
}
|
||||
|
||||
// Zip fetch zip file.
|
||||
func (*ops) Zip(ctx context.Context, m module.Version) (proxy.File, error) {
|
||||
d, err := download(m)
|
||||
if err != nil {
|
||||
|
|
|
@ -34,40 +34,15 @@ type RouterOptions struct {
|
|||
// which implements Route Filter to
|
||||
// routing private module or public module .
|
||||
type Router struct {
|
||||
opts *RouterOptions
|
||||
srv *Server
|
||||
proxy *httputil.ReverseProxy
|
||||
pattern string
|
||||
downloadRoot string
|
||||
}
|
||||
|
||||
// NewRouter returns a new Router using the given operations.
|
||||
func NewRouter(srv *Server, opts *RouterOptions) *Router {
|
||||
rt := &Router{
|
||||
srv: srv,
|
||||
}
|
||||
if opts != 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) {
|
||||
director(r)
|
||||
r.Host = remote.Host
|
||||
}
|
||||
rt.proxy = proxy
|
||||
|
||||
rt.proxy.Transport = &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
rt.proxy.ModifyResponse = func(r *http.Response) error {
|
||||
func (router *Router) customModResponse(r *http.Response) error {
|
||||
var err error
|
||||
if r.StatusCode == http.StatusOK {
|
||||
var buf []byte
|
||||
if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
|
||||
|
@ -89,7 +64,7 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
|
|||
}
|
||||
r.Body = ioutil.NopCloser(bytes.NewReader(buf))
|
||||
if buf != nil {
|
||||
file := filepath.Join(opts.DownloadRoot, r.Request.URL.Path)
|
||||
file := filepath.Join(router.opts.DownloadRoot, r.Request.URL.Path)
|
||||
os.MkdirAll(path.Dir(file), os.ModePerm)
|
||||
err = renameio.WriteFile(file, buf, 0666)
|
||||
if err != nil {
|
||||
|
@ -97,7 +72,6 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// support 302 status code.
|
||||
if r.StatusCode == http.StatusFound {
|
||||
loc := r.Header.Get("Location")
|
||||
|
@ -136,7 +110,7 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
|
|||
}
|
||||
resp.Body = ioutil.NopCloser(bytes.NewReader(buf))
|
||||
if buf != nil {
|
||||
file := filepath.Join(opts.DownloadRoot, r.Request.URL.Path)
|
||||
file := filepath.Join(router.opts.DownloadRoot, r.Request.URL.Path)
|
||||
os.MkdirAll(path.Dir(file), os.ModePerm)
|
||||
err = renameio.WriteFile(file, buf, 0666)
|
||||
if err != nil {
|
||||
|
@ -146,6 +120,37 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewRouter returns a new Router using the given operations.
|
||||
func NewRouter(srv *Server, opts *RouterOptions) *Router {
|
||||
rt := &Router{
|
||||
opts: opts,
|
||||
srv: srv,
|
||||
}
|
||||
if opts != 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) {
|
||||
director(r)
|
||||
r.Host = remote.Host
|
||||
}
|
||||
|
||||
rt.proxy = proxy
|
||||
|
||||
rt.proxy.Transport = &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
rt.proxy.ModifyResponse = rt.customModResponse
|
||||
rt.pattern = opts.Pattern
|
||||
rt.downloadRoot = opts.DownloadRoot
|
||||
}
|
||||
|
@ -160,6 +165,7 @@ func (rt *Router) Direct(path string) bool {
|
|||
return GlobsMatchPath(rt.pattern, path)
|
||||
}
|
||||
|
||||
// ServveHTTP implements http handler.
|
||||
func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// sumdb handler
|
||||
if strings.HasPrefix(r.URL.Path, "/sumdb/") {
|
||||
|
|
|
@ -221,8 +221,13 @@ type memFile struct {
|
|||
stat memStat
|
||||
}
|
||||
|
||||
// Close closes file.
|
||||
func (f *memFile) Close() error { return nil }
|
||||
|
||||
// Stat stats file.
|
||||
func (f *memFile) Stat() (os.FileInfo, error) { return &f.stat, nil }
|
||||
|
||||
// Readdir read dir.
|
||||
func (f *memFile) Readdir(count int) ([]os.FileInfo, error) { return nil, os.ErrInvalid }
|
||||
|
||||
type memStat struct {
|
||||
|
@ -230,11 +235,22 @@ type memStat struct {
|
|||
size int64
|
||||
}
|
||||
|
||||
// Name returns file name.
|
||||
func (s *memStat) Name() string { return "memfile" }
|
||||
|
||||
// Size returns file size.
|
||||
func (s *memStat) Size() int64 { return s.size }
|
||||
|
||||
// Mode returns file mode.
|
||||
func (s *memStat) Mode() os.FileMode { return 0444 }
|
||||
|
||||
// ModTime returns file modtime.
|
||||
func (s *memStat) ModTime() time.Time { return s.t }
|
||||
|
||||
// IsDir returns if file is a dir.
|
||||
func (s *memStat) IsDir() bool { return false }
|
||||
|
||||
// Sys return nil.
|
||||
func (s *memStat) Sys() interface{} { return nil }
|
||||
|
||||
// NewInfo returns a formatted info file for the given version, time pair.
|
||||
|
|
|
@ -50,30 +50,15 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
p := "https://" + strings.TrimPrefix(r.URL.Path, "/sumdb/")
|
||||
_, err := url.Parse(p)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusGone)
|
||||
fmt.Fprintf(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := http.Get(p)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusGone)
|
||||
fmt.Fprintf(w, err.Error())
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
if _, err := io.Copy(w, resp.Body); err != nil {
|
||||
fmt.Fprintf(w, err.Error())
|
||||
return
|
||||
}
|
||||
return
|
||||
proxySumdb(p, w, r)
|
||||
}
|
||||
|
||||
func sumViaGoproxy(w http.ResponseWriter, r *http.Request) {
|
||||
p := "https://goproxy.io" + r.URL.Path
|
||||
proxySumdb(p, w, r)
|
||||
}
|
||||
|
||||
func proxySumdb(p string, w http.ResponseWriter, r *http.Request) {
|
||||
_, err := url.Parse(p)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusGone)
|
||||
|
@ -94,4 +79,5 @@ func sumViaGoproxy(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue