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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Baipyrus/ProxySwitcher/util"
|
"github.com/Baipyrus/ProxySwitcher/util"
|
||||||
@ -20,7 +21,13 @@ func mapCmdsToStr(commands []*util.Command) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Debug() {
|
func Debug() {
|
||||||
proxy, _ := ReadSystemProxy()
|
proxy, err := ReadSystemProxy()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
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)
|
||||||
@ -34,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))
|
||||||
|
@ -1,16 +1,19 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/Baipyrus/ProxySwitcher/util"
|
"github.com/Baipyrus/ProxySwitcher/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Set() {
|
func Set() {
|
||||||
stdin, closeFunc, _ := util.ReadyCmd()
|
stdin, closeFunc, _ := util.ReadyCmd()
|
||||||
|
|
||||||
proxy, _ := ReadSystemProxy()
|
|
||||||
// Set system proxy, if not already
|
// Set system proxy, if not already
|
||||||
if !proxy.Enabled {
|
proxy, err := ReadSystemProxy()
|
||||||
SetSystemProxy(true)
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
configs, _ := util.ReadConfigs()
|
configs, _ := util.ReadConfigs()
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/Baipyrus/ProxySwitcher/util"
|
"github.com/Baipyrus/ProxySwitcher/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -8,7 +10,12 @@ func Unset() {
|
|||||||
stdin, closeFunc, _ := util.ReadyCmd()
|
stdin, closeFunc, _ := util.ReadyCmd()
|
||||||
|
|
||||||
// Unset system proxy, if not already
|
// Unset system proxy, if not already
|
||||||
proxy, _ := ReadSystemProxy()
|
proxy, err := ReadSystemProxy()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
if proxy.Enabled {
|
if proxy.Enabled {
|
||||||
SetSystemProxy(false)
|
SetSystemProxy(false)
|
||||||
}
|
}
|
||||||
|
@ -66,32 +66,64 @@ 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()
|
||||||
|
|
||||||
enabled, _, err := key.GetIntegerValue("proxyEnable")
|
// Read registry value for proxy enabled
|
||||||
|
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
|
||||||
|
|
||||||
server, _, err := key.GetStringValue("proxyServer")
|
// Read registry value for proxy servers
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
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
|
||||||
@ -99,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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user