From 919942ae0fa0ca5299af294ec7faab227c6035c7 Mon Sep 17 00:00:00 2001 From: github Date: Sun, 7 Jan 2024 18:52:59 +0100 Subject: [PATCH] Check if vpn files exists --- cmd/vpn.go | 3 ++- config/config.go | 23 ++++++++++++----------- lib/vpn/vpn.go | 30 +++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/cmd/vpn.go b/cmd/vpn.go index 8f1129e..b4d2f3c 100644 --- a/cmd/vpn.go +++ b/cmd/vpn.go @@ -107,11 +107,12 @@ var vpnCmd = &cobra.Command{ os.Exit(1) } } else if stopVPNParam { - _, err := vpn.Stop() + message, err := vpn.Stop() if err != nil { config.GlobalConfig.Logger.Error("", zap.Error(err)) os.Exit(1) } + fmt.Println(message) } config.GlobalConfig.Logger.Info("Exit vpn command correctly") }, diff --git a/config/config.go b/config/config.go index fec4ed2..dd36a35 100644 --- a/config/config.go +++ b/config/config.go @@ -63,7 +63,7 @@ func ConfigureLogger() error { var err error GlobalConfig.Logger, err = cfg.Build() if err != nil { - return fmt.Errorf("Logger configuration error: %v", err) + return fmt.Errorf("logger configuration error: %v", err) } zap.ReplaceGlobals(GlobalConfig.Logger) return nil @@ -88,7 +88,7 @@ func LoadConfig(filepath string) (map[string]string, error) { parts := strings.SplitN(line, "=", 2) if len(parts) != 2 { - return nil, fmt.Errorf("Incorrectly formatted line in configuration file: %s", line) + return nil, fmt.Errorf("incorrectly formatted line in configuration file: %s", line) } key := strings.TrimSpace(parts[0]) @@ -112,15 +112,15 @@ func validateConfig(key, value string) error { switch key { case "Logging", "Batch": if value != "True" && value != "False" { - return fmt.Errorf("The value for '%s' must be 'True' or 'False', got : %s", key, value) + return fmt.Errorf("the value for '%s' must be 'True' or 'False', got : %s", key, value) } case "Proxy": if value != "False" && !isValidHTTPorHTTPSURL(value) { - return fmt.Errorf("The URL for '%s' must be a valid URL starting with http or https, got : %s", key, value) + return fmt.Errorf("the URL for '%s' must be a valid URL starting with http or https, got : %s", key, value) } case "Discord": if value != "False" && !isValidDiscordWebhook(value) { - return fmt.Errorf("The Discord webhook URL is invalid : %s", value) + return fmt.Errorf("the Discord webhook URL is invalid : %s", value) } } @@ -145,7 +145,7 @@ func Init() error { GlobalConfig.Logger.Info(fmt.Sprintf("The \"%s\" folder does not exist, creation in progress...\n", BaseDirectory)) err := os.MkdirAll(BaseDirectory, os.ModePerm) if err != nil { - return fmt.Errorf("Error folder creation: %s", err) + return fmt.Errorf("error folder creation: %s", err) } GlobalConfig.Logger.Info(fmt.Sprintf("\"%s\" folder created successfully\n\n", BaseDirectory)) @@ -155,21 +155,22 @@ func Init() error { if _, err := os.Stat(confFilePath); os.IsNotExist(err) { file, err := os.Create(confFilePath) if err != nil { - return fmt.Errorf("Error creating file: %v", err) + return fmt.Errorf("error creating file: %v", err) } defer file.Close() - configContent := `Discord = False` + configContent := `Discord = False + Update = False` writer := bufio.NewWriter(file) _, err = writer.WriteString(configContent) if err != nil { - return fmt.Errorf("Error when writing to file: %v", err) + return fmt.Errorf("error when writing to file: %v", err) } err = writer.Flush() if err != nil { - return fmt.Errorf("Error clearing buffer: %v", err) + return fmt.Errorf("error clearing buffer: %v", err) } GlobalConfig.Logger.Info("Configuration file created successfully.") @@ -178,7 +179,7 @@ func Init() error { GlobalConfig.Logger.Info("Loading configuration file...") config, err := LoadConfig(BaseDirectory + "/default.conf") if err != nil { - return fmt.Errorf("Error loading configuration file: %v", err) + return fmt.Errorf("error loading configuration file: %v", err) } GlobalConfig.Logger.Info("Configuration successfully loaded") diff --git a/lib/vpn/vpn.go b/lib/vpn/vpn.go index 6b6537e..c85e70c 100644 --- a/lib/vpn/vpn.go +++ b/lib/vpn/vpn.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "os/exec" + "path/filepath" "strings" "sync" @@ -182,7 +183,21 @@ func DownloadAll() error { // Start starts the VPN connection using an OpenVPN configuration file. func Start(configPath string) (string, error) { - fmt.Println("VPN is starting...") + config.GlobalConfig.Logger.Debug(fmt.Sprintf("VPN config file : %s", configPath)) + files, err := filepath.Glob(configPath) + if err != nil { + return "", fmt.Errorf("search error : %v", err) + } + if len(files) == 0 { + isConfirmed := utils.AskConfirmation("VPN was not found. Would you like to download it ?") + if isConfirmed { + err := DownloadAll() + if err != nil { + return "", err + } + } + } + config.GlobalConfig.Logger.Info("VPN is starting...") cmd := "pgrep -fa openvpn" hacktheboxFound := false processes, err := exec.Command("sh", "-c", cmd).Output() @@ -278,12 +293,12 @@ func Status() (bool, error) { var result []map[string]interface{} jsonBody, err := io.ReadAll(resp.Body) if err != nil { - return false, fmt.Errorf("Error reading response body: %s", err) + return false, fmt.Errorf("error reading response body: %s", err) } err = json.Unmarshal(jsonBody, &result) if err != nil { - return false, fmt.Errorf("Error unmarshalling JSON: %s", err) + return false, fmt.Errorf("error unmarshalling JSON: %s", err) } if len(result) == 0 { @@ -293,7 +308,7 @@ func Status() (bool, error) { for _, item := range result { connectionData, ok := item["connection"].(map[string]interface{}) if !ok { - return false, errors.New("Error asserting connection data") + return false, errors.New("error asserting connection data") } name := connectionData["name"] @@ -329,7 +344,7 @@ func Stop() (string, error) { cmd := "pgrep -fa openvpn" processes, err := exec.Command("sh", "-c", cmd).Output() if err != nil { - return "", fmt.Errorf("error retrieving OpenVPN processes : %v", err) + return "No vpn connection is active", nil } lines := strings.Split(string(processes), "\n") @@ -359,7 +374,8 @@ func Stop() (string, error) { fmt.Printf("Killed HackTheBox VPN process %s\n", processID) } } - } + return "Completed checking and stopping HackTheBox VPN processes.", nil - return "Completed checking and stopping HackTheBox VPN processes.", nil + } + return "", nil }