Establish a proxy in console command if it's not present

This commit is contained in:
M. Mert Yildiran
2023-02-15 00:56:33 +03:00
parent 384f62f94f
commit dcd42798c7

View File

@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"net/http"
"net/url" "net/url"
"os" "os"
"os/signal" "os/signal"
@@ -12,6 +13,7 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/kubeshark/kubeshark/config" "github.com/kubeshark/kubeshark/config"
"github.com/kubeshark/kubeshark/config/configStructs" "github.com/kubeshark/kubeshark/config/configStructs"
"github.com/kubeshark/kubeshark/kubernetes"
"github.com/kubeshark/kubeshark/utils" "github.com/kubeshark/kubeshark/utils"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -39,6 +41,13 @@ func init() {
} }
func runConsole() { func runConsole() {
hubUrl := kubernetes.GetLocalhostOnPort(config.Config.Tap.Proxy.Hub.SrcPort)
response, err := http.Get(fmt.Sprintf("%s/echo", hubUrl))
if err != nil || response.StatusCode != 200 {
log.Info().Msg(fmt.Sprintf(utils.Yellow, "Couldn't connect to Hub. Establishing proxy..."))
runProxy(false)
}
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt) signal.Notify(interrupt, os.Interrupt)
@@ -52,6 +61,7 @@ func runConsole() {
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil { if err != nil {
log.Error().Err(err).Send() log.Error().Err(err).Send()
return
} }
defer c.Close() defer c.Close()