From 7fb94564922c336b2bc0cc5efdc04d898935a76b Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 1 Oct 2024 19:58:41 +0200 Subject: [PATCH 1/6] rename 'readArgs' to better describe variable replacement --- proxy/util.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/proxy/util.go b/proxy/util.go index 8e25fff..389a64c 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -9,7 +9,7 @@ import ( "golang.org/x/sys/windows/registry" ) -func readArgs(replaceVariable bool, args []string, configCmd string) ([]string, string) { +func processVars(replaceVariable bool, args []string, configCmd string) ([]string, string) { var configArgs []string for _, arg := range args { @@ -62,7 +62,7 @@ func generateCommands(variants []*util.Variant, configCmd, proxyServer string) [ for _, variant := range variants { replaceVariable := variant.Type == util.VARIABLE - configArgs, configCmd := readArgs(replaceVariable, variant.Arguments, configCmd) + configArgs, configCmd := processVars(replaceVariable, variant.Arguments, configCmd) configArgs, configCmd = applyProxy(configArgs, configCmd, proxyServer, variant) commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs}) From 06637360d61d8af546849c0cb503add5b4325bbb Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 1 Oct 2024 19:59:34 +0200 Subject: [PATCH 2/6] rename 'applyProxy' to better describe applications of proxy server string --- proxy/util.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/proxy/util.go b/proxy/util.go index 389a64c..c959b29 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -26,7 +26,7 @@ func processVars(replaceVariable bool, args []string, configCmd string) ([]strin return configArgs, configCmd } -func applyProxy(configArgs []string, configCmd, proxyServer string, variant *util.Variant) ([]string, string) { +func injectProxy(configArgs []string, configCmd, proxyServer string, variant *util.Variant) ([]string, string) { // Skip, no proxy provided or proxy option discarded if proxyServer == "" || variant.DiscardProxy { return configArgs, configCmd @@ -63,7 +63,7 @@ func generateCommands(variants []*util.Variant, configCmd, proxyServer string) [ replaceVariable := variant.Type == util.VARIABLE configArgs, configCmd := processVars(replaceVariable, variant.Arguments, configCmd) - configArgs, configCmd = applyProxy(configArgs, configCmd, proxyServer, variant) + configArgs, configCmd = injectProxy(configArgs, configCmd, proxyServer, variant) commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs}) } From 9eb5982bb7a55fb45b2155e1c424be77427a4ca0 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 1 Oct 2024 20:00:31 +0200 Subject: [PATCH 3/6] rename 'isVariableType' to standardized boolean meaning of variable --- proxy/util.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/proxy/util.go b/proxy/util.go index c959b29..3569977 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -9,12 +9,12 @@ import ( "golang.org/x/sys/windows/registry" ) -func processVars(replaceVariable bool, args []string, configCmd string) ([]string, string) { +func processVars(isVariableType bool, args []string, configCmd string) ([]string, string) { var configArgs []string for _, arg := range args { // If not replacable, append - if !replaceVariable { + if !isVariableType { configArgs = append(configArgs, arg) continue } @@ -60,9 +60,9 @@ func generateCommands(variants []*util.Variant, configCmd, proxyServer string) [ // Generate one command per variant for _, variant := range variants { - replaceVariable := variant.Type == util.VARIABLE + isVariableType := variant.Type == util.VARIABLE - configArgs, configCmd := processVars(replaceVariable, variant.Arguments, configCmd) + configArgs, configCmd := processVars(isVariableType, variant.Arguments, configCmd) configArgs, configCmd = injectProxy(configArgs, configCmd, proxyServer, variant) commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs}) From 024535de6f8bd748ed7b067cdddae1e30ff4999e Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 1 Oct 2024 20:09:54 +0200 Subject: [PATCH 4/6] modify base signature of 'generateCommands' to avoid overused variables --- proxy/debug.go | 4 ++-- proxy/set.go | 2 +- proxy/unset.go | 2 +- proxy/util.go | 12 +++++++++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/proxy/debug.go b/proxy/debug.go index 45a12c2..bac5a74 100644 --- a/proxy/debug.go +++ b/proxy/debug.go @@ -51,12 +51,12 @@ func Debug(cfgPath string) { fmt.Printf("Loading commands for '%s':\n", config.Name) // Debug Proxy Set Commands - setCmds := generateCommands(config.Set, configCmd, "[PROXY PLACEHOLDER]") + setCmds := generateCommands(configCmd, config.Set, "[PROXY PLACEHOLDER]") fmt.Println("Set Commands:") fmt.Printf("%s\n", mapCmdsToStr(setCmds)) // Debug Proxy Unset Commands - unsetCmds := generateCommands(config.Unset, configCmd, "") + unsetCmds := generateCommands(configCmd, config.Unset, "") fmt.Println("Unset Commands:") fmt.Printf("%s\n\n", mapCmdsToStr(unsetCmds)) } diff --git a/proxy/set.go b/proxy/set.go index 4621165..b4778e3 100644 --- a/proxy/set.go +++ b/proxy/set.go @@ -21,7 +21,7 @@ func Set(cfgPath string) { configCmd = config.Cmd } - commands := generateCommands(config.Set, configCmd, proxy.Server) + commands := generateCommands(configCmd, config.Set, proxy.Server) util.ExecCmds(commands, stdin) } diff --git a/proxy/unset.go b/proxy/unset.go index 425c41f..7dc21b8 100644 --- a/proxy/unset.go +++ b/proxy/unset.go @@ -21,7 +21,7 @@ func Unset(cfgPath string) { configCmd = config.Cmd } - commands := generateCommands(config.Unset, configCmd, "") + commands := generateCommands(configCmd, config.Unset, "") util.ExecCmds(commands, stdin) } diff --git a/proxy/util.go b/proxy/util.go index 3569977..5da197d 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -55,14 +55,20 @@ func injectProxy(configArgs []string, configCmd, proxyServer string, variant *ut return configArgs, configCmd } -func generateCommands(variants []*util.Variant, configCmd, proxyServer string) []*util.Command { +func generateCommands(base string, variants []*util.Variant, proxyServer string) []*util.Command { var commands []*util.Command - // Generate one command per variant + // Iterate through all variants and generate a command for each for _, variant := range variants { isVariableType := variant.Type == util.VARIABLE - configArgs, configCmd := processVars(isVariableType, variant.Arguments, configCmd) + // Create command from default parameters + cmd := &util.Command{ + Name: base, + Arguments: append([]string{}, variant.Arguments...), + } + + configArgs, configCmd := processVars(isVariableType, cmd.Arguments, cmd.Name) configArgs, configCmd = injectProxy(configArgs, configCmd, proxyServer, variant) commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs}) From 39a0a6d828807046066f708c9fa649d15c1e9a4a Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 1 Oct 2024 20:13:47 +0200 Subject: [PATCH 5/6] passing a command by reference to 'processVars' instead of return types --- proxy/util.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/proxy/util.go b/proxy/util.go index 5da197d..8e7a119 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -9,21 +9,16 @@ import ( "golang.org/x/sys/windows/registry" ) -func processVars(isVariableType bool, args []string, configCmd string) ([]string, string) { - var configArgs []string - - for _, arg := range args { - // If not replacable, append - if !isVariableType { - configArgs = append(configArgs, arg) - continue - } - - // Replace specific "ProxySwitcher Argument" in command - configCmd = strings.Replace(configCmd, "$PRSW_ARG", arg, 1) +func processVars(cmd *util.Command, isVariableType bool) { + // If not a variable type, return early as there's nothing to replace + if !isVariableType { + return } - return configArgs, configCmd + // Replace specific $PRSW_ARG in the command's Name with each argument + for _, arg := range cmd.Arguments { + cmd.Name = strings.Replace(cmd.Name, "$PRSW_ARG", arg, 1) + } } func injectProxy(configArgs []string, configCmd, proxyServer string, variant *util.Variant) ([]string, string) { @@ -68,8 +63,8 @@ func generateCommands(base string, variants []*util.Variant, proxyServer string) Arguments: append([]string{}, variant.Arguments...), } - configArgs, configCmd := processVars(isVariableType, cmd.Arguments, cmd.Name) - configArgs, configCmd = injectProxy(configArgs, configCmd, proxyServer, variant) + processVars(cmd, isVariableType) + configArgs, configCmd := injectProxy(cmd.Arguments, cmd.Name, proxyServer, variant) commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs}) } From b865c7c5af2fc6daa17830a2377df0dfd2ceff2b Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 1 Oct 2024 20:19:02 +0200 Subject: [PATCH 6/6] passing a command by reference to 'injectProxy' instead of return types --- proxy/util.go | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/proxy/util.go b/proxy/util.go index 8e7a119..4598ec9 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -21,33 +21,32 @@ func processVars(cmd *util.Command, isVariableType bool) { } } -func injectProxy(configArgs []string, configCmd, proxyServer string, variant *util.Variant) ([]string, string) { - // Skip, no proxy provided or proxy option discarded +func injectProxy(cmd *util.Command, variant *util.Variant, proxyServer string) { + // Skip if no proxy is provided or proxy option is discarded if proxyServer == "" || variant.DiscardProxy { - return configArgs, configCmd + return } - // Surround 'proxyServer' with any given string, if provided + // Surround 'proxyServer' with the provided surround string, if any if variant.Surround != "" { proxyServer = fmt.Sprintf("%[1]s%[2]s%[1]s", variant.Surround, proxyServer) } - // 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 into last place of command if the variant is VARIABLE + if variant.Type == util.VARIABLE && strings.Count(cmd.Name, "$PRSW_ARG") == 1 { + cmd.Name = strings.Replace(cmd.Name, "$PRSW_ARG", proxyServer, 1) + return } - // Insert proxy right after equator + // Insert proxy after the equator if specified if variant.Equator != "" { - configArgs[len(configArgs)-1] += variant.Equator + proxyServer - return configArgs, configCmd + lastArgIdx := len(cmd.Arguments) - 1 + cmd.Arguments[lastArgIdx] += variant.Equator + proxyServer + return } - // Or otherwise just append it as an argument - configArgs = append(configArgs, proxyServer) - - return configArgs, configCmd + // Otherwise, append the proxy as a new argument + cmd.Arguments = append(cmd.Arguments, proxyServer) } func generateCommands(base string, variants []*util.Variant, proxyServer string) []*util.Command { @@ -64,9 +63,9 @@ func generateCommands(base string, variants []*util.Variant, proxyServer string) } processVars(cmd, isVariableType) - configArgs, configCmd := injectProxy(cmd.Arguments, cmd.Name, proxyServer, variant) + injectProxy(cmd, variant, proxyServer) - commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs}) + commands = append(commands, cmd) } return commands