mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 20:51:45 +00:00
Merge pull request #11 from Baipyrus/detect-sysconfig-protocol
Detecting System Proxy From Different Protocols
This commit is contained in:
commit
77532b6df9
@ -2,6 +2,7 @@ package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/Baipyrus/ProxySwitcher/util"
|
||||
@ -20,7 +21,13 @@ func mapCmdsToStr(commands []*util.Command) string {
|
||||
}
|
||||
|
||||
func Debug() {
|
||||
proxy, _ := ReadSystemProxy()
|
||||
proxy, err := ReadSystemProxy()
|
||||
|
||||
if err != nil {
|
||||
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)
|
||||
@ -34,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))
|
||||
|
@ -1,16 +1,19 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/Baipyrus/ProxySwitcher/util"
|
||||
)
|
||||
|
||||
func Set() {
|
||||
stdin, closeFunc, _ := util.ReadyCmd()
|
||||
|
||||
proxy, _ := ReadSystemProxy()
|
||||
// Set system proxy, if not already
|
||||
if !proxy.Enabled {
|
||||
SetSystemProxy(true)
|
||||
proxy, err := ReadSystemProxy()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
configs, _ := util.ReadConfigs()
|
||||
|
@ -1,6 +1,8 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/Baipyrus/ProxySwitcher/util"
|
||||
)
|
||||
|
||||
@ -8,7 +10,12 @@ func Unset() {
|
||||
stdin, closeFunc, _ := util.ReadyCmd()
|
||||
|
||||
// Unset system proxy, if not already
|
||||
proxy, _ := ReadSystemProxy()
|
||||
proxy, err := ReadSystemProxy()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if proxy.Enabled {
|
||||
SetSystemProxy(false)
|
||||
}
|
||||
|
@ -66,32 +66,64 @@ 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()
|
||||
|
||||
enabled, _, err := key.GetIntegerValue("proxyEnable")
|
||||
// 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
|
||||
|
||||
server, _, err := key.GetStringValue("proxyServer")
|
||||
// Read registry value for proxy servers
|
||||
servers, _, err := key.GetStringValue("proxyServer")
|
||||
if err != nil && !errors.Is(err, registry.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Proxy{Enabled: enabled != 0, Server: server}, nil
|
||||
// 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 {
|
||||
subSplit := strings.Split(substr, "=")
|
||||
key, value := subSplit[0], subSplit[1]
|
||||
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
|
||||
}
|
||||
|
||||
// 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
|
||||
@ -99,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