12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package nginx
- import (
- "errors"
- "github.com/astaxie/beego/logs"
- "log"
- "nginx-ui/server/models"
- "nginx-ui/server/modules/agent_server"
- "path/filepath"
- "time"
- )
- // AgentInstance 远程,agent代理
- type AgentInstance struct {
- nginx *models.Nginx
- client *agent_server.WsClient
- LastResult string
- }
- func (n *AgentInstance) SetNginx(nginx *models.Nginx) {
- log.Println("SetNginx:", nginx)
- n.nginx = nginx
- c, ok := agent_server.AgentHub.FindClient(n.nginx.Token)
- if !ok {
- return
- }
- n.client = c
- data := &models.AgentData{
- Data: nginx,
- Type: models.NginxUpdateType,
- }
- res, err := c.Send(data, 10*time.Second)
- if err != nil {
- logs.Error("send agent data err: %v\n", err)
- }
- logs.Info("SetNginx: %v\n", res)
- }
- // Connect 连接到nginx实例客户端,检查客户端是否可用
- func (n *AgentInstance) Connect() error {
- return nil
- }
- // Run RemoteInstance 这里应该要处理session断开的流程吧
- func (n *AgentInstance) Run(cmd string) (string, error) {
- logs.Info("Run: ", cmd)
- if n.client == nil {
- return "", errors.New("agent not online")
- }
- data := &models.AgentData{
- Data: &models.AgentCMD{
- Cmd: cmd,
- },
- Type: models.AgentCmdType,
- }
- res, err := n.client.Send(data, 10*time.Second)
- if err != nil {
- return "", err
- }
- if !res.Success {
- return "", errors.New(res.Msg)
- }
- n.LastResult = res.Msg
- logger.Printf("out: %v", n.LastResult)
- return n.LastResult, err
- }
- func (n *AgentInstance) Close(onlySession bool) {
- log.Printf("agent clone,skip")
- }
- // SendFile RemoteInstance 这里应该要处理session断开的流程吧
- func (n *AgentInstance) SendFile(src string, remote string) error {
- logs.Info("SendFile: ", src, remote)
- if n.client == nil {
- return errors.New("agent not online")
- }
- data := &models.AgentData{
- Data: &models.AgentSendFile{
- FileName: filepath.Base(src),
- Dst: remote,
- },
- Type: models.SendFileType,
- }
- res, err := n.client.Send(data, 120*time.Second)
- if err != nil {
- return err
- }
- if !res.Success {
- return errors.New(res.Msg)
- }
- logger.Printf("out: %v", n.LastResult)
- return nil
- }
|