Handle redirects for bosh.io

This commit is contained in:
Ruben Koster 2019-02-13 15:38:49 +01:00
parent 8b12193c46
commit e46dae41c0
3 changed files with 45 additions and 7 deletions

40
cache/artifactory.go vendored
View File

@ -3,6 +3,7 @@ package cache
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"io/ioutil"
"log"
@ -45,7 +46,7 @@ func NewCache(config ArtifactoryConfig, logger *log.Logger) (*Cache, error) {
return &cache, nil
}
func (c *Cache) Handle(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
func (c *Cache) ReqHandle(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
url, ok := c.isCached(req)
if ok {
c.log.Printf("Hitting cache: %s for: %s", url, req.URL.String())
@ -57,6 +58,27 @@ func (c *Cache) Handle(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request,
return req, nil
}
func (c *Cache) RespHandle(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
if resp.StatusCode == http.StatusFound {
location, _ := resp.Location()
req, err := http.NewRequest(resp.Request.Method, location.String(), nil)
if err != nil {
c.log.Printf("Error while creating redirect request for %s: %s",
req.URL.String(), err)
return nil
}
c.log.Printf("resp handler: %s", location)
resp, err = ctx.RoundTrip(req)
if err != nil {
c.log.Printf("Error while resolving redirect request for %s: %s",
req.URL.String(), err)
return nil
}
}
return resp
}
func (c *Cache) HandleReq(req *http.Request, _ *goproxy.ProxyCtx) bool {
if req.Method == http.MethodGet {
return true
@ -76,7 +98,9 @@ func (c *Cache) cache(req *http.Request, ctx *goproxy.ProxyCtx) {
return
}
resp, err := ctx.RoundTrip(creq)
client := &http.Client{}
client.Transport = ctx
resp, err := client.Do(creq)
if err != nil {
c.log.Printf("Error while performing cache request for %s: %s",
req.URL.String(), err)
@ -94,6 +118,11 @@ func (c *Cache) cache(req *http.Request, ctx *goproxy.ProxyCtx) {
c.getCachePath(req),
bytes.NewReader(body),
)
sha := sha256.Sum256(body)
areq.Header.Add("X-Checksum-Deploy", `true`)
areq.Header.Add("X-Checksum-Sha256", fmt.Sprintf("%x", sha))
if err != nil {
c.log.Printf("Error while building artifactory cache request for %s: %s",
req.URL.String(), err)
@ -125,7 +154,12 @@ func (c *Cache) getCacheURL(req *http.Request) *url.URL {
}
func (c *Cache) getCachePath(req *http.Request) string {
return filepath.Join(c.config.Repository, getId(req).String())
return filepath.Join(c.config.Repository,
req.URL.Hostname(),
req.URL.Path,
req.URL.RawQuery,
getId(req).String(),
)
}
func getId(req *http.Request) uuid.UUID {

View File

@ -34,8 +34,9 @@ func main() {
proxy = goproxy.NewProxyHttpServer()
proxy.Verbose = *verbose
proxy.Logger = logger
proxy.OnRequest(cache).HandleConnect(goproxy.AlwaysMitm)
proxy.OnRequest(cache).Do(cache)
proxy.OnRequest().HandleConnect(goproxy.AlwaysMitm)
proxy.OnRequest(cache).DoFunc(cache.ReqHandle)
proxy.OnResponse(cache).DoFunc(cache.RespHandle)
log.Fatal(http.ListenAndServe(*addr, proxy))
}

View File

@ -3,7 +3,10 @@
go run main.go -v &
sleep 1
# curl --proxy localhost:8080 --proxytunnel 'https://example.com?foo=bar'
curl --proxy localhost:8080 'http://example.com?foo=bar'
# curl -k --proxy localhost:8080 --proxytunnel 'https://example.com?foo=bar'
# curl -k --proxy localhost:8080 'https://example.com?foo=baz'
curl -k --proxy localhost:8080 -o release.tgz 'https://bosh.io/d/github.com/cloudfoundry-community/cron-boshrelease?v=1.1.3'
sleep 5
kill -9 $(lsof -iTCP:8080 -sTCP:LISTEN | tail -n1 | xargs | cut -d' ' -f2)