From ee8e4330c1a5228fa53b20f5c18ef48726267a84 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Sun, 24 Nov 2019 17:30:39 +0100 Subject: [PATCH] Move proxy logic into `proxy` package --- main.go | 4 +++- cert.go => proxy/cert.go | 2 +- net.go => proxy/net.go | 2 +- proxy.go => proxy/proxy.go | 16 +++++++--------- 4 files changed, 12 insertions(+), 12 deletions(-) rename cert.go => proxy/cert.go (99%) rename net.go => proxy/net.go (98%) rename proxy.go => proxy/proxy.go (89%) diff --git a/main.go b/main.go index cd66a91..7b804f2 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "flag" "log" "net/http" + + "github.com/dstotijn/gurp/proxy" ) var ( @@ -26,7 +28,7 @@ func main() { log.Fatalf("[FATAL] Could not parse CA: %v", err) } - proxy, err := NewProxy(caCert, tlsCA.PrivateKey) + proxy, err := proxy.NewProxy(caCert, tlsCA.PrivateKey) if err != nil { log.Fatalf("[FATAL] Could not create Proxy: %v", err) } diff --git a/cert.go b/proxy/cert.go similarity index 99% rename from cert.go rename to proxy/cert.go index ff432a8..a997069 100644 --- a/cert.go +++ b/proxy/cert.go @@ -1,4 +1,4 @@ -package main +package proxy import ( "bytes" diff --git a/net.go b/proxy/net.go similarity index 98% rename from net.go rename to proxy/net.go index 3104839..24ea8cb 100644 --- a/net.go +++ b/proxy/net.go @@ -1,4 +1,4 @@ -package main +package proxy import ( "errors" diff --git a/proxy.go b/proxy/proxy.go similarity index 89% rename from proxy.go rename to proxy/proxy.go index b1d7e98..c24bac0 100644 --- a/proxy.go +++ b/proxy/proxy.go @@ -1,4 +1,4 @@ -package main +package proxy import ( "context" @@ -13,11 +13,8 @@ import ( ) var httpHandler = &httputil.ReverseProxy{ - Director: func(r *http.Request) { - r.URL.Host = r.Host - r.URL.Scheme = "http" - }, - ErrorHandler: proxyErrorHandler, + Director: func(r *http.Request) {}, + ErrorHandler: errorHandler, } var httpsHandler = &httputil.ReverseProxy{ @@ -25,10 +22,10 @@ var httpsHandler = &httputil.ReverseProxy{ r.URL.Host = r.Host r.URL.Scheme = "https" }, - ErrorHandler: proxyErrorHandler, + ErrorHandler: errorHandler, } -func proxyErrorHandler(w http.ResponseWriter, r *http.Request, err error) { +func errorHandler(w http.ResponseWriter, r *http.Request, err error) { if err == context.Canceled { return } @@ -36,7 +33,8 @@ func proxyErrorHandler(w http.ResponseWriter, r *http.Request, err error) { w.WriteHeader(http.StatusBadGateway) } -// Proxy is used to forward HTTP requests. +// Proxy implements http.Handler and offers MITM behaviour for modifying +// HTTP requests and responses. type Proxy struct { certConfig *CertConfig }