From 306511be1304b33876654332ec7ab59c39dc5e9a Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 8 Sep 2024 21:06:31 +0200 Subject: [PATCH 1/3] detecting system proxy config protocols --- proxy/util.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/proxy/util.go b/proxy/util.go index 1984d65..3ead839 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -72,17 +72,38 @@ func ReadSystemProxy() (*Proxy, error) { } defer key.Close() - enabled, _, err := key.GetIntegerValue("proxyEnable") + enableVal, _, err := key.GetIntegerValue("proxyEnable") if err != nil { return nil, err } + enabled := enableVal != 0 - server, _, err := key.GetStringValue("proxyServer") + servers, _, err := key.GetStringValue("proxyServer") if err != nil && !errors.Is(err, registry.ErrNotExist) { return nil, err } - return &Proxy{Enabled: enabled != 0, Server: server}, nil + if !strings.ContainsAny(servers, ";=") { + return &Proxy{Enabled: enabled, Server: servers}, nil + } + + serverSplit := strings.Split(servers, ";") + serverDict := make(map[string]string) + for _, substr := range serverSplit { + subSplit := strings.Split(substr, "=") + key, value := subSplit[0], subSplit[1] + serverDict[key] = value + } + + if serverDict["http"] != "" { + return &Proxy{Enabled: enabled, Server: serverDict["http"]}, nil + } + + if serverDict["https"] != "" { + return &Proxy{Enabled: enabled, Server: serverDict["https"]}, nil + } + + return nil, errors.New("You need configure either HTTP or HTTPS proxy to proceed.") } func SetSystemProxy(state bool) error { From 7b09ac0cda1ba1ceef989d467634b97b7e08b676 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 8 Sep 2024 21:30:18 +0200 Subject: [PATCH 2/3] basic proxy error handling; exit on read proxy error --- proxy/debug.go | 8 +++++++- proxy/set.go | 9 ++++++--- proxy/unset.go | 9 ++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/proxy/debug.go b/proxy/debug.go index bba1499..5d2cbb8 100644 --- a/proxy/debug.go +++ b/proxy/debug.go @@ -2,6 +2,7 @@ package proxy import ( "fmt" + "log" "strings" "github.com/Baipyrus/ProxySwitcher/util" @@ -20,7 +21,12 @@ func mapCmdsToStr(commands []*util.Command) string { } func Debug() { - proxy, _ := ReadSystemProxy() + proxy, err := ReadSystemProxy() + + if err != nil { + log.Fatal(err) + } + fmt.Println("\nSystem Proxy:") fmt.Printf("Enabled: %t\n", proxy.Enabled) fmt.Printf("Server: %s\n\n", proxy.Server) diff --git a/proxy/set.go b/proxy/set.go index 00c0b7f..6892490 100644 --- a/proxy/set.go +++ b/proxy/set.go @@ -1,16 +1,19 @@ package proxy import ( + "log" + "github.com/Baipyrus/ProxySwitcher/util" ) func Set() { stdin, closeFunc, _ := util.ReadyCmd() - proxy, _ := ReadSystemProxy() // Set system proxy, if not already - if !proxy.Enabled { - SetSystemProxy(true) + proxy, err := ReadSystemProxy() + + if err != nil { + log.Fatal(err) } configs, _ := util.ReadConfigs() diff --git a/proxy/unset.go b/proxy/unset.go index f4eade3..b9964d4 100644 --- a/proxy/unset.go +++ b/proxy/unset.go @@ -1,6 +1,8 @@ package proxy import ( + "log" + "github.com/Baipyrus/ProxySwitcher/util" ) @@ -8,7 +10,12 @@ func Unset() { stdin, closeFunc, _ := util.ReadyCmd() // Unset system proxy, if not already - proxy, _ := ReadSystemProxy() + proxy, err := ReadSystemProxy() + + if err != nil { + log.Fatal(err) + } + if proxy.Enabled { SetSystemProxy(false) } From 2628ce2cfd75e11a8d61457b9df7ee73a93eb349 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 8 Sep 2024 21:46:35 +0200 Subject: [PATCH 3/3] additional comments for new proxy registry functions --- proxy/debug.go | 3 +++ proxy/util.go | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/proxy/debug.go b/proxy/debug.go index 5d2cbb8..9827d8c 100644 --- a/proxy/debug.go +++ b/proxy/debug.go @@ -27,6 +27,7 @@ func Debug() { log.Fatal(err) } + // Debug system proxy configuration fmt.Println("\nSystem Proxy:") fmt.Printf("Enabled: %t\n", proxy.Enabled) fmt.Printf("Server: %s\n\n", proxy.Server) @@ -40,10 +41,12 @@ func Debug() { } fmt.Printf("Loading commands for '%s':\n", config.Name) + // Debug Proxy Set Commands setCmds := generateCommands(config.Set, configCmd, "[PROXY PLACEHOLDER]") fmt.Println("Set Commands:") fmt.Printf("%s\n", mapCmdsToStr(setCmds)) + // Debug Proxy Unset Commands unsetCmds := generateCommands(config.Unset, configCmd, "") fmt.Println("Unset Commands:") fmt.Printf("%s\n\n", mapCmdsToStr(unsetCmds)) diff --git a/proxy/util.go b/proxy/util.go index 3ead839..da18bda 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -66,27 +66,33 @@ func generateCommands(variants []*util.Variant, configCmd, proxyServer string) [ } func ReadSystemProxy() (*Proxy, error) { + // Open registry key for internet settings key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE) if err != nil { return nil, err } defer key.Close() + // Read registry value for proxy enabled enableVal, _, err := key.GetIntegerValue("proxyEnable") if err != nil { return nil, err } + // Convert int value to bool enabled := enableVal != 0 + // Read registry value for proxy servers servers, _, err := key.GetStringValue("proxyServer") if err != nil && !errors.Is(err, registry.ErrNotExist) { return nil, err } + // Use entire value if singular server if !strings.ContainsAny(servers, ";=") { return &Proxy{Enabled: enabled, Server: servers}, nil } + // Map proxy servers into dictionary serverSplit := strings.Split(servers, ";") serverDict := make(map[string]string) for _, substr := range serverSplit { @@ -95,24 +101,29 @@ func ReadSystemProxy() (*Proxy, error) { serverDict[key] = value } + // Grab HTTP proxy server first if serverDict["http"] != "" { return &Proxy{Enabled: enabled, Server: serverDict["http"]}, nil } + // Grab HTTP proxy server second if serverDict["https"] != "" { return &Proxy{Enabled: enabled, Server: serverDict["https"]}, nil } - return nil, errors.New("You need configure either HTTP or HTTPS proxy to proceed.") + // Throw error on no usable proxy server + return nil, errors.New("You need to configure either HTTP or HTTPS proxy servers to proceed.") } func SetSystemProxy(state bool) error { + // Open registry key for internet settings key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.SET_VALUE) if err != nil { return err } defer key.Close() + // Get state as int instead of bool var value uint32 if state { value = 1 @@ -120,6 +131,7 @@ func SetSystemProxy(state bool) error { value = 0 } + // Write registry value to enable/disable proxy err = key.SetDWordValue("proxyEnable", value) return err }