Skip to content

Commit

Permalink
remove http recorder to support websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
mvanholsteijn committed Dec 7, 2021
1 parent c73e4fb commit 79be2f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 48 deletions.
14 changes: 7 additions & 7 deletions gke_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ type Proxy struct {
ConfigurationName string
UseDefaultCredentials bool

TargetURL string
targetURL *url.URL
credentials *google.Credentials
tokenSource oauth2.TokenSource
clusterInfoCache *clusterinfo.Cache
TargetURL string
targetURL *url.URL
credentials *google.Credentials
tokenSource oauth2.TokenSource
clusterInfo *clusterinfo.Cache
}

func (p *Proxy) getCredentials(ctx context.Context) error {
Expand All @@ -60,7 +60,7 @@ func (p *Proxy) getCredentials(ctx context.Context) error {
// IsClusterEndpoint return true if the request is targeting an GKE cluster endpoint
func (p *Proxy) IsClusterEndpoint() goproxy.ReqConditionFunc {
return func(req *http.Request, ctx *goproxy.ProxyCtx) bool {
return p.clusterInfoCache.GetConnectInfoForEndpoint(req.URL.Host) != nil
return p.clusterInfo.GetConnectInfoForEndpoint(req.URL.Host) != nil
}
}

Expand Down Expand Up @@ -135,7 +135,7 @@ func (p *Proxy) Run() {
log.Fatalf("%s", err)
}

p.clusterInfoCache, err = clusterinfo.NewCache(ctx, p.ProjectID, p.credentials, 5*time.Minute)
p.clusterInfo, err = clusterinfo.NewCache(ctx, p.ProjectID, p.credentials, 5*time.Minute)
if err != nil {
log.Fatalf("%s", err)
}
Expand Down
50 changes: 9 additions & 41 deletions gke_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ import (
"golang.org/x/oauth2/google"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"time"
)

// ReverseProxy provides the runtime configuration of the Reverse Proxy
type ReverseProxy struct {
Debug bool
Port int
ProjectID string
KeyFile string
CertificateFile string
clusterInfoCache *clusterinfo.Cache
Debug bool
Port int
ProjectID string
KeyFile string
CertificateFile string
clusterInfo *clusterinfo.Cache
}

func (p *ReverseProxy) retrieveClusterInfo(ctx context.Context) error {
Expand All @@ -37,13 +36,13 @@ func (p *ReverseProxy) retrieveClusterInfo(ctx context.Context) error {
return fmt.Errorf("specify a --project as there is no default one")
}

p.clusterInfoCache, err = clusterinfo.NewCache(ctx, p.ProjectID, credentials, 5*time.Minute)
p.clusterInfo, err = clusterinfo.NewCache(ctx, p.ProjectID, credentials, 5*time.Minute)
return err
}

func (p *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {

clusterInfo := p.clusterInfoCache.GetConnectInfoForEndpoint(r.Host)
clusterInfo := p.clusterInfo.GetConnectInfoForEndpoint(r.Host)
if clusterInfo == nil {
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte(fmt.Sprintf("%s is not a cluster endpoint", r.Host)))
Expand Down Expand Up @@ -71,38 +70,7 @@ func (p *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

if p.Debug {
x, err := httputil.DumpRequest(r, true)
if err != nil {
log.Printf("failed to dump the response body, %s", err)
} else {
log.Println(fmt.Sprintf("%q", x))
}
}

rec := httptest.NewRecorder()
proxy.ServeHTTP(rec, r)

if p.Debug {
x, err := httputil.DumpResponse(rec.Result(), true)
if err != nil {
log.Printf("failed to dump the response body, %s", err)
} else {
log.Println(fmt.Sprintf("%q", x))
}
}

for key, values := range rec.Header() {
for _, value := range values {
w.Header().Add(key, value)
}
}

w.WriteHeader(rec.Code)
_, err = rec.Body.WriteTo(w)
if err != nil {
log.Printf("error writing body, %s", err)
}
proxy.ServeHTTP(w, r)
}

// Run the reverse proxy until stopped
Expand Down

0 comments on commit 79be2f4

Please sign in to comment.