Compare commits

..

No commits in common. "d9df5cad1e3aa92df2f976992ef4b4c72cca2eed" and "a69abda660243a9afeadee49a9a3b9a56b88b8a8" have entirely different histories.

8 changed files with 44 additions and 59 deletions

View File

@ -1,5 +1,3 @@
//go:build windows
package cmd package cmd
import ( import (

View File

@ -1,5 +1,3 @@
//go:build windows
package cmd package cmd
import ( import (

View File

@ -1,5 +1,3 @@
//go:build windows
package cmd package cmd
import ( import (

View File

@ -1,11 +1,8 @@
//go:build windows
package proxy package proxy
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -34,11 +31,9 @@ func Debug(cfgPath string) {
fmt.Printf("\nConfig:\n") fmt.Printf("\nConfig:\n")
fmt.Printf("%s\n\n", path) fmt.Printf("%s\n\n", path)
proxy, err := ReadSystemProxy() proxy, _ := ReadSystemProxy()
proxyServer := proxy.Server proxyServer := proxy.Server
if err != nil { if proxyServer == "" {
log.Fatal(err)
} else if proxyServer == "" {
proxyServer = "[N/A]" proxyServer = "[N/A]"
} }
@ -46,11 +41,7 @@ func Debug(cfgPath string) {
fmt.Printf("Enabled: %t\n", proxy.Enabled) fmt.Printf("Enabled: %t\n", proxy.Enabled)
fmt.Printf("Server: %s\n\n", proxyServer) fmt.Printf("Server: %s\n\n", proxyServer)
configs, err := util.ReadConfigs(cfgPath) configs, _ := util.ReadConfigs(cfgPath)
if err != nil {
log.Fatal(err)
}
for _, config := range configs { for _, config := range configs {
configCmd := config.Name configCmd := config.Name
// Use command instead of name, if given // Use command instead of name, if given

View File

@ -1,21 +1,18 @@
//go:build windows
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
@ -25,11 +22,8 @@ func Set(cfgPath string) {
} }
commands := generateCommands(configCmd, config.Set, proxy.Server) commands := generateCommands(configCmd, config.Set, proxy.Server)
failed = util.ExecCmds(commands) util.ExecCmds(commands, stdin)
} }
// Additional feedback on error closeFunc()
if failed {
log.Printf("One or more commands failed to execute. Run command 'debug' to see more.\n")
}
} }

View File

@ -1,21 +1,18 @@
//go:build windows
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
@ -25,11 +22,8 @@ func Unset(cfgPath string) {
} }
commands := generateCommands(configCmd, config.Unset, "") commands := generateCommands(configCmd, config.Unset, "")
failed = util.ExecCmds(commands) util.ExecCmds(commands, stdin)
} }
// Additional feedback on error closeFunc()
if failed {
log.Printf("One or more commands failed to execute. Run command 'debug' to see more.\n")
}
} }

View File

@ -21,8 +21,6 @@ func processVars(cmd *util.Command, isVariableType bool) {
for _, arg := range cmd.Arguments { for _, arg := range cmd.Arguments {
cmd.Name = strings.Replace(cmd.Name, "$PRSW_ARG", arg, 1) cmd.Name = strings.Replace(cmd.Name, "$PRSW_ARG", arg, 1)
} }
cmd.Arguments = nil
} }
func injectProxy(cmd *util.Command, variant *util.Variant, proxyServer string) { func injectProxy(cmd *util.Command, variant *util.Variant, proxyServer string) {

View File

@ -1,31 +1,45 @@
//go:build windows
package util package util
import ( import (
"bytes"
"fmt" "fmt"
"log" "io"
"os/exec" "os/exec"
"strings" "strings"
) )
func ExecCmds(commands []*Command) bool { // Create a single powershell process and leave closing, input and output open
var failed bool func ReadyCmd() (*io.WriteCloser, func() error, error) {
for _, command := range commands { cmd := exec.Command("powershell", "-NoLogo", "-NoProfile", "-Command", "-")
var cmdStr string = command.Name
// Combine command into single string if args are given var stdout bytes.Buffer
if len(command.Arguments) > 0 { cmd.Stdout = &stdout
cmdArgs := strings.Join(command.Arguments, " ") cmd.Stderr = &stdout
cmdStr = fmt.Sprintf("%s %s", command.Name, cmdArgs)
}
// Try executing command in default shell stdin, err := cmd.StdinPipe()
cmd := exec.Command("powershell", "-NoLogo", "-NoProfile", "-Command", cmdStr) if err != nil {
if err := cmd.Run(); err != nil { return nil, nil, err
log.Printf("Command '%s' failed!\n", cmdStr) }
failed = true
} 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 {
cmdArgs := strings.Join(command.Arguments, " ")
cmdStr := fmt.Sprintf("%s %s", command.Name, cmdArgs)
fmt.Fprintln(*stdin, cmdStr)
} }
return failed
} }