From 989b4ff13e2a33e85c16ea7b5aabe8eacc46ee9c Mon Sep 17 00:00:00 2001 From: yuzuki999 Date: Fri, 2 Jun 2023 08:45:22 +0800 Subject: [PATCH] move some code, add message color for x25519 command --- cmd/action_linux.go | 9 +++++---- cmd/common.go | 3 ++- cmd/install_linux.go | 7 ++++--- cmd/x25519.go | 7 ++++--- {cmd => common/exec}/exec.go | 6 +++--- 5 files changed, 18 insertions(+), 14 deletions(-) rename {cmd => common/exec}/exec.go (75%) diff --git a/cmd/action_linux.go b/cmd/action_linux.go index eedd066..a032a5f 100644 --- a/cmd/action_linux.go +++ b/cmd/action_linux.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/Yuzuki616/V2bX/common/exec" "github.com/spf13/cobra" "time" ) @@ -26,7 +27,7 @@ var ( Use: "log", Short: "Output V2bX log", Run: func(_ *cobra.Command, _ []string) { - execCommandStd("journalctl", "-u", "V2bX.service", "-e", "--no-pager", "-f") + exec.RunCommandStd("journalctl", "-u", "V2bX.service", "-e", "--no-pager", "-f") }, } ) @@ -48,7 +49,7 @@ func startHandle(_ *cobra.Command, _ []string) { if r { fmt.Println(Ok("V2bX已运行,无需再次启动,如需重启请选择重启")) } - _, err = execCommand("systemctl start V2bX.service") + _, err = exec.RunCommandByShell("systemctl start V2bX.service") if err != nil { fmt.Println(Err("exec start cmd error: ", err)) fmt.Println(Err("V2bX启动失败")) @@ -68,7 +69,7 @@ func startHandle(_ *cobra.Command, _ []string) { } func stopHandle(_ *cobra.Command, _ []string) { - _, err := execCommand("systemctl stop V2bX.service") + _, err := exec.RunCommandByShell("systemctl stop V2bX.service") if err != nil { fmt.Println(Err("exec stop cmd error: ", err)) fmt.Println(Err("V2bX停止失败")) @@ -89,7 +90,7 @@ func stopHandle(_ *cobra.Command, _ []string) { } func restartHandle(_ *cobra.Command, _ []string) { - _, err := execCommand("systemctl restart V2bX.service") + _, err := exec.RunCommandByShell("systemctl restart V2bX.service") if err != nil { fmt.Println(Err("exec restart cmd error: ", err)) fmt.Println(Err("V2bX重启失败")) diff --git a/cmd/common.go b/cmd/common.go index 615b3f2..640accb 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/Yuzuki616/V2bX/common/exec" "strings" ) @@ -13,7 +14,7 @@ const ( ) func checkRunning() (bool, error) { - o, err := execCommand("systemctl status V2bX | grep Active") + o, err := exec.RunCommandByShell("systemctl status V2bX | grep Active") if err != nil { return false, err } diff --git a/cmd/install_linux.go b/cmd/install_linux.go index 47448f5..1f1eada 100644 --- a/cmd/install_linux.go +++ b/cmd/install_linux.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/Yuzuki616/V2bX/common/exec" "github.com/spf13/cobra" "os" "strings" @@ -14,7 +15,7 @@ var ( Use: "update", Short: "Update V2bX version", Run: func(_ *cobra.Command, _ []string) { - execCommandStd("bash", + exec.RunCommandStd("bash", "<(curl -Ls https://raw.githubusercontents.com/Yuzuki616/V2bX-script/master/install.sh)", targetVersion) }, @@ -40,7 +41,7 @@ func uninstallHandle(_ *cobra.Command, _ []string) { if strings.ToLower(yes) != "y" { fmt.Println("已取消卸载") } - _, err := execCommand("systemctl stop V2bX&&systemctl disable V2bX") + _, err := exec.RunCommandByShell("systemctl stop V2bX&&systemctl disable V2bX") if err != nil { fmt.Println(Err("exec cmd error: ", err)) fmt.Println(Err("卸载失败")) @@ -50,7 +51,7 @@ func uninstallHandle(_ *cobra.Command, _ []string) { _ = os.RemoveAll("/etc/V2bX/") _ = os.RemoveAll("/usr/local/V2bX/") _ = os.RemoveAll("/bin/V2bX") - _, err = execCommand("systemctl daemon-reload&&systemctl reset-failed") + _, err = exec.RunCommandByShell("systemctl daemon-reload&&systemctl reset-failed") if err != nil { fmt.Println(Err("exec cmd error: ", err)) fmt.Println(Err("卸载失败")) diff --git a/cmd/x25519.go b/cmd/x25519.go index 9371466..2e3b9a3 100644 --- a/cmd/x25519.go +++ b/cmd/x25519.go @@ -30,7 +30,7 @@ func executeX25519() { var publicKey []byte privateKey = make([]byte, curve25519.ScalarSize) if _, err = rand.Read(privateKey); err != nil { - output = fmt.Sprintf("read rand error: %s", err) + output = Err("read rand error: ", err) return } @@ -41,11 +41,12 @@ func executeX25519() { privateKey[31] |= 64 if publicKey, err = curve25519.X25519(privateKey, curve25519.Basepoint); err != nil { - output = fmt.Sprintf("gen X25519 error: %s", err) + output = Err("gen X25519 error: ", err) return } - output = fmt.Sprintf("Private key: %v\nPublic key: %v", + output = Err("Private key: ", base64.RawURLEncoding.EncodeToString(privateKey), + "\nPublic key: ", base64.RawURLEncoding.EncodeToString(publicKey)) } diff --git a/cmd/exec.go b/common/exec/exec.go similarity index 75% rename from cmd/exec.go rename to common/exec/exec.go index 3951990..f0cc791 100644 --- a/cmd/exec.go +++ b/common/exec/exec.go @@ -1,4 +1,4 @@ -package cmd +package exec import ( "errors" @@ -6,7 +6,7 @@ import ( "os/exec" ) -func execCommand(cmd string) (string, error) { +func RunCommandByShell(cmd string) (string, error) { e := exec.Command("bash", "-c", cmd) out, err := e.CombinedOutput() if errors.Unwrap(err) == exec.ErrNotFound { @@ -16,7 +16,7 @@ func execCommand(cmd string) (string, error) { return string(out), err } -func execCommandStd(name string, args ...string) { +func RunCommandStd(name string, args ...string) { e := exec.Command(name, args...) e.Stdout = os.Stdout e.Stdin = os.Stdin