From 395e028e97c76786c8a52ca53770b1bd5d4b90b2 Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Tue, 30 Mar 2021 14:36:42 +0800 Subject: [PATCH 1/5] fix(#154): failed to judge the path of V2Ray binary --- service/core/ipforward/ipforward.go | 3 +- service/core/v2ray/asset/asset.go | 56 ++++++++----------------- service/core/v2ray/where/where.go | 63 ++++++++++++++++------------- service/go.mod | 2 +- 4 files changed, 55 insertions(+), 69 deletions(-) diff --git a/service/core/ipforward/ipforward.go b/service/core/ipforward/ipforward.go index 88f8e9e8..e1faf71c 100644 --- a/service/core/ipforward/ipforward.go +++ b/service/core/ipforward/ipforward.go @@ -1,12 +1,13 @@ package ipforward import ( + "os" "os/exec" "strings" ) func IsIpForwardOn() bool { - out, err := exec.Command("sh", "-c", "cat /proc/sys/net/ipv4/ip_forward").Output() + out, err := os.ReadFile("/proc/sys/net/ipv4/ip_forward") return err == nil && strings.TrimSpace(string(out)) == "1" } diff --git a/service/core/v2ray/asset/asset.go b/service/core/v2ray/asset/asset.go index 7a0a4ae8..9c85924f 100644 --- a/service/core/v2ray/asset/asset.go +++ b/service/core/v2ray/asset/asset.go @@ -11,60 +11,36 @@ import ( "io/ioutil" "log" "os" - "os/exec" "path" "regexp" - "strings" "sync" "time" v2router "v2ray.com/core/app/router" "v2ray.com/core/common/strmatcher" ) -var v2rayLocationAsset *string func GetV2rayLocationAsset() (s string) { - if v2rayLocationAsset != nil { - return *v2rayLocationAsset - } - switch global.ServiceControlMode { - case global.SystemctlMode, global.ServiceMode: - p, _ := where.GetV2rayServiceFilePath() - out, err := exec.Command("sh", "-c", "cat "+p+"|grep Environment=V2RAY_LOCATION_ASSET").CombinedOutput() - if err != nil { - break - } - s = strings.TrimSpace(string(out)) - s = s[len("Environment=V2RAY_LOCATION_ASSET="):] - } - var err error - if s == "" { - //fine, guess one - var candidates = []string{`/usr/local/share/v2ray`, `/usr/share/v2ray`} - var ver string - var is bool - if ver, err = where.GetV2rayServiceVersion(); err == nil { - if is, err = common.VersionGreaterEqual(ver, "4.27.1"); is { - for _, c := range candidates { - if _, err := os.Stat(c); os.IsNotExist(err) { - continue - } - s = c - break + var candidates = []string{`/usr/local/share/v2ray`, `/usr/share/v2ray`} + var is bool + if ver, err := where.GetV2rayServiceVersion(); err == nil { + if is, err = common.VersionGreaterEqual(ver, "4.27.1"); is { + for _, c := range candidates { + if _, err := os.Stat(c); os.IsNotExist(err) { + continue } + s = c + break } } - if s == "" { - //maybe v2ray working directory - v2rayPath, err := where.GetV2rayBinPath() - if err != nil { - s = "/etc/v2ray" - } - s = path.Dir(v2rayPath) + } + if s == "" { + //maybe v2ray working directory + v2rayPath, err := where.GetV2rayBinPath() + if err != nil { + s = "/etc/v2ray" } - } else { - //save the result if not by guess - v2rayLocationAsset = &s + s = path.Dir(v2rayPath) } return } diff --git a/service/core/v2ray/where/where.go b/service/core/v2ray/where/where.go index d1a61af8..605258e8 100644 --- a/service/core/v2ray/where/where.go +++ b/service/core/v2ray/where/where.go @@ -1,6 +1,7 @@ package where import ( + "bytes" "fmt" "github.com/v2rayA/v2rayA/global" "os/exec" @@ -10,15 +11,41 @@ import ( var NotFoundErr = fmt.Errorf("not found") var ServiceNameList = []string{"v2ray"} -func GetV2rayServiceFilePath() (path string, err error) { +func GetV2rayServiceFileContent() (content string, err error) { + switch global.ServiceControlMode { + case global.SystemctlMode: + content, err = getV2raySystemdServiceFileContent() + if err != nil { + break + } + case global.ServiceMode: + p, _ := getV2rayServiceFilePath() + var out []byte + out, err = exec.Command("sh", "-c", "cat "+p+"|grep Environment=V2RAY_LOCATION_ASSET").CombinedOutput() + if err != nil { + break + } + content = string(bytes.TrimSpace(out)) + } + return +} +func getV2rayServiceFilePath() (path string, err error) { for _, target := range ServiceNameList { - if path, err = getV2rayServiceFilePath(target); err == nil { + if path, err = _getV2rayServiceFilePath(target); err == nil { return } } return } -func getV2rayServiceFilePath(target string) (path string, err error) { +func getV2raySystemdServiceFileContent() (content string, err error) { + for _, target := range ServiceNameList { + if out, err := exec.Command("sh", "-c", "systemctl cat "+target).CombinedOutput(); err == nil { + return string(bytes.TrimSpace(out)), nil + } + } + return +} +func _getV2rayServiceFilePath(target string) (path string, err error) { var out []byte if global.ServiceControlMode == global.SystemctlMode { out, err = exec.Command("sh", "-c", "systemctl status "+target+"|grep /"+target+".service").CombinedOutput() @@ -51,7 +78,7 @@ func getV2rayServiceFilePath(target string) (path string, err error) { l := strings.Index(sout, "/") r := strings.Index(sout, "/"+target+".service") if l < 0 || r < 0 { - err = newError("failure: getV2rayServiceFilePath") + err = newError("failure: _getV2rayServiceFilePath") return } path = sout[l : r+len("/"+target+".service")] @@ -95,30 +122,12 @@ func getV2rayBinPathAnyway() (path string, err error) { func getV2rayBinPath(target string) (string, error) { var pa string - switch global.ServiceControlMode { - case global.SystemctlMode, global.ServiceMode: - //从systemd的启动参数里找 - p, err := getV2rayServiceFilePath(target) - if err != nil { - pa = "" - break - } - out, err := exec.Command("sh", "-c", "cat "+p+"|grep ExecStart=").CombinedOutput() - if err != nil { - pa = "" - break - } - arr := strings.SplitN(strings.TrimSpace(string(out)), " ", 2) - pa = strings.TrimPrefix(arr[0], "ExecStart=") - } - if pa == "" { - //从环境变量里找 - out, err := exec.Command("sh", "-c", "which "+target).CombinedOutput() - if err != nil { - return "", NotFoundErr - } - pa = strings.TrimSpace(string(out)) + //从环境变量里找 + out, err := exec.Command("sh", "-c", "which "+target).CombinedOutput() + if err != nil { + return "", NotFoundErr } + pa = strings.TrimSpace(string(out)) if pa == "" { return "", NotFoundErr } diff --git a/service/go.mod b/service/go.mod index 1ea6e71c..db51eb26 100644 --- a/service/go.mod +++ b/service/go.mod @@ -1,6 +1,6 @@ module github.com/v2rayA/v2rayA -go 1.13 +go 1.16 require ( github.com/beevik/ntp v0.3.0 From 171bb2a403ddaf614a2084b5686956959afc6ca1 Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Tue, 30 Mar 2021 14:39:59 +0800 Subject: [PATCH 2/5] feat: find xray if v2ray not exists --- service/core/v2ray/asset/asset.go | 3 +- service/core/v2ray/where/where.go | 77 +------------------------------ 2 files changed, 2 insertions(+), 78 deletions(-) diff --git a/service/core/v2ray/asset/asset.go b/service/core/v2ray/asset/asset.go index 9c85924f..d4bcebf6 100644 --- a/service/core/v2ray/asset/asset.go +++ b/service/core/v2ray/asset/asset.go @@ -19,9 +19,8 @@ import ( "v2ray.com/core/common/strmatcher" ) - func GetV2rayLocationAsset() (s string) { - var candidates = []string{`/usr/local/share/v2ray`, `/usr/share/v2ray`} + var candidates = []string{`/usr/local/share/v2ray`, `/usr/share/v2ray`, `/usr/local/share/xray`, `/usr/share/xray`} var is bool if ver, err := where.GetV2rayServiceVersion(); err == nil { if is, err = common.VersionGreaterEqual(ver, "4.27.1"); is { diff --git a/service/core/v2ray/where/where.go b/service/core/v2ray/where/where.go index 605258e8..5ead3cf6 100644 --- a/service/core/v2ray/where/where.go +++ b/service/core/v2ray/where/where.go @@ -1,7 +1,6 @@ package where import ( - "bytes" "fmt" "github.com/v2rayA/v2rayA/global" "os/exec" @@ -9,81 +8,7 @@ import ( ) var NotFoundErr = fmt.Errorf("not found") -var ServiceNameList = []string{"v2ray"} - -func GetV2rayServiceFileContent() (content string, err error) { - switch global.ServiceControlMode { - case global.SystemctlMode: - content, err = getV2raySystemdServiceFileContent() - if err != nil { - break - } - case global.ServiceMode: - p, _ := getV2rayServiceFilePath() - var out []byte - out, err = exec.Command("sh", "-c", "cat "+p+"|grep Environment=V2RAY_LOCATION_ASSET").CombinedOutput() - if err != nil { - break - } - content = string(bytes.TrimSpace(out)) - } - return -} -func getV2rayServiceFilePath() (path string, err error) { - for _, target := range ServiceNameList { - if path, err = _getV2rayServiceFilePath(target); err == nil { - return - } - } - return -} -func getV2raySystemdServiceFileContent() (content string, err error) { - for _, target := range ServiceNameList { - if out, err := exec.Command("sh", "-c", "systemctl cat "+target).CombinedOutput(); err == nil { - return string(bytes.TrimSpace(out)), nil - } - } - return -} -func _getV2rayServiceFilePath(target string) (path string, err error) { - var out []byte - if global.ServiceControlMode == global.SystemctlMode { - out, err = exec.Command("sh", "-c", "systemctl status "+target+"|grep /"+target+".service").CombinedOutput() - if err != nil { - err = newError(strings.TrimSpace(string(out))) - if !strings.Contains(string(out), "not be found") { - path = `/usr/lib/systemd/system/` + target + `.service` - return - } - } - } else if global.ServiceControlMode == global.ServiceMode { - out, err = exec.Command("sh", "-c", "service "+target+" status|grep /"+target+".service").CombinedOutput() - if err != nil || strings.TrimSpace(string(out)) == "(Reason:" { - if !strings.Contains(string(out), "not be found") { - path = `/lib/systemd/system/` + target + `.service` - return - } - if err != nil { - err = newError(strings.TrimSpace(string(out))) - } - } - } else { - err = newError("commands systemctl and service not found") - return - } - if err != nil { - return - } - sout := string(out) - l := strings.Index(sout, "/") - r := strings.Index(sout, "/"+target+".service") - if l < 0 || r < 0 { - err = newError("failure: _getV2rayServiceFilePath") - return - } - path = sout[l : r+len("/"+target+".service")] - return -} +var ServiceNameList = []string{"v2ray", "xray"} /* get the version of v2ray-core without 'v' like 4.23.1 */ func GetV2rayServiceVersion() (ver string, err error) { From ace97159e4e640b22f20481cb01274a3f6097e3c Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Sat, 17 Apr 2021 19:20:36 +0800 Subject: [PATCH 3/5] chore(aur): optdepends v2ray and xray --- install/aur/v2raya-bin/.SRCINFO | 3 ++- install/aur/v2raya-bin/PKGBUILD | 3 ++- install/aur/v2raya/.SRCINFO | 3 ++- install/aur/v2raya/PKGBUILD | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/install/aur/v2raya-bin/.SRCINFO b/install/aur/v2raya-bin/.SRCINFO index 8eb6d5e5..d87a33ed 100644 --- a/install/aur/v2raya-bin/.SRCINFO +++ b/install/aur/v2raya-bin/.SRCINFO @@ -11,7 +11,8 @@ pkgbase = v2raya-bin arch = aarch64 license = GPL3 depends = glibc - depends = v2ray + optdepends = v2ray>=4.37.0-1 + optdepends = xray>=1.4.2-1 provides = v2raya conflicts = v2raya source = v2raya.service diff --git a/install/aur/v2raya-bin/PKGBUILD b/install/aur/v2raya-bin/PKGBUILD index 2b844e27..e43545e0 100644 --- a/install/aur/v2raya-bin/PKGBUILD +++ b/install/aur/v2raya-bin/PKGBUILD @@ -9,7 +9,8 @@ pkgdesc="A web GUI client of Project V which supports VMess, VLESS, SS, SSR, Tro arch=('i686' 'x86_64' 'armv7h' 'armv6h' 'aarch64') url="https://github.com/v2rayA/v2rayA" license=('GPL3') -depends=('glibc' 'v2ray') +depends=('glibc') +optdepends=('v2ray>=4.37.0-1' 'xray>=1.4.2-1') provides=('v2raya') conflicts=('v2raya') diff --git a/install/aur/v2raya/.SRCINFO b/install/aur/v2raya/.SRCINFO index 0604eef1..e2f7f3a5 100644 --- a/install/aur/v2raya/.SRCINFO +++ b/install/aur/v2raya/.SRCINFO @@ -15,7 +15,8 @@ pkgbase = v2raya makedepends = yarn makedepends = python2 depends = glibc - depends = v2ray + optdepends = v2ray>=4.37.0-1 + optdepends = xray>=1.4.2-1 source = v2raya-{{pkgver}}.tar.gz::https://github.com/v2rayA/v2rayA/archive/v{{pkgver}}.tar.gz sha512sums = SKIP diff --git a/install/aur/v2raya/PKGBUILD b/install/aur/v2raya/PKGBUILD index ae87c82f..11a74481 100644 --- a/install/aur/v2raya/PKGBUILD +++ b/install/aur/v2raya/PKGBUILD @@ -8,7 +8,8 @@ pkgdesc="A web GUI client of Project V which supports VMess, VLESS, SS, SSR, Tro arch=('i686' 'x86_64' 'armv7h' 'armv6h' 'aarch64') url="https://github.com/v2rayA/v2rayA" license=('GPL3') -depends=('glibc' 'v2ray') +depends=('glibc') +optdepends=('v2ray>=4.37.0-1' 'xray>=1.4.2-1') makedepends=('go>=2:1.12.3-1' 'nodejs>=14' 'yarn' 'python2') source=("$pkgname-$pkgver.tar.gz::https://github.com/v2rayA/v2rayA/archive/v$pkgver.tar.gz") sha512sums=('SKIP') From 09fb67f68196d99a0acd86ba94fc421173b23b49 Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Sat, 17 Apr 2021 19:22:11 +0800 Subject: [PATCH 4/5] chore(aur):go version --- install/aur/v2raya/.SRCINFO | 2 +- install/aur/v2raya/PKGBUILD | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/install/aur/v2raya/.SRCINFO b/install/aur/v2raya/.SRCINFO index e2f7f3a5..736ab817 100644 --- a/install/aur/v2raya/.SRCINFO +++ b/install/aur/v2raya/.SRCINFO @@ -10,7 +10,7 @@ pkgbase = v2raya arch = armv6h arch = aarch64 license = GPL3 - makedepends = go>=2:1.12.3-1 + makedepends = go>=2:1.16.0-1 makedepends = nodejs>=14 makedepends = yarn makedepends = python2 diff --git a/install/aur/v2raya/PKGBUILD b/install/aur/v2raya/PKGBUILD index 11a74481..6635df50 100644 --- a/install/aur/v2raya/PKGBUILD +++ b/install/aur/v2raya/PKGBUILD @@ -10,7 +10,7 @@ url="https://github.com/v2rayA/v2rayA" license=('GPL3') depends=('glibc') optdepends=('v2ray>=4.37.0-1' 'xray>=1.4.2-1') -makedepends=('go>=2:1.12.3-1' 'nodejs>=14' 'yarn' 'python2') +makedepends=('go>=2:1.16.0-1' 'nodejs>=14' 'yarn' 'python2') source=("$pkgname-$pkgver.tar.gz::https://github.com/v2rayA/v2rayA/archive/v$pkgver.tar.gz") sha512sums=('SKIP') From 3340590daaaa5e3af6904aecc3c4afafc2da3ca7 Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Sat, 17 Apr 2021 19:32:03 +0800 Subject: [PATCH 5/5] chore(travis): use go1.16 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 87928d0e..f495e112 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ language: - node_js - ruby go: - - 1.15.x + - 1.16.x if: (tag =~ ^v) OR (tag =~ ^test) OR (branch = master) OR (branch == test)