mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 04:31:45 +00:00
implement separate powershell calls for each command
This commit is contained in:
parent
af8ca529a2
commit
4778d287fc
12
proxy/set.go
12
proxy/set.go
@ -3,18 +3,19 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/Baipyrus/ProxySwitcher/util"
|
"github.com/Baipyrus/ProxySwitcher/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Set(cfgPath string) {
|
func Set(cfgPath string) {
|
||||||
stdin, closeFunc, _ := util.ReadyCmd()
|
|
||||||
|
|
||||||
proxy, _ := ReadSystemProxy()
|
proxy, _ := ReadSystemProxy()
|
||||||
// Set system proxy, if not already
|
// Set system proxy, if not already
|
||||||
if !proxy.Enabled {
|
if !proxy.Enabled {
|
||||||
SetSystemProxy(true)
|
SetSystemProxy(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var failed bool
|
||||||
configs, _ := util.ReadConfigs(cfgPath)
|
configs, _ := util.ReadConfigs(cfgPath)
|
||||||
for _, config := range configs {
|
for _, config := range configs {
|
||||||
configCmd := config.Name
|
configCmd := config.Name
|
||||||
@ -24,8 +25,11 @@ func Set(cfgPath string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
commands := generateCommands(configCmd, config.Set, proxy.Server)
|
commands := generateCommands(configCmd, config.Set, proxy.Server)
|
||||||
util.ExecCmds(commands, stdin)
|
failed = util.ExecCmds(commands)
|
||||||
}
|
}
|
||||||
|
|
||||||
closeFunc()
|
// Additional feedback on error
|
||||||
|
if failed {
|
||||||
|
log.Printf("One or more commands failed to execute. Run command 'debug' to see more.\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,18 +3,19 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/Baipyrus/ProxySwitcher/util"
|
"github.com/Baipyrus/ProxySwitcher/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Unset(cfgPath string) {
|
func Unset(cfgPath string) {
|
||||||
stdin, closeFunc, _ := util.ReadyCmd()
|
|
||||||
|
|
||||||
proxy, _ := ReadSystemProxy()
|
proxy, _ := ReadSystemProxy()
|
||||||
// Unset system proxy, if not already
|
// Unset system proxy, if not already
|
||||||
if proxy.Enabled {
|
if proxy.Enabled {
|
||||||
SetSystemProxy(false)
|
SetSystemProxy(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var failed bool
|
||||||
configs, _ := util.ReadConfigs(cfgPath)
|
configs, _ := util.ReadConfigs(cfgPath)
|
||||||
for _, config := range configs {
|
for _, config := range configs {
|
||||||
configCmd := config.Name
|
configCmd := config.Name
|
||||||
@ -24,8 +25,11 @@ func Unset(cfgPath string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
commands := generateCommands(configCmd, config.Unset, "")
|
commands := generateCommands(configCmd, config.Unset, "")
|
||||||
util.ExecCmds(commands, stdin)
|
failed = util.ExecCmds(commands)
|
||||||
}
|
}
|
||||||
|
|
||||||
closeFunc()
|
// Additional feedback on error
|
||||||
|
if failed {
|
||||||
|
log.Printf("One or more commands failed to execute. Run command 'debug' to see more.\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,45 +3,29 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create a single powershell process and leave closing, input and output open
|
func ExecCmds(commands []*Command) bool {
|
||||||
func ReadyCmd() (*io.WriteCloser, func() error, error) {
|
var failed bool
|
||||||
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 {
|
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, " ")
|
cmdArgs := strings.Join(command.Arguments, " ")
|
||||||
cmdStr := fmt.Sprintf("%s %s", command.Name, cmdArgs)
|
cmdStr = fmt.Sprintf("%s %s", command.Name, cmdArgs)
|
||||||
fmt.Fprintln(*stdin, cmdStr)
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return failed
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user