mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 12:04:44 +03:00
e409ed7244
An operator running a master with sub-nodes could probe or update a node from the Nodes page but had to log in to the node, or SSH into it, to restart Xray or the panel (#6937). Add Restart Xray and Restart panel to the node actions, behind a confirmation. Xray restarts through the node runtime; the panel restart is a new Remote call that the node accepts with node-admin credentials, the authority that already covers updatePanel. Both are refused for a disabled node.
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func (s *NodeService) restartTarget(id int) (*model.Node, *runtime.Manager, error) {
|
|
if id <= 0 {
|
|
return nil, nil, fmt.Errorf("invalid node ID")
|
|
}
|
|
node, err := s.GetById(id)
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil, fmt.Errorf("node not found")
|
|
}
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if !node.Enable {
|
|
return nil, nil, fmt.Errorf("node is disabled")
|
|
}
|
|
mgr := runtime.GetManager()
|
|
if mgr == nil {
|
|
return nil, nil, fmt.Errorf("runtime manager unavailable")
|
|
}
|
|
return node, mgr, nil
|
|
}
|
|
|
|
func (s *NodeService) RestartXray(ctx context.Context, id int) error {
|
|
node, mgr, err := s.restartTarget(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rt, err := mgr.RuntimeFor(&node.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
return rt.RestartXray(ctx)
|
|
}
|
|
|
|
func (s *NodeService) RestartPanel(ctx context.Context, id int) error {
|
|
node, mgr, err := s.restartTarget(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
remote, err := mgr.RemoteFor(node)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
return remote.RestartPanel(ctx)
|
|
}
|