mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 12:41:45 +00:00
Merge pull request #13 from Baipyrus/apply-proxy-option
Discard Proxy Specified In Variant Option
This commit is contained in:
commit
4c890ff574
@ -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 {
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user