mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 12:41:45 +00:00
generic implementation of config to command conversion
This commit is contained in:
parent
bb304b0bf7
commit
60c915b389
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user