additional comments for new proxy registry functions

This commit is contained in:
Baipyrus 2024-09-08 21:46:35 +02:00
parent 7b09ac0cda
commit 2628ce2cfd
2 changed files with 16 additions and 1 deletions

View File

@ -27,6 +27,7 @@ func Debug() {
log.Fatal(err) log.Fatal(err)
} }
// Debug system proxy configuration
fmt.Println("\nSystem Proxy:") fmt.Println("\nSystem Proxy:")
fmt.Printf("Enabled: %t\n", proxy.Enabled) fmt.Printf("Enabled: %t\n", proxy.Enabled)
fmt.Printf("Server: %s\n\n", proxy.Server) fmt.Printf("Server: %s\n\n", proxy.Server)
@ -40,10 +41,12 @@ func Debug() {
} }
fmt.Printf("Loading commands for '%s':\n", config.Name) fmt.Printf("Loading commands for '%s':\n", config.Name)
// Debug Proxy Set Commands
setCmds := generateCommands(config.Set, configCmd, "[PROXY PLACEHOLDER]") setCmds := generateCommands(config.Set, configCmd, "[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
unsetCmds := generateCommands(config.Unset, configCmd, "") unsetCmds := generateCommands(config.Unset, configCmd, "")
fmt.Println("Unset Commands:") fmt.Println("Unset Commands:")
fmt.Printf("%s\n\n", mapCmdsToStr(unsetCmds)) fmt.Printf("%s\n\n", mapCmdsToStr(unsetCmds))

View File

@ -66,27 +66,33 @@ func generateCommands(variants []*util.Variant, configCmd, proxyServer string) [
} }
func ReadSystemProxy() (*Proxy, error) { func ReadSystemProxy() (*Proxy, error) {
// Open registry key for internet settings
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 {
return nil, err return nil, err
} }
defer key.Close() defer key.Close()
// Read registry value for proxy enabled
enableVal, _, err := key.GetIntegerValue("proxyEnable") enableVal, _, err := key.GetIntegerValue("proxyEnable")
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Convert int value to bool
enabled := enableVal != 0 enabled := enableVal != 0
// Read registry value for proxy servers
servers, _, err := key.GetStringValue("proxyServer") servers, _, err := key.GetStringValue("proxyServer")
if err != nil && !errors.Is(err, registry.ErrNotExist) { if err != nil && !errors.Is(err, registry.ErrNotExist) {
return nil, err return nil, err
} }
// Use entire value if singular server
if !strings.ContainsAny(servers, ";=") { if !strings.ContainsAny(servers, ";=") {
return &Proxy{Enabled: enabled, Server: servers}, nil return &Proxy{Enabled: enabled, Server: servers}, nil
} }
// Map proxy servers into dictionary
serverSplit := strings.Split(servers, ";") serverSplit := strings.Split(servers, ";")
serverDict := make(map[string]string) serverDict := make(map[string]string)
for _, substr := range serverSplit { for _, substr := range serverSplit {
@ -95,24 +101,29 @@ func ReadSystemProxy() (*Proxy, error) {
serverDict[key] = value serverDict[key] = value
} }
// Grab HTTP proxy server first
if serverDict["http"] != "" { if serverDict["http"] != "" {
return &Proxy{Enabled: enabled, Server: serverDict["http"]}, nil return &Proxy{Enabled: enabled, Server: serverDict["http"]}, nil
} }
// Grab HTTP proxy server second
if serverDict["https"] != "" { if serverDict["https"] != "" {
return &Proxy{Enabled: enabled, Server: serverDict["https"]}, nil return &Proxy{Enabled: enabled, Server: serverDict["https"]}, nil
} }
return nil, errors.New("You need configure either HTTP or HTTPS proxy to proceed.") // Throw error on no usable proxy server
return nil, errors.New("You need to configure either HTTP or HTTPS proxy servers to proceed.")
} }
func SetSystemProxy(state bool) error { func SetSystemProxy(state bool) error {
// Open registry key for internet settings
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.SET_VALUE) key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.SET_VALUE)
if err != nil { if err != nil {
return err return err
} }
defer key.Close() defer key.Close()
// Get state as int instead of bool
var value uint32 var value uint32
if state { if state {
value = 1 value = 1
@ -120,6 +131,7 @@ func SetSystemProxy(state bool) error {
value = 0 value = 0
} }
// Write registry value to enable/disable proxy
err = key.SetDWordValue("proxyEnable", value) err = key.SetDWordValue("proxyEnable", value)
return err return err
} }