1
1
package controller
2
2
3
3
import (
4
+ "errors"
4
5
"encoding/json"
5
6
"fmt"
6
7
"strconv"
7
-
8
8
"x-ui/database/model"
9
9
"x-ui/web/service"
10
10
"x-ui/web/session"
@@ -33,9 +33,13 @@ func (a *InboundController) initRouter(g *gin.RouterGroup) {
33
33
g .POST ("/clientIps/:email" , a .getClientIps )
34
34
g .POST ("/clearClientIps/:email" , a .clearClientIps )
35
35
g .POST ("/addClient" , a .addInboundClient )
36
+ g .POST ("/addGroupClient" , a .addGroupInboundClient )
36
37
g .POST ("/:id/delClient/:clientId" , a .delInboundClient )
38
+ g .POST ("/delGroupClients" , a .delGroupClients )
37
39
g .POST ("/updateClient/:clientId" , a .updateInboundClient )
40
+ g .POST ("/updateClients" , a .updateGroupInboundClient )
38
41
g .POST ("/:id/resetClientTraffic/:email" , a .resetClientTraffic )
42
+ g .POST ("/resetGroupClientTraffic" , a .resetGroupClientTraffic )
39
43
g .POST ("/resetAllTraffics" , a .resetAllTraffics )
40
44
g .POST ("/resetAllClientTraffics/:id" , a .resetAllClientTraffics )
41
45
g .POST ("/delDepletedClients/:id" , a .delDepletedClients )
@@ -190,6 +194,34 @@ func (a *InboundController) addInboundClient(c *gin.Context) {
190
194
}
191
195
}
192
196
197
+ func (a * InboundController ) addGroupInboundClient (c * gin.Context ) {
198
+ var requestData []model.Inbound
199
+
200
+ err := c .ShouldBindJSON (& requestData )
201
+
202
+ if err != nil {
203
+ jsonMsg (c , I18nWeb (c , "pages.inbounds.update" ), err )
204
+ return
205
+ }
206
+
207
+ needRestart := true
208
+
209
+ for _ , data := range requestData {
210
+
211
+ needRestart , err = a .inboundService .AddInboundClient (& data )
212
+ if err != nil {
213
+ jsonMsg (c , "Something went wrong!" , err )
214
+ return
215
+ }
216
+ }
217
+
218
+ jsonMsg (c , "Client(s) added" , nil )
219
+ if err == nil && needRestart {
220
+ a .xrayService .SetToNeedRestart ()
221
+ }
222
+
223
+ }
224
+
193
225
func (a * InboundController ) delInboundClient (c * gin.Context ) {
194
226
id , err := strconv .Atoi (c .Param ("id" ))
195
227
if err != nil {
@@ -211,6 +243,38 @@ func (a *InboundController) delInboundClient(c *gin.Context) {
211
243
}
212
244
}
213
245
246
+ func (a * InboundController ) delGroupClients (c * gin.Context ) {
247
+ var requestData []struct {
248
+ InboundID int `json:"inboundId"`
249
+ ClientID string `json:"clientId"`
250
+ }
251
+
252
+ if err := c .ShouldBindJSON (& requestData ); err != nil {
253
+ jsonMsg (c , "Invalid request data" , err )
254
+ return
255
+ }
256
+
257
+ needRestart := false
258
+
259
+ for _ , req := range requestData {
260
+ needRestartTmp , err := a .inboundService .DelInboundClient (req .InboundID , req .ClientID )
261
+ if err != nil {
262
+ jsonMsg (c , "Failed to delete client" , err )
263
+ return
264
+ }
265
+
266
+ if needRestartTmp {
267
+ needRestart = true
268
+ }
269
+ }
270
+
271
+ jsonMsg (c , "Clients deleted successfully" , nil )
272
+
273
+ if needRestart {
274
+ a .xrayService .SetToNeedRestart ()
275
+ }
276
+ }
277
+
214
278
func (a * InboundController ) updateInboundClient (c * gin.Context ) {
215
279
clientId := c .Param ("clientId" )
216
280
@@ -234,6 +298,56 @@ func (a *InboundController) updateInboundClient(c *gin.Context) {
234
298
}
235
299
}
236
300
301
+ func (a * InboundController ) updateGroupInboundClient (c * gin.Context ) {
302
+ var requestData []map [string ]interface {}
303
+
304
+ if err := c .ShouldBindJSON (& requestData ); err != nil {
305
+ jsonMsg (c , I18nWeb (c , "pages.inbounds.update" ), err )
306
+ return
307
+ }
308
+
309
+ needRestart := false
310
+
311
+ for _ , item := range requestData {
312
+
313
+ inboundMap , ok := item ["inbound" ].(map [string ]interface {})
314
+ if ! ok {
315
+ jsonMsg (c , "Something went wrong!" , errors .New ("Failed to convert 'inbound' to map" ))
316
+ return
317
+ }
318
+
319
+ clientId , ok := item ["clientId" ].(string )
320
+ if ! ok {
321
+ jsonMsg (c , "Something went wrong!" , errors .New ("Failed to convert 'clientId' to string" ))
322
+ return
323
+ }
324
+
325
+ inboundJSON , err := json .Marshal (inboundMap )
326
+ if err != nil {
327
+ jsonMsg (c , "Something went wrong!" , err )
328
+ return
329
+ }
330
+
331
+ var inboundModel model.Inbound
332
+ if err := json .Unmarshal (inboundJSON , & inboundModel ); err != nil {
333
+ jsonMsg (c , "Something went wrong!" , err )
334
+ return
335
+ }
336
+
337
+ if restart , err := a .inboundService .UpdateInboundClient (& inboundModel , clientId ); err != nil {
338
+ jsonMsg (c , "Something went wrong!" , err )
339
+ return
340
+ } else {
341
+ needRestart = needRestart || restart
342
+ }
343
+ }
344
+
345
+ jsonMsg (c , "Client updated" , nil )
346
+ if needRestart {
347
+ a .xrayService .SetToNeedRestart ()
348
+ }
349
+ }
350
+
237
351
func (a * InboundController ) resetClientTraffic (c * gin.Context ) {
238
352
id , err := strconv .Atoi (c .Param ("id" ))
239
353
if err != nil {
@@ -253,6 +367,44 @@ func (a *InboundController) resetClientTraffic(c *gin.Context) {
253
367
}
254
368
}
255
369
370
+ func (a * InboundController ) resetGroupClientTraffic (c * gin.Context ) {
371
+ var requestData []struct {
372
+ InboundID int `json:"inboundId"` // Map JSON "inboundId" to struct field "InboundID"
373
+ Email string `json:"email"` // Map JSON "email" to struct field "Email"
374
+ }
375
+
376
+ // Parse JSON body directly using ShouldBindJSON
377
+ if err := c .ShouldBindJSON (& requestData ); err != nil {
378
+ jsonMsg (c , "Invalid request data" , err )
379
+ return
380
+ }
381
+
382
+ needRestart := false
383
+
384
+ // Process each request data
385
+ for _ , req := range requestData {
386
+ needRestartTmp , err := a .inboundService .ResetClientTraffic (req .InboundID , req .Email )
387
+ if err != nil {
388
+ jsonMsg (c , "Failed to reset client traffic" , err )
389
+ return
390
+ }
391
+
392
+ // If any request requires a restart, set needRestart to true
393
+ if needRestartTmp {
394
+ needRestart = true
395
+ }
396
+ }
397
+
398
+ // Send response back to the client
399
+ jsonMsg (c , "Traffic reset for all clients" , nil )
400
+
401
+ // Restart the service if required
402
+ if needRestart {
403
+ a .xrayService .SetToNeedRestart ()
404
+ }
405
+ }
406
+
407
+
256
408
func (a * InboundController ) resetAllTraffics (c * gin.Context ) {
257
409
err := a .inboundService .ResetAllTraffics ()
258
410
if err != nil {
0 commit comments