2024-08-31 16:15:50 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-09-23 16:45:27 +00:00
|
|
|
"fmt"
|
2024-08-31 16:27:24 +00:00
|
|
|
"strings"
|
2024-08-31 16:15:50 +00:00
|
|
|
|
2024-08-31 16:27:24 +00:00
|
|
|
"github.com/Baipyrus/ProxySwitcher/util"
|
2024-08-31 16:15:50 +00:00
|
|
|
"golang.org/x/sys/windows/registry"
|
|
|
|
)
|
|
|
|
|
2024-10-01 18:00:31 +00:00
|
|
|
func processVars(isVariableType bool, args []string, configCmd string) ([]string, string) {
|
2024-08-31 16:27:24 +00:00
|
|
|
var configArgs []string
|
|
|
|
|
|
|
|
for _, arg := range args {
|
2024-08-31 17:13:01 +00:00
|
|
|
// If not replacable, append
|
2024-10-01 18:00:31 +00:00
|
|
|
if !isVariableType {
|
2024-08-31 16:27:24 +00:00
|
|
|
configArgs = append(configArgs, arg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-31 17:13:01 +00:00
|
|
|
// Replace specific "ProxySwitcher Argument" in command
|
2024-08-31 16:27:24 +00:00
|
|
|
configCmd = strings.Replace(configCmd, "$PRSW_ARG", arg, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return configArgs, configCmd
|
|
|
|
}
|
|
|
|
|
2024-10-01 17:59:34 +00:00
|
|
|
func injectProxy(configArgs []string, configCmd, proxyServer string, variant *util.Variant) ([]string, string) {
|
2024-09-12 21:17:41 +00:00
|
|
|
// Skip, no proxy provided or proxy option discarded
|
|
|
|
if proxyServer == "" || variant.DiscardProxy {
|
2024-08-31 16:27:24 +00:00
|
|
|
return configArgs, configCmd
|
|
|
|
}
|
|
|
|
|
2024-09-23 16:45:27 +00:00
|
|
|
// Surround 'proxyServer' with any given string, if provided
|
|
|
|
if variant.Surround != "" {
|
|
|
|
proxyServer = fmt.Sprintf("%[1]s%[2]s%[1]s", variant.Surround, proxyServer)
|
|
|
|
}
|
|
|
|
|
2024-08-31 17:13:01 +00:00
|
|
|
// Insert proxy only on last VARIABLE type
|
2024-08-31 16:27:24 +00:00
|
|
|
if variant.Type == util.VARIABLE && strings.Count(configCmd, "$PRSW_ARG") == 1 {
|
|
|
|
configCmd = strings.Replace(configCmd, "$PRSW_ARG", proxyServer, 1)
|
|
|
|
return configArgs, configCmd
|
|
|
|
}
|
|
|
|
|
2024-08-31 17:13:01 +00:00
|
|
|
// Insert proxy right after equator
|
2024-08-31 16:27:24 +00:00
|
|
|
if variant.Equator != "" {
|
|
|
|
configArgs[len(configArgs)-1] += variant.Equator + proxyServer
|
|
|
|
return configArgs, configCmd
|
|
|
|
}
|
|
|
|
|
2024-08-31 17:13:01 +00:00
|
|
|
// Or otherwise just append it as an argument
|
2024-08-31 16:27:24 +00:00
|
|
|
configArgs = append(configArgs, proxyServer)
|
|
|
|
|
|
|
|
return configArgs, configCmd
|
|
|
|
}
|
|
|
|
|
2024-08-31 17:13:01 +00:00
|
|
|
func generateCommands(variants []*util.Variant, configCmd, proxyServer string) []*util.Command {
|
2024-08-31 16:27:24 +00:00
|
|
|
var commands []*util.Command
|
|
|
|
|
2024-08-31 17:13:01 +00:00
|
|
|
// Generate one command per variant
|
2024-08-31 16:27:24 +00:00
|
|
|
for _, variant := range variants {
|
2024-10-01 18:00:31 +00:00
|
|
|
isVariableType := variant.Type == util.VARIABLE
|
2024-08-31 16:27:24 +00:00
|
|
|
|
2024-10-01 18:00:31 +00:00
|
|
|
configArgs, configCmd := processVars(isVariableType, variant.Arguments, configCmd)
|
2024-10-01 17:59:34 +00:00
|
|
|
configArgs, configCmd = injectProxy(configArgs, configCmd, proxyServer, variant)
|
2024-08-31 16:27:24 +00:00
|
|
|
|
|
|
|
commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs})
|
|
|
|
}
|
|
|
|
|
|
|
|
return commands
|
|
|
|
}
|
|
|
|
|
2024-08-31 16:15:50 +00:00
|
|
|
func ReadSystemProxy() (*Proxy, error) {
|
2024-09-08 19:46:35 +00:00
|
|
|
// 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()
|
|
|
|
|
2024-09-08 19:46:35 +00:00
|
|
|
// Read registry value for proxy enabled
|
2024-09-08 19:06:31 +00:00
|
|
|
enableVal, _, err := key.GetIntegerValue("proxyEnable")
|
2024-08-31 16:15:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-08 19:46:35 +00:00
|
|
|
// Convert int value to bool
|
2024-09-08 19:06:31 +00:00
|
|
|
enabled := enableVal != 0
|
2024-08-31 16:15:50 +00:00
|
|
|
|
2024-09-08 19:46:35 +00:00
|
|
|
// Read registry value for proxy servers
|
2024-09-08 19:06:31 +00:00
|
|
|
servers, _, err := key.GetStringValue("proxyServer")
|
2024-08-31 16:15:50 +00:00
|
|
|
if err != nil && !errors.Is(err, registry.ErrNotExist) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-09-08 19:46:35 +00:00
|
|
|
// Use entire value if singular server
|
2024-09-12 21:39:02 +00:00
|
|
|
proxy := &Proxy{Enabled: enabled, Server: ""}
|
2024-09-08 19:06:31 +00:00
|
|
|
if !strings.ContainsAny(servers, ";=") {
|
2024-09-12 21:39:02 +00:00
|
|
|
proxy.Server = servers
|
|
|
|
return proxy, nil
|
2024-09-08 19:06:31 +00:00
|
|
|
}
|
|
|
|
|
2024-09-08 19:46:35 +00:00
|
|
|
// Map proxy servers into dictionary
|
2024-09-08 19:06:31 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-08 19:46:35 +00:00
|
|
|
// Grab HTTP proxy server first
|
2024-09-08 19:06:31 +00:00
|
|
|
if serverDict["http"] != "" {
|
2024-09-12 21:39:02 +00:00
|
|
|
proxy.Server = serverDict["http"]
|
|
|
|
return proxy, nil
|
2024-09-08 19:06:31 +00:00
|
|
|
}
|
|
|
|
|
2024-09-08 19:46:35 +00:00
|
|
|
// Grab HTTP proxy server second
|
2024-09-08 19:06:31 +00:00
|
|
|
if serverDict["https"] != "" {
|
2024-09-12 21:39:02 +00:00
|
|
|
proxy.Server = serverDict["https"]
|
|
|
|
return proxy, nil
|
2024-09-08 19:06:31 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 21:39:02 +00:00
|
|
|
// Return with empty proxy server ("discarded"; not detected)
|
|
|
|
return proxy, nil
|
2024-08-31 16:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetSystemProxy(state bool) error {
|
2024-09-08 19:46:35 +00:00
|
|
|
// 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()
|
|
|
|
|
2024-09-08 19:46:35 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-09-08 19:46:35 +00:00
|
|
|
// 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
|
|
|
}
|