From 9366b98d10e135d9fd50c3037588d0202edf3c0e Mon Sep 17 00:00:00 2001 From: Corey Butler Date: Mon, 13 Feb 2023 19:41:21 -0600 Subject: [PATCH] Add checks for network connectivity --- src/nvm.go | 23 +++++++++++++++++++++++ src/web/web.go | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/nvm.go b/src/nvm.go index 9ea8524..4a64a2e 100644 --- a/src/nvm.go +++ b/src/nvm.go @@ -928,10 +928,33 @@ func checkLocalEnvironment() { fmt.Printf("\nWindows Developer Mode: %v\n", devmode) } + fmt.Printf("\nInformation\n-----------\nPath: %v\nVersion: %v\nNVM_HOME: %v\nNVM_SYMLINK: %v\n", os.Args[0], NvmVersion, home, symlink) + if !nvmsymlinkfound { problems = append(problems, "The NVM4W symlink ("+env.symlink+") was not found in the PATH environment variable.") } + nodelist := web.Ping(web.GetFullNodeUrl("index.json")) + if !nodelist { + if len(env.node_mirror) > 0 && env.node_mirror != "none" { + problems = append(problems, "Connection to "+env.node_mirror+" (mirror) cannot be established. Check the mirror server to assure it is online.") + } else { + if len(env.proxy) > 0 { + problems = append(problems, "Connection to nodejs.org cannot be established. Check your proxy ("+env.proxy+") and your physical internet connection.") + } else { + problems = append(problems, "Connection to nodejs.org cannot be established. Check your internet connection.") + } + } + } + + if len(env.npm_mirror) > 0 { + fmt.Println("If you are experiencing npm problems, check the npm mirror (" + env.npm_mirror + ") to assure it is online and accessible.") + } + + if _, err := os.Stat(env.settings); err != nil { + problems = append(problems, "Cannot find "+env.settings) + } + if len(problems) == 0 { fmt.Println("\nNo problems detected.") return diff --git a/src/web/web.go b/src/web/web.go index 4ed7d48..2130717 100644 --- a/src/web/web.go +++ b/src/web/web.go @@ -66,6 +66,27 @@ func GetFullNpmUrl(path string) string { return npmBaseAddress + path } +func Ping(url string) bool { + req, err := http.NewRequest("HEAD", url, nil) + if err != nil { + fmt.Println(err) + return false + } + + req.Header.Set("User-Agent", "NVM for Windows") + + response, err := client.Do(req) + if err != nil { + return false + } + + if response.StatusCode == 200 { + return true + } + + return false +} + func Download(url string, target string, version string) bool { output, err := os.Create(target) if err != nil {