fix: add nil checks in xray API to prevent panics

This commit is contained in:
Mohamadhosein Moazennia 2026-02-18 20:11:42 +03:30
parent 87265403c5
commit f754246e3e

View file

@ -72,6 +72,9 @@ func (x *XrayAPI) Close() {
// AddInbound adds a new inbound configuration to the Xray core via gRPC.
func (x *XrayAPI) AddInbound(inbound []byte) error {
if x.HandlerServiceClient == nil {
return common.NewError("xray handler service client is not initialized")
}
client := *x.HandlerServiceClient
conf := new(conf.InboundDetourConfig)
@ -94,6 +97,9 @@ func (x *XrayAPI) AddInbound(inbound []byte) error {
// DelInbound removes an inbound configuration from the Xray core by tag.
func (x *XrayAPI) DelInbound(tag string) error {
if x.HandlerServiceClient == nil {
return common.NewError("xray handler service client is not initialized")
}
client := *x.HandlerServiceClient
_, err := client.RemoveInbound(context.Background(), &command.RemoveInboundRequest{
Tag: tag,
@ -103,6 +109,9 @@ func (x *XrayAPI) DelInbound(tag string) error {
// AddUser adds a user to an inbound in the Xray core using the specified protocol and user data.
func (x *XrayAPI) AddUser(Protocol string, inboundTag string, user map[string]any) error {
if x.HandlerServiceClient == nil {
return common.NewError("xray handler service client is not initialized")
}
var account *serial.TypedMessage
switch Protocol {
case "vmess":
@ -187,6 +196,9 @@ func (x *XrayAPI) AddUser(Protocol string, inboundTag string, user map[string]an
// RemoveUser removes a user from an inbound in the Xray core by email.
func (x *XrayAPI) RemoveUser(inboundTag, email string) error {
if x.HandlerServiceClient == nil {
return common.NewError("xray handler service client is not initialized")
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()