Browse Source

minor: agent代理

tuonian 19 hours ago
parent
commit
be090cd022
4 changed files with 53 additions and 2 deletions
  1. 1 2
      agent/handler.go
  2. 30 0
      agent/instance.go
  3. 16 0
      server/nginx/instance.go
  4. 6 0
      server/nginx/remote.go

+ 1 - 2
agent/handler.go

@@ -8,7 +8,6 @@ import (
 	"net/http"
 	"nginx-ui/server/constants"
 	"nginx-ui/server/models"
-	nginx2 "nginx-ui/server/nginx"
 	"nginx-ui/server/utils"
 	"os"
 	"strings"
@@ -82,7 +81,7 @@ func OnCMD(c *Agent, data *models.AgentData) (interface{}, error) {
 		return nil, err
 	}
 
-	instance := nginx2.NewLocalNginx(nginx)
+	instance := GetInstance(nginx)
 	res, err := instance.Run(request.Cmd)
 	return res, err
 }

+ 30 - 0
agent/instance.go

@@ -0,0 +1,30 @@
+package agent
+
+import (
+	"nginx-ui/server/models"
+	"nginx-ui/server/nginx"
+)
+
+var proxyNginx *nginx.Instance
+
+func GetInstance(ngx *models.Nginx) *nginx.Instance {
+	var instance = proxyNginx
+	if instance != nil {
+		old := instance.GetNginx()
+		if old.IpAddr != ngx.IpAddr || old.Port != ngx.Port || old.User != ngx.User || old.Password != ngx.Password {
+			instance.Close(false)
+			instance = nil
+		} else {
+			instance.SetNginx(ngx)
+			return instance
+		}
+	}
+	if ngx.IsLocal {
+		instance = nginx.NewInstance(nginx.NewLocalNginx(ngx), ngx)
+	} else {
+		instance = nginx.NewInstance(nginx.NewRemoteInstance(ngx), ngx)
+	}
+	instance.SetNginx(ngx)
+	proxyNginx = instance
+	return instance
+}

+ 16 - 0
server/nginx/instance.go

@@ -32,6 +32,22 @@ type Instance struct {
 	nginx *models.Nginx
 }
 
+func NewInstance(inter InstanceInter, nginx *models.Nginx) *Instance {
+	return &Instance{
+		InstanceInter: inter,
+		nginx:         nginx,
+	}
+}
+
+func (n *Instance) GetNginx() *models.Nginx {
+	return n.nginx
+}
+
+func (n *Instance) SetNginx(nginx *models.Nginx) {
+	n.nginx = nginx
+	n.InstanceInter.SetNginx(nginx)
+}
+
 func (n *Instance) CheckDirs() Dirs {
 	nginx := n.nginx
 	dataDir := nginx.DataDir

+ 6 - 0
server/nginx/remote.go

@@ -20,6 +20,12 @@ type RemoteInstance struct {
 	LastResult string
 }
 
+func NewRemoteInstance(ngx *models.Nginx) *RemoteInstance {
+	return &RemoteInstance{
+		nginx: ngx,
+	}
+}
+
 func (n *RemoteInstance) Connect() error {
 	if n.client != nil {
 		return nil