-
Notifications
You must be signed in to change notification settings - Fork 88
/
user.go
124 lines (105 loc) · 2.67 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
package v2scar
import (
"errors"
"fmt"
"sync"
"sync/atomic"
)
// User V2ray User
type User struct {
UserId int `json:"user_id"`
Email string `json:"email"`
UUID string `json:"uuid"`
AlterId uint32 `json:"alter_id"`
Level uint32 `json:"level"`
Enable bool `json:"enable"`
UploadTraffic int64 `json:"upload_traffic"`
DownloadTraffic int64 `json:"download_traffic"`
running bool
}
func newUser(userId int, email, uuid string, level, alterId uint32, enable bool) *User {
return &User{
UserId: userId,
Email: email,
UUID: uuid,
Level: level,
Enable: enable,
AlterId: alterId,
}
}
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
}
// 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, email, uuid 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 {
user := newUser(userId, email, uuid, level, alterId, enable)
up.users[user.Email] = user
return user, 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)
}