Skip to content

Commit

Permalink
fix: database config change sync service status;user traffic log upda…
Browse files Browse the repository at this point in the history
…te the user_id is 0 problem;service online problem
  • Loading branch information
若尘(樱の泪) committed Jan 21, 2019
1 parent 49f7b05 commit 001b3bf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 40 deletions.
8 changes: 4 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
# go-tests = true
# unused-packages = true

[[constraint]]
branch = "master"
name = "golang.org/x/crypto"

[prune]
go-tests = true
unused-packages = true
Expand All @@ -47,3 +43,7 @@
[[constraint]]
branch = "master"
name = "github.com/asaskevich/EventBus"

[[constraint]]
name = "github.com/takama/daemon"
version = "0.11.0"
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ func LoadConfig(file string) (*Config, error) {
}
configFile = file
config = &Config{
Mode: "db",
DbConfig: DbConfig{},
Mode: "db",
DbConfig: DbConfig{
Rate: -1,
},
}
data, _ := json.MarshalIndent(config, "", " ")
ioutil.WriteFile(configFile, data, 0644)
Expand Down
58 changes: 25 additions & 33 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"bytes"
"context"
"fmt"
"net"
"strconv"
"strings"
"time"

"github.com/rc452860/vnet/utils/addr"

"github.com/rc452860/vnet/proxy"
"github.com/rc452860/vnet/record"

Expand Down Expand Up @@ -138,7 +138,7 @@ func DbStarted(ctx context.Context) {
}, &conf.DbConfig.NodeId, nil)
}

if conf.DbConfig.Rate == 0 {
if conf.DbConfig.Rate == -1 {
survey.AskOne(&survey.Input{
Message: "what is your want rate?",
Default: "1",
Expand Down Expand Up @@ -182,6 +182,8 @@ func DBOnlineMonitor(ctx context.Context) {
defer connect.Close()

for {
ss_node_info := fmt.Sprintf("INSERT INTO `ss_node_info` (`id`,`node_id`,`uptime`,`load`,`log_time`)"+
"VALUES (NULL,%v,unix_timestamp(),'%s',unix_timestamp())", conf.NodeId, FormatLoad())
ss_node_online_log := fmt.Sprintf("INSERT INTO `ss_node_online_log` (id,node_id,online_user,log_time) "+
" VALUES (NULL,%v,%v,unix_timestamp())", conf.NodeId, grmInstance.GetLastOneMinuteOnlineCount())
ss_node_ip := bytes.NewBufferString("")
Expand All @@ -197,6 +199,7 @@ func DBOnlineMonitor(ctx context.Context) {
ss_node_ip.WriteString(fmt.Sprintf("(NULL,%v,%v,'%s','%s',unix_timestamp()),", conf.NodeId, k, "-", ips.String()[:ips.Len()-1]))
}
connect.Exec(ss_node_online_log)
connect.Exec(ss_node_info)
if ss_node_ip.Len() > headLen {
connect.Exec(ss_node_ip.String()[:ss_node_ip.Len()-1])
}
Expand All @@ -220,41 +223,42 @@ func DBServiceMonitor(ctx context.Context) {
users, err := GetEnableUser()
if err != nil {
log.Err(err)
return
continue
}

sslist := service.CurrentShadowsocksService().List()
userMap := make(map[int]User)
for _, user := range users {
userMap[user.Port] = user

for _, ss := range sslist {
flag := false
for _, ss := range sslist {
if ss.Port == user.Port {
for _, user := range users {
if ss.Port == user.Port && ss.Method == user.Method && ss.Password == user.Password {
flag = true
break
}
}
if !flag {
log.Info("start port [%d]", user.Port)
StartShadowsocks(user)
log.Info("stop port [%d]", ss.Port)
service.CurrentShadowsocksService().Del(ss.Port)
}
}

for _, ss := range sslist {
for _, user := range users {
userMap[user.Port] = user
flag := false
for _, user := range users {
for _, ss := range sslist {
if ss.Port == user.Port {
flag = true
break
}
}
if !flag {
log.Info("stop port [%d]", ss.Port)
service.CurrentShadowsocksService().Del(ss.Port)
log.Info("start port [%d]", user.Port)
StartShadowsocks(user)
}
}

UpdateTrafficByUser(userMap)
//TODO update user upload and download
<-tick
}
}
Expand Down Expand Up @@ -330,12 +334,9 @@ func UpdateTrafficByUser(users map[int]User) {
inserStr := insertBuf.String()
user_traffic_log := fmt.Sprintf("INSERT INTO `user_traffic_log` (`id`, `user_id`, `u`, `d`, "+
"`node_id`, `rate`, `traffic`, `log_time`) VALUES %s", inserStr[:len(inserStr)-1])
ss_node_info := fmt.Sprintf("INSERT INTO `ss_node_info` (`id`,`node_id`,`uptime`,`load`,`log_time`)"+
"VALUES (NULL,%v,unix_timestamp(),'%s',unix_timestamp())", conf.NodeId, FormatLoad())

connect.Exec(user)
connect.Exec(user_traffic_log)
connect.Exec(ss_node_info)
}

func FormatLoad() string {
Expand All @@ -354,25 +355,16 @@ func DBTrafficMonitor(ctx context.Context) {
return
case data = <-traffic:
}
_, port, err := net.SplitHostPort(data.ClientAddr.String())
if err != nil {
log.Err(err)
continue
}
porti, err := strconv.Atoi(port)
if err != nil {
log.Err(err)
continue
}
if trafficTable[porti] == nil {
trafficTable[porti] = &DBTraffic{
Port: porti,
port := addr.GetPortFromAddr(data.ProxyAddr)
if trafficTable[port] == nil {
trafficTable[port] = &DBTraffic{
Port: port,
Up: data.Up,
Down: data.Down,
}
} else {
trafficTable[porti].Up += data.Up
trafficTable[porti].Down += data.Down
trafficTable[port].Up += data.Up
trafficTable[port].Down += data.Down
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/asaskevich/EventBus/event_bus.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 001b3bf

Please sign in to comment.