refactor: Extract services form router
This commit is contained in:
@@ -38,11 +38,7 @@ func (s *CommandServer) handleSetClashMode(conn net.Conn) error {
|
||||
if service == nil {
|
||||
return writeError(conn, E.New("service not ready"))
|
||||
}
|
||||
clashServer := service.instance.Router().ClashServer()
|
||||
if clashServer == nil {
|
||||
return writeError(conn, E.New("Clash API disabled"))
|
||||
}
|
||||
clashServer.(*clashapi.Server).SetMode(newMode)
|
||||
service.clashServer.(*clashapi.Server).SetMode(newMode)
|
||||
return writeError(conn, nil)
|
||||
}
|
||||
|
||||
@@ -69,18 +65,14 @@ func (s *CommandServer) handleModeConn(conn net.Conn) error {
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
clashServer := s.service.instance.Router().ClashServer()
|
||||
if clashServer == nil {
|
||||
return binary.Write(conn, binary.BigEndian, uint16(0))
|
||||
}
|
||||
err := writeClashModeList(conn, clashServer)
|
||||
err := writeClashModeList(conn, s.service.clashServer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-s.modeUpdate:
|
||||
err = varbin.Write(conn, binary.BigEndian, clashServer.Mode())
|
||||
err = varbin.Write(conn, binary.BigEndian, s.service.clashServer.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -45,11 +45,7 @@ func (s *CommandServer) handleCloseConnection(conn net.Conn) error {
|
||||
if service == nil {
|
||||
return writeError(conn, E.New("service not ready"))
|
||||
}
|
||||
clashServer := service.instance.Router().ClashServer()
|
||||
if clashServer == nil {
|
||||
return writeError(conn, E.New("Clash API disabled"))
|
||||
}
|
||||
targetConn := clashServer.(*clashapi.Server).TrafficManager().Connection(uuid.FromStringOrNil(connId))
|
||||
targetConn := service.clashServer.(*clashapi.Server).TrafficManager().Connection(uuid.FromStringOrNil(connId))
|
||||
if targetConn == nil {
|
||||
return writeError(conn, E.New("connection already closed"))
|
||||
}
|
||||
|
||||
@@ -49,11 +49,7 @@ func (s *CommandServer) handleConnectionsConn(conn net.Conn) error {
|
||||
for {
|
||||
service := s.service
|
||||
if service != nil {
|
||||
clashServer := service.instance.Router().ClashServer()
|
||||
if clashServer == nil {
|
||||
return E.New("Clash API disabled")
|
||||
}
|
||||
trafficManager = clashServer.(*clashapi.Server).TrafficManager()
|
||||
trafficManager = service.clashServer.(*clashapi.Server).TrafficManager()
|
||||
break
|
||||
}
|
||||
select {
|
||||
|
||||
@@ -60,7 +60,7 @@ func NewCommandServer(handler CommandServerHandler, maxLines int32) *CommandServ
|
||||
func (s *CommandServer) SetService(newService *BoxService) {
|
||||
if newService != nil {
|
||||
service.PtrFromContext[urltest.HistoryStorage](newService.ctx).SetHook(s.urlTestUpdate)
|
||||
newService.instance.Router().ClashServer().(*clashapi.Server).SetModeUpdateHook(s.modeUpdate)
|
||||
newService.clashServer.(*clashapi.Server).SetModeUpdateHook(s.modeUpdate)
|
||||
}
|
||||
s.service = newService
|
||||
s.notifyURLTestUpdate()
|
||||
|
||||
@@ -31,12 +31,10 @@ func (s *CommandServer) readStatus() StatusMessage {
|
||||
message.ConnectionsOut = int32(conntrack.Count())
|
||||
|
||||
if s.service != nil {
|
||||
if clashServer := s.service.instance.Router().ClashServer(); clashServer != nil {
|
||||
message.TrafficAvailable = true
|
||||
trafficManager := clashServer.(*clashapi.Server).TrafficManager()
|
||||
message.UplinkTotal, message.DownlinkTotal = trafficManager.Total()
|
||||
message.ConnectionsIn = int32(trafficManager.ConnectionsLen())
|
||||
}
|
||||
message.TrafficAvailable = true
|
||||
trafficManager := s.service.clashServer.(*clashapi.Server).TrafficManager()
|
||||
message.UplinkTotal, message.DownlinkTotal = trafficManager.Total()
|
||||
message.ConnectionsIn = int32(trafficManager.ConnectionsLen())
|
||||
}
|
||||
|
||||
return message
|
||||
|
||||
@@ -34,17 +34,18 @@ import (
|
||||
type BoxService struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
instance *box.Box
|
||||
pauseManager pause.Manager
|
||||
urlTestHistoryStorage *urltest.HistoryStorage
|
||||
instance *box.Box
|
||||
clashServer adapter.ClashServer
|
||||
pauseManager pause.Manager
|
||||
|
||||
servicePauseFields
|
||||
}
|
||||
|
||||
func NewService(configContent string, platformInterface PlatformInterface) (*BoxService, error) {
|
||||
ctx := box.Context(context.Background(), include.InboundRegistry(), include.OutboundRegistry())
|
||||
ctx = service.ContextWith[deprecated.Manager](ctx, new(deprecatedManager))
|
||||
ctx = filemanager.WithDefault(ctx, sWorkingPath, sTempPath, sUserID, sGroupID)
|
||||
service.MustRegister[deprecated.Manager](ctx, new(deprecatedManager))
|
||||
options, err := parseConfig(ctx, configContent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -54,7 +55,7 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
|
||||
urlTestHistoryStorage := urltest.NewHistoryStorage()
|
||||
ctx = service.ContextWithPtr(ctx, urlTestHistoryStorage)
|
||||
platformWrapper := &platformInterfaceWrapper{iif: platformInterface, useProcFS: platformInterface.UseProcFS()}
|
||||
ctx = service.ContextWith[platform.Interface](ctx, platformWrapper)
|
||||
service.MustRegister[platform.Interface](ctx, platformWrapper)
|
||||
instance, err := box.New(box.Options{
|
||||
Context: ctx,
|
||||
Options: options,
|
||||
@@ -71,6 +72,7 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
|
||||
instance: instance,
|
||||
urlTestHistoryStorage: urlTestHistoryStorage,
|
||||
pauseManager: service.FromContext[pause.Manager](ctx),
|
||||
clashServer: service.FromContext[adapter.ClashServer](ctx),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user