mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 12:41:45 +00:00
Compare commits
4 Commits
a69abda660
...
d9df5cad1e
Author | SHA1 | Date | |
---|---|---|---|
d9df5cad1e | |||
4778d287fc | |||
af8ca529a2 | |||
56a6df9259 |
@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
|
@ -1,8 +1,11 @@
|
||||
//go:build windows
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -31,9 +34,11 @@ func Debug(cfgPath string) {
|
||||
fmt.Printf("\nConfig:\n")
|
||||
fmt.Printf("%s\n\n", path)
|
||||
|
||||
proxy, _ := ReadSystemProxy()
|
||||
proxy, err := ReadSystemProxy()
|
||||
proxyServer := proxy.Server
|
||||
if proxyServer == "" {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else if proxyServer == "" {
|
||||
proxyServer = "[N/A]"
|
||||
}
|
||||
|
||||
@ -41,7 +46,11 @@ func Debug(cfgPath string) {
|
||||
fmt.Printf("Enabled: %t\n", proxy.Enabled)
|
||||
fmt.Printf("Server: %s\n\n", proxyServer)
|
||||
|
||||
configs, _ := util.ReadConfigs(cfgPath)
|
||||
configs, err := util.ReadConfigs(cfgPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, config := range configs {
|
||||
configCmd := config.Name
|
||||
// Use command instead of name, if given
|
||||
|
14
proxy/set.go
14
proxy/set.go
@ -1,18 +1,21 @@
|
||||
//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
|
||||
@ -22,8 +25,11 @@ func Set(cfgPath string) {
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,21 @@
|
||||
//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
|
||||
@ -22,8 +25,11 @@ func Unset(cfgPath string) {
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,8 @@ 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,45 +1,31 @@
|
||||
//go:build windows
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
func ExecCmds(commands []*Command) bool {
|
||||
var failed bool
|
||||
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)
|
||||
fmt.Fprintln(*stdin, cmdStr)
|
||||
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
|
||||
}
|
||||
}
|
||||
return failed
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user