mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 12:41: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
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/Baipyrus/ProxySwitcher/util"
|
"github.com/Baipyrus/ProxySwitcher/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,6 +8,7 @@ func Set() {
|
|||||||
stdin, closeFunc, _ := util.ReadyCmd()
|
stdin, closeFunc, _ := util.ReadyCmd()
|
||||||
|
|
||||||
proxy, _ := ReadSystemProxy()
|
proxy, _ := ReadSystemProxy()
|
||||||
|
// Set system proxy, if not already
|
||||||
if !proxy.Enabled {
|
if !proxy.Enabled {
|
||||||
SetSystemProxy(true)
|
SetSystemProxy(true)
|
||||||
}
|
}
|
||||||
@ -18,19 +16,13 @@ func Set() {
|
|||||||
configs, _ := util.ReadConfigs()
|
configs, _ := util.ReadConfigs()
|
||||||
for _, config := range configs {
|
for _, config := range configs {
|
||||||
configCmd := config.Name
|
configCmd := config.Name
|
||||||
|
// Use command instead of name, if given
|
||||||
if config.Cmd != "" {
|
if config.Cmd != "" {
|
||||||
configCmd = config.Cmd
|
configCmd = config.Cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
commands := getVariants(config.Set, configCmd, proxy.Server)
|
commands := generateCommands(config.Set, configCmd, proxy.Server)
|
||||||
|
util.ExecCmds(commands, stdin)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
closeFunc()
|
closeFunc()
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/Baipyrus/ProxySwitcher/util"
|
"github.com/Baipyrus/ProxySwitcher/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Unset() {
|
func Unset() {
|
||||||
stdin, closeFunc, _ := util.ReadyCmd()
|
stdin, closeFunc, _ := util.ReadyCmd()
|
||||||
|
|
||||||
|
// Unset system proxy, if not already
|
||||||
proxy, _ := ReadSystemProxy()
|
proxy, _ := ReadSystemProxy()
|
||||||
if proxy.Enabled {
|
if proxy.Enabled {
|
||||||
SetSystemProxy(false)
|
SetSystemProxy(false)
|
||||||
@ -18,19 +16,13 @@ func Unset() {
|
|||||||
configs, _ := util.ReadConfigs()
|
configs, _ := util.ReadConfigs()
|
||||||
for _, config := range configs {
|
for _, config := range configs {
|
||||||
configCmd := config.Name
|
configCmd := config.Name
|
||||||
|
// Use command instead of name, if given
|
||||||
if config.Cmd != "" {
|
if config.Cmd != "" {
|
||||||
configCmd = config.Cmd
|
configCmd = config.Cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
commands := getVariants(config.Unset, configCmd, "")
|
commands := generateCommands(config.Unset, configCmd, "")
|
||||||
|
util.ExecCmds(commands, stdin)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
closeFunc()
|
closeFunc()
|
||||||
|
@ -12,11 +12,13 @@ func readArgs(replaceVariable bool, args []string, configCmd string) ([]string,
|
|||||||
var configArgs []string
|
var configArgs []string
|
||||||
|
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
|
// If not replacable, append
|
||||||
if !replaceVariable {
|
if !replaceVariable {
|
||||||
configArgs = append(configArgs, arg)
|
configArgs = append(configArgs, arg)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Replace specific "ProxySwitcher Argument" in command
|
||||||
configCmd = strings.Replace(configCmd, "$PRSW_ARG", arg, 1)
|
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) {
|
func applyProxy(configArgs []string, configCmd string, proxyServer string, variant *util.Variant) ([]string, string) {
|
||||||
|
// Skip, no proxy provided
|
||||||
if proxyServer == "" {
|
if proxyServer == "" {
|
||||||
return configArgs, configCmd
|
return configArgs, configCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Insert proxy only on last VARIABLE type
|
||||||
if variant.Type == util.VARIABLE && strings.Count(configCmd, "$PRSW_ARG") == 1 {
|
if variant.Type == util.VARIABLE && strings.Count(configCmd, "$PRSW_ARG") == 1 {
|
||||||
configCmd = strings.Replace(configCmd, "$PRSW_ARG", proxyServer, 1)
|
configCmd = strings.Replace(configCmd, "$PRSW_ARG", proxyServer, 1)
|
||||||
return configArgs, configCmd
|
return configArgs, configCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Insert proxy right after equator
|
||||||
if variant.Equator != "" {
|
if variant.Equator != "" {
|
||||||
configArgs[len(configArgs)-1] += variant.Equator + proxyServer
|
configArgs[len(configArgs)-1] += variant.Equator + proxyServer
|
||||||
return configArgs, configCmd
|
return configArgs, configCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Or otherwise just append it as an argument
|
||||||
configArgs = append(configArgs, proxyServer)
|
configArgs = append(configArgs, proxyServer)
|
||||||
|
|
||||||
return configArgs, configCmd
|
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
|
var commands []*util.Command
|
||||||
|
|
||||||
|
// Generate one command per variant
|
||||||
for _, variant := range variants {
|
for _, variant := range variants {
|
||||||
replaceVariable := variant.Type == util.VARIABLE
|
replaceVariable := variant.Type == util.VARIABLE
|
||||||
|
|
||||||
configArgs, configCmd := readArgs(replaceVariable, variant.Arguments, configCmd)
|
configArgs, configCmd := readArgs(replaceVariable, variant.Arguments, configCmd)
|
||||||
|
|
||||||
configArgs, configCmd = applyProxy(configArgs, configCmd, proxyServer, variant)
|
configArgs, configCmd = applyProxy(configArgs, configCmd, proxyServer, variant)
|
||||||
|
|
||||||
commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs})
|
commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs})
|
||||||
|
19
util/util.go
19
util/util.go
@ -2,10 +2,13 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Create a single powershell process and leave closing, input and output open
|
||||||
func ReadyCmd() (*io.WriteCloser, func() error, error) {
|
func ReadyCmd() (*io.WriteCloser, func() error, error) {
|
||||||
cmd := exec.Command("powershell", "-NoLogo", "-NoProfile", "-Command", "-")
|
cmd := exec.Command("powershell", "-NoLogo", "-NoProfile", "-Command", "-")
|
||||||
|
|
||||||
@ -18,17 +21,29 @@ func ReadyCmd() (*io.WriteCloser, func() error, error) {
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
err = cmd.Start()
|
||||||
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &stdin, func() error {
|
return &stdin, func() error {
|
||||||
|
// Close stdin pipe
|
||||||
stdin.Close()
|
stdin.Close()
|
||||||
|
|
||||||
if err := cmd.Wait(); err != nil {
|
// Wait for command to flush
|
||||||
|
err := cmd.Wait()
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}, 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