move some code, add message color for x25519 command

This commit is contained in:
yuzuki999 2023-06-02 08:45:22 +08:00
parent 138540cd16
commit 989b4ff13e
5 changed files with 18 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/common/exec"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"time" "time"
) )
@ -26,7 +27,7 @@ var (
Use: "log", Use: "log",
Short: "Output V2bX log", Short: "Output V2bX log",
Run: func(_ *cobra.Command, _ []string) { 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 { if r {
fmt.Println(Ok("V2bX已运行无需再次启动如需重启请选择重启")) fmt.Println(Ok("V2bX已运行无需再次启动如需重启请选择重启"))
} }
_, err = execCommand("systemctl start V2bX.service") _, err = exec.RunCommandByShell("systemctl start V2bX.service")
if err != nil { if err != nil {
fmt.Println(Err("exec start cmd error: ", err)) fmt.Println(Err("exec start cmd error: ", err))
fmt.Println(Err("V2bX启动失败")) fmt.Println(Err("V2bX启动失败"))
@ -68,7 +69,7 @@ func startHandle(_ *cobra.Command, _ []string) {
} }
func stopHandle(_ *cobra.Command, _ []string) { func stopHandle(_ *cobra.Command, _ []string) {
_, err := execCommand("systemctl stop V2bX.service") _, err := exec.RunCommandByShell("systemctl stop V2bX.service")
if err != nil { if err != nil {
fmt.Println(Err("exec stop cmd error: ", err)) fmt.Println(Err("exec stop cmd error: ", err))
fmt.Println(Err("V2bX停止失败")) fmt.Println(Err("V2bX停止失败"))
@ -89,7 +90,7 @@ func stopHandle(_ *cobra.Command, _ []string) {
} }
func restartHandle(_ *cobra.Command, _ []string) { func restartHandle(_ *cobra.Command, _ []string) {
_, err := execCommand("systemctl restart V2bX.service") _, err := exec.RunCommandByShell("systemctl restart V2bX.service")
if err != nil { if err != nil {
fmt.Println(Err("exec restart cmd error: ", err)) fmt.Println(Err("exec restart cmd error: ", err))
fmt.Println(Err("V2bX重启失败")) fmt.Println(Err("V2bX重启失败"))

View File

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/common/exec"
"strings" "strings"
) )
@ -13,7 +14,7 @@ const (
) )
func checkRunning() (bool, error) { func checkRunning() (bool, error) {
o, err := execCommand("systemctl status V2bX | grep Active") o, err := exec.RunCommandByShell("systemctl status V2bX | grep Active")
if err != nil { if err != nil {
return false, err return false, err
} }

View File

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/common/exec"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os" "os"
"strings" "strings"
@ -14,7 +15,7 @@ var (
Use: "update", Use: "update",
Short: "Update V2bX version", Short: "Update V2bX version",
Run: func(_ *cobra.Command, _ []string) { Run: func(_ *cobra.Command, _ []string) {
execCommandStd("bash", exec.RunCommandStd("bash",
"<(curl -Ls https://raw.githubusercontents.com/Yuzuki616/V2bX-script/master/install.sh)", "<(curl -Ls https://raw.githubusercontents.com/Yuzuki616/V2bX-script/master/install.sh)",
targetVersion) targetVersion)
}, },
@ -40,7 +41,7 @@ func uninstallHandle(_ *cobra.Command, _ []string) {
if strings.ToLower(yes) != "y" { if strings.ToLower(yes) != "y" {
fmt.Println("已取消卸载") fmt.Println("已取消卸载")
} }
_, err := execCommand("systemctl stop V2bX&&systemctl disable V2bX") _, err := exec.RunCommandByShell("systemctl stop V2bX&&systemctl disable V2bX")
if err != nil { if err != nil {
fmt.Println(Err("exec cmd error: ", err)) fmt.Println(Err("exec cmd error: ", err))
fmt.Println(Err("卸载失败")) fmt.Println(Err("卸载失败"))
@ -50,7 +51,7 @@ func uninstallHandle(_ *cobra.Command, _ []string) {
_ = os.RemoveAll("/etc/V2bX/") _ = os.RemoveAll("/etc/V2bX/")
_ = os.RemoveAll("/usr/local/V2bX/") _ = os.RemoveAll("/usr/local/V2bX/")
_ = os.RemoveAll("/bin/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 { if err != nil {
fmt.Println(Err("exec cmd error: ", err)) fmt.Println(Err("exec cmd error: ", err))
fmt.Println(Err("卸载失败")) fmt.Println(Err("卸载失败"))

View File

@ -30,7 +30,7 @@ func executeX25519() {
var publicKey []byte var publicKey []byte
privateKey = make([]byte, curve25519.ScalarSize) privateKey = make([]byte, curve25519.ScalarSize)
if _, err = rand.Read(privateKey); err != nil { if _, err = rand.Read(privateKey); err != nil {
output = fmt.Sprintf("read rand error: %s", err) output = Err("read rand error: ", err)
return return
} }
@ -41,11 +41,12 @@ func executeX25519() {
privateKey[31] |= 64 privateKey[31] |= 64
if publicKey, err = curve25519.X25519(privateKey, curve25519.Basepoint); err != nil { 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 return
} }
output = fmt.Sprintf("Private key: %v\nPublic key: %v", output = Err("Private key: ",
base64.RawURLEncoding.EncodeToString(privateKey), base64.RawURLEncoding.EncodeToString(privateKey),
"\nPublic key: ",
base64.RawURLEncoding.EncodeToString(publicKey)) base64.RawURLEncoding.EncodeToString(publicKey))
} }

View File

@ -1,4 +1,4 @@
package cmd package exec
import ( import (
"errors" "errors"
@ -6,7 +6,7 @@ import (
"os/exec" "os/exec"
) )
func execCommand(cmd string) (string, error) { func RunCommandByShell(cmd string) (string, error) {
e := exec.Command("bash", "-c", cmd) e := exec.Command("bash", "-c", cmd)
out, err := e.CombinedOutput() out, err := e.CombinedOutput()
if errors.Unwrap(err) == exec.ErrNotFound { if errors.Unwrap(err) == exec.ErrNotFound {
@ -16,7 +16,7 @@ func execCommand(cmd string) (string, error) {
return string(out), err return string(out), err
} }
func execCommandStd(name string, args ...string) { func RunCommandStd(name string, args ...string) {
e := exec.Command(name, args...) e := exec.Command(name, args...)
e.Stdout = os.Stdout e.Stdout = os.Stdout
e.Stdin = os.Stdin e.Stdin = os.Stdin