forked from xiaokangwang/AndroidLibV2ray
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathescort.go
101 lines (90 loc) · 2.09 KB
/
escort.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package libv2ray
import (
"os"
"os/exec"
"log"
)
func (v *V2RayPoint) escortRun(proc string, pt []string, forgiveable bool, tapfd int) {
count := 42
for count > 0 {
cmd := exec.Command(proc, pt...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
env := v.conf.additionalEnv
env = append(env, os.Environ()...)
env = v.addEnvironment(env)
cmd.Env = env
if tapfd != 0 {
file := os.NewFile(uintptr(tapfd), "/dev/tap0")
var files []*os.File
cmd.ExtraFiles = append(files, file)
}
err := cmd.Start()
log.Println(proc)
log.Println(pt)
if err != nil {
log.Println(err)
}
*v.escortProcess = append(*v.escortProcess, cmd.Process)
log.Println("Waiting....")
err = cmd.Wait()
log.Println("Exit")
log.Println(err)
if v.IsRunning {
log.Println("Unexpected Exit")
count--
} else {
return
}
}
if v.IsRunning && !forgiveable {
v.unforgivnesschan <- 0
}
}
func (v *V2RayPoint) escortBeg(proc string, pt []string, forgiveable bool) {
go v.escortRun(proc, pt, forgiveable, 0)
}
func (v *V2RayPoint) unforgivenessCloser() {
log.Println("unforgivenessCloser() <-v.unforgivnesschan")
<-v.unforgivnesschan
if v.IsRunning {
v.StopLoop()
log.Println("Closed As unforgivenessCloser decided so.")
v.Callbacks.OnEmitStatus(0, "Closed As unforgivenessCloser decided so.")
}
remain := true
for remain {
select {
case <-v.unforgivnesschan:
log.Println("unforgivenessCloser() removing reminder unforgivness sign")
break
default:
remain = false
}
}
log.Println("unforgivenessCloser() quit")
}
func (v *V2RayPoint) escortingUP() {
if v.escortProcess != nil {
return
}
v.escortProcess = new([](*os.Process))
go v.unforgivenessCloser()
for _, esct := range v.conf.esco {
v.escortBeg(esct.Target, esct.Args, esct.Forgiveable)
}
}
func (v *V2RayPoint) escortingDown() {
log.Println("escortingDown() Killing all escorted process ")
if v.escortProcess == nil {
return
}
for _, pr := range *v.escortProcess {
pr.Kill()
}
log.Println("escortingDown() v.unforgivnesschan <- 0")
select {
case v.unforgivnesschan <- 0:
}
v.escortProcess = nil
}