-
Notifications
You must be signed in to change notification settings - Fork 1
/
exec.go
79 lines (68 loc) · 1.37 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package you
import (
"github.com/sirupsen/logrus"
"io"
"os"
"os/exec"
"runtime"
"time"
)
var cmd *exec.Cmd
var cmdPort = ""
// tips: arm64 平台 特征过于明显,无法过盾
func Exec(port, proxies string, stdout io.Writer, stderr io.Writer) {
cmdPort = port
app := appPath()
if !fileExists(app) {
logrus.Fatalf("executable file not exists: %s", app)
return
}
args := []string{app, "--port", port}
if proxies != "" {
args = append(args, "--proxies", proxies)
}
cmd = exec.Command(app, args...)
if stdout == nil {
cmd.Stdout = os.Stdout
}
if stderr == nil {
cmd.Stderr = os.Stderr
}
go func() {
if err := cmd.Run(); err != nil {
logrus.Fatalf("executable file error: %v", err)
return
}
}()
time.Sleep(5 * time.Second)
logrus.Info("helper exec running ...")
}
func appPath() string {
app := "bin/"
switch runtime.GOOS {
case "linux":
// 可惜了,arm过不了验证
if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
app += "linux/helper-arm64"
} else {
app += "linux/helper"
}
case "darwin":
app += "osx/helper"
case "windows":
app += "windows/helper.exe"
default:
logrus.Fatalf("Unsupported platform: %s", runtime.GOOS)
}
return app
}
func Exit() {
if cmd == nil {
return
}
_ = cmd.Process.Kill()
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}