forked from Ehco1996/v2scar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.go
More file actions
126 lines (119 loc) · 3.55 KB
/
Copy pathservices.go
File metadata and controls
126 lines (119 loc) · 3.55 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
package v2scar
import (
"context"
"log"
"strings"
"v2ray.com/core/proxy/trojan"
v2proxyman "v2ray.com/core/app/proxyman/command"
v2stats "v2ray.com/core/app/stats/command"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy/vmess"
)
// GetAndResetUserTraffic 统计所有user的上行下行流量
// V2ray的stats的统计模块设计的非常奇怪,具体规则如下
// 上传流量:"user>>>" + user.Email + ">>>traffic>>>uplink"
// 下载流量:"user>>>" + user.Email + ">>>traffic>>>downlink"
func GetAndResetUserTraffic(c v2stats.StatsServiceClient, up *UserPool) {
req := &v2stats.QueryStatsRequest{
Pattern: "user>>>",
Reset_: true,
}
resp, err := c.QueryStats(context.Background(), req)
if err != nil {
log.Println("[ERROR]:", err)
} else {
for _, stat := range resp.Stat {
email, trafficType := getEmailAndTrafficType(stat.Name)
user, err := up.GetUserByEmail(email)
if err != nil {
log.Println(err)
} else {
switch trafficType {
case "uplink":
user.setUploadTraffic(stat.Value)
case "downlink":
user.setDownloadTraffic(stat.Value)
}
}
}
}
}
func getEmailAndTrafficType(input string) (string, string) {
s := strings.Split(input, ">>>")
return s[1], s[len(s)-1]
}
// AddInboundUser add user to inbound by tag
func AddInboundUser(c v2proxyman.HandlerServiceClient, tag, rProtocol string, user *User) {
var err error
switch rProtocol {
case VMESS:
_, err = c.AlterInbound(context.Background(), &v2proxyman.AlterInboundRequest{
Tag: tag,
Operation: serial.ToTypedMessage(&v2proxyman.AddUserOperation{
User: &protocol.User{
Level: user.Level,
Email: user.Email,
Account: serial.ToTypedMessage(&vmess.Account{
Id: user.UUID,
AlterId: user.AlterId,
SecuritySettings: &protocol.SecurityConfig{Type: protocol.SecurityType_AUTO},
}),
},
}),
})
case TROJAN:
_, err = c.AlterInbound(context.Background(), &v2proxyman.AlterInboundRequest{
Tag: tag,
Operation: serial.ToTypedMessage(&v2proxyman.AddUserOperation{
User: &protocol.User{
Level: user.Level,
Email: user.Email,
Account: serial.ToTypedMessage(&trojan.Account{
Password: user.Password,
}),
},
}),
})
default:
_, err = c.AlterInbound(context.Background(), &v2proxyman.AlterInboundRequest{
Tag: tag,
Operation: serial.ToTypedMessage(&v2proxyman.AddUserOperation{
User: &protocol.User{
Level: user.Level,
Email: user.Email,
Account: serial.ToTypedMessage(&vmess.Account{
Id: user.UUID,
AlterId: user.AlterId,
SecuritySettings: &protocol.SecurityConfig{Type: protocol.SecurityType_AUTO},
}),
},
}),
})
}
if err != nil {
log.Println("[ERROR]:", err, tag)
if strings.Contains(err.Error(), "already exists.") {
// TODO 优化这里的逻辑 这里针对side car重启而v2ray没重启的状态
user.setRunning(true)
}
} else {
log.Printf("[INFO] User: %v Add To V2ray Server Tag: %v", user.Email, tag)
user.setRunning(true)
}
}
//RemoveInboundUser remove user from inbound by tag
func RemoveInboundUser(c v2proxyman.HandlerServiceClient, tag string, user *User) {
_, err := c.AlterInbound(context.Background(), &v2proxyman.AlterInboundRequest{
Tag: tag,
Operation: serial.ToTypedMessage(&v2proxyman.RemoveUserOperation{
Email: user.Email,
}),
})
if err != nil {
log.Println("[ERROR]:", err)
} else {
log.Printf("[INFO] User: %v Removed From V2ray Server Tag: %v", user.Email, tag)
user.setRunning(false)
}
}