fix: capture full output of v2ray

This commit is contained in:
vzz64 committed 2023-01-03 22:45:35 +08:00
1 parent 84abb87977
commit 6d7069595b
1 file changed
+6 -27
+6 -27
View File
@@ -1,8 +1,8 @@
package where
import (
"bytes"
"fmt"
"github.com/v2rayA/v2rayA/conf"
"os"
"os/exec"
"path/filepath"
@@ -10,6 +10,8 @@ import (
"strings"
"sync"
"time"
"github.com/v2rayA/v2rayA/conf"
)
type Variant string
@@ -29,26 +31,6 @@ var v2rayVersion struct {
mu sync.Mutex
}
type onceWriter struct {
buf []byte
callback func(buf []byte)
}
func newOnceWriter(callback func(buf []byte)) *onceWriter {
return &onceWriter{
callback: callback,
}
}
func (r *onceWriter) Write(p []byte) (n int, err error) {
defer func() {
go r.callback(r.buf)
}()
r.buf = make([]byte, len(p))
copy(r.buf, p)
return len(p), nil
}
/* get the version of v2ray-core without 'v' like 4.23.1 */
func GetV2rayServiceVersion() (variant Variant, ver string, err error) {
// cache for 10 seconds
@@ -61,13 +43,10 @@ func GetV2rayServiceVersion() (variant Variant, ver string, err error) {
if err != nil || len(v2rayPath) <= 0 {
return Unknown, "", fmt.Errorf("cannot find v2ray executable binary")
}
var output []byte
var done = make(chan struct{}, 2)
cmd := exec.Command(v2rayPath, "version")
cmd.Stdout = newOnceWriter(func(buf []byte) {
output = buf
done <- struct{}{}
})
output := bytes.NewBuffer(nil)
cmd.Stdout = output
cmd.Stderr = cmd.Stdout
if err := cmd.Start(); err != nil {
return Unknown, "", err
@@ -80,7 +59,7 @@ func GetV2rayServiceVersion() (variant Variant, ver string, err error) {
}()
<-done
var fields []string
if fields = strings.Fields(strings.TrimSpace(string(output))); len(fields) < 2 {
if fields = strings.Fields(output.String()); len(fields) < 2 {
return Unknown, "", fmt.Errorf("cannot parse version of v2ray")
}
ver = fields[1]