refactor: require features only once

This commit is contained in:
pocketW
2022-07-21 00:40:44 +10:00
parent 9947d7e4f3
commit 61b501de3d
2 changed files with 33 additions and 36 deletions

View File

@@ -5,30 +5,24 @@ import (
"fmt" "fmt"
"github.com/XrayR-project/XrayR/api" "github.com/XrayR-project/XrayR/api"
"github.com/XrayR-project/XrayR/app/mydispatcher"
"github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/core" "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/inbound" "github.com/xtls/xray-core/features/inbound"
"github.com/xtls/xray-core/features/outbound" "github.com/xtls/xray-core/features/outbound"
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/proxy" "github.com/xtls/xray-core/proxy"
) )
func (c *Controller) removeInbound(tag string) error { func (c *Controller) removeInbound(tag string) error {
inboundManager := c.server.GetFeature(inbound.ManagerType()).(inbound.Manager) err := c.ihm.RemoveHandler(context.Background(), tag)
err := inboundManager.RemoveHandler(context.Background(), tag)
return err return err
} }
func (c *Controller) removeOutbound(tag string) error { func (c *Controller) removeOutbound(tag string) error {
outboundManager := c.server.GetFeature(outbound.ManagerType()).(outbound.Manager) err := c.ohm.RemoveHandler(context.Background(), tag)
err := outboundManager.RemoveHandler(context.Background(), tag)
return err return err
} }
func (c *Controller) addInbound(config *core.InboundHandlerConfig) error { func (c *Controller) addInbound(config *core.InboundHandlerConfig) error {
inboundManager := c.server.GetFeature(inbound.ManagerType()).(inbound.Manager)
rawHandler, err := core.CreateObject(c.server, config) rawHandler, err := core.CreateObject(c.server, config)
if err != nil { if err != nil {
return err return err
@@ -37,14 +31,13 @@ func (c *Controller) addInbound(config *core.InboundHandlerConfig) error {
if !ok { if !ok {
return fmt.Errorf("not an InboundHandler: %s", err) return fmt.Errorf("not an InboundHandler: %s", err)
} }
if err := inboundManager.AddHandler(context.Background(), handler); err != nil { if err := c.ihm.AddHandler(context.Background(), handler); err != nil {
return err return err
} }
return nil return nil
} }
func (c *Controller) addOutbound(config *core.OutboundHandlerConfig) error { func (c *Controller) addOutbound(config *core.OutboundHandlerConfig) error {
outboundManager := c.server.GetFeature(outbound.ManagerType()).(outbound.Manager)
rawHandler, err := core.CreateObject(c.server, config) rawHandler, err := core.CreateObject(c.server, config)
if err != nil { if err != nil {
return err return err
@@ -53,26 +46,25 @@ func (c *Controller) addOutbound(config *core.OutboundHandlerConfig) error {
if !ok { if !ok {
return fmt.Errorf("not an InboundHandler: %s", err) return fmt.Errorf("not an InboundHandler: %s", err)
} }
if err := outboundManager.AddHandler(context.Background(), handler); err != nil { if err := c.ohm.AddHandler(context.Background(), handler); err != nil {
return err return err
} }
return nil return nil
} }
func (c *Controller) addUsers(users []*protocol.User, tag string) error { func (c *Controller) addUsers(users []*protocol.User, tag string) error {
inboundManager := c.server.GetFeature(inbound.ManagerType()).(inbound.Manager) handler, err := c.ihm.GetHandler(context.Background(), tag)
handler, err := inboundManager.GetHandler(context.Background(), tag)
if err != nil { if err != nil {
return fmt.Errorf("No such inbound tag: %s", err) return fmt.Errorf("No such inbound tag: %s", err)
} }
inboundInstance, ok := handler.(proxy.GetInbound) inboundInstance, ok := handler.(proxy.GetInbound)
if !ok { if !ok {
return fmt.Errorf("handler %s is not implement proxy.GetInbound", tag) return fmt.Errorf("handler %s has not implemented proxy.GetInbound", tag)
} }
userManager, ok := inboundInstance.GetInbound().(proxy.UserManager) userManager, ok := inboundInstance.GetInbound().(proxy.UserManager)
if !ok { if !ok {
return fmt.Errorf("handler %s is not implement proxy.UserManager", err) return fmt.Errorf("handler %s has not implemented proxy.UserManager", tag)
} }
for _, item := range users { for _, item := range users {
mUser, err := item.ToMemoryUser() mUser, err := item.ToMemoryUser()
@@ -88,8 +80,7 @@ func (c *Controller) addUsers(users []*protocol.User, tag string) error {
} }
func (c *Controller) removeUsers(users []string, tag string) error { func (c *Controller) removeUsers(users []string, tag string) error {
inboundManager := c.server.GetFeature(inbound.ManagerType()).(inbound.Manager) handler, err := c.ihm.GetHandler(context.Background(), tag)
handler, err := inboundManager.GetHandler(context.Background(), tag)
if err != nil { if err != nil {
return fmt.Errorf("No such inbound tag: %s", err) return fmt.Errorf("No such inbound tag: %s", err)
} }
@@ -114,9 +105,8 @@ func (c *Controller) removeUsers(users []string, tag string) error {
func (c *Controller) getTraffic(email string) (up int64, down int64) { func (c *Controller) getTraffic(email string) (up int64, down int64) {
upName := "user>>>" + email + ">>>traffic>>>uplink" upName := "user>>>" + email + ">>>traffic>>>uplink"
downName := "user>>>" + email + ">>>traffic>>>downlink" downName := "user>>>" + email + ">>>traffic>>>downlink"
statsManager := c.server.GetFeature(stats.ManagerType()).(stats.Manager) upCounter := c.stm.GetCounter(upName)
upCounter := statsManager.GetCounter(upName) downCounter := c.stm.GetCounter(downName)
downCounter := statsManager.GetCounter(downName)
if upCounter != nil { if upCounter != nil {
up = upCounter.Value() up = upCounter.Value()
upCounter.Set(0) upCounter.Set(0)
@@ -130,35 +120,29 @@ func (c *Controller) getTraffic(email string) (up int64, down int64) {
} }
func (c *Controller) AddInboundLimiter(tag string, nodeSpeedLimit uint64, userList *[]api.UserInfo) error { func (c *Controller) AddInboundLimiter(tag string, nodeSpeedLimit uint64, userList *[]api.UserInfo) error {
dispather := c.server.GetFeature(routing.DispatcherType()).(*mydispatcher.DefaultDispatcher) err := c.dispatcher.Limiter.AddInboundLimiter(tag, nodeSpeedLimit, userList)
err := dispather.Limiter.AddInboundLimiter(tag, nodeSpeedLimit, userList)
return err return err
} }
func (c *Controller) UpdateInboundLimiter(tag string, updatedUserList *[]api.UserInfo) error { func (c *Controller) UpdateInboundLimiter(tag string, updatedUserList *[]api.UserInfo) error {
dispather := c.server.GetFeature(routing.DispatcherType()).(*mydispatcher.DefaultDispatcher) err := c.dispatcher.Limiter.UpdateInboundLimiter(tag, updatedUserList)
err := dispather.Limiter.UpdateInboundLimiter(tag, updatedUserList)
return err return err
} }
func (c *Controller) DeleteInboundLimiter(tag string) error { func (c *Controller) DeleteInboundLimiter(tag string) error {
dispather := c.server.GetFeature(routing.DispatcherType()).(*mydispatcher.DefaultDispatcher) err := c.dispatcher.Limiter.DeleteInboundLimiter(tag)
err := dispather.Limiter.DeleteInboundLimiter(tag)
return err return err
} }
func (c *Controller) GetOnlineDevice(tag string) (*[]api.OnlineUser, error) { func (c *Controller) GetOnlineDevice(tag string) (*[]api.OnlineUser, error) {
dispather := c.server.GetFeature(routing.DispatcherType()).(*mydispatcher.DefaultDispatcher) return c.dispatcher.Limiter.GetOnlineDevice(tag)
return dispather.Limiter.GetOnlineDevice(tag)
} }
func (c *Controller) UpdateRule(tag string, newRuleList []api.DetectRule) error { func (c *Controller) UpdateRule(tag string, newRuleList []api.DetectRule) error {
dispather := c.server.GetFeature(routing.DispatcherType()).(*mydispatcher.DefaultDispatcher) err := c.dispatcher.RuleManager.UpdateRule(tag, newRuleList)
err := dispather.RuleManager.UpdateRule(tag, newRuleList)
return err return err
} }
func (c *Controller) GetDetectResult(tag string) (*[]api.DetectResult, error) { func (c *Controller) GetDetectResult(tag string) (*[]api.DetectResult, error) {
dispather := c.server.GetFeature(routing.DispatcherType()).(*mydispatcher.DefaultDispatcher) return c.dispatcher.RuleManager.GetDetectResult(tag)
return dispather.RuleManager.GetDetectResult(tag)
} }

View File

@@ -8,11 +8,16 @@ import (
"time" "time"
"github.com/XrayR-project/XrayR/api" "github.com/XrayR-project/XrayR/api"
"github.com/XrayR-project/XrayR/app/mydispatcher"
"github.com/XrayR-project/XrayR/common/legocmd" "github.com/XrayR-project/XrayR/common/legocmd"
"github.com/XrayR-project/XrayR/common/serverstatus" "github.com/XrayR-project/XrayR/common/serverstatus"
"github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/task" "github.com/xtls/xray-core/common/task"
"github.com/xtls/xray-core/core" "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/inbound"
"github.com/xtls/xray-core/features/outbound"
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/features/stats"
) )
type Controller struct { type Controller struct {
@@ -26,15 +31,23 @@ type Controller struct {
nodeInfoMonitorPeriodic *task.Periodic nodeInfoMonitorPeriodic *task.Periodic
userReportPeriodic *task.Periodic userReportPeriodic *task.Periodic
panelType string panelType string
ihm inbound.Manager
ohm outbound.Manager
stm stats.Manager
dispatcher *mydispatcher.DefaultDispatcher
} }
// New return a Controller service with default parameters. // New return a Controller service with default parameters.
func New(server *core.Instance, api api.API, config *Config, panelType string) *Controller { func New(server *core.Instance, api api.API, config *Config, panelType string) *Controller {
controller := &Controller{ controller := &Controller{
server: server, server: server,
config: config, config: config,
apiClient: api, apiClient: api,
panelType: panelType, panelType: panelType,
ihm: server.GetFeature(inbound.ManagerType()).(inbound.Manager),
ohm: server.GetFeature(outbound.ManagerType()).(outbound.Manager),
stm: server.GetFeature(stats.ManagerType()).(stats.Manager),
dispatcher: server.GetFeature(routing.DispatcherType()).(*mydispatcher.DefaultDispatcher),
} }
return controller return controller
} }