-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathuser.go
155 lines (132 loc) · 3.31 KB
/
user.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
package v2scar
import (
"errors"
"fmt"
"sync"
"sync/atomic"
)
// User V2ray User
type User struct {
UserId int `json:"user_id"`
Email string `json:"email"`
Level uint32 `json:"level"`
Enable bool `json:"enable"`
UploadTraffic int64 `json:"upload_traffic"`
DownloadTraffic int64 `json:"download_traffic"`
Vmess
Trojan
running bool
}
type Vmess struct {
UUID string `json:"uuid"`
AlterId uint32 `json:"alter_id"`
}
type Trojan struct {
Password string `json:"password"`
}
func newVmessUser(userId int, email, uuid string, level, alterId uint32, enable bool) *User {
return &User{
UserId: userId,
Email: email,
Level: level,
Enable: enable,
Vmess: Vmess{UUID: uuid, AlterId: alterId},
}
}
func newTrojanUser(userId int, email, password string, level uint32, enable bool) *User {
return &User{
UserId: userId,
Email: email,
Level: level,
Enable: enable,
Trojan: Trojan{Password: password},
}
}
func (u *User) setUploadTraffic(ut int64) {
atomic.StoreInt64(&u.UploadTraffic, ut)
}
func (u *User) setDownloadTraffic(dt int64) {
atomic.StoreInt64(&u.DownloadTraffic, dt)
}
func (u *User) resetTraffic() {
atomic.StoreInt64(&u.DownloadTraffic, 0)
atomic.StoreInt64(&u.UploadTraffic, 0)
}
func (u *User) setEnable(enable bool) {
// NOTE not thread safe!
u.Enable = enable
}
func (u *User) setRunning(status bool) {
// NOTE not thread safe!
u.running = status
}
func (u *User) setUUID(uuid string) {
// NOTE not thread safe!
u.UUID = uuid
}
func (u *User) setPassword(password string) {
// NOTE not thread safe!
u.Password = password
}
// UserPool user pool
type UserPool struct {
access sync.RWMutex
users map[string]*User
}
// NewUserPool New UserPool
func NewUserPool() *UserPool {
// map key : email
return &UserPool{
users: make(map[string]*User),
}
}
// CreateUser get create user
func (up *UserPool) CreateUser(userId int, protocol, email, uuid, password string, level, alterId uint32, enable bool) (*User, error) {
up.access.Lock()
defer up.access.Unlock()
if user, found := up.users[email]; found {
return user, errors.New(fmt.Sprintf("UserId: %d Already Exists Email: %s", user.UserId, email))
} else {
var newUser *User
switch protocol {
case TROJAN:
newUser = newTrojanUser(userId, email, password, level, enable)
case VMESS:
newUser = newVmessUser(userId, email, uuid, level, alterId, enable)
default:
newUser = newVmessUser(userId, email, uuid, level, alterId, enable)
}
up.users[newUser.Email] = newUser
return newUser, nil
}
}
// GetUserByEmail get user by email
func (up *UserPool) GetUserByEmail(email string) (*User, error) {
up.access.Lock()
defer up.access.Unlock()
if user, found := up.users[email]; found {
return user, nil
} else {
return nil, errors.New(fmt.Sprintf("User Not Found Email: %s", email))
}
}
// RemoveUserByEmail get user by email
func (up *UserPool) RemoveUserByEmail(email string) {
up.access.Lock()
defer up.access.Unlock()
delete(up.users, email)
}
// GetAllUsers GetAllUsers
func (up *UserPool) GetAllUsers() []*User {
up.access.Lock()
defer up.access.Unlock()
users := make([]*User, 0, len(up.users))
for _, user := range up.users {
users = append(users, user)
}
return users
}
// GetUsersNum GetUsersNum
func (up *UserPool) GetUsersNum() int {
return len(up.users)
}