/
v2ray.go
376 lines (331 loc) · 8.35 KB
/
v2ray.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package v2ray
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/gopherty/v2ray-web/v2raylogs"
"github.com/gin-gonic/gin/binding"
"github.com/gopherty/v2ray-web/model"
"github.com/gopherty/v2ray-web/token"
"github.com/gorilla/websocket"
// v2ray core 启动的依赖
_ "v2ray.com/core/app/dispatcher"
_ "v2ray.com/core/app/proxyman/inbound"
_ "v2ray.com/core/app/proxyman/outbound"
"v2ray.com/core"
"github.com/gin-gonic/gin"
"github.com/gopherty/v2ray-web/logger"
"github.com/gopherty/v2ray-web/serve"
"github.com/gopherty/v2ray-web/utils"
)
// 启动成功后保存实例的状态用于关闭
var (
instance *core.Instance
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// v2ray 服务状态, true 代表运行,false 代表停止。
Protocol string // 协议名称
ID int // 协议id
Running = false
)
// Dispatcher v2ray 功能相关的控制器
type Dispatcher struct {
}
// Start 启动 v2ray 服务
func (Dispatcher) Start(c *gin.Context) {
// 将前端传过来的参数解析成 JSON 格式写入到文件中。
protocol, id, err := parmasToJSON(c)
if err != nil {
logger.Logger().Error(err.Error())
c.JSON(http.StatusInternalServerError, model.BackToFrontEndData{
Code: serve.StatusParamNotMatched,
Description: "解析参数错误",
Error: err.Error(),
})
return
}
if instance != nil {
err := instance.Start()
if err != nil {
logger.Logger().Error(err.Error())
c.JSON(http.StatusInternalServerError, model.BackToFrontEndData{
Code: serve.StatusV2rayError,
Description: "v2ray 启动错误",
Error: err.Error(),
})
return
}
// 获取到控制台资源
_, w := v2raylogs.Source()
os.Stdout = w
// 保存 v2ray 的状态
Protocol = protocol
ID = id
Running = true
c.JSON(http.StatusOK, model.BackToFrontEndData{
Code: serve.StatusOK,
Data: map[string]interface{}{
"msg": "v2ray 服务启动成功",
},
})
return
}
// 暂定 v2ray 配置文件名称和位置硬编码,因为是通过 web-ui 来对配置文件进行操作。
path := utils.BasePath() + "/v2ray.json"
file, err := os.Open(path)
if err != nil {
logger.Logger().Error(err.Error())
c.JSON(http.StatusInternalServerError, model.BackToFrontEndData{
Code: serve.StatusServerError,
Description: "打开 v2ray 配置文件出错",
Error: err.Error(),
})
return
}
defer file.Close()
// 需要 json 格式注册解析器,默认为 protobuf。
// 注册配置文件加载器
core.RegisterConfigLoader(&core.ConfigFormat{
Name: "JSON",
Extension: []string{"json"},
Loader: loaderJSON,
})
// 解析 v2ray 的配置文件
config, err := core.LoadConfig("json", "v2ray.json", file)
if err != nil {
logger.Logger().Error(err.Error())
c.JSON(http.StatusInternalServerError, model.BackToFrontEndData{
Code: serve.StatusServerError,
Description: "加载 v2ray 配置文件出错",
Error: err.Error(),
})
return
}
// 创建 v2ray 实例
instance, err = core.New(config)
if err != nil {
logger.Logger().Error(err.Error())
c.JSON(http.StatusInternalServerError, model.BackToFrontEndData{
Code: serve.StatusV2rayError,
Description: "创建 v2ray 实例出错",
Error: err.Error(),
})
return
}
err = instance.Start()
if err != nil {
logger.Logger().Error(err.Error())
c.JSON(http.StatusInternalServerError, model.BackToFrontEndData{
Code: serve.StatusV2rayError,
Description: "v2ray 启动错误",
Error: err.Error(),
})
return
}
// 保存 v2ray 的状态
Protocol = protocol
ID = id
Running = true
c.JSON(http.StatusOK, model.BackToFrontEndData{
Code: serve.StatusOK,
Data: map[string]interface{}{
"msg": "服务启动成功",
},
})
}
// Stop 关闭 v2ray 服务
func (Dispatcher) Stop(c *gin.Context) {
if instance == nil {
c.JSON(http.StatusInternalServerError, model.BackToFrontEndData{
Code: serve.StatusV2rayError,
Error: "服务未启动",
})
return
}
err := instance.Close()
if err != nil {
logger.Logger().Error(err.Error())
c.JSON(http.StatusInternalServerError, model.BackToFrontEndData{
Code: serve.StatusV2rayError,
Description: "v2ray 关闭服务失败",
Error: err.Error(),
})
return
}
// 保存 v2ray 的状态
Protocol = ""
ID = 0
Running = false
c.JSON(http.StatusOK, model.BackToFrontEndData{
Code: serve.StatusOK,
Data: map[string]interface{}{
"msg": "服务关闭成功",
},
})
}
// Logs v2ray 启动后日志输出接口
func (Dispatcher) Logs(c *gin.Context) {
// 获取日志输出
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
defer conn.Close()
if err != nil {
logger.Logger().Error(err.Error())
return
}
// 先将 http 请求升级为 websocket ,在验证 token 是否过期,如果过期则返回 websocket 中的保留状态码 5000-10000
// 客户端知道 token 过期,就将刷新 token 或 重新登录后再次请求该接口。
err = token.ValidWSToken(c.Request)
if err != nil {
logger.Logger().Error(err.Error())
closed := conn.CloseHandler()
err = closed(5001, "unauthrizationed")
if err != nil {
logger.Logger().Error(err.Error())
}
return
}
// 信号量,用于关闭发送消息协程。
signal := make(chan int)
go func() {
for {
select {
case msg := <-v2raylogs.LogsMsg():
err = conn.WriteMessage(websocket.TextMessage, msg)
if err != nil {
logger.Logger().Error(err.Error())
return
}
case <-signal:
return
}
}
}()
// 读取客户端发送的停止日志输出。
for {
_, _, err := conn.ReadMessage()
if err != nil {
signal <- 1 // 中断发送消息操作
logger.Logger().Error(err.Error())
break
}
}
}
// Status 服务器端 v2ray 的状态
func (Dispatcher) Status(c *gin.Context) {
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
defer conn.Close()
if err != nil {
logger.Logger().Error(err.Error())
return
}
// 先将 http 请求升级为 websocket ,在验证 token 是否过期,如果过期则返回 websocket 中的保留状态码 5000-10000
// 客户端知道 token 过期,就将刷新 token 或 重新登录后再次请求该接口。
err = token.ValidWSToken(c.Request)
if err != nil {
logger.Logger().Error(err.Error())
closed := conn.CloseHandler()
err = closed(5001, "unauthrizationed")
if err != nil {
logger.Logger().Error(err.Error())
}
return
}
// 发送服务器端 v2ray 的状态
go func() {
for {
err := conn.WriteJSON(gin.H{
"running": Running,
"protocol": Protocol,
"id": ID,
})
if err != nil {
break
}
time.Sleep(1e9)
}
}()
// 读取客户端发送的关闭连接。
for {
_, _, err := conn.ReadMessage()
if err != nil {
logger.Logger().Error(err.Error())
break
}
}
}
// parmasToJSON 将 v2ray 启动参数转化为配置文件
func parmasToJSON(c *gin.Context) (protocol string, id int, err error) {
// 绑定参数
var param ParamStart
err = c.ShouldBindWith(¶m, binding.Default(c.Request.Method, c.ContentType()))
if err != nil {
return
}
// 入口协议
destOverride := []string{"http", "tls"}
sock := Inbound{
Port: 1080,
Listen: "127.0.0.1",
Protocol: "socks",
Settings: Socks{
Auth: "noauth",
},
Sniffing: Sniffing{
Enabled: true,
DestOverride: destOverride,
},
}
inbounds := []Inbound{sock}
user := User{
ID: param.UserID,
AlterID: param.AlertID,
Level: param.Level,
Security: param.Security,
}
users := []User{user}
vmess := Vmess{
Address: param.Address,
Port: param.Port,
Users: users,
}
protocols := []interface{}{vmess}
outboundConf := OutboundConfiguration{
Vnext: protocols,
}
outbound := Outbound{
Protocol: "vmess",
Settings: outboundConf,
StreamSettings: StreamSettings{
NetWork: param.Network,
Security: param.NetSecurity,
WSSettings: WebSocket{
Path: param.Path,
},
},
Mux: Mux{
Enabled: true,
},
}
outbounds := []Outbound{outbound}
cnf := Config{
Inbounds: inbounds,
Outbounds: outbounds,
}
str, err := json.MarshalIndent(&cnf, "", " ")
if err != nil {
return
}
path := utils.BasePath() + "/v2ray.json"
err = ioutil.WriteFile(path, str, 0666)
if err != nil {
return
}
return param.Protocol, param.ID, err
}