diff --git a/cmd/debug.go b/cmd/debug.go index e225a12..19299c4 100644 --- a/cmd/debug.go +++ b/cmd/debug.go @@ -10,7 +10,7 @@ var debugCmd = &cobra.Command{ Use: "debug", Short: "Output all parsed configurations for debugging", Run: func(cmd *cobra.Command, args []string) { - proxy.Debug(cfgFile) + proxy.Debug(cfgPath) }, } diff --git a/cmd/root.go b/cmd/root.go index 79589a3..c6dfe60 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,7 +7,7 @@ import ( ) var ( - cfgFile string + cfgPath string // rootCmd represents the base command when called without any subcommands rootCmd = &cobra.Command{ @@ -33,7 +33,7 @@ func init() { // Cobra supports persistent flags, which, if defined here, // will be global for your application. - rootCmd.PersistentFlags().StringVarP(&cfgFile, "configs", "c", "configs.json", "configurations file") + rootCmd.PersistentFlags().StringVarP(&cfgPath, "configs", "c", "configs/", "configurations path") // Cobra also supports local flags, which will only run // when this action is called directly. diff --git a/cmd/save.go b/cmd/save.go index 56f4e7a..12e6308 100644 --- a/cmd/save.go +++ b/cmd/save.go @@ -37,8 +37,9 @@ var saveCmd = &cobra.Command{ var input string fmt.Print("Save this data? (Y/n) ") + fmt.Scanln(&input) if input == "" || strings.ToLower(input) == "y" { - util.SaveConfig(cfgFile, config) + util.SaveConfig(cfgPath, config) } }, } diff --git a/cmd/set.go b/cmd/set.go index 011ff22..1b0d49d 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -10,7 +10,7 @@ var setCmd = &cobra.Command{ Use: "set", Short: "Enable the current internet proxy settings", Run: func(cmd *cobra.Command, args []string) { - proxy.Set(cfgFile) + proxy.Set(cfgPath) }, } diff --git a/cmd/unset.go b/cmd/unset.go index c619448..d751baa 100644 --- a/cmd/unset.go +++ b/cmd/unset.go @@ -10,7 +10,7 @@ var unsetCmd = &cobra.Command{ Use: "unset", Short: "Disable the current internet proxy settings", Run: func(cmd *cobra.Command, args []string) { - proxy.Unset(cfgFile) + proxy.Unset(cfgPath) }, } diff --git a/configs.json b/configs.json deleted file mode 100644 index 7485545..0000000 --- a/configs.json +++ /dev/null @@ -1,104 +0,0 @@ -[ - { - "name": "env", - "cmd": "[System.Environment]::SetEnvironmentVariable('$PRSW_ARG', $PRSW_ARG, 'User')", - "set": [ - { - "args": [ - "http_proxy" - ], - "surround": "\"", - "type": "variable" - }, { - "args": [ - "https_proxy" - ], - "surround": "\"", - "type": "variable" - } - ], - "unset": [ - { - "args": [ - "http_proxy", - "[NullString]::Value" - ], - "type": "variable" - }, { - "args": [ - "https_proxy", - "[NullString]::Value" - ], - "type": "variable" - } - ] - }, { - "name": "git", - "set": [ - { - "args": [ - "config", - "--global", - "http.proxy" - ] - }, { - "args": [ - "config", - "--global", - "https.proxy" - ] - } - ], - "unset": [ - { - "args": [ - "config", - "--global", - "--unset", - "http.proxy" - ] - }, { - "args": [ - "config", - "--global", - "--unset", - "https.proxy" - ] - } - ] - }, { - "name": "npm", - "set": [ - { - "args": [ - "config", - "set", - "proxy" - ], - "equator": "=" - }, { - "args": [ - "config", - "set", - "https-proxy" - ], - "equator": "=" - } - ], - "unset": [ - { - "args": [ - "config", - "delete", - "proxy" - ] - }, { - "args": [ - "config", - "delete", - "https-proxy" - ] - } - ] - } -] diff --git a/configs/env.json b/configs/env.json new file mode 100644 index 0000000..387d347 --- /dev/null +++ b/configs/env.json @@ -0,0 +1,34 @@ +{ + "name": "env", + "cmd": "[System.Environment]::SetEnvironmentVariable('$PRSW_ARG', $PRSW_ARG, 'User')", + "set": [ + { + "args": [ + "http_proxy" + ], + "surround": "\"", + "type": "variable" + }, { + "args": [ + "https_proxy" + ], + "surround": "\"", + "type": "variable" + } + ], + "unset": [ + { + "args": [ + "http_proxy", + "[NullString]::Value" + ], + "type": "variable" + }, { + "args": [ + "https_proxy", + "[NullString]::Value" + ], + "type": "variable" + } + ] +} diff --git a/configs/git.json b/configs/git.json new file mode 100644 index 0000000..bca4d6e --- /dev/null +++ b/configs/git.json @@ -0,0 +1,35 @@ +{ + "name": "git", + "set": [ + { + "args": [ + "config", + "--global", + "http.proxy" + ] + }, { + "args": [ + "config", + "--global", + "https.proxy" + ] + } + ], + "unset": [ + { + "args": [ + "config", + "--global", + "--unset", + "http.proxy" + ] + }, { + "args": [ + "config", + "--global", + "--unset", + "https.proxy" + ] + } + ] +} diff --git a/configs/npm.json b/configs/npm.json new file mode 100644 index 0000000..24501c9 --- /dev/null +++ b/configs/npm.json @@ -0,0 +1,35 @@ +{ + "name": "npm", + "set": [ + { + "args": [ + "config", + "set", + "proxy" + ], + "equator": "=" + }, { + "args": [ + "config", + "set", + "https-proxy" + ], + "equator": "=" + } + ], + "unset": [ + { + "args": [ + "config", + "delete", + "proxy" + ] + }, { + "args": [ + "config", + "delete", + "https-proxy" + ] + } + ] +} diff --git a/configs/pip.json b/configs/pip.json new file mode 100644 index 0000000..83d7d5f --- /dev/null +++ b/configs/pip.json @@ -0,0 +1,21 @@ +{ + "name": "pip", + "set": [ + { + "args": [ + "config", + "set", + "global.proxy" + ] + } + ], + "unset": [ + { + "args": [ + "config", + "unset", + "global.proxy" + ] + } + ] +} diff --git a/configs/socks.example.json b/configs/socks.example.json new file mode 100644 index 0000000..0e4be5f --- /dev/null +++ b/configs/socks.example.json @@ -0,0 +1,21 @@ +{ + "name": "SOCKS", + "cmd": "[void] 'Toggle SOCKS v5'; $PRSW_ARG", + "set": [ + { + "args": [ + "Start-Process -FilePath 'cmd.exe' -ArgumentList '/c', 'start', 'ssh', '-D', 'PORT', '-q', '-C', '-N', '-f', 'HOST'" + ], + "type": "variable", + "discard": true + } + ], + "unset": [ + { + "args": [ + "TASKKILL /F /PID ((Get-NetTCPConnection -LocalPort 1337).OwningProcess | Select -First 1)" + ], + "type": "variable" + } + ] +} diff --git a/proxy/debug.go b/proxy/debug.go index 9db4400..45a12c2 100644 --- a/proxy/debug.go +++ b/proxy/debug.go @@ -22,8 +22,8 @@ func mapCmdsToStr(commands []*util.Command) string { return strings.Join(output, "\n") } -func Debug(cfgFile string) { - path, _ := filepath.Abs(cfgFile) +func Debug(cfgPath string) { + path, _ := filepath.Abs(cfgPath) if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { path = "[N/A]" } @@ -41,7 +41,7 @@ func Debug(cfgFile string) { fmt.Printf("Enabled: %t\n", proxy.Enabled) fmt.Printf("Server: %s\n\n", proxyServer) - configs, _ := util.ReadConfigs(cfgFile) + configs, _ := util.ReadConfigs(cfgPath) for _, config := range configs { configCmd := config.Name // Use command instead of name, if given diff --git a/proxy/set.go b/proxy/set.go index a531657..4621165 100644 --- a/proxy/set.go +++ b/proxy/set.go @@ -4,7 +4,7 @@ import ( "github.com/Baipyrus/ProxySwitcher/util" ) -func Set(cfgFile string) { +func Set(cfgPath string) { stdin, closeFunc, _ := util.ReadyCmd() proxy, _ := ReadSystemProxy() @@ -13,7 +13,7 @@ func Set(cfgFile string) { SetSystemProxy(true) } - configs, _ := util.ReadConfigs(cfgFile) + configs, _ := util.ReadConfigs(cfgPath) for _, config := range configs { configCmd := config.Name // Use command instead of name, if given diff --git a/proxy/unset.go b/proxy/unset.go index c24b4b8..425c41f 100644 --- a/proxy/unset.go +++ b/proxy/unset.go @@ -4,7 +4,7 @@ import ( "github.com/Baipyrus/ProxySwitcher/util" ) -func Unset(cfgFile string) { +func Unset(cfgPath string) { stdin, closeFunc, _ := util.ReadyCmd() proxy, _ := ReadSystemProxy() @@ -13,7 +13,7 @@ func Unset(cfgFile string) { SetSystemProxy(false) } - configs, _ := util.ReadConfigs(cfgFile) + configs, _ := util.ReadConfigs(cfgPath) for _, config := range configs { configCmd := config.Name // Use command instead of name, if given diff --git a/util/config.go b/util/config.go index a29b506..52c5708 100644 --- a/util/config.go +++ b/util/config.go @@ -2,38 +2,60 @@ package util import ( "encoding/json" + "fmt" + "io/fs" + "path/filepath" + "strings" + "io" "os" ) -func ReadConfigs(name string) ([]*Config, error) { - file, err := os.Open(name) +func ReadConfigs(cfgPath string) ([]*Config, error) { + var configs []*Config - if err != nil { - return nil, err - } - defer file.Close() + err := filepath.Walk(cfgPath, func(path string, info fs.FileInfo, err error) error { + name := info.Name() + isExample := strings.HasSuffix(name, ".example.json") + notJson := !strings.HasSuffix(name, ".json") + if info.IsDir() || isExample || notJson { + return nil + } - bytes, _ := io.ReadAll(file) + file, err := os.Open(path) - var config []*Config - err = json.Unmarshal(bytes, &config) + if err != nil { + return nil + } + defer file.Close() - if err != nil { - return nil, err - } - return config, nil + bytes, err := io.ReadAll(file) + if err != nil { + return nil + } + + var config *Config + err = json.Unmarshal(bytes, &config) + + if err != nil { + return nil + } + + configs = append(configs, config) + return nil + }) + + return configs, err } -func SaveConfig(name string, config Config) error { - configs, _ := ReadConfigs(name) - configs = append(configs, &config) - - data, err := json.Marshal(configs) +func SaveConfig(cfgPath string, config Config) error { + data, err := json.Marshal(config) if err != nil { return err } - err = os.WriteFile(name, data, 0666) + cfgName := fmt.Sprintf("%s.json", config.Name) + cfgFile := filepath.Join(cfgPath, cfgName) + err = os.WriteFile(cfgFile, data, 0666) return err }