Some minor fixes
This commit is contained in:
parent
7c71cf928e
commit
9fbde9dae8
18
xray/api.go
18
xray/api.go
@ -139,9 +139,16 @@ func (x *XrayAPI) AddUser(Protocol string, inboundTag string, user map[string]an
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 〔中文注释〕: (修改点) 创建一个有5秒超时限制的上下文(Context)。
|
||||||
|
// 这确保了如果 Xray-Core API 因为某些原因没有及时响应,
|
||||||
|
// 这个操作不会永久阻塞,而是在5秒后自动失败,从而提高程序的健壮性。
|
||||||
|
// 这与 RemoveUser 函数中的超时设置保持了一致。
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
client := *x.HandlerServiceClient
|
client := *x.HandlerServiceClient
|
||||||
|
|
||||||
_, err := client.AlterInbound(context.Background(), &command.AlterInboundRequest{
|
_, err := client.AlterInbound(ctx, &command.AlterInboundRequest{ // 〔中文注释〕: (修改点) 使用上面创建的带超时的 ctx
|
||||||
Tag: inboundTag,
|
Tag: inboundTag,
|
||||||
Operation: serial.ToTypedMessage(&command.AddUserOperation{
|
Operation: serial.ToTypedMessage(&command.AddUserOperation{
|
||||||
User: &protocol.User{
|
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 {
|
func (x *XrayAPI) RemoveUser(inboundTag, email string) error {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user