Skip to content

Commit

Permalink
config: add 'wsl-network-access' to toggle access to CRC from WSL
Browse files Browse the repository at this point in the history
  • Loading branch information
anjannath committed Aug 21, 2023
1 parent 87ebc1b commit 9076983
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
21 changes: 15 additions & 6 deletions cmd/crc/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/signal"
"regexp"
"runtime"
"syscall"
"time"

Expand Down Expand Up @@ -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 {
Expand All @@ -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())
Expand Down
7 changes: 5 additions & 2 deletions pkg/crc/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)")
Expand Down

0 comments on commit 9076983

Please sign in to comment.