mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 04:31:45 +00:00
additional commenting for better readability
This commit is contained in:
parent
e84e0af672
commit
9b4fb7af4d
16
proxy/set.go
16
proxy/set.go
@ -1,9 +1,6 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Baipyrus/ProxySwitcher/util"
|
||||
)
|
||||
|
||||
@ -11,6 +8,7 @@ func Set() {
|
||||
stdin, closeFunc, _ := util.ReadyCmd()
|
||||
|
||||
proxy, _ := ReadSystemProxy()
|
||||
// Set system proxy, if not already
|
||||
if !proxy.Enabled {
|
||||
SetSystemProxy(true)
|
||||
}
|
||||
@ -18,19 +16,13 @@ func Set() {
|
||||
configs, _ := util.ReadConfigs()
|
||||
for _, config := range configs {
|
||||
configCmd := config.Name
|
||||
// Use command instead of name, if given
|
||||
if config.Cmd != "" {
|
||||
configCmd = config.Cmd
|
||||
}
|
||||
|
||||
commands := getVariants(config.Set, configCmd, proxy.Server)
|
||||
|
||||
for _, command := range commands {
|
||||
cmdArgs := strings.Join(command.Arguments, " ")
|
||||
cmdStr := fmt.Sprintf("%s %s", command.Name, cmdArgs)
|
||||
|
||||
fmt.Printf("%s\n", cmdStr)
|
||||
fmt.Fprintln(*stdin, cmdStr)
|
||||
}
|
||||
commands := generateCommands(config.Set, configCmd, proxy.Server)
|
||||
util.ExecCmds(commands, stdin)
|
||||
}
|
||||
|
||||
closeFunc()
|
||||
|
@ -1,15 +1,13 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Baipyrus/ProxySwitcher/util"
|
||||
)
|
||||
|
||||
func Unset() {
|
||||
stdin, closeFunc, _ := util.ReadyCmd()
|
||||
|
||||
// Unset system proxy, if not already
|
||||
proxy, _ := ReadSystemProxy()
|
||||
if proxy.Enabled {
|
||||
SetSystemProxy(false)
|
||||
@ -18,19 +16,13 @@ func Unset() {
|
||||
configs, _ := util.ReadConfigs()
|
||||
for _, config := range configs {
|
||||
configCmd := config.Name
|
||||
// Use command instead of name, if given
|
||||
if config.Cmd != "" {
|
||||
configCmd = config.Cmd
|
||||
}
|
||||
|
||||
commands := getVariants(config.Unset, configCmd, "")
|
||||
|
||||
for _, command := range commands {
|
||||
cmdArgs := strings.Join(command.Arguments, " ")
|
||||
cmdStr := fmt.Sprintf("%s %s", command.Name, cmdArgs)
|
||||
|
||||
fmt.Printf("%s\n", cmdStr)
|
||||
fmt.Fprintln(*stdin, cmdStr)
|
||||
}
|
||||
commands := generateCommands(config.Unset, configCmd, "")
|
||||
util.ExecCmds(commands, stdin)
|
||||
}
|
||||
|
||||
closeFunc()
|
||||
|
@ -12,11 +12,13 @@ func readArgs(replaceVariable bool, args []string, configCmd string) ([]string,
|
||||
var configArgs []string
|
||||
|
||||
for _, arg := range args {
|
||||
// If not replacable, append
|
||||
if !replaceVariable {
|
||||
configArgs = append(configArgs, arg)
|
||||
continue
|
||||
}
|
||||
|
||||
// Replace specific "ProxySwitcher Argument" in command
|
||||
configCmd = strings.Replace(configCmd, "$PRSW_ARG", arg, 1)
|
||||
}
|
||||
|
||||
@ -24,33 +26,37 @@ func readArgs(replaceVariable bool, args []string, configCmd string) ([]string,
|
||||
}
|
||||
|
||||
func applyProxy(configArgs []string, configCmd string, proxyServer string, variant *util.Variant) ([]string, string) {
|
||||
// Skip, no proxy provided
|
||||
if proxyServer == "" {
|
||||
return configArgs, configCmd
|
||||
}
|
||||
|
||||
// Insert proxy only on last VARIABLE type
|
||||
if variant.Type == util.VARIABLE && strings.Count(configCmd, "$PRSW_ARG") == 1 {
|
||||
configCmd = strings.Replace(configCmd, "$PRSW_ARG", proxyServer, 1)
|
||||
return configArgs, configCmd
|
||||
}
|
||||
|
||||
// Insert proxy right after equator
|
||||
if variant.Equator != "" {
|
||||
configArgs[len(configArgs)-1] += variant.Equator + proxyServer
|
||||
return configArgs, configCmd
|
||||
}
|
||||
|
||||
// Or otherwise just append it as an argument
|
||||
configArgs = append(configArgs, proxyServer)
|
||||
|
||||
return configArgs, configCmd
|
||||
}
|
||||
|
||||
func getVariants(variants []*util.Variant, configCmd, proxyServer string) []*util.Command {
|
||||
func generateCommands(variants []*util.Variant, configCmd, proxyServer string) []*util.Command {
|
||||
var commands []*util.Command
|
||||
|
||||
// Generate one command per variant
|
||||
for _, variant := range variants {
|
||||
replaceVariable := variant.Type == util.VARIABLE
|
||||
|
||||
configArgs, configCmd := readArgs(replaceVariable, variant.Arguments, configCmd)
|
||||
|
||||
configArgs, configCmd = applyProxy(configArgs, configCmd, proxyServer, variant)
|
||||
|
||||
commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs})
|
||||
|
19
util/util.go
19
util/util.go
@ -2,10 +2,13 @@ package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"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", "-")
|
||||
|
||||
@ -18,17 +21,29 @@ func ReadyCmd() (*io.WriteCloser, func() error, error) {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return &stdin, func() error {
|
||||
// Close stdin pipe
|
||||
stdin.Close()
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
// Wait for command to flush
|
||||
err := cmd.Wait()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}, 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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user