mirror of https://github.com/XTLS/Xray-core
simplify rmu API command
parent
4f4e2fb059
commit
73d80f48da
|
@ -67,11 +67,11 @@ func addInboundUserAction(ctx context.Context, client handlerService.HandlerServ
|
|||
return err
|
||||
}
|
||||
|
||||
func extractInboundUsers(i *core.InboundHandlerConfig) []*protocol.User {
|
||||
if i == nil {
|
||||
func extractInboundUsers(inb *core.InboundHandlerConfig) []*protocol.User {
|
||||
if inb == nil {
|
||||
return nil
|
||||
}
|
||||
inst, err := i.ProxySettings.GetInstance()
|
||||
inst, err := inb.ProxySettings.GetInstance()
|
||||
if err != nil || inst == nil {
|
||||
fmt.Println("failed to get inbound instance:", err)
|
||||
return nil
|
||||
|
@ -109,7 +109,6 @@ func extractInboundsConfig(unnamedArgs []string) []conf.InboundDetourConfig {
|
|||
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 {
|
||||
success := 0
|
||||
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/xtls/xray-core/common/protocol"
|
||||
|
||||
handlerService "github.com/xtls/xray-core/app/proxyman/command"
|
||||
cserial "github.com/xtls/xray-core/common/serial"
|
||||
|
||||
|
@ -14,7 +11,7 @@ import (
|
|||
|
||||
var cmdRemoveInboundUsers = &base.Command{
|
||||
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",
|
||||
Long: `
|
||||
Remove users from inbounds.
|
||||
|
@ -23,37 +20,43 @@ Arguments:
|
|||
The API server address. Default 127.0.0.1:8080
|
||||
-t, -timeout
|
||||
Timeout seconds to call API. Default 3
|
||||
-tag
|
||||
Inbound tag
|
||||
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,
|
||||
}
|
||||
|
||||
func executeRemoveUsers(cmd *base.Command, args []string) {
|
||||
setSharedFlags(cmd)
|
||||
var tag string
|
||||
cmd.Flag.StringVar(&tag, "tag", "", "")
|
||||
cmd.Flag.Parse(args)
|
||||
unnamedArgs := cmd.Flag.Args()
|
||||
inbs := extractInboundsConfig(unnamedArgs)
|
||||
emails := cmd.Flag.Args()
|
||||
if len(tag) < 1 {
|
||||
base.Fatalf("inbound tag not specified")
|
||||
}
|
||||
|
||||
conn, ctx, close := dialAPIServer()
|
||||
defer close()
|
||||
client := handlerService.NewHandlerServiceClient(conn)
|
||||
|
||||
success := 0
|
||||
for _, inb := range inbs {
|
||||
success += executeInboundUserAction(ctx, client, inb, removeInboundUserAction)
|
||||
for _, email := range emails {
|
||||
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.")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue