forked from xiaokangwang/AndroidLibV2ray
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathinteract.go
More file actions
163 lines (130 loc) · 3.78 KB
/
interact.go
File metadata and controls
163 lines (130 loc) · 3.78 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package libv2ray
import (
"os"
// The following are necessary as they register handlers in their init functions.
_ "v2ray.com/core/app/router/rules"
"v2ray.com/core/common/log"
"v2ray.com/core/shell/point"
// The following are necessary as they register handlers in their init functions.
_ "v2ray.com/core/proxy/blackhole"
_ "v2ray.com/core/proxy/dokodemo"
_ "v2ray.com/core/proxy/freedom"
_ "v2ray.com/core/proxy/http"
_ "v2ray.com/core/proxy/shadowsocks"
_ "v2ray.com/core/proxy/socks"
_ "v2ray.com/core/proxy/vmess/inbound"
_ "v2ray.com/core/proxy/vmess/outbound"
// The following are necessary as they register handlers in their init functions.
_ "v2ray.com/core/transport/internet/kcp"
_ "v2ray.com/core/transport/internet/tcp"
_ "v2ray.com/core/transport/internet/udp"
_ "v2ray.com/core/transport/internet/ws"
// The following are necessary as they register handlers in their init functions.
_ "v2ray.com/core/transport/internet/authenticators/noop"
_ "v2ray.com/core/transport/internet/authenticators/srtp"
_ "v2ray.com/core/transport/internet/authenticators/utp"
)
/*V2RayPoint V2Ray Point Server
This is territory of Go, so no getter and setters!
Notice:
ConfigureFile can be either the path of config file or
"V2Ray_internal/ConfigureFileContent" in case you wish to
*/
type V2RayPoint struct {
ConfigureFile string
ConfigureFileContent string
Callbacks V2RayCallbacks
vpoint *point.Point
IsRunning bool
conf *libv2rayconf
escortProcess *[](*os.Process)
unforgivnesschan chan int
VpnSupportSet V2RayVPNServiceSupportsSet
VpnSupportnodup bool
PackageName string
cfgtmpvarsi cfgtmpvars
}
/*V2RayCallbacks a Callback set for V2Ray
*/
type V2RayCallbacks interface {
OnEmitStatus(int, string) int
}
func (v *V2RayPoint) pointloop() {
v.VpnSupportnodup = false
if v.parseConf() != nil {
return
}
err := v.checkIfRcExist()
if err != nil {
log.Error("Failed to copy asset", err)
v.Callbacks.OnEmitStatus(-1, "Failed to copy asset ("+err.Error()+")")
}
log.Info("v.renderAll() ")
v.renderAll()
config, err := point.LoadConfig(v.parseCfg())
if err != nil {
log.Error("Failed to read config file (", v.ConfigureFile, "): ", v.ConfigureFile, err)
v.Callbacks.OnEmitStatus(-1, "Failed to read config file ("+v.ConfigureFile+")")
return
}
vPoint, err := point.NewPoint(config)
if err != nil {
log.Error("Failed to create Point server: ", err)
v.Callbacks.OnEmitStatus(-1, "Failed to create Point server ("+err.Error()+")")
return
}
v.IsRunning = true
log.Info("vPoint.Start() ")
vPoint.Start()
v.vpoint = vPoint
log.Info("v.escortingUP() ")
v.escortingUP()
v.vpnSetup()
if v.conf != nil {
env := v.conf.additionalEnv
log.Info("Exec Upscript() ")
err = v.runbash(v.conf.upscript, env)
if err != nil {
log.Error("OnUp failed to exec: ", err)
}
}
v.Callbacks.OnEmitStatus(0, "Running")
v.parseCfgDone()
}
/*RunLoop Run V2Ray main loop
*/
func (v *V2RayPoint) RunLoop() {
go v.pointloop()
}
func (v *V2RayPoint) stopLoopW() {
v.IsRunning = false
v.vpoint.Close()
if v.conf != nil {
env := v.conf.additionalEnv
log.Info("Running downscript")
err := v.runbash(v.conf.downscript, env)
if err != nil {
log.Error("OnDown failed to exec: ", err)
}
log.Info("v.escortingDown() ")
v.escortingDown()
}
v.Callbacks.OnEmitStatus(0, "Closed")
}
/*StopLoop Stop V2Ray main loop
*/
func (v *V2RayPoint) StopLoop() {
v.vpnShutdown()
go v.stopLoopW()
}
/*NewV2RayPoint new V2RayPoint*/
func NewV2RayPoint() *V2RayPoint {
return &V2RayPoint{unforgivnesschan: make(chan int)}
}
/*NetworkInterrupted inform us to restart the v2ray,
closing dead connections.
*/
func (v *V2RayPoint) NetworkInterrupted() {
v.vpoint.Close()
v.vpoint.Start()
}