diff --git a/proxy/debug.go b/proxy/debug.go index 9827d8c..8430d06 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,16 +20,15 @@ func mapCmdsToStr(commands []*util.Command) string { } func Debug() { - proxy, err := ReadSystemProxy() - - if err != nil { - log.Fatal(err) + proxy, _ := ReadSystemProxy() + proxyServer := proxy.Server + if proxyServer == "" { + proxyServer = "[N/A]" } - // Debug system proxy configuration 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/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) } diff --git a/proxy/util.go b/proxy/util.go index 4082b3f..59f0e14 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 } @@ -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 { 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 {