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 8e25fff..4598ec9 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -9,63 +9,63 @@ import ( "golang.org/x/sys/windows/registry" ) -func readArgs(replaceVariable bool, args []string, configCmd string) ([]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) +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 applyProxy(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(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 { - replaceVariable := variant.Type == util.VARIABLE + isVariableType := variant.Type == util.VARIABLE - configArgs, configCmd := readArgs(replaceVariable, variant.Arguments, configCmd) - configArgs, configCmd = applyProxy(configArgs, configCmd, proxyServer, variant) + // Create command from default parameters + cmd := &util.Command{ + Name: base, + Arguments: append([]string{}, variant.Arguments...), + } - commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs}) + processVars(cmd, isVariableType) + injectProxy(cmd, variant, proxyServer) + + commands = append(commands, cmd) } return commands