Полная зачистка legacy + закрытие fix24.1/fix24.2 + обновление логотипа
This commit is contained in:
+61
-34
@@ -2,12 +2,12 @@ package service
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
"hy2xs-admin/dao"
|
||||
"hy2xs-admin/model/bo"
|
||||
"hy2xs-admin/model/constant"
|
||||
"hy2xs-admin/model/entity"
|
||||
"hy2xs-admin/proxy"
|
||||
"hy2xs-admin/util"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -40,17 +40,17 @@ func CronHandleAccount() {
|
||||
}
|
||||
|
||||
func CronResetTraffic() {
|
||||
accounts, err := dao.ListAccount(nil, nil)
|
||||
peers, err := dao.ListPeer("1=1")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var ids []int64
|
||||
for _, item := range accounts {
|
||||
for _, item := range peers {
|
||||
ids = append(ids, *item.Id)
|
||||
}
|
||||
idsList := util.SplitArr(ids, 100)
|
||||
for _, item := range idsList {
|
||||
if err := dao.UpdateAccount(item, map[string]interface{}{"download": 0, "upload": 0}); err != nil {
|
||||
if err := dao.UpdatePeer(item, map[string]interface{}{"download_bytes": 0, "upload_bytes": 0}); err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -62,38 +62,65 @@ func saveAccountTraffic(apiPort int64, trafficStatsSecret string) {
|
||||
}
|
||||
defer trafficMutex.Unlock()
|
||||
|
||||
hysteria2TrafficTime, err := dao.GetConfig("key = ?", constant.Hysteria2TrafficTime)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
hysteria2TrafficTimeFloat, err := strconv.ParseFloat(*hysteria2TrafficTime.Value, 64)
|
||||
if err != nil {
|
||||
logrus.Errorf("hysteria2TrafficTime string conv int64 err: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
users, err := proxy.NewHysteria2Api(apiPort).ListUsers(true, trafficStatsSecret)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(users) > 0 {
|
||||
userLists := util.SplitMap(users, 10)
|
||||
var wg sync.WaitGroup
|
||||
for _, userList := range userLists {
|
||||
wg.Add(1)
|
||||
go func(userList map[string]bo.Hysteria2UserTraffic) {
|
||||
defer wg.Done()
|
||||
for username, traffic := range userList {
|
||||
if err = dao.UpdateAccountTraffic(username, int64(float64(traffic.Rx)*hysteria2TrafficTimeFloat), int64(float64(traffic.Tx)*hysteria2TrafficTimeFloat)); err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}(userList)
|
||||
if len(users) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
nowMs := time.Now().UnixMilli()
|
||||
hourStart := nowMs - (nowMs % int64(time.Hour/time.Millisecond))
|
||||
dayStart := nowMs - (nowMs % int64(24*time.Hour/time.Millisecond))
|
||||
|
||||
for key, traffic := range users {
|
||||
rxBytes := traffic.Rx
|
||||
txBytes := traffic.Tx
|
||||
if rxBytes == 0 && txBytes == 0 {
|
||||
continue
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
peer, peerErr := dao.GetPeer("auth_id = ?", key)
|
||||
if peerErr != nil {
|
||||
peer, peerErr = dao.GetPeer("name = ?", key)
|
||||
if peerErr != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if peer.Id == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
authId := key
|
||||
if peer.AuthId != nil && *peer.AuthId != "" {
|
||||
authId = *peer.AuthId
|
||||
}
|
||||
|
||||
sample := entity.TrafficSample{
|
||||
PeerId: peer.Id,
|
||||
AuthId: &authId,
|
||||
RxBytes: &rxBytes,
|
||||
TxBytes: &txBytes,
|
||||
SampledAt: &nowMs,
|
||||
}
|
||||
if err = dao.SaveTrafficSample(sample); err != nil {
|
||||
logrus.Errorf("save traffic_sample failed: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err = dao.UpdatePeer([]int64{*peer.Id}, map[string]interface{}{
|
||||
"download_bytes": gorm.Expr("download_bytes + ?", rxBytes),
|
||||
"upload_bytes": gorm.Expr("upload_bytes + ?", txBytes),
|
||||
}); err != nil {
|
||||
logrus.Errorf("update peer traffic failed: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
_ = dao.UpsertTrafficAggregateHourly(*peer.Id, hourStart, rxBytes, txBytes)
|
||||
_ = dao.UpsertTrafficAggregateDaily(*peer.Id, dayStart, rxBytes, txBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func kickAccount(apiPort int64, trafficStatsSecret string) {
|
||||
if !kickMutex.TryLock() {
|
||||
return
|
||||
@@ -118,14 +145,14 @@ func kickAccount(apiPort int64, trafficStatsSecret string) {
|
||||
go func(usernameList []string) {
|
||||
defer wg.Done()
|
||||
now := time.Now().UnixMilli()
|
||||
accounts, err := dao.ListAccount("username in ? and (deleted = 1 or (quota > 0 and quota < download + upload)) or ? > expire_time or ? < kick_util_time", usernameList, now, now)
|
||||
peers, err := dao.ListPeer("name in ? and (disabled = 1 or (quota_bytes > 0 and quota_bytes < download_bytes + upload_bytes) or ? > expires_at or ? < banned_until)", usernameList, now, now)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
kickUsernames := make([]string, len(accounts))
|
||||
kickUsernames := make([]string, len(peers))
|
||||
j := 0
|
||||
for _, item := range accounts {
|
||||
kickUsernames[j] = *item.Username
|
||||
for _, item := range peers {
|
||||
kickUsernames[j] = *item.Name
|
||||
j++
|
||||
}
|
||||
if err = proxy.NewHysteria2Api(apiPort).KickUsers(kickUsernames, trafficStatsSecret); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user