diff options
| author | MHSanaei <ho3ein.sanaei@gmail.com> | 2023-04-24 14:37:11 +0300 |
|---|---|---|
| committer | MHSanaei <ho3ein.sanaei@gmail.com> | 2023-04-24 14:37:11 +0300 |
| commit | 16e3107d23b4bc536d509c2dd832d10366fb895e (patch) | |
| tree | 3592c9b3d2cd703a73aeed971bed65c0c37a0fa6 /web | |
| parent | 262e3c0985c8c315c78f86370e8b338df9e69593 (diff) | |
Better client delete + api
Co-Authored-By: Alireza Ahmadi <alireza7@gmail.com>
Diffstat (limited to 'web')
| -rw-r--r-- | web/controller/api.go | 2 | ||||
| -rw-r--r-- | web/controller/inbound.go | 11 | ||||
| -rw-r--r-- | web/html/xui/inbounds.html | 16 | ||||
| -rw-r--r-- | web/service/inbound.go | 44 |
4 files changed, 46 insertions, 27 deletions
diff --git a/web/controller/api.go b/web/controller/api.go index c8ad2a67..b8da9243 100644 --- a/web/controller/api.go +++ b/web/controller/api.go @@ -26,7 +26,7 @@ func (a *APIController) initRouter(g *gin.RouterGroup) { g.POST("/clientIps/:email", a.getClientIps) g.POST("/clearClientIps/:email", a.clearClientIps) g.POST("/addClient/", a.addInboundClient) - g.POST("/delClient/:email", a.delInboundClient) + g.POST("/:id/delClient/:clientId", a.delInboundClient) g.POST("/updateClient/:index", a.updateInboundClient) g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic) g.POST("/resetAllTraffics", a.resetAllTraffics) diff --git a/web/controller/inbound.go b/web/controller/inbound.go index f32cb766..ec37dcc7 100644 --- a/web/controller/inbound.go +++ b/web/controller/inbound.go @@ -34,7 +34,7 @@ func (a *InboundController) initRouter(g *gin.RouterGroup) { g.POST("/clientIps/:email", a.getClientIps) g.POST("/clearClientIps/:email", a.clearClientIps) g.POST("/addClient", a.addInboundClient) - g.POST("/delClient/:email", a.delInboundClient) + g.POST("/:id/delClient/:clientId", a.delInboundClient) g.POST("/updateClient/:index", a.updateInboundClient) g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic) g.POST("/resetAllTraffics", a.resetAllTraffics) @@ -155,7 +155,7 @@ func (a *InboundController) clearClientIps(c *gin.Context) { err := a.inboundService.ClearClientIps(email) if err != nil { - jsonMsg(c, "修改", err) + jsonMsg(c, "Revise", err) return } jsonMsg(c, "Log Cleared", nil) @@ -180,15 +180,14 @@ func (a *InboundController) addInboundClient(c *gin.Context) { } func (a *InboundController) delInboundClient(c *gin.Context) { - email := c.Param("email") - inbound := &model.Inbound{} - err := c.ShouldBind(inbound) + id, err := strconv.Atoi(c.Param("id")) if err != nil { jsonMsg(c, I18n(c, "pages.inbounds.revise"), err) return } + clientId := c.Param("clientId") - err = a.inboundService.DelInboundClient(inbound, email) + err = a.inboundService.DelInboundClient(id, clientId) if err != nil { jsonMsg(c, "something worng!", err) return diff --git a/web/html/xui/inbounds.html b/web/html/xui/inbounds.html index 5bfcaccd..6f13cd86 100644 --- a/web/html/xui/inbounds.html +++ b/web/html/xui/inbounds.html @@ -70,7 +70,7 @@ <a-button type="primary" icon="export" @click="exportAllLinks">{{ i18n "pages.inbounds.export" }}</a-button> <a-button type="primary" icon="reload" @click="resetAllTraffic">{{ i18n "pages.inbounds.resetAllTraffic" }}</a-button> </div> - <a-input v-model.lazy="searchKey" placeholder="{{ i18n "search" }}" autofocus style="max-width: 300px"></a-input> + <a-input v-model.lazy="searchKey" placeholder='{{ i18n "search" }}' autofocus style="max-width: 300px"></a-input> <a-table :columns="columns" :row-key="dbInbound => dbInbound.id" :data-source="searchedInbounds" :loading="spinning" :scroll="{ x: 1300 }" @@ -426,7 +426,7 @@ }, openCloneInbound(dbInbound) { this.$confirm({ - title: '{{ i18n "pages.inbounds.cloneInbound"}} ' + dbInbound.remark, + title: '{{ i18n "pages.inbounds.cloneInbound"}}' + dbInbound.remark, content: '{{ i18n "pages.inbounds.cloneInboundContent"}}', okText: '{{ i18n "pages.inbounds.cloneInboundOk"}}', cancelText: '{{ i18n "cancel" }}', @@ -615,22 +615,14 @@ }, delClient(dbInboundId,client) { dbInbound = this.dbInbounds.find(row => row.id === dbInboundId); - newDbInbound = new DBInbound(dbInbound); - inbound = newDbInbound.toInbound(); - clients = this.getClients(dbInbound.protocol, inbound.settings); - index = this.findIndexOfClient(clients, client); - clients.splice(index, 1); - const data = { - id: dbInboundId, - settings: inbound.settings.toString(), - }; + clientId = dbInbound.protocol == "trojan" ? client.password : client.id; this.$confirm({ title: '{{ i18n "pages.inbounds.deleteInbound"}}', content: '{{ i18n "pages.inbounds.deleteInboundContent"}}', class: siderDrawer.isDarkTheme ? darkClass : '', okText: '{{ i18n "delete"}}', cancelText: '{{ i18n "cancel"}}', - onOk: () => this.submit('/xui/inbound/delClient/' + client.email, data), + onOk: () => this.submit(`/xui/inbound/${dbInboundId}/delClient/${clientId}`), }); }, getClients(protocol, clientSettings) { diff --git a/web/service/inbound.go b/web/service/inbound.go index 3f736470..5d6acb92 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -314,28 +314,56 @@ func (s *InboundService) AddInboundClient(data *model.Inbound) error { return db.Save(oldInbound).Error } -func (s *InboundService) DelInboundClient(inbound *model.Inbound, email string) error { - db := database.GetDB() - err := s.DelClientStat(db, email) +func (s *InboundService) DelInboundClient(inboundId int, clientId string) error { + oldInbound, err := s.GetInbound(inboundId) + if err != nil { + logger.Error("Load Old Data Error") + return err + } + var settings map[string]interface{} + err = json.Unmarshal([]byte(oldInbound.Settings), &settings) if err != nil { - logger.Error("Delete stats Data Error") return err } - oldInbound, err := s.GetInbound(inbound.Id) + email := "" + client_key := "id" + if oldInbound.Protocol == "trojan" { + client_key = "password" + } + + inerfaceClients := settings["clients"].([]interface{}) + var newClients []interface{} + for _, client := range inerfaceClients { + c := client.(map[string]interface{}) + c_id := c[client_key].(string) + if c_id == clientId { + email = c["email"].(string) + } else { + newClients = append(newClients, client) + } + } + + settings["clients"] = newClients + newSettings, err := json.MarshalIndent(settings, "", " ") if err != nil { - logger.Error("Load Old Data Error") return err } - oldInbound.Settings = inbound.Settings + oldInbound.Settings = string(newSettings) + + db := database.GetDB() + err = s.DelClientStat(db, email) + if err != nil { + logger.Error("Delete stats Data Error") + return err + } err = s.DelClientIPs(db, email) if err != nil { logger.Error("Error in delete client IPs") return err } - return db.Save(oldInbound).Error } |
