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. // AddInbound adds a new inbound configuration to the Xray core via gRPC.
func (x *XrayAPI) AddInbound(inbound []byte) error { func (x *XrayAPI) AddInbound(inbound []byte) error {
if x.HandlerServiceClient == nil {
return common.NewError("xray handler service client is not initialized")
}
client := *x.HandlerServiceClient client := *x.HandlerServiceClient
conf := new(conf.InboundDetourConfig) 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. // DelInbound removes an inbound configuration from the Xray core by tag.
func (x *XrayAPI) DelInbound(tag string) error { func (x *XrayAPI) DelInbound(tag string) error {
if x.HandlerServiceClient == nil {
return common.NewError("xray handler service client is not initialized")
}
client := *x.HandlerServiceClient client := *x.HandlerServiceClient
_, err := client.RemoveInbound(context.Background(), &command.RemoveInboundRequest{ _, err := client.RemoveInbound(context.Background(), &command.RemoveInboundRequest{
Tag: tag, 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. // 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 { 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 var account *serial.TypedMessage
switch Protocol { switch Protocol {
case "vmess": 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. // RemoveUser removes a user from an inbound in the Xray core by email.
func (x *XrayAPI) RemoveUser(inboundTag, email string) error { 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) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()