mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-12-17 20:54:40 +03:00
update pack 2
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
"x-ui/database"
|
||||
"encoding/json"
|
||||
"x-ui/database/model"
|
||||
"x-ui/logger"
|
||||
"x-ui/util/common"
|
||||
"x-ui/xray"
|
||||
"x-ui/logger"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -64,11 +64,11 @@ func (s *InboundService) getClients(inbound *model.Inbound) ([]model.Client, err
|
||||
return clients, nil
|
||||
}
|
||||
|
||||
func (s *InboundService) checkEmailsExist(emails map[string] bool, ignoreId int) (string, error) {
|
||||
func (s *InboundService) checkEmailsExist(emails map[string]bool, ignoreId int) (string, error) {
|
||||
db := database.GetDB()
|
||||
var inbounds []*model.Inbound
|
||||
db = db.Model(model.Inbound{}).Where("Protocol in ?", []model.Protocol{model.VMess, model.VLESS})
|
||||
if (ignoreId > 0) {
|
||||
var inbounds []*model.Inbound
|
||||
db = db.Model(model.Inbound{}).Where("Protocol in ?", []model.Protocol{model.VMess, model.VLESS, model.Trojan})
|
||||
if ignoreId > 0 {
|
||||
db = db.Where("id != ?", ignoreId)
|
||||
}
|
||||
db = db.Find(&inbounds)
|
||||
@@ -96,25 +96,25 @@ func (s *InboundService) checkEmailExistForInbound(inbound *model.Inbound) (stri
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
emails := make(map[string] bool)
|
||||
emails := make(map[string]bool)
|
||||
for _, client := range clients {
|
||||
if (client.Email != "") {
|
||||
if client.Email != "" {
|
||||
if emails[client.Email] {
|
||||
return client.Email, nil
|
||||
}
|
||||
emails[client.Email] = true;
|
||||
emails[client.Email] = true
|
||||
}
|
||||
}
|
||||
return s.checkEmailsExist(emails, inbound.Id)
|
||||
}
|
||||
|
||||
func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound,error) {
|
||||
func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, error) {
|
||||
exist, err := s.checkPortExist(inbound.Port, 0)
|
||||
if err != nil {
|
||||
return inbound, err
|
||||
}
|
||||
if exist {
|
||||
return inbound, common.NewError("端口已存在:", inbound.Port)
|
||||
return inbound, common.NewError("Port already exists:", inbound.Port)
|
||||
}
|
||||
|
||||
existEmail, err := s.checkEmailExistForInbound(inbound)
|
||||
@@ -129,7 +129,7 @@ func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound,erro
|
||||
|
||||
err = db.Save(inbound).Error
|
||||
if err == nil {
|
||||
s.UpdateClientStat(inbound.Id,inbound.Settings)
|
||||
s.UpdateClientStat(inbound.Id, inbound.Settings)
|
||||
}
|
||||
return inbound, err
|
||||
}
|
||||
@@ -141,7 +141,7 @@ func (s *InboundService) AddInbounds(inbounds []*model.Inbound) error {
|
||||
return err
|
||||
}
|
||||
if exist {
|
||||
return common.NewError("端口已存在:", inbound.Port)
|
||||
return common.NewError("Port already exists:", inbound.Port)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,9 +187,9 @@ func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound,
|
||||
return inbound, err
|
||||
}
|
||||
if exist {
|
||||
return inbound, common.NewError("端口已存在:", inbound.Port)
|
||||
return inbound, common.NewError("Port already exists:", inbound.Port)
|
||||
}
|
||||
|
||||
|
||||
existEmail, err := s.checkEmailExistForInbound(inbound)
|
||||
if err != nil {
|
||||
return inbound, err
|
||||
@@ -216,7 +216,7 @@ func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound,
|
||||
oldInbound.Sniffing = inbound.Sniffing
|
||||
oldInbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
|
||||
|
||||
s.UpdateClientStat(inbound.Id,inbound.Settings)
|
||||
s.UpdateClientStat(inbound.Id, inbound.Settings)
|
||||
db := database.GetDB()
|
||||
return inbound, db.Save(oldInbound).Error
|
||||
}
|
||||
@@ -276,13 +276,13 @@ func (s *InboundService) AddClientTraffic(traffics []*xray.ClientTraffic) (err e
|
||||
for _, traffic := range traffics {
|
||||
inbound := &model.Inbound{}
|
||||
|
||||
err := txInbound.Where("settings like ?", "%" + traffic.Email + "%").First(inbound).Error
|
||||
err := txInbound.Where("settings like ?", "%"+traffic.Email+"%").First(inbound).Error
|
||||
traffic.InboundId = inbound.Id
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
// delete removed client record
|
||||
clientErr := s.DelClientStat(tx, traffic.Email)
|
||||
logger.Warning(err, traffic.Email,clientErr)
|
||||
logger.Warning(err, traffic.Email, clientErr)
|
||||
|
||||
}
|
||||
continue
|
||||
@@ -298,19 +298,19 @@ func (s *InboundService) AddClientTraffic(traffics []*xray.ClientTraffic) (err e
|
||||
}
|
||||
}
|
||||
if tx.Where("inbound_id = ?", inbound.Id).Where("email = ?", traffic.Email).
|
||||
UpdateColumn("enable", true).
|
||||
UpdateColumn("expiry_time", traffic.ExpiryTime).
|
||||
UpdateColumn("total",traffic.Total).
|
||||
UpdateColumn("up", gorm.Expr("up + ?", traffic.Up)).
|
||||
UpdateColumn("down", gorm.Expr("down + ?", traffic.Down)).RowsAffected == 0 {
|
||||
UpdateColumn("enable", true).
|
||||
UpdateColumn("expiry_time", traffic.ExpiryTime).
|
||||
UpdateColumn("total", traffic.Total).
|
||||
UpdateColumn("up", gorm.Expr("up + ?", traffic.Up)).
|
||||
UpdateColumn("down", gorm.Expr("down + ?", traffic.Down)).RowsAffected == 0 {
|
||||
err = tx.Create(traffic).Error
|
||||
}
|
||||
|
||||
|
||||
if err != nil {
|
||||
logger.Warning("AddClientTraffic update data ", err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -335,7 +335,7 @@ func (s *InboundService) DisableInvalidClients() (int64, error) {
|
||||
count := result.RowsAffected
|
||||
return count, err
|
||||
}
|
||||
func (s *InboundService) UpdateClientStat(inboundId int, inboundSettings string) (error) {
|
||||
func (s *InboundService) UpdateClientStat(inboundId int, inboundSettings string) error {
|
||||
db := database.GetDB()
|
||||
|
||||
// get settings clients
|
||||
@@ -344,8 +344,8 @@ func (s *InboundService) UpdateClientStat(inboundId int, inboundSettings string)
|
||||
clients := settings["clients"]
|
||||
for _, client := range clients {
|
||||
result := db.Model(xray.ClientTraffic{}).
|
||||
Where("inbound_id = ? and email = ?", inboundId, client.Email).
|
||||
Updates(map[string]interface{}{"enable": true, "total": client.TotalGB, "expiry_time": client.ExpiryTime})
|
||||
Where("inbound_id = ? and email = ?", inboundId, client.Email).
|
||||
Updates(map[string]interface{}{"enable": true, "total": client.TotalGB, "expiry_time": client.ExpiryTime})
|
||||
if result.RowsAffected == 0 {
|
||||
clientTraffic := xray.ClientTraffic{}
|
||||
clientTraffic.InboundId = inboundId
|
||||
@@ -361,7 +361,7 @@ func (s *InboundService) UpdateClientStat(inboundId int, inboundSettings string)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -369,7 +369,7 @@ func (s *InboundService) DelClientStat(tx *gorm.DB, email string) error {
|
||||
return tx.Where("email = ?", email).Delete(xray.ClientTraffic{}).Error
|
||||
}
|
||||
|
||||
func (s *InboundService) ResetClientTraffic(clientEmail string) (error) {
|
||||
func (s *InboundService) ResetClientTraffic(clientEmail string) error {
|
||||
db := database.GetDB()
|
||||
|
||||
result := db.Model(xray.ClientTraffic{}).
|
||||
@@ -379,7 +379,6 @@ func (s *InboundService) ResetClientTraffic(clientEmail string) (error) {
|
||||
|
||||
err := result.Error
|
||||
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -390,7 +389,7 @@ func (s *InboundService) GetClientTrafficById(uuid string) (traffic *xray.Client
|
||||
inbound := &model.Inbound{}
|
||||
traffic = &xray.ClientTraffic{}
|
||||
|
||||
err = db.Model(model.Inbound{}).Where("settings like ?", "%" + uuid + "%").First(inbound).Error
|
||||
err = db.Model(model.Inbound{}).Where("settings like ?", "%"+uuid+"%").First(inbound).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
logger.Warning(err)
|
||||
|
||||
Reference in New Issue
Block a user