From 9b4fb7af4da50b68aa143585a021718d475b0b4d Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sat, 31 Aug 2024 19:13:01 +0200 Subject: [PATCH] additional commenting for better readability --- proxy/set.go | 16 ++++------------ proxy/unset.go | 16 ++++------------ proxy/util.go | 10 ++++++++-- util/util.go | 19 +++++++++++++++++-- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/proxy/set.go b/proxy/set.go index 618b222..00c0b7f 100644 --- a/proxy/set.go +++ b/proxy/set.go @@ -1,9 +1,6 @@ package proxy import ( - "fmt" - "strings" - "github.com/Baipyrus/ProxySwitcher/util" ) @@ -11,6 +8,7 @@ func Set() { stdin, closeFunc, _ := util.ReadyCmd() proxy, _ := ReadSystemProxy() + // Set system proxy, if not already if !proxy.Enabled { SetSystemProxy(true) } @@ -18,19 +16,13 @@ func Set() { configs, _ := util.ReadConfigs() for _, config := range configs { configCmd := config.Name + // Use command instead of name, if given if config.Cmd != "" { configCmd = config.Cmd } - commands := getVariants(config.Set, configCmd, proxy.Server) - - for _, command := range commands { - cmdArgs := strings.Join(command.Arguments, " ") - cmdStr := fmt.Sprintf("%s %s", command.Name, cmdArgs) - - fmt.Printf("%s\n", cmdStr) - fmt.Fprintln(*stdin, cmdStr) - } + commands := generateCommands(config.Set, configCmd, proxy.Server) + util.ExecCmds(commands, stdin) } closeFunc() diff --git a/proxy/unset.go b/proxy/unset.go index af05745..f4eade3 100644 --- a/proxy/unset.go +++ b/proxy/unset.go @@ -1,15 +1,13 @@ package proxy import ( - "fmt" - "strings" - "github.com/Baipyrus/ProxySwitcher/util" ) func Unset() { stdin, closeFunc, _ := util.ReadyCmd() + // Unset system proxy, if not already proxy, _ := ReadSystemProxy() if proxy.Enabled { SetSystemProxy(false) @@ -18,19 +16,13 @@ func Unset() { configs, _ := util.ReadConfigs() for _, config := range configs { configCmd := config.Name + // Use command instead of name, if given if config.Cmd != "" { configCmd = config.Cmd } - commands := getVariants(config.Unset, configCmd, "") - - for _, command := range commands { - cmdArgs := strings.Join(command.Arguments, " ") - cmdStr := fmt.Sprintf("%s %s", command.Name, cmdArgs) - - fmt.Printf("%s\n", cmdStr) - fmt.Fprintln(*stdin, cmdStr) - } + commands := generateCommands(config.Unset, configCmd, "") + util.ExecCmds(commands, stdin) } closeFunc() diff --git a/proxy/util.go b/proxy/util.go index f5760de..bbc1e7d 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -12,11 +12,13 @@ func readArgs(replaceVariable bool, args []string, configCmd string) ([]string, var configArgs []string for _, arg := range args { + // If not replacable, append if !replaceVariable { configArgs = append(configArgs, arg) continue } + // Replace specific "ProxySwitcher Argument" in command configCmd = strings.Replace(configCmd, "$PRSW_ARG", arg, 1) } @@ -24,33 +26,37 @@ func readArgs(replaceVariable bool, args []string, configCmd string) ([]string, } func applyProxy(configArgs []string, configCmd string, proxyServer string, variant *util.Variant) ([]string, string) { + // Skip, no proxy provided if proxyServer == "" { return configArgs, configCmd } + // Insert proxy only on last VARIABLE type if variant.Type == util.VARIABLE && strings.Count(configCmd, "$PRSW_ARG") == 1 { configCmd = strings.Replace(configCmd, "$PRSW_ARG", proxyServer, 1) return configArgs, configCmd } + // Insert proxy right after equator if variant.Equator != "" { configArgs[len(configArgs)-1] += variant.Equator + proxyServer return configArgs, configCmd } + // Or otherwise just append it as an argument configArgs = append(configArgs, proxyServer) return configArgs, configCmd } -func getVariants(variants []*util.Variant, configCmd, proxyServer string) []*util.Command { +func generateCommands(variants []*util.Variant, configCmd, proxyServer string) []*util.Command { var commands []*util.Command + // Generate one command per variant for _, variant := range variants { replaceVariable := variant.Type == util.VARIABLE configArgs, configCmd := readArgs(replaceVariable, variant.Arguments, configCmd) - configArgs, configCmd = applyProxy(configArgs, configCmd, proxyServer, variant) commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs}) diff --git a/util/util.go b/util/util.go index 7b04e07..a946fab 100644 --- a/util/util.go +++ b/util/util.go @@ -2,10 +2,13 @@ package util import ( "bytes" + "fmt" "io" "os/exec" + "strings" ) +// Create a single powershell process and leave closing, input and output open func ReadyCmd() (*io.WriteCloser, func() error, error) { cmd := exec.Command("powershell", "-NoLogo", "-NoProfile", "-Command", "-") @@ -18,17 +21,29 @@ func ReadyCmd() (*io.WriteCloser, func() error, error) { return nil, nil, err } - if err := cmd.Start(); err != nil { + err = cmd.Start() + if err != nil { return nil, nil, err } return &stdin, func() error { + // Close stdin pipe stdin.Close() - if err := cmd.Wait(); err != nil { + // Wait for command to flush + err := cmd.Wait() + if err != nil { return err } return nil }, nil } + +func ExecCmds(commands []*Command, stdin *io.WriteCloser) { + for _, command := range commands { + cmdArgs := strings.Join(command.Arguments, " ") + cmdStr := fmt.Sprintf("%s %s", command.Name, cmdArgs) + fmt.Fprintln(*stdin, cmdStr) + } +}