From 90769832e5ff699d4efeac4619cd9d2bf6fc6ab2 Mon Sep 17 00:00:00 2001 From: Anjan Nath Date: Mon, 21 Aug 2023 13:31:21 +0530 Subject: [PATCH] config: add 'wsl-network-access' to toggle access to CRC from WSL --- cmd/crc/cmd/daemon.go | 21 +++++++++++++++------ pkg/crc/config/settings.go | 7 +++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/cmd/crc/cmd/daemon.go b/cmd/crc/cmd/daemon.go index 7c5b3dd8b4..c9fb4f2a44 100644 --- a/cmd/crc/cmd/daemon.go +++ b/cmd/crc/cmd/daemon.go @@ -9,6 +9,7 @@ import ( "os" "os/signal" "regexp" + "runtime" "syscall" "time" @@ -238,12 +239,7 @@ func run(configuration *types.Configuration) error { func gatewayAPIMux() *http.ServeMux { mux := http.NewServeMux() mux.HandleFunc("/hosts/add", func(w http.ResponseWriter, r *http.Request) { - acceptJSONStringArray(w, r, func(hostnames []string) error { - // Most of the time we will add a hosts entry to resolve this to 127.0.0.1 - // On a Windows machine running WSL this is the "IP" of the machine within the WSL2 network, - // so it is accessible from both WSL2 and Windows - return adminhelper.AddToHostsFile(machine.VsockPrivateAddress(), hostnames...) - }) + acceptJSONStringArray(w, r, addEntriesToHostsFile) }) mux.HandleFunc("/hosts/remove", func(w http.ResponseWriter, r *http.Request) { acceptJSONStringArray(w, r, func(hostnames []string) error { @@ -253,6 +249,19 @@ func gatewayAPIMux() *http.ServeMux { return mux } +func addEntriesToHostsFile(hostnames []string) error { + // Most of the time we will add a hosts entry to resolv to 127.0.0.1 + // On a Windows machine running WSL and config 'wsl-network-access' is true + // "IP" of the machine within the WSL2 network, so it is accessible from both WSL2 and Windows + if runtime.GOOS == "windows" { + if config.Get(crcConfig.WSLHostAccess).AsBool() { + log.Debugf("Enabling WSL to CRC network access") + return adminhelper.AddToHostsFile(machine.VsockPrivateAddress(), hostnames...) + } + } + return adminhelper.AddToHostsFile("127.0.0.1", hostnames...) +} + func networkAPIMux(vn *virtualnetwork.VirtualNetwork) *http.ServeMux { mux := http.NewServeMux() mux.Handle("/", vn.Mux()) diff --git a/pkg/crc/config/settings.go b/pkg/crc/config/settings.go index 503d9f97ed..8e672b5c02 100644 --- a/pkg/crc/config/settings.go +++ b/pkg/crc/config/settings.go @@ -35,6 +35,7 @@ const ( IngressHTTPPort = "ingress-http-port" IngressHTTPSPort = "ingress-https-port" EmergencyLogin = "enable-emergency-login" + WSLHostAccess = "wsl-host-access" ) func RegisterSettings(cfg *Config) { @@ -92,13 +93,15 @@ func RegisterSettings(cfg *Config) { cfg.AddSetting(EmergencyLogin, false, ValidateBool, SuccessfullyApplied, "Enable emergency login for 'core' user. Password is randomly generated. (true/false, default: false)") - // Shared directories configs if runtime.GOOS == "windows" { + // Shared directories configs cfg.AddSetting(SharedDirPassword, Secret(""), validateString, SuccessfullyApplied, "Password used while using CIFS/SMB file sharing (It is the password for the current logged in user)") - cfg.AddSetting(EnableSharedDirs, false, validateSmbSharedDirs, SuccessfullyApplied, "Mounts host's user profile folder at '/' in the CRC VM (true/false, default: false)") + // Setting to enable access to CRC vm from wsl + cfg.AddSetting(WSLHostAccess, false, ValidateBool, SuccessfullyApplied, + "Enable acess to CRC VM within WSL environment (true/false, default: false)") } else { cfg.AddSetting(EnableSharedDirs, true, ValidateBool, SuccessfullyApplied, "Mounts host's home directory at '/' in the CRC VM (true/false, default: true)")