simplify rmu API command

pull/4943/head
nobody 2025-07-26 14:48:36 +08:00
parent 4f4e2fb059
commit 73d80f48da
2 changed files with 27 additions and 25 deletions

View File

@ -67,11 +67,11 @@ func addInboundUserAction(ctx context.Context, client handlerService.HandlerServ
return err return err
} }
func extractInboundUsers(i *core.InboundHandlerConfig) []*protocol.User { func extractInboundUsers(inb *core.InboundHandlerConfig) []*protocol.User {
if i == nil { if inb == nil {
return nil return nil
} }
inst, err := i.ProxySettings.GetInstance() inst, err := inb.ProxySettings.GetInstance()
if err != nil || inst == nil { if err != nil || inst == nil {
fmt.Println("failed to get inbound instance:", err) fmt.Println("failed to get inbound instance:", err)
return nil return nil
@ -109,7 +109,6 @@ func extractInboundsConfig(unnamedArgs []string) []conf.InboundDetourConfig {
return ins return ins
} }
// inbound_user_remove.go require this function too
func executeInboundUserAction(ctx context.Context, client handlerService.HandlerServiceClient, inb conf.InboundDetourConfig, action func(ctx context.Context, client handlerService.HandlerServiceClient, tag string, user *protocol.User) error) int { func executeInboundUserAction(ctx context.Context, client handlerService.HandlerServiceClient, inb conf.InboundDetourConfig, action func(ctx context.Context, client handlerService.HandlerServiceClient, tag string, user *protocol.User) error) int {
success := 0 success := 0

View File

@ -1,11 +1,8 @@
package api package api
import ( import (
"context"
"fmt" "fmt"
"github.com/xtls/xray-core/common/protocol"
handlerService "github.com/xtls/xray-core/app/proxyman/command" handlerService "github.com/xtls/xray-core/app/proxyman/command"
cserial "github.com/xtls/xray-core/common/serial" cserial "github.com/xtls/xray-core/common/serial"
@ -14,7 +11,7 @@ import (
var cmdRemoveInboundUsers = &base.Command{ var cmdRemoveInboundUsers = &base.Command{
CustomFlags: true, CustomFlags: true,
UsageLine: "{{.Exec}} api rmu [--server=127.0.0.1:8080] <c1.json> [c2.json]...", UsageLine: "{{.Exec}} api rmu [--server=127.0.0.1:8080] -tag=tag <email1> [email2]...",
Short: "Remove users from inbounds", Short: "Remove users from inbounds",
Long: ` Long: `
Remove users from inbounds. Remove users from inbounds.
@ -23,37 +20,43 @@ Arguments:
The API server address. Default 127.0.0.1:8080 The API server address. Default 127.0.0.1:8080
-t, -timeout -t, -timeout
Timeout seconds to call API. Default 3 Timeout seconds to call API. Default 3
-tag
Inbound tag
Example: Example:
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 c1.json c2.json {{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -tag="vless-in" "xray@love.com" ...
`, `,
Run: executeRemoveUsers, Run: executeRemoveUsers,
} }
func executeRemoveUsers(cmd *base.Command, args []string) { func executeRemoveUsers(cmd *base.Command, args []string) {
setSharedFlags(cmd) setSharedFlags(cmd)
var tag string
cmd.Flag.StringVar(&tag, "tag", "", "")
cmd.Flag.Parse(args) cmd.Flag.Parse(args)
unnamedArgs := cmd.Flag.Args() emails := cmd.Flag.Args()
inbs := extractInboundsConfig(unnamedArgs) if len(tag) < 1 {
base.Fatalf("inbound tag not specified")
}
conn, ctx, close := dialAPIServer() conn, ctx, close := dialAPIServer()
defer close() defer close()
client := handlerService.NewHandlerServiceClient(conn) client := handlerService.NewHandlerServiceClient(conn)
success := 0 success := 0
for _, inb := range inbs { for _, email := range emails {
success += executeInboundUserAction(ctx, client, inb, removeInboundUserAction) fmt.Println("remove user:", email)
_, err := client.AlterInbound(ctx, &handlerService.AlterInboundRequest{
Tag: tag,
Operation: cserial.ToTypedMessage(
&handlerService.RemoveUserOperation{
Email: email,
}),
})
if err == nil {
success += 1
} else {
fmt.Println(err)
}
} }
fmt.Println("Removed", success, "user(s) in total.") fmt.Println("Removed", success, "user(s) in total.")
} }
func removeInboundUserAction(ctx context.Context, client handlerService.HandlerServiceClient, tag string, user *protocol.User) error {
fmt.Println("remove user:", user.Email)
_, err := client.AlterInbound(ctx, &handlerService.AlterInboundRequest{
Tag: tag,
Operation: cserial.ToTypedMessage(
&handlerService.RemoveUserOperation{
Email: user.Email,
}),
})
return err
}