From 9fbde9dae8107c463b874cde0e009022ad1154fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=83=E9=9A=A8=E7=B7=A3=E5=8B=95?= Date: Sat, 20 Sep 2025 23:30:28 +0800 Subject: [PATCH] Some minor fixes --- xray/api.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/xray/api.go b/xray/api.go index d68b1f96..3f81f298 100644 --- a/xray/api.go +++ b/xray/api.go @@ -139,9 +139,16 @@ func (x *XrayAPI) AddUser(Protocol string, inboundTag string, user map[string]an return nil } + // 〔中文注释〕: (修改点) 创建一个有5秒超时限制的上下文(Context)。 + // 这确保了如果 Xray-Core API 因为某些原因没有及时响应, + // 这个操作不会永久阻塞,而是在5秒后自动失败,从而提高程序的健壮性。 + // 这与 RemoveUser 函数中的超时设置保持了一致。 + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + client := *x.HandlerServiceClient - _, err := client.AlterInbound(context.Background(), &command.AlterInboundRequest{ + _, err := client.AlterInbound(ctx, &command.AlterInboundRequest{ // 〔中文注释〕: (修改点) 使用上面创建的带超时的 ctx Tag: inboundTag, Operation: serial.ToTypedMessage(&command.AddUserOperation{ User: &protocol.User{ @@ -150,7 +157,14 @@ func (x *XrayAPI) AddUser(Protocol string, inboundTag string, user map[string]an }, }), }) - return err + + // 〔中文注释〕: (修改点) 增加更详细的错误日志,方便排查问题。 + if err != nil { + emailStr, _ := user["email"].(string) + return fmt.Errorf("failed to add user '%s' to inbound '%s': %w", emailStr, inboundTag, err) + } + + return nil } func (x *XrayAPI) RemoveUser(inboundTag, email string) error {