Merge pull request #13 from Baipyrus/apply-proxy-option

Discard Proxy Specified In Variant Option
This commit is contained in:
Baipyrus 2024-09-12 23:52:39 +02:00 committed by GitHub
commit 4c890ff574
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 24 additions and 31 deletions

View File

@ -2,7 +2,6 @@ package proxy
import ( import (
"fmt" "fmt"
"log"
"strings" "strings"
"github.com/Baipyrus/ProxySwitcher/util" "github.com/Baipyrus/ProxySwitcher/util"
@ -21,16 +20,15 @@ func mapCmdsToStr(commands []*util.Command) string {
} }
func Debug() { func Debug() {
proxy, err := ReadSystemProxy() proxy, _ := ReadSystemProxy()
proxyServer := proxy.Server
if err != nil { if proxyServer == "" {
log.Fatal(err) proxyServer = "[N/A]"
} }
// Debug system proxy configuration
fmt.Println("\nSystem Proxy:") fmt.Println("\nSystem Proxy:")
fmt.Printf("Enabled: %t\n", proxy.Enabled) 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() configs, _ := util.ReadConfigs()
for _, config := range configs { for _, config := range configs {

View File

@ -1,19 +1,16 @@
package proxy package proxy
import ( import (
"log"
"github.com/Baipyrus/ProxySwitcher/util" "github.com/Baipyrus/ProxySwitcher/util"
) )
func Set() { func Set() {
stdin, closeFunc, _ := util.ReadyCmd() stdin, closeFunc, _ := util.ReadyCmd()
proxy, _ := ReadSystemProxy()
// Set system proxy, if not already // Set system proxy, if not already
proxy, err := ReadSystemProxy() if !proxy.Enabled {
SetSystemProxy(true)
if err != nil {
log.Fatal(err)
} }
configs, _ := util.ReadConfigs() configs, _ := util.ReadConfigs()

View File

@ -1,8 +1,6 @@
package proxy package proxy
import ( import (
"log"
"github.com/Baipyrus/ProxySwitcher/util" "github.com/Baipyrus/ProxySwitcher/util"
) )
@ -10,12 +8,7 @@ func Unset() {
stdin, closeFunc, _ := util.ReadyCmd() stdin, closeFunc, _ := util.ReadyCmd()
// Unset system proxy, if not already // Unset system proxy, if not already
proxy, err := ReadSystemProxy() proxy, _ := ReadSystemProxy()
if err != nil {
log.Fatal(err)
}
if proxy.Enabled { if proxy.Enabled {
SetSystemProxy(false) SetSystemProxy(false)
} }

View File

@ -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) { func applyProxy(configArgs []string, configCmd, proxyServer string, variant *util.Variant) ([]string, string) {
// Skip, no proxy provided // Skip, no proxy provided or proxy option discarded
if proxyServer == "" { if proxyServer == "" || variant.DiscardProxy {
return configArgs, configCmd return configArgs, configCmd
} }
@ -88,8 +88,10 @@ func ReadSystemProxy() (*Proxy, error) {
} }
// Use entire value if singular server // Use entire value if singular server
proxy := &Proxy{Enabled: enabled, Server: ""}
if !strings.ContainsAny(servers, ";=") { if !strings.ContainsAny(servers, ";=") {
return &Proxy{Enabled: enabled, Server: servers}, nil proxy.Server = servers
return proxy, nil
} }
// Map proxy servers into dictionary // Map proxy servers into dictionary
@ -103,16 +105,18 @@ func ReadSystemProxy() (*Proxy, error) {
// Grab HTTP proxy server first // Grab HTTP proxy server first
if serverDict["http"] != "" { if serverDict["http"] != "" {
return &Proxy{Enabled: enabled, Server: serverDict["http"]}, nil proxy.Server = serverDict["http"]
return proxy, nil
} }
// Grab HTTP proxy server second // Grab HTTP proxy server second
if serverDict["https"] != "" { 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 with empty proxy server ("discarded"; not detected)
return nil, errors.New("You need to configure either HTTP or HTTPS proxy servers to proceed.") return proxy, nil
} }
func SetSystemProxy(state bool) error { func SetSystemProxy(state bool) error {

View File

@ -15,9 +15,10 @@ const (
) )
type Variant struct { type Variant struct {
Arguments []string `json:"args"` Arguments []string `json:"args"`
Type VariantType `json:"type,omitempty"` Type VariantType `json:"type,omitempty"`
Equator string `json:"equator,omitempty"` Equator string `json:"equator,omitempty"`
DiscardProxy bool `json:"discard,omitempty"`
} }
type Command struct { type Command struct {