From 76d282f53deee1beba063295fc0deb3df5582408 Mon Sep 17 00:00:00 2001 From: Quentin Machu Date: Wed, 9 Mar 2016 19:07:12 -0500 Subject: [PATCH] Update project to have a somewhat working Signer proxy --- cmd/hmacproxy/main.go | 12 +++--------- handlers.go | 30 +++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/cmd/hmacproxy/main.go b/cmd/hmacproxy/main.go index 2b2075b..4f8a537 100644 --- a/cmd/hmacproxy/main.go +++ b/cmd/hmacproxy/main.go @@ -16,8 +16,8 @@ package main import ( "flag" + "net/http" "net/http/httptest" - "net/url" "os" log "github.com/Sirupsen/logrus" @@ -58,17 +58,11 @@ func main() { proxyConfig.Signer.Key.Region, } - signingDest, err := url.Parse("https://www.google.com") + signingProxy, err := hmacproxy.NewSigningProxy(signingCredential) if err != nil { log.Fatal(err) } - - signingProxy, err := hmacproxy.NewSigningProxy(signingDest, signingCredential) - if err != nil { - log.Fatal(err) - } - signingServer := httptest.NewServer(signingProxy) - defer signingServer.Close() + log.Fatal(http.ListenAndServe(proxyConfig.Signer.ListenerAddr, signingProxy)) } if proxyConfig.Verifier != nil { diff --git a/handlers.go b/handlers.go index 14cc898..589d8f5 100644 --- a/handlers.go +++ b/handlers.go @@ -15,23 +15,35 @@ package hmacproxy import ( + "fmt" "log" "net/http" "net/http/httputil" "net/url" "github.com/coreos-inc/hmacproxy/credential" + "github.com/elazarl/goproxy" ) -// NewSigningProxy instantiates a new signing proxy with the target url and the -// statc credential specified. -func NewSigningProxy(target *url.URL, cred credential.Credential) (*httputil.ReverseProxy, error) { - director := func(req *http.Request) { - log.Printf("Proxying request %v", req) - req.URL.Scheme = target.Scheme - req.URL.Host = target.Host - } - return &httputil.ReverseProxy{Director: director}, nil +// NewSigningProxy instantiates a new signing proxy with the static credential specified. +func NewSigningProxy(cred credential.Credential) (*goproxy.ProxyHttpServer, error) { + proxy := goproxy.NewProxyHttpServer() + + proxy.OnRequest().DoFunc( + func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { + if err := Sign4(r, cred); err != nil { + response := goproxy.NewResponse( + r, + goproxy.ContentTypeText, + http.StatusBadRequest, + fmt.Sprintf("Could not sign request: %v", err), + ) + return r, response + } + return r, nil + }) + + return proxy, nil } // NewVerifyingProxy instantiates a new verifying proxy with the specified