generic implementation of config to command conversion

This commit is contained in:
Baipyrus 2024-08-31 18:27:24 +02:00
parent bb304b0bf7
commit 60c915b389

View File

@ -2,10 +2,63 @@ package proxy
import ( import (
"errors" "errors"
"strings"
"github.com/Baipyrus/ProxySwitcher/util"
"golang.org/x/sys/windows/registry" "golang.org/x/sys/windows/registry"
) )
func readArgs(replaceVariable bool, args []string, configCmd string) ([]string, string) {
var configArgs []string
for _, arg := range args {
if !replaceVariable {
configArgs = append(configArgs, arg)
continue
}
configCmd = strings.Replace(configCmd, "$PRSW_ARG", arg, 1)
}
return configArgs, configCmd
}
func applyProxy(configArgs []string, configCmd string, proxyServer string, variant *util.Variant) ([]string, string) {
if proxyServer == "" {
return configArgs, configCmd
}
if variant.Type == util.VARIABLE && strings.Count(configCmd, "$PRSW_ARG") == 1 {
configCmd = strings.Replace(configCmd, "$PRSW_ARG", proxyServer, 1)
return configArgs, configCmd
}
if variant.Equator != "" {
configArgs[len(configArgs)-1] += variant.Equator + proxyServer
return configArgs, configCmd
}
configArgs = append(configArgs, proxyServer)
return configArgs, configCmd
}
func getVariants(variants []*util.Variant, configCmd, proxyServer string) []*util.Command {
var commands []*util.Command
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})
}
return commands
}
func ReadSystemProxy() (*Proxy, error) { func ReadSystemProxy() (*Proxy, error) {
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE) key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE)
if err != nil { if err != nil {