Files
Archive/echo/pkg/xray/services.go
2025-11-12 19:37:33 +01:00

59 lines
1.7 KiB
Go

package xray
import (
"context"
"strings"
proxy "github.com/xtls/xray-core/app/proxyman/command"
"github.com/xtls/xray-core/common/serial"
"go.uber.org/zap"
)
func getEmailAndTrafficType(input string) (string, string) {
s := strings.Split(input, ">>>")
return s[1], s[len(s)-1]
}
// AddInboundUser add user to inbound by tag
func AddInboundUser(ctx context.Context, c proxy.HandlerServiceClient, tag string, user *User) error {
_, err := c.AlterInbound(ctx, &proxy.AlterInboundRequest{
Tag: tag,
Operation: serial.ToTypedMessage(
&proxy.AddUserOperation{User: user.ToXrayUser()}),
})
if err != nil {
if strings.Contains(err.Error(), "already exists") {
zap.S().Named("xray").Infof("User %s already exists", user.GetEmail())
return nil
}
zap.S().Named("xray").Errorf("Failed to Add User: %s To Server Tag: %s", user.GetEmail(), tag)
}
user.running = true
zap.S().Named("xray").Infof("Add User: %s To Server Tag: %s", user.GetEmail(), tag)
return nil
}
// RemoveInboundUser remove user from inbound by tag
func RemoveInboundUser(ctx context.Context, c proxy.HandlerServiceClient, tag string, user *User) error {
_, err := c.AlterInbound(ctx, &proxy.AlterInboundRequest{
Tag: tag,
Operation: serial.ToTypedMessage(&proxy.RemoveUserOperation{
Email: user.GetEmail(),
}),
})
// mute not found error
if err != nil && strings.Contains(err.Error(), "not found") {
zap.S().Named("xray").Warnf("User Not Found %s", user.GetEmail())
err = nil
}
if err != nil {
zap.S().Named("xray").Error("Failed to Remove User: %s To Server", user.GetEmail())
return err
}
user.running = false
zap.S().Named("xray").Infof("[xray] Remove User: %v From Server", user.ID)
return nil
}