ProxySwitcher/proxy/util.go

148 lines
3.9 KiB
Go
Raw Normal View History

2024-08-31 16:15:50 +00:00
package proxy
import (
"errors"
"fmt"
"strings"
2024-08-31 16:15:50 +00:00
"github.com/Baipyrus/ProxySwitcher/util"
2024-08-31 16:15:50 +00:00
"golang.org/x/sys/windows/registry"
)
func processVars(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)
}
return configArgs, configCmd
}
func applyProxy(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
}
// Surround 'proxyServer' with any given string, if provided
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 right after equator
if variant.Equator != "" {
configArgs[len(configArgs)-1] += variant.Equator + proxyServer
return configArgs, configCmd
}
// Or otherwise just append it as an argument
configArgs = append(configArgs, proxyServer)
return configArgs, configCmd
}
func generateCommands(variants []*util.Variant, configCmd, proxyServer string) []*util.Command {
var commands []*util.Command
// Generate one command per variant
for _, variant := range variants {
replaceVariable := variant.Type == util.VARIABLE
configArgs, configCmd := processVars(replaceVariable, variant.Arguments, configCmd)
configArgs, configCmd = applyProxy(configArgs, configCmd, proxyServer, variant)
commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs})
}
return commands
}
2024-08-31 16:15:50 +00:00
func ReadSystemProxy() (*Proxy, error) {
// Open registry key for internet settings
2024-08-31 16:15:50 +00:00
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE)
if err != nil {
return nil, err
}
defer key.Close()
// Read registry value for proxy enabled
enableVal, _, err := key.GetIntegerValue("proxyEnable")
2024-08-31 16:15:50 +00:00
if err != nil {
return nil, err
}
// Convert int value to bool
enabled := enableVal != 0
2024-08-31 16:15:50 +00:00
// Read registry value for proxy servers
servers, _, err := key.GetStringValue("proxyServer")
2024-08-31 16:15:50 +00:00
if err != nil && !errors.Is(err, registry.ErrNotExist) {
return nil, err
}
// Use entire value if singular server
proxy := &Proxy{Enabled: enabled, Server: ""}
if !strings.ContainsAny(servers, ";=") {
proxy.Server = servers
return proxy, nil
}
// Map proxy servers into dictionary
serverSplit := strings.Split(servers, ";")
serverDict := make(map[string]string)
for _, substr := range serverSplit {
subSplit := strings.Split(substr, "=")
key, value := subSplit[0], subSplit[1]
serverDict[key] = value
}
// Grab HTTP proxy server first
if serverDict["http"] != "" {
proxy.Server = serverDict["http"]
return proxy, nil
}
// Grab HTTP proxy server second
if serverDict["https"] != "" {
proxy.Server = serverDict["https"]
return proxy, nil
}
// Return with empty proxy server ("discarded"; not detected)
return proxy, nil
2024-08-31 16:15:50 +00:00
}
func SetSystemProxy(state bool) error {
// Open registry key for internet settings
2024-08-31 16:15:50 +00:00
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.SET_VALUE)
if err != nil {
return err
}
defer key.Close()
// Get state as int instead of bool
2024-08-31 16:15:50 +00:00
var value uint32
if state {
value = 1
} else {
value = 0
}
// Write registry value to enable/disable proxy
2024-08-31 16:15:50 +00:00
err = key.SetDWordValue("proxyEnable", value)
2024-08-31 19:11:54 +00:00
return err
2024-08-31 16:15:50 +00:00
}