mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 04:31:45 +00:00
additional comments for new proxy registry functions
This commit is contained in:
parent
7b09ac0cda
commit
2628ce2cfd
@ -27,6 +27,7 @@ func Debug() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Debug system proxy configuration
|
||||
fmt.Println("\nSystem Proxy:")
|
||||
fmt.Printf("Enabled: %t\n", proxy.Enabled)
|
||||
fmt.Printf("Server: %s\n\n", proxy.Server)
|
||||
@ -40,10 +41,12 @@ func Debug() {
|
||||
}
|
||||
fmt.Printf("Loading commands for '%s':\n", config.Name)
|
||||
|
||||
// Debug Proxy Set Commands
|
||||
setCmds := generateCommands(config.Set, configCmd, "[PROXY PLACEHOLDER]")
|
||||
fmt.Println("Set Commands:")
|
||||
fmt.Printf("%s\n", mapCmdsToStr(setCmds))
|
||||
|
||||
// Debug Proxy Unset Commands
|
||||
unsetCmds := generateCommands(config.Unset, configCmd, "")
|
||||
fmt.Println("Unset Commands:")
|
||||
fmt.Printf("%s\n\n", mapCmdsToStr(unsetCmds))
|
||||
|
@ -66,27 +66,33 @@ func generateCommands(variants []*util.Variant, configCmd, proxyServer string) [
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer key.Close()
|
||||
|
||||
// Read registry value for proxy enabled
|
||||
enableVal, _, err := key.GetIntegerValue("proxyEnable")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Convert int value to bool
|
||||
enabled := enableVal != 0
|
||||
|
||||
// Read registry value for proxy servers
|
||||
servers, _, err := key.GetStringValue("proxyServer")
|
||||
if err != nil && !errors.Is(err, registry.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Use entire value if singular server
|
||||
if !strings.ContainsAny(servers, ";=") {
|
||||
return &Proxy{Enabled: enabled, Server: servers}, nil
|
||||
}
|
||||
|
||||
// Map proxy servers into dictionary
|
||||
serverSplit := strings.Split(servers, ";")
|
||||
serverDict := make(map[string]string)
|
||||
for _, substr := range serverSplit {
|
||||
@ -95,24 +101,29 @@ func ReadSystemProxy() (*Proxy, error) {
|
||||
serverDict[key] = value
|
||||
}
|
||||
|
||||
// Grab HTTP proxy server first
|
||||
if serverDict["http"] != "" {
|
||||
return &Proxy{Enabled: enabled, Server: serverDict["http"]}, nil
|
||||
}
|
||||
|
||||
// Grab HTTP proxy server second
|
||||
if serverDict["https"] != "" {
|
||||
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 {
|
||||
// Open registry key for internet settings
|
||||
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
|
||||
var value uint32
|
||||
if state {
|
||||
value = 1
|
||||
@ -120,6 +131,7 @@ func SetSystemProxy(state bool) error {
|
||||
value = 0
|
||||
}
|
||||
|
||||
// Write registry value to enable/disable proxy
|
||||
err = key.SetDWordValue("proxyEnable", value)
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user