forked from alliswell2day/v2ray-sspanel-v3-mod_Uim-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
panel.go
357 lines (338 loc) · 11.1 KB
/
panel.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
package v2ray_sspanel_v3_mod_Uim_plugin
import (
"fmt"
"github.com/rico93/v2ray-sspanel-v3-mod_Uim-plugin/Manager"
"github.com/rico93/v2ray-sspanel-v3-mod_Uim-plugin/client"
"github.com/rico93/v2ray-sspanel-v3-mod_Uim-plugin/config"
"github.com/rico93/v2ray-sspanel-v3-mod_Uim-plugin/model"
"github.com/rico93/v2ray-sspanel-v3-mod_Uim-plugin/speedtest"
"github.com/rico93/v2ray-sspanel-v3-mod_Uim-plugin/webapi"
"github.com/robfig/cron"
"google.golang.org/grpc"
"reflect"
"runtime"
)
type Panel struct {
db *webapi.Webapi
manager *Manager.Manager
speedtestClient speedtest.Client
downwithpanel int
}
func NewPanel(gRPCConn *grpc.ClientConn, db *webapi.Webapi, cfg *config.Config) (*Panel, error) {
opts := speedtest.NewOpts()
speedtestClient := speedtest.NewClient(opts)
var newpanel = Panel{
speedtestClient: speedtestClient,
db: db,
downwithpanel: cfg.DownWithPanel,
manager: &Manager.Manager{
HandlerServiceClient: client.NewHandlerServiceClient(gRPCConn, "MAIN_INBOUND"),
StatsServiceClient: client.NewStatsServiceClient(gRPCConn),
UserRuleServiceClient: client.NewUserRuleServerClient(gRPCConn),
NodeID: cfg.NodeID,
CheckRate: cfg.CheckRate,
SpeedTestCheckRate: cfg.SpeedTestCheckRate,
CurrentNodeInfo: &model.NodeInfo{},
NextNodeInfo: &model.NodeInfo{},
Users: map[string]model.UserModel{},
UserToBeMoved: map[string]model.UserModel{},
UserToBeAdd: map[string]model.UserModel{},
Id2PrefixedIdmap: map[uint]string{},
Id2DisServer: map[uint]string{},
},
}
return &newpanel, nil
}
func (p *Panel) Start() {
doFunc := func() {
if err := p.do(); err != nil {
newError("panel#do").Base(err).AtError().WriteToLog()
}
// Explicitly triggering GC to remove garbage
runtime.GC()
}
doFunc()
speedTestFunc := func() {
result, err := speedtest.GetSpeedtest(p.speedtestClient)
if err != nil {
newError("panel#speedtest").Base(err).AtError().WriteToLog()
}
newError(result).AtInfo().WriteToLog()
if p.db.UploadSpeedTest(p.manager.NodeID, result) {
newError("succesfully upload speedtest result").AtInfo().WriteToLog()
} else {
newError("failed to upload speedtest result").AtInfo().WriteToLog()
}
// Explicitly triggering GC to remove garbage
runtime.GC()
}
c := cron.New()
err := c.AddFunc(fmt.Sprintf("@every %ds", p.manager.CheckRate), doFunc)
if err != nil {
fatal(err)
}
if p.manager.SpeedTestCheckRate > 0 {
newErrorf("@every %dh", p.manager.SpeedTestCheckRate).AtInfo().WriteToLog()
err = c.AddFunc(fmt.Sprintf("@every %dh", p.manager.SpeedTestCheckRate), speedTestFunc)
if err != nil {
newError("Can't add speed test into cron").AtWarning().WriteToLog()
}
}
c.Start()
c.Run()
}
func (p *Panel) do() error {
if p.updateManager() {
p.updateThroughout()
}
return nil
}
func (p *Panel) initial() {
newError("initial system").AtWarning().WriteToLog()
p.manager.RemoveInbound()
p.manager.CopyUsers()
p.manager.UpdataUsers()
p.manager.RemoveAllUserOutBound()
p.manager.CurrentNodeInfo = &model.NodeInfo{}
p.manager.NextNodeInfo = &model.NodeInfo{}
p.manager.UserToBeAdd = map[string]model.UserModel{}
p.manager.UserToBeMoved = map[string]model.UserModel{}
p.manager.Users = map[string]model.UserModel{}
p.manager.Id2PrefixedIdmap = map[uint]string{}
p.manager.Id2DisServer = map[uint]string{}
}
func (p *Panel) updateManager() bool {
newNodeinfo, err := p.db.GetNodeInfo(p.manager.NodeID)
if err != nil {
newError(err).AtWarning().WriteToLog()
if p.downwithpanel == 1 {
p.initial()
}
return false
}
if newNodeinfo.Ret != 1 {
newError(newNodeinfo.Data).AtWarning().WriteToLog()
if p.downwithpanel == 1 {
p.initial()
}
return false
}
newErrorf("old node info %s ", p.manager.NextNodeInfo.Server_raw).AtInfo().WriteToLog()
newErrorf("new node info %s", newNodeinfo.Data.Server_raw).AtInfo().WriteToLog()
if p.manager.NextNodeInfo.Server_raw != newNodeinfo.Data.Server_raw {
p.manager.NextNodeInfo = newNodeinfo.Data
if err = p.manager.UpdateServer(); err != nil {
newError(err).AtWarning().WriteToLog()
}
p.manager.UserChanged = true
}
users, err := p.db.GetALLUsers(p.manager.NextNodeInfo)
if err != nil {
newError(err).AtDebug().WriteToLog()
}
newError("now begin to check users").AtInfo().WriteToLog()
current_user := p.manager.GetUsers()
// remove user by prefixed_id
for key, _ := range current_user {
_, ok := users.Data[key]
if !ok {
p.manager.Remove(key)
newErrorf("need to remove client: %s.", key).AtInfo().WriteToLog()
}
}
// add users
for key, value := range users.Data {
current, ok := current_user[key]
if !ok {
p.manager.Add(value)
newErrorf("need to add user email %s", key).AtInfo().WriteToLog()
} else {
if !reflect.DeepEqual(value, current) {
p.manager.Remove(key)
p.manager.Add(value)
newErrorf("need to add user email %s due to method or password changed", key).AtInfo().WriteToLog()
}
}
}
if p.manager.UserChanged {
p.manager.UserChanged = false
newErrorf("Before Update, Current Users %d need to be add %d need to be romved %d", len(p.manager.Users),
len(p.manager.UserToBeAdd), len(p.manager.UserToBeMoved)).AtWarning().WriteToLog()
p.manager.UpdataUsers()
newErrorf("After Update, Current Users %d need to be add %d need to be romved %d", len(p.manager.Users),
len(p.manager.UserToBeAdd), len(p.manager.UserToBeMoved)).AtWarning().WriteToLog()
p.manager.CopyNodeinfo()
} else {
newError("check ports finished. No need to update ").AtInfo().WriteToLog()
}
if newNodeinfo.Data.Sort == 12 {
newError("Start to check relay rules ").AtInfo().WriteToLog()
p.updateOutbounds()
}
return true
}
func (p *Panel) updateOutbounds() {
data, err := p.db.GetDisNodeInfo(p.manager.NodeID)
if err != nil {
newError(err).AtWarning().WriteToLog()
if p.downwithpanel == 1 {
p.initial()
}
return
}
if data.Ret != 1 {
newError(data.Data).AtWarning().WriteToLog()
if p.downwithpanel == 1 {
p.initial()
}
return
}
if len(data.Data) > 0 {
newErrorf("Recieve %d User Rules", len(data.Data)).AtInfo().WriteToLog()
globalSettingindex := -1
for index, value := range data.Data {
if value.UserId == 0 {
globalSettingindex = index
break
}
}
if globalSettingindex != -1 {
nextserver := data.Data[globalSettingindex]
newErrorf("Got A Global Rule %s ", nextserver.Server_raw).AtInfo().WriteToLog()
remove_count := 0
add_count := 0
for _, user := range p.manager.Users {
currentserver, find := p.manager.Id2DisServer[user.UserID]
nextserver.UserId = user.UserID
if find {
if currentserver != nextserver.Server_raw {
p.manager.RemoveOutBound(currentserver+fmt.Sprintf("%d", user.UserID), user.UserID)
err := p.manager.AddOuntBound(nextserver)
if err != nil {
newError("ADDOUTBOUND ").Base(err).AtInfo().WriteToLog()
} else {
remove_count += 1
add_count += 1
}
}
} else {
err := p.manager.AddOuntBound(nextserver)
if err != nil {
newError("ADDOUTBOUND ").Base(err).AtInfo().WriteToLog()
} else {
add_count += 1
}
}
}
p.manager.Id2DisServer = map[uint]string{}
for _, user := range p.manager.Users {
p.manager.Id2DisServer[user.UserID] = nextserver.Server_raw
}
newErrorf("Add %d and REMOVE %d Rules, Current Rules %d", add_count, remove_count, len(p.manager.Id2DisServer)).AtInfo().WriteToLog()
} else {
remove_count := 0
add_count := 0
for _, value := range data.Data {
_, find := p.manager.Id2DisServer[value.UserId]
if !find {
err := p.manager.AddOuntBound(value)
if err != nil {
newError("ADDOUTBOUND ").Base(err).AtInfo().WriteToLog()
} else {
add_count += 1
}
}
}
for id, currentserver := range p.manager.Id2DisServer {
flag := false
currenttag := currentserver + fmt.Sprintf("%d", id)
for _, nextserver := range data.Data {
if id == nextserver.UserId && currenttag == nextserver.Server_raw+fmt.Sprintf("%d", nextserver.UserId) {
flag = true
break
} else if id == nextserver.UserId && currenttag != nextserver.Server_raw+fmt.Sprintf("%d", nextserver.UserId) {
p.manager.RemoveOutBound(currenttag, id)
err := p.manager.AddOuntBound(nextserver)
if err != nil {
newError("ADDOUTBOUND ").Base(err).AtInfo().WriteToLog()
} else {
remove_count += 1
add_count += 1
}
flag = true
break
}
if !flag {
p.manager.RemoveOutBound(currenttag, id)
remove_count += 1
}
}
}
p.manager.Id2DisServer = map[uint]string{}
for _, nextserver := range data.Data {
p.manager.Id2DisServer[nextserver.UserId] = nextserver.Server_raw
}
newErrorf("Add %d and REMOVE %d Rules, Current Rules %d", add_count, remove_count, len(p.manager.Id2DisServer)).AtInfo().WriteToLog()
}
} else {
newErrorf("There is No User Rules, Need To Remove %d RULEs", len(p.manager.Id2DisServer)).AtInfo().WriteToLog()
if len(p.manager.Id2DisServer) > 0 {
remove_count := 0
add_count := 0
for id, currentserver := range p.manager.Id2DisServer {
currenttag := currentserver + fmt.Sprintf("%d", id)
p.manager.RemoveOutBound(currenttag, id)
remove_count += 1
}
p.manager.Id2DisServer = map[uint]string{}
newErrorf("Add %d and REMOVE %d Rules, Current Rules %d", add_count, remove_count, len(p.manager.Id2DisServer)).AtInfo().WriteToLog()
}
}
}
func (p *Panel) updateThroughout() {
current_user := p.manager.GetUsers()
usertraffic := []model.UserTrafficLog{}
userIPS := []model.UserOnLineIP{}
for _, value := range current_user {
current_upload, err := p.manager.StatsServiceClient.GetUserUplink(value.Email)
current_download, err := p.manager.StatsServiceClient.GetUserDownlink(value.Email)
if err != nil {
newError(err).AtDebug().WriteToLog()
}
if current_upload+current_download > 0 {
newErrorf("USER %s has use %d", value.Email, current_upload+current_download).AtDebug().WriteToLog()
usertraffic = append(usertraffic, model.UserTrafficLog{
UserID: value.UserID,
Downlink: current_download,
Uplink: current_upload,
})
current_user_ips, err := p.manager.StatsServiceClient.GetUserIPs(value.Email)
if current_upload+current_download > 1024 {
if err != nil {
newError(err).AtDebug().WriteToLog()
}
for index := range current_user_ips {
userIPS = append(userIPS, model.UserOnLineIP{
UserId: value.UserID,
Ip: current_user_ips[index],
})
}
}
}
}
if p.db.UpLoadUserTraffics(p.manager.NodeID, usertraffic) {
newErrorf("Successfully upload %d users traffics", len(usertraffic)).AtInfo().WriteToLog()
} else {
newError("update trafic failed").AtDebug().WriteToLog()
}
if p.db.UpLoadOnlineIps(p.manager.NodeID, userIPS) {
newErrorf("Successfully upload %d ips", len(userIPS)).AtInfo().WriteToLog()
} else {
newError("update trafic failed").AtDebug().WriteToLog()
}
if p.db.UploadSystemLoad(p.manager.NodeID) {
newError("Uploaded systemLoad successfully").AtInfo().WriteToLog()
} else {
newError("Failed to uploaded systemLoad ").AtDebug().WriteToLog()
}
}