update by client id

Co-Authored-By: Alireza Ahmadi <alireza7@gmail.com>
This commit is contained in:
MHSanaei 2023-04-25 14:38:35 +03:30
parent 045717010a
commit cc3ff61ae2
6 changed files with 69 additions and 51 deletions

View file

@ -27,7 +27,7 @@ func (a *APIController) initRouter(g *gin.RouterGroup) {
g.POST("/clearClientIps/:email", a.clearClientIps) g.POST("/clearClientIps/:email", a.clearClientIps)
g.POST("/addClient/", a.addInboundClient) g.POST("/addClient/", a.addInboundClient)
g.POST("/:id/delClient/:clientId", a.delInboundClient) g.POST("/:id/delClient/:clientId", a.delInboundClient)
g.POST("/updateClient/:index", a.updateInboundClient) g.POST("/updateClient/:clientId", a.updateInboundClient)
g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic) g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
g.POST("/resetAllTraffics", a.resetAllTraffics) g.POST("/resetAllTraffics", a.resetAllTraffics)
g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics) g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)

View file

@ -35,7 +35,7 @@ func (a *InboundController) initRouter(g *gin.RouterGroup) {
g.POST("/clearClientIps/:email", a.clearClientIps) g.POST("/clearClientIps/:email", a.clearClientIps)
g.POST("/addClient", a.addInboundClient) g.POST("/addClient", a.addInboundClient)
g.POST("/:id/delClient/:clientId", a.delInboundClient) g.POST("/:id/delClient/:clientId", a.delInboundClient)
g.POST("/updateClient/:index", a.updateInboundClient) g.POST("/updateClient/:clientId", a.updateInboundClient)
g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic) g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
g.POST("/resetAllTraffics", a.resetAllTraffics) g.POST("/resetAllTraffics", a.resetAllTraffics)
g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics) g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
@ -199,20 +199,16 @@ func (a *InboundController) delInboundClient(c *gin.Context) {
} }
func (a *InboundController) updateInboundClient(c *gin.Context) { func (a *InboundController) updateInboundClient(c *gin.Context) {
index, err := strconv.Atoi(c.Param("index")) clientId := c.Param("clientId")
if err != nil {
jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
return
}
inbound := &model.Inbound{} inbound := &model.Inbound{}
err = c.ShouldBind(inbound) err := c.ShouldBind(inbound)
if err != nil { if err != nil {
jsonMsg(c, I18n(c, "pages.inbounds.revise"), err) jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
return return
} }
err = a.inboundService.UpdateInboundClient(inbound, index) err = a.inboundService.UpdateInboundClient(inbound, clientId)
if err != nil { if err != nil {
jsonMsg(c, "something worng!", err) jsonMsg(c, "something worng!", err)
return return

View file

@ -17,13 +17,14 @@
inbound: new Inbound(), inbound: new Inbound(),
clients: [], clients: [],
clientStats: [], clientStats: [],
oldClientId: "",
index: null, index: null,
clientIps: null, clientIps: null,
isExpired: false, isExpired: false,
delayedStart: false, delayedStart: false,
ok() { ok() {
if(clientModal.isEdit){ if(clientModal.isEdit){
ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id, clientModal.index); ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id, clientModal.oldClientId);
} else { } else {
ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id); ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id);
} }
@ -39,12 +40,13 @@
this.index = index === null ? this.clients.length : index; this.index = index === null ? this.clients.length : index;
this.isExpired = isEdit ? this.inbound.isExpiry(this.index) : false; this.isExpired = isEdit ? this.inbound.isExpiry(this.index) : false;
this.delayedStart = false; this.delayedStart = false;
if (!isEdit){ if (isEdit){
this.addClient(this.inbound.protocol, this.clients);
} else {
if (this.clients[index].expiryTime < 0){ if (this.clients[index].expiryTime < 0){
this.delayedStart = true; this.delayedStart = true;
} }
this.oldClientId = this.dbInbound.protocol == "trojan" ? this.clients[index].password : this.clients[index].id;
} else {
this.addClient(this.inbound.protocol, this.clients);
} }
this.clientStats = this.dbInbound.clientStats.find(row => row.email === this.clients[this.index].email); this.clientStats = this.dbInbound.clientStats.find(row => row.email === this.clients[this.index].email);
this.confirm = confirm; this.confirm = confirm;

View file

@ -561,9 +561,9 @@
okText: '{{ i18n "pages.client.submitEdit"}}', okText: '{{ i18n "pages.client.submitEdit"}}',
dbInbound: dbInbound, dbInbound: dbInbound,
index: index, index: index,
confirm: async (client, dbInboundId, index) => { confirm: async (client, dbInboundId, clientId) => {
clientModal.loading(); clientModal.loading();
await this.updateClient(client, dbInboundId, index); await this.updateClient(client, dbInboundId, clientId);
clientModal.close(); clientModal.close();
}, },
isEdit: true isEdit: true
@ -580,12 +580,12 @@
}; };
await this.submit(`/xui/inbound/addClient`, data); await this.submit(`/xui/inbound/addClient`, data);
}, },
async updateClient(client, dbInboundId, index) { async updateClient(client, dbInboundId, clientId) {
const data = { const data = {
id: dbInboundId, id: dbInboundId,
settings: '{"clients": [' + client.toString() +']}', settings: '{"clients": [' + client.toString() +']}',
}; };
await this.submit(`/xui/inbound/updateClient/${index}`, data); await this.submit(`/xui/inbound/updateClient/${clientId}`, data);
}, },
resetTraffic(dbInboundId) { resetTraffic(dbInboundId) {
dbInbound = this.dbInbounds.find(row => row.id === dbInboundId); dbInbound = this.dbInbounds.find(row => row.id === dbInboundId);

View file

@ -367,7 +367,7 @@ func (s *InboundService) DelInboundClient(inboundId int, clientId string) error
return db.Save(oldInbound).Error return db.Save(oldInbound).Error
} }
func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) error { func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId string) error {
clients, err := s.getClients(data) clients, err := s.getClients(data)
if err != nil { if err != nil {
return err return err
@ -391,7 +391,23 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err
return err return err
} }
if len(clients[0].Email) > 0 && clients[0].Email != oldClients[index].Email { oldEmail := ""
clientIndex := 0
for index, oldClient := range oldClients {
oldClientId := ""
if oldInbound.Protocol == "trojan" {
oldClientId = oldClient.Password
} else {
oldClientId = oldClient.ID
}
if clientId == oldClientId {
oldEmail = oldClient.Email
clientIndex = index
break
}
}
if len(clients[0].Email) > 0 && clients[0].Email != oldEmail {
existEmail, err := s.checkEmailsExistForClients(clients) existEmail, err := s.checkEmailsExistForClients(clients)
if err != nil { if err != nil {
return err return err
@ -406,10 +422,8 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err
if err != nil { if err != nil {
return err return err
} }
settingsClients := oldSettings["clients"].([]interface{}) settingsClients := oldSettings["clients"].([]interface{})
settingsClients[index] = inerfaceClients[0] settingsClients[clientIndex] = inerfaceClients[0]
oldSettings["clients"] = settingsClients oldSettings["clients"] = settingsClients
newSettings, err := json.MarshalIndent(oldSettings, "", " ") newSettings, err := json.MarshalIndent(oldSettings, "", " ")
@ -421,12 +435,12 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err
db := database.GetDB() db := database.GetDB()
if len(clients[0].Email) > 0 { if len(clients[0].Email) > 0 {
if len(oldClients[index].Email) > 0 { if len(oldEmail) > 0 {
err = s.UpdateClientStat(oldClients[index].Email, &clients[0]) err = s.UpdateClientStat(oldEmail, &clients[0])
if err != nil { if err != nil {
return err return err
} }
err = s.UpdateClientIPs(db, oldClients[index].Email, clients[0].Email) err = s.UpdateClientIPs(db, oldEmail, clients[0].Email)
if err != nil { if err != nil {
return err return err
} }
@ -434,11 +448,11 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err
s.AddClientStat(data.Id, &clients[0]) s.AddClientStat(data.Id, &clients[0])
} }
} else { } else {
err = s.DelClientStat(db, oldClients[index].Email) err = s.DelClientStat(db, oldEmail)
if err != nil { if err != nil {
return err return err
} }
err = s.DelClientIPs(db, oldClients[index].Email) err = s.DelClientIPs(db, oldEmail)
if err != nil { if err != nil {
return err return err
} }
@ -667,8 +681,15 @@ func (s *InboundService) ResetClientTraffic(id int, clientEmail string) error {
func (s *InboundService) ResetAllClientTraffics(id int) error { func (s *InboundService) ResetAllClientTraffics(id int) error {
db := database.GetDB() db := database.GetDB()
whereText := "inbound_id "
if id == -1 {
whereText += " > ?"
} else {
whereText += " = ?"
}
result := db.Model(xray.ClientTraffic{}). result := db.Model(xray.ClientTraffic{}).
Where("inbound_id = ?", id). Where(whereText, id).
Updates(map[string]interface{}{"enable": true, "up": 0, "down": 0}) Updates(map[string]interface{}{"enable": true, "up": 0, "down": 0})
err := result.Error err := result.Error
@ -724,7 +745,7 @@ func (s *InboundService) GetClientTrafficTgBot(tguname string) ([]*xray.ClientTr
return traffics, err return traffics, err
} }
func (s *InboundService) GetClientTrafficByEmail(email string) (traffic []*xray.ClientTraffic, err error) { func (s *InboundService) GetClientTrafficByEmail(email string) (traffic *xray.ClientTraffic, err error) {
db := database.GetDB() db := database.GetDB()
var traffics []*xray.ClientTraffic var traffics []*xray.ClientTraffic
@ -735,7 +756,7 @@ func (s *InboundService) GetClientTrafficByEmail(email string) (traffic []*xray.
return nil, err return nil, err
} }
} }
return traffics, err return traffics[0], err
} }
func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) { func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) {
@ -809,6 +830,7 @@ func (s *InboundService) SearchInbounds(query string) ([]*model.Inbound, error)
} }
return inbounds, nil return inbounds, nil
} }
func (s *InboundService) MigrationRequirements() { func (s *InboundService) MigrationRequirements() {
db := database.GetDB() db := database.GetDB()
var inbounds []*model.Inbound var inbounds []*model.Inbound

View file

@ -404,19 +404,18 @@ func (t *Tgbot) getClientUsage(chatId int64, tgUserName string) {
} }
func (t *Tgbot) searchClient(chatId int64, email string) { func (t *Tgbot) searchClient(chatId int64, email string) {
traffics, err := t.inboundService.GetClientTrafficByEmail(email) traffic, err := t.inboundService.GetClientTrafficByEmail(email)
if err != nil { if err != nil {
logger.Warning(err) logger.Warning(err)
msg := "❌ Something went wrong!" msg := "❌ Something went wrong!"
t.SendMsgToTgbot(chatId, msg) t.SendMsgToTgbot(chatId, msg)
return return
} }
if len(traffics) == 0 { if traffic == nil {
msg := "No result!" msg := "No result!"
t.SendMsgToTgbot(chatId, msg) t.SendMsgToTgbot(chatId, msg)
return return
} }
for _, traffic := range traffics {
expiryTime := "" expiryTime := ""
if traffic.ExpiryTime == 0 { if traffic.ExpiryTime == 0 {
expiryTime = "♾Unlimited" expiryTime = "♾Unlimited"
@ -436,7 +435,6 @@ func (t *Tgbot) searchClient(chatId int64, email string) {
total, expiryTime) total, expiryTime)
t.SendMsgToTgbot(chatId, output) t.SendMsgToTgbot(chatId, output)
} }
}
func (t *Tgbot) searchInbound(chatId int64, remark string) { func (t *Tgbot) searchInbound(chatId int64, remark string) {
inbouds, err := t.inboundService.SearchInbounds(remark) inbouds, err := t.inboundService.SearchInbounds(remark)