From b2dda4afbcc1eb7c70abce80fc67a0cab75712c8 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 12 Sep 2024 23:17:41 +0200 Subject: [PATCH 1/3] do not apply proxy if discard option is set --- proxy/util.go | 4 ++-- util/types.go | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/proxy/util.go b/proxy/util.go index 4082b3f..bb74169 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -26,8 +26,8 @@ func readArgs(replaceVariable bool, args []string, configCmd string) ([]string, } func applyProxy(configArgs []string, configCmd, proxyServer string, variant *util.Variant) ([]string, string) { - // Skip, no proxy provided - if proxyServer == "" { + // Skip, no proxy provided or proxy option discarded + if proxyServer == "" || variant.DiscardProxy { return configArgs, configCmd } diff --git a/util/types.go b/util/types.go index 87195c5..5c87de4 100644 --- a/util/types.go +++ b/util/types.go @@ -15,9 +15,10 @@ const ( ) type Variant struct { - Arguments []string `json:"args"` - Type VariantType `json:"type,omitempty"` - Equator string `json:"equator,omitempty"` + Arguments []string `json:"args"` + Type VariantType `json:"type,omitempty"` + Equator string `json:"equator,omitempty"` + DiscardProxy bool `json:"discard,omitempty"` } type Command struct { From 0f285a5534bc79e6396b988f1b36ad43d76751dd Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 12 Sep 2024 23:19:57 +0200 Subject: [PATCH 2/3] Revert "basic proxy error handling; exit on read proxy error" This reverts commit 7b09ac0cda1ba1ceef989d467634b97b7e08b676. --- proxy/debug.go | 9 +-------- proxy/set.go | 9 +++------ proxy/unset.go | 9 +-------- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/proxy/debug.go b/proxy/debug.go index 9827d8c..3e7c29e 100644 --- a/proxy/debug.go +++ b/proxy/debug.go @@ -2,7 +2,6 @@ package proxy import ( "fmt" - "log" "strings" "github.com/Baipyrus/ProxySwitcher/util" @@ -21,13 +20,7 @@ func mapCmdsToStr(commands []*util.Command) string { } func Debug() { - proxy, err := ReadSystemProxy() - - if err != nil { - log.Fatal(err) - } - - // Debug system proxy configuration + proxy, _ := ReadSystemProxy() 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 6892490..00c0b7f 100644 --- a/proxy/set.go +++ b/proxy/set.go @@ -1,19 +1,16 @@ package proxy import ( - "log" - "github.com/Baipyrus/ProxySwitcher/util" ) func Set() { stdin, closeFunc, _ := util.ReadyCmd() + proxy, _ := ReadSystemProxy() // Set system proxy, if not already - proxy, err := ReadSystemProxy() - - if err != nil { - log.Fatal(err) + if !proxy.Enabled { + SetSystemProxy(true) } configs, _ := util.ReadConfigs() diff --git a/proxy/unset.go b/proxy/unset.go index b9964d4..f4eade3 100644 --- a/proxy/unset.go +++ b/proxy/unset.go @@ -1,8 +1,6 @@ package proxy import ( - "log" - "github.com/Baipyrus/ProxySwitcher/util" ) @@ -10,12 +8,7 @@ func Unset() { stdin, closeFunc, _ := util.ReadyCmd() // Unset system proxy, if not already - proxy, err := ReadSystemProxy() - - if err != nil { - log.Fatal(err) - } - + proxy, _ := ReadSystemProxy() if proxy.Enabled { SetSystemProxy(false) } From 531285c8c32f78371163bb7bedeaa27765796006 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 12 Sep 2024 23:39:02 +0200 Subject: [PATCH 3/3] return zero value for proxy server if not detected --- proxy/debug.go | 7 ++++++- proxy/util.go | 14 +++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/proxy/debug.go b/proxy/debug.go index 3e7c29e..8430d06 100644 --- a/proxy/debug.go +++ b/proxy/debug.go @@ -21,9 +21,14 @@ func mapCmdsToStr(commands []*util.Command) string { func Debug() { proxy, _ := ReadSystemProxy() + proxyServer := proxy.Server + if proxyServer == "" { + proxyServer = "[N/A]" + } + fmt.Println("\nSystem Proxy:") fmt.Printf("Enabled: %t\n", proxy.Enabled) - fmt.Printf("Server: %s\n\n", proxy.Server) + fmt.Printf("Server: %s\n\n", proxyServer) configs, _ := util.ReadConfigs() for _, config := range configs { diff --git a/proxy/util.go b/proxy/util.go index bb74169..59f0e14 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -88,8 +88,10 @@ func ReadSystemProxy() (*Proxy, error) { } // Use entire value if singular server + proxy := &Proxy{Enabled: enabled, Server: ""} if !strings.ContainsAny(servers, ";=") { - return &Proxy{Enabled: enabled, Server: servers}, nil + proxy.Server = servers + return proxy, nil } // Map proxy servers into dictionary @@ -103,16 +105,18 @@ func ReadSystemProxy() (*Proxy, error) { // Grab HTTP proxy server first if serverDict["http"] != "" { - return &Proxy{Enabled: enabled, Server: serverDict["http"]}, nil + proxy.Server = serverDict["http"] + return proxy, nil } // Grab HTTP proxy server second if serverDict["https"] != "" { - return &Proxy{Enabled: enabled, Server: serverDict["https"]}, nil + proxy.Server = serverDict["https"] + return proxy, nil } - // Throw error on no usable proxy server - return nil, errors.New("You need to configure either HTTP or HTTPS proxy servers to proceed.") + // Return with empty proxy server ("discarded"; not detected) + return proxy, nil } func SetSystemProxy(state bool) error {