From b412df70f1b2e9f10b75e095a2329f10b48e01b5 Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Sat, 18 Feb 2023 16:07:32 +0330 Subject: update pack 2 --- web/assets/js/model/xray.js | 6 +- web/assets/js/util/utils.js | 10 ++++ web/controller/base.go | 13 ++--- web/controller/inbound.go | 25 ++++---- web/controller/index.go | 8 +-- web/controller/server.go | 4 +- web/controller/setting.go | 16 ++--- web/controller/util.go | 6 +- web/html/xui/form/protocol/trojan.html | 4 +- web/html/xui/form/protocol/vless.html | 4 +- web/html/xui/form/protocol/vmess.html | 4 +- web/html/xui/inbounds.html | 22 ++++--- web/html/xui/inbounds_client_row.html | 13 +---- web/job/stats_notify_job.go | 104 ++++++++++++++++----------------- web/service/inbound.go | 63 ++++++++++---------- web/service/setting.go | 2 +- web/service/xray.go | 15 ++--- web/web.go | 18 +++--- 18 files changed, 172 insertions(+), 165 deletions(-) (limited to 'web') diff --git a/web/assets/js/model/xray.js b/web/assets/js/model/xray.js index f4b874b1..23d73930 100644 --- a/web/assets/js/model/xray.js +++ b/web/assets/js/model/xray.js @@ -1359,7 +1359,7 @@ Inbound.VmessSettings = class extends Inbound.Settings { } }; Inbound.VmessSettings.Vmess = class extends XrayCommonClass { - constructor(id=RandomUtil.randomUUID(), alterId=0, email='', totalGB=0, expiryTime='') { + constructor(id=RandomUtil.randomUUID(), alterId=0, email=RandomUtil.randomText(), totalGB=0, expiryTime='') { super(); this.id = id; this.alterId = alterId; @@ -1441,7 +1441,7 @@ Inbound.VLESSSettings = class extends Inbound.Settings { }; Inbound.VLESSSettings.VLESS = class extends XrayCommonClass { - constructor(id=RandomUtil.randomUUID(), flow='', email='', totalGB=0, fingerprint = UTLS_FINGERPRINT.UTLS_CHROME, expiryTime='') { + constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomText(), totalGB=0, fingerprint = UTLS_FINGERPRINT.UTLS_CHROME, expiryTime='') { super(); this.id = id; this.flow = flow; @@ -1557,7 +1557,7 @@ Inbound.TrojanSettings = class extends Inbound.Settings { } }; Inbound.TrojanSettings.Trojan = class extends XrayCommonClass { - constructor(password=RandomUtil.randomSeq(10), flow ='', email='', totalGB=0, expiryTime='') { + constructor(password=RandomUtil.randomSeq(10), flow ='', email=RandomUtil.randomText(), totalGB=0, expiryTime='') { super(); this.password = password; this.flow = flow; diff --git a/web/assets/js/util/utils.js b/web/assets/js/util/utils.js index 6b4e5ed9..f3a412e5 100644 --- a/web/assets/js/util/utils.js +++ b/web/assets/js/util/utils.js @@ -136,6 +136,16 @@ class RandomUtil { return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16); }); } + + static randomText() { + var chars = 'abcdefghijklmnopqrstuvwxyz1234567890'; + var string = ''; + var len = 6 + Math.floor(Math.random() * 5) + for(var ii=0; ii - Username + Email - Username + Email - Username + Email row.id === dbInbound_id); const inbound = dbInbound.toInbound(); inModal.show({ title: '{{ i18n "pages.inbounds.modifyInbound"}}', diff --git a/web/html/xui/inbounds_client_row.html b/web/html/xui/inbounds_client_row.html index 6e03e4ca..0fe707e4 100644 --- a/web/html/xui/inbounds_client_row.html +++ b/web/html/xui/inbounds_client_row.html @@ -1,15 +1,8 @@ {{define "client_row"}} /usage uuid | id \n example : /usage fc3239ed-8f3b-4151-ff51-b183d5182142" - msg.ParseMode = "HTML" - } + case "get_usage": + msg.Text = "for get your usage send command like this : \n /usage uuid | id \n example : /usage fc3239ed-8f3b-4151-ff51-b183d5182142" + msg.ParseMode = "HTML" + } if _, err := bot.Send(msg); err != nil { logger.Warning(err) } } - - continue - } - - if !update.Message.IsCommand() { // ignore any non-command Messages - continue - } - - // Create a new MessageConfig. We don't have text yet, - // so we leave it empty. - msg := tgbotapi.NewMessage(update.Message.Chat.ID, "") - - // Extract the command from the Message. - switch update.Message.Command() { - case "help": - msg.Text = "What you need?" + + continue + } + + if !update.Message.IsCommand() { // ignore any non-command Messages + continue + } + + // Create a new MessageConfig. We don't have text yet, + // so we leave it empty. + msg := tgbotapi.NewMessage(update.Message.Chat.ID, "") + + // Extract the command from the Message. + switch update.Message.Command() { + case "help": + msg.Text = "What you need?" msg.ReplyMarkup = numericKeyboard - case "start": - msg.Text = "Hi :) \n What you need?" + case "start": + msg.Text = "Hi :) \n What you need?" msg.ReplyMarkup = numericKeyboard - case "status": - msg.Text = "bot is ok." + case "status": + msg.Text = "bot is ok." - case "usage": - msg.Text = j.getClientUsage(update.Message.CommandArguments()) - default: - msg.Text = "I don't know that command, /help" + case "usage": + msg.Text = j.getClientUsage(update.Message.CommandArguments()) + default: + msg.Text = "I don't know that command, /help" msg.ReplyMarkup = numericKeyboard - } + } - if _, err := bot.Send(msg); err != nil { - logger.Warning(err) - } - } + if _, err := bot.Send(msg); err != nil { + logger.Warning(err) + } + } return j } func (j *StatsNotifyJob) getClientUsage(id string) string { - traffic , err := j.inboundService.GetClientTrafficById(id) + traffic, err := j.inboundService.GetClientTrafficById(id) if err != nil { logger.Warning(err) return "something wrong!" @@ -241,8 +241,8 @@ func (j *StatsNotifyJob) getClientUsage(id string) string { total = fmt.Sprintf("%s", common.FormatTraffic((traffic.Total))) } output := fmt.Sprintf("💡 Active: %t\r\n📧 Email: %s\r\n🔼 Upload↑: %s\r\n🔽 Download↓: %s\r\n🔄 Total: %s / %s\r\n📅 Expire in: %s\r\n", - traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)), - total, expiryTime) - + traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)), + total, expiryTime) + return output } 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() diff --git a/web/web.go b/web/web.go index 383203b6..7e5f73de 100644 --- a/web/web.go +++ b/web/web.go @@ -21,7 +21,7 @@ import ( "x-ui/web/network" "x-ui/web/service" - "github.com/BurntSushi/toml" + "github.com/pelletier/go-toml/v2" "github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions/cookie" "github.com/gin-gonic/gin" @@ -271,7 +271,7 @@ func (s *Server) initI18n(engine *gin.Engine) error { }) } - engine.FuncMap["i18n"] = I18n; + engine.FuncMap["i18n"] = I18n engine.Use(func(c *gin.Context) { //accept := c.GetHeader("Accept-Language") @@ -286,7 +286,7 @@ func (s *Server) initI18n(engine *gin.Engine) error { localizer = i18n.NewLocalizer(bundle, lang) c.Set("localizer", localizer) - c.Set("I18n" , I18n) + c.Set("I18n", I18n) c.Next() }) @@ -298,19 +298,19 @@ func (s *Server) startTask() { if err != nil { logger.Warning("start xray failed:", err) } - // 每 30 秒检查一次 xray 是否在运行 + // Check whether xray is running every 30 seconds s.cron.AddJob("@every 30s", job.NewCheckXrayRunningJob()) go func() { time.Sleep(time.Second * 5) - // 每 10 秒统计一次流量,首次启动延迟 5 秒,与重启 xray 的时间错开 + // Statistics every 10 seconds, start the delay for 5 seconds for the first time, and staggered with the time to restart xray s.cron.AddJob("@every 10s", job.NewXrayTrafficJob()) }() - // 每 30 秒检查一次 inbound 流量超出和到期的情况 + // Check the inbound traffic every 30 seconds that the traffic exceeds and expires s.cron.AddJob("@every 30s", job.NewCheckInboundJob()) - // 每一天提示一次流量情况,上海时间8点30 + // Make a traffic condition every day, 8:30 var entry cron.EntryID isTgbotenabled, err := s.settingService.GetTgbotenabled() if (err == nil) && (isTgbotenabled) { @@ -320,7 +320,7 @@ func (s *Server) startTask() { runtime = "@daily" } logger.Infof("Tg notify enabled,run at %s", runtime) - entry, err = s.cron.AddJob(runtime, job.NewStatsNotifyJob()) + _, err = s.cron.AddJob(runtime, job.NewStatsNotifyJob()) if err != nil { logger.Warning("Add NewStatsNotifyJob error", err) return @@ -333,7 +333,7 @@ func (s *Server) startTask() { } func (s *Server) Start() (err error) { - //这是一个匿名函数,没没有函数名 + //This is an anonymous function, no function name defer func() { if err != nil { s.Stop() -- cgit v1.2.3