mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-27 05:01:45 +00:00
Compare commits
No commits in common. "d9df5cad1e3aa92df2f976992ef4b4c72cca2eed" and "a69abda660243a9afeadee49a9a3b9a56b88b8a8" have entirely different histories.
d9df5cad1e
...
a69abda660
@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
|
@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
|
@ -1,5 +1,3 @@
|
||||
//go:build windows
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
|
@ -1,11 +1,8 @@
|
||||
//go:build windows
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -34,11 +31,9 @@ func Debug(cfgPath string) {
|
||||
fmt.Printf("\nConfig:\n")
|
||||
fmt.Printf("%s\n\n", path)
|
||||
|
||||
proxy, err := ReadSystemProxy()
|
||||
proxy, _ := ReadSystemProxy()
|
||||
proxyServer := proxy.Server
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else if proxyServer == "" {
|
||||
if proxyServer == "" {
|
||||
proxyServer = "[N/A]"
|
||||
}
|
||||
|
||||
@ -46,11 +41,7 @@ func Debug(cfgPath string) {
|
||||
fmt.Printf("Enabled: %t\n", proxy.Enabled)
|
||||
fmt.Printf("Server: %s\n\n", proxyServer)
|
||||
|
||||
configs, err := util.ReadConfigs(cfgPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
configs, _ := util.ReadConfigs(cfgPath)
|
||||
for _, config := range configs {
|
||||
configCmd := config.Name
|
||||
// Use command instead of name, if given
|
||||
|
14
proxy/set.go
14
proxy/set.go
@ -1,21 +1,18 @@
|
||||
//go:build windows
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/Baipyrus/ProxySwitcher/util"
|
||||
)
|
||||
|
||||
func Set(cfgPath string) {
|
||||
stdin, closeFunc, _ := util.ReadyCmd()
|
||||
|
||||
proxy, _ := ReadSystemProxy()
|
||||
// Set system proxy, if not already
|
||||
if !proxy.Enabled {
|
||||
SetSystemProxy(true)
|
||||
}
|
||||
|
||||
var failed bool
|
||||
configs, _ := util.ReadConfigs(cfgPath)
|
||||
for _, config := range configs {
|
||||
configCmd := config.Name
|
||||
@ -25,11 +22,8 @@ func Set(cfgPath string) {
|
||||
}
|
||||
|
||||
commands := generateCommands(configCmd, config.Set, proxy.Server)
|
||||
failed = util.ExecCmds(commands)
|
||||
util.ExecCmds(commands, stdin)
|
||||
}
|
||||
|
||||
// Additional feedback on error
|
||||
if failed {
|
||||
log.Printf("One or more commands failed to execute. Run command 'debug' to see more.\n")
|
||||
}
|
||||
closeFunc()
|
||||
}
|
||||
|
@ -1,21 +1,18 @@
|
||||
//go:build windows
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/Baipyrus/ProxySwitcher/util"
|
||||
)
|
||||
|
||||
func Unset(cfgPath string) {
|
||||
stdin, closeFunc, _ := util.ReadyCmd()
|
||||
|
||||
proxy, _ := ReadSystemProxy()
|
||||
// Unset system proxy, if not already
|
||||
if proxy.Enabled {
|
||||
SetSystemProxy(false)
|
||||
}
|
||||
|
||||
var failed bool
|
||||
configs, _ := util.ReadConfigs(cfgPath)
|
||||
for _, config := range configs {
|
||||
configCmd := config.Name
|
||||
@ -25,11 +22,8 @@ func Unset(cfgPath string) {
|
||||
}
|
||||
|
||||
commands := generateCommands(configCmd, config.Unset, "")
|
||||
failed = util.ExecCmds(commands)
|
||||
util.ExecCmds(commands, stdin)
|
||||
}
|
||||
|
||||
// Additional feedback on error
|
||||
if failed {
|
||||
log.Printf("One or more commands failed to execute. Run command 'debug' to see more.\n")
|
||||
}
|
||||
closeFunc()
|
||||
}
|
||||
|
@ -21,8 +21,6 @@ func processVars(cmd *util.Command, isVariableType bool) {
|
||||
for _, arg := range cmd.Arguments {
|
||||
cmd.Name = strings.Replace(cmd.Name, "$PRSW_ARG", arg, 1)
|
||||
}
|
||||
|
||||
cmd.Arguments = nil
|
||||
}
|
||||
|
||||
func injectProxy(cmd *util.Command, variant *util.Variant, proxyServer string) {
|
||||
|
@ -1,31 +1,45 @@
|
||||
//go:build windows
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExecCmds(commands []*Command) bool {
|
||||
var failed bool
|
||||
// Create a single powershell process and leave closing, input and output open
|
||||
func ReadyCmd() (*io.WriteCloser, func() error, error) {
|
||||
cmd := exec.Command("powershell", "-NoLogo", "-NoProfile", "-Command", "-")
|
||||
|
||||
var stdout bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stdout
|
||||
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return &stdin, func() error {
|
||||
// Close stdin pipe
|
||||
stdin.Close()
|
||||
|
||||
// Wait for command to flush
|
||||
err := cmd.Wait()
|
||||
return err
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ExecCmds(commands []*Command, stdin *io.WriteCloser) {
|
||||
for _, command := range commands {
|
||||
var cmdStr string = command.Name
|
||||
|
||||
// Combine command into single string if args are given
|
||||
if len(command.Arguments) > 0 {
|
||||
cmdArgs := strings.Join(command.Arguments, " ")
|
||||
cmdStr = fmt.Sprintf("%s %s", command.Name, cmdArgs)
|
||||
}
|
||||
|
||||
// Try executing command in default shell
|
||||
cmd := exec.Command("powershell", "-NoLogo", "-NoProfile", "-Command", cmdStr)
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Printf("Command '%s' failed!\n", cmdStr)
|
||||
failed = true
|
||||
cmdStr := fmt.Sprintf("%s %s", command.Name, cmdArgs)
|
||||
fmt.Fprintln(*stdin, cmdStr)
|
||||
}
|
||||
}
|
||||
return failed
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user