Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'web/service')
-rw-r--r--web/service/inbound.go63
-rw-r--r--web/service/setting.go2
-rw-r--r--web/service/xray.go15
3 files changed, 40 insertions, 40 deletions
diff --git a/web/service/inbound.go b/web/service/inbound.go
index 37888729..dedf76a0 100644
--- a/web/service/inbound.go
+++ b/web/service/inbound.go
@@ -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)
diff --git a/web/service/setting.go b/web/service/setting.go
index 7c42b5fe..4d9231b3 100644
--- a/web/service/setting.go
+++ b/web/service/setting.go
@@ -69,7 +69,7 @@ func (s *SettingService) GetAllSetting() (*entity.AllSetting, error) {
}
if !found {
- // 有些设置自动生成,不需要返回到前端给用户修改
+ // Some settings are automatically generated, no need to return to the front end to modify the user
return nil
}
diff --git a/web/service/xray.go b/web/service/xray.go
index 37fd3b05..33425c3c 100644
--- a/web/service/xray.go
+++ b/web/service/xray.go
@@ -6,6 +6,7 @@ import (
"sync"
"x-ui/logger"
"x-ui/xray"
+
"go.uber.org/atomic"
)
@@ -50,6 +51,7 @@ func (s *XrayService) GetXrayVersion() string {
}
return p.GetVersion()
}
+
func RemoveIndex(s []interface{}, index int) []interface{} {
return append(s[:index], s[index+1:]...)
}
@@ -79,25 +81,24 @@ func (s *XrayService) GetXrayConfig() (*xray.Config, error) {
// get settings clients
settings := map[string]interface{}{}
json.Unmarshal([]byte(inbound.Settings), &settings)
- clients, ok := settings["clients"].([]interface{})
+ clients, ok := settings["clients"].([]interface{})
if ok {
// check users active or not
clientStats := inbound.ClientStats
for _, clientTraffic := range clientStats {
-
+
for index, client := range clients {
c := client.(map[string]interface{})
if c["email"] == clientTraffic.Email {
- if ! clientTraffic.Enable {
- clients = RemoveIndex(clients,index)
- logger.Info("Remove Inbound User",c["email"] ,"due the expire or traffic limit")
+ if !clientTraffic.Enable {
+ clients = RemoveIndex(clients, index)
+ logger.Info("Remove Inbound User", c["email"], "due the expire or traffic limit")
}
}
}
-
}
settings["clients"] = clients
@@ -105,7 +106,7 @@ func (s *XrayService) GetXrayConfig() (*xray.Config, error) {
if err != nil {
return nil, err
}
-
+
inbound.Settings = string(modifiedSettings)
}
inboundConfig := inbound.GenXrayInboundConfig()