mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 12:41:45 +00:00
Merge pull request #21 from Baipyrus/refactor-proxy-util
Refactor proxy/util.go Code For Readability
This commit is contained in:
commit
d1f88f54e1
@ -51,12 +51,12 @@ func Debug(cfgPath string) {
|
|||||||
fmt.Printf("Loading commands for '%s':\n", config.Name)
|
fmt.Printf("Loading commands for '%s':\n", config.Name)
|
||||||
|
|
||||||
// Debug Proxy Set Commands
|
// Debug Proxy Set Commands
|
||||||
setCmds := generateCommands(config.Set, configCmd, "[PROXY PLACEHOLDER]")
|
setCmds := generateCommands(configCmd, config.Set, "[PROXY PLACEHOLDER]")
|
||||||
fmt.Println("Set Commands:")
|
fmt.Println("Set Commands:")
|
||||||
fmt.Printf("%s\n", mapCmdsToStr(setCmds))
|
fmt.Printf("%s\n", mapCmdsToStr(setCmds))
|
||||||
|
|
||||||
// Debug Proxy Unset Commands
|
// Debug Proxy Unset Commands
|
||||||
unsetCmds := generateCommands(config.Unset, configCmd, "")
|
unsetCmds := generateCommands(configCmd, config.Unset, "")
|
||||||
fmt.Println("Unset Commands:")
|
fmt.Println("Unset Commands:")
|
||||||
fmt.Printf("%s\n\n", mapCmdsToStr(unsetCmds))
|
fmt.Printf("%s\n\n", mapCmdsToStr(unsetCmds))
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ func Set(cfgPath string) {
|
|||||||
configCmd = config.Cmd
|
configCmd = config.Cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
commands := generateCommands(config.Set, configCmd, proxy.Server)
|
commands := generateCommands(configCmd, config.Set, proxy.Server)
|
||||||
util.ExecCmds(commands, stdin)
|
util.ExecCmds(commands, stdin)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ func Unset(cfgPath string) {
|
|||||||
configCmd = config.Cmd
|
configCmd = config.Cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
commands := generateCommands(config.Unset, configCmd, "")
|
commands := generateCommands(configCmd, config.Unset, "")
|
||||||
util.ExecCmds(commands, stdin)
|
util.ExecCmds(commands, stdin)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,63 +9,63 @@ import (
|
|||||||
"golang.org/x/sys/windows/registry"
|
"golang.org/x/sys/windows/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
func readArgs(replaceVariable bool, args []string, configCmd string) ([]string, string) {
|
func processVars(cmd *util.Command, isVariableType bool) {
|
||||||
var configArgs []string
|
// If not a variable type, return early as there's nothing to replace
|
||||||
|
if !isVariableType {
|
||||||
for _, arg := range args {
|
return
|
||||||
// If not replacable, append
|
|
||||||
if !replaceVariable {
|
|
||||||
configArgs = append(configArgs, arg)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace specific "ProxySwitcher Argument" in command
|
// Replace specific $PRSW_ARG in the command's Name with each argument
|
||||||
configCmd = strings.Replace(configCmd, "$PRSW_ARG", arg, 1)
|
for _, arg := range cmd.Arguments {
|
||||||
|
cmd.Name = strings.Replace(cmd.Name, "$PRSW_ARG", arg, 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return configArgs, configCmd
|
func injectProxy(cmd *util.Command, variant *util.Variant, proxyServer string) {
|
||||||
}
|
// Skip if no proxy is provided or proxy option is discarded
|
||||||
|
|
||||||
func applyProxy(configArgs []string, configCmd, proxyServer string, variant *util.Variant) ([]string, string) {
|
|
||||||
// Skip, no proxy provided or proxy option discarded
|
|
||||||
if proxyServer == "" || variant.DiscardProxy {
|
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 != "" {
|
if variant.Surround != "" {
|
||||||
proxyServer = fmt.Sprintf("%[1]s%[2]s%[1]s", variant.Surround, proxyServer)
|
proxyServer = fmt.Sprintf("%[1]s%[2]s%[1]s", variant.Surround, proxyServer)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert proxy only on last VARIABLE type
|
// Insert proxy into last place of command if the variant is VARIABLE
|
||||||
if variant.Type == util.VARIABLE && strings.Count(configCmd, "$PRSW_ARG") == 1 {
|
if variant.Type == util.VARIABLE && strings.Count(cmd.Name, "$PRSW_ARG") == 1 {
|
||||||
configCmd = strings.Replace(configCmd, "$PRSW_ARG", proxyServer, 1)
|
cmd.Name = strings.Replace(cmd.Name, "$PRSW_ARG", proxyServer, 1)
|
||||||
return configArgs, configCmd
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert proxy right after equator
|
// Insert proxy after the equator if specified
|
||||||
if variant.Equator != "" {
|
if variant.Equator != "" {
|
||||||
configArgs[len(configArgs)-1] += variant.Equator + proxyServer
|
lastArgIdx := len(cmd.Arguments) - 1
|
||||||
return configArgs, configCmd
|
cmd.Arguments[lastArgIdx] += variant.Equator + proxyServer
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Or otherwise just append it as an argument
|
// Otherwise, append the proxy as a new argument
|
||||||
configArgs = append(configArgs, proxyServer)
|
cmd.Arguments = append(cmd.Arguments, proxyServer)
|
||||||
|
|
||||||
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
|
var commands []*util.Command
|
||||||
|
|
||||||
// Generate one command per variant
|
// Iterate through all variants and generate a command for each
|
||||||
for _, variant := range variants {
|
for _, variant := range variants {
|
||||||
replaceVariable := variant.Type == util.VARIABLE
|
isVariableType := variant.Type == util.VARIABLE
|
||||||
|
|
||||||
configArgs, configCmd := readArgs(replaceVariable, variant.Arguments, configCmd)
|
// Create command from default parameters
|
||||||
configArgs, configCmd = applyProxy(configArgs, configCmd, proxyServer, variant)
|
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
|
return commands
|
||||||
|
Loading…
Reference in New Issue
Block a user