forked from orvice/v2ray-mu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
125 lines (108 loc) · 2.58 KB
/
manager.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
package main
import (
"time"
"errors"
"github.com/catpie/musdk-go"
"github.com/orvice/v2ray-manager"
"github.com/orvice/shadowsocks-go/mu/system"
"net/http"
"fmt"
)
func getV2rayManager() (*v2raymanager.Manager, error) {
vm, err := v2raymanager.NewManager(cfg.V2rayClientAddr, cfg.V2rayTag)
return vm, err
}
func (u *UserManager) check() error {
logger.Info("Checking users from mu...")
users, err := apiClient.GetUsers()
if err != nil {
logger.Errorf("Get users from error: %v", err)
return err
}
logger.Infof("Get %d users from mu", len(users))
for _, user := range users {
u.checkUser(user)
}
return nil
}
func (u *UserManager) checkUser(user musdk.User) error {
var err error
if user.IsEnable() && !u.Exist(user) {
logger.Infof("Run user id %d uuid %s", user.Id, user.V2rayUser.UUID)
// run user
err = u.vm.AddUser(&user.V2rayUser)
if err != nil {
logger.Errorf("Add user %s error %v", user.V2rayUser.UUID, err)
return err
}
logger.Infof("Add user success %s", user.V2rayUser.UUID)
u.AddUser(user)
return nil
}
if !user.IsEnable() && u.Exist(user) {
logger.Infof("Stop user id %d uuid %s", user.Id, user.V2rayUser.UUID)
// stop user
err = u.vm.RemoveUser(&user.V2rayUser)
if err != nil {
logger.Errorf("Remove user error %v", err)
time.Sleep(time.Second * 10)
return err
}
u.RemoveUser(user)
return nil
}
return nil
}
func (u *UserManager) restartUser() {}
func (u *UserManager) Run() error {
for {
u.saveTrafficDaemon()
u.postNodeInfo()
u.check()
time.Sleep(cfg.SyncTime)
}
return nil
}
func (u *UserManager) Down() {
u.cancel()
}
func (u *UserManager) saveTrafficDaemon() {
logger.Infof("Runing save traffic daemon...")
u.usersMu.RLock()
defer u.usersMu.RUnlock()
for _, user := range u.users {
u.saveUserTraffic(user)
}
}
func (u *UserManager) postNodeInfo() error {
logger.Infof("Posting node info...")
err := u.PostNodeInfo()
if err != nil {
logger.Errorf("Post node info error %v", err)
}
return nil
}
func (u *UserManager) postNodeInfoUri() string {
return fmt.Sprintf("%s/nodes/%d/info", cfg.WebApi.Url, cfg.WebApi.NodeId)
}
func (u *UserManager) PostNodeInfo() error {
uptime, err := system.GetUptime()
if err != nil {
uptime = "0"
}
load, err := system.GetLoad()
if err != nil {
load = "- - -"
} else {
load = load[0:13]
}
data := `{"load":"`+load+`","uptime":"`+uptime+`"}`
_, statusCode, err := u.httpPost(u.postNodeInfoUri(), string(data))
if err != nil {
return err
}
if statusCode != http.StatusOK {
return errors.New(fmt.Sprintf("status code: %d", statusCode))
}
return nil
}