From d12722cb6bec2669e6106ee6df92557a98845f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20B=C3=BClow?= Date: Mon, 5 Feb 2024 09:45:28 +0100 Subject: [PATCH] weird nil pointer deref --- .../machines/default/libvirt/created_networks | 1 + web/responses.go | 18 +++++ web/reverseProxy.go | 74 +++++++++++++------ 3 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 web/responses.go diff --git a/.vagrant/machines/default/libvirt/created_networks b/.vagrant/machines/default/libvirt/created_networks index ded9d28..a3c6b7c 100644 --- a/.vagrant/machines/default/libvirt/created_networks +++ b/.vagrant/machines/default/libvirt/created_networks @@ -11,3 +11,4 @@ 5533801d-70e3-4c21-9942-82d20930c789 5533801d-70e3-4c21-9942-82d20930c789 5533801d-70e3-4c21-9942-82d20930c789 +5533801d-70e3-4c21-9942-82d20930c789 diff --git a/web/responses.go b/web/responses.go new file mode 100644 index 0000000..25f93d7 --- /dev/null +++ b/web/responses.go @@ -0,0 +1,18 @@ +package web + +var clientReloadContent = ` + + + + Reloading + + + reloading + + + +` diff --git a/web/reverseProxy.go b/web/reverseProxy.go index 02274dc..05ce4bf 100644 --- a/web/reverseProxy.go +++ b/web/reverseProxy.go @@ -29,15 +29,15 @@ func createReverseProxy(backendService string) (*httputil.ReverseProxy, error) { request.SetURL(backendURL) request.Out.Host = request.In.Host }, - ModifyResponse: func(response *http.Response) error { - if response.StatusCode == http.StatusBadGateway { - time.Sleep(time.Second) - response.StatusCode = 307 - response.Header.Set("Location", "/") + // ModifyResponse: func(response *http.Response) error { + // if response.StatusCode == http.StatusBadGateway { + // waitForAnswer(backendURL.String()) + // response.StatusCode = 200 + // response.Header.Set("Location", "/") - } - return nil - }, + // } + // return nil + // }, } return proxy, err @@ -46,7 +46,7 @@ func createReverseProxy(backendService string) (*httputil.ReverseProxy, error) { func containerProxy(c *gin.Context) { session := sessions.Default(c) sessionID := "" - if session.Get("ct") == nil && session.Get("ready") == nil { + if session.Get("ct") == nil { session.Set("ready", false) session.Save() log.Println("Creating Container for Session ", sessionID) @@ -58,6 +58,7 @@ func containerProxy(c *gin.Context) { session.Delete("ct") session.Save() c.Abort() + return } err = pods.StartContainer(ct) if err != nil { @@ -65,6 +66,8 @@ func containerProxy(c *gin.Context) { session.Delete("ct") session.Save() c.Abort() + + return } ctip, err := pods.GetContainerIP(ct) @@ -74,11 +77,13 @@ func containerProxy(c *gin.Context) { session.Delete("ct") session.Save() c.Abort() + return } // Soft fail Skel // _ = pods.CopySkelToContainer(ct) + err = waitForAnswer(fmt.Sprintf("http://%s:%d", ctip, viper.GetInt("container_port"))) proxies[ct], err = createReverseProxy( fmt.Sprintf("http://%s:%d", ctip, viper.GetInt("container_port")), ) @@ -92,25 +97,28 @@ func containerProxy(c *gin.Context) { session.Delete("ct") session.Save() c.Abort() + return + } + if err != nil { + c.HTML( + 500, + "Error", + fmt.Sprintf("[%s] Timed out waiting for Container: %v", sessionID, err), + ) + session.Delete("ct") + session.Save() + c.Abort() + return } session.Set("ready", true) session.Save() - c.Redirect(307, "/") + c.HTML(200, "", clientReloadContent) + return } else { - sessionCT := session.Get("ct") - switch sessionCT.(type) { - case string: - - default: - c.HTML(500, "Error", "Session Container ID is not a string") - session.Delete("ct") - session.Delete("ready") - session.Save() - c.Abort() - } if session.Get("ready").(bool) == false { - time.Sleep(time.Second) + time.Sleep(2 * time.Second) c.Redirect(307, "/") + return } id := session.Get("ct").(string) proxy := proxies[id] @@ -121,8 +129,30 @@ func containerProxy(c *gin.Context) { session.Delete("ready") session.Save() c.Abort() + return } } } + +func waitForAnswer(url string) error { + retries := 0 + var err error + for retries < 50 { + err = nil + response, err := http.Get(url) + if err != nil { + log.Printf("Error connecting to %s: %v", url, err) + } + if response.StatusCode == 200 { + return nil + } + + retries++ + time.Sleep(500 * time.Millisecond) + } + log.Println("Timed out waiting for Container") + return err + +}