Skip to content

Commit

Permalink
feature:增加ssh连接功能
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelmaxQm committed Mar 31, 2024
1 parent c226a94 commit 1f74774
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 0 deletions.
17 changes: 17 additions & 0 deletions server/api/v1/cloud/enter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cloud

import "github.com/flipped-aurora/gin-vue-admin/server/service"

type ApiGroup struct {
ServerApi
}

// 一键环境检测

// 一键安装环境

// 一键发布

var (
cloudServiceGroup = service.ServiceGroupApp.CloudServiceGroup
)
50 changes: 50 additions & 0 deletions server/api/v1/cloud/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cloud

import (
"context"
"github.com/flipped-aurora/gin-vue-admin/server/model/cloud/request"
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
"github.com/gin-gonic/gin"
)

type ServerApi struct{}

// 环境检测
func (s *ServerApi) Check(c *gin.Context) {
var checkRequest request.SSHRequest
msg := make(chan string, 1)
err := c.ShouldBindJSON(&checkRequest)
if err != nil {
response.FailWithMessage(err.Error(), c)
}

ctx := context.Background()

connect, err := checkRequest.Connection(ctx)

go cloudServiceGroup.Cmd("docker-compose version", msg, *connect)

var remsg string

for {
select {
case msg := <-msg:
remsg += msg
case <-connect.CTX.Done():
response.OkWithMessage(remsg, c)
return
default:
break
}
}
}

// 安装环境
func (s *ServerApi) Install() {

}

// 发布
func (s *ServerApi) Deploy() {

}
2 changes: 2 additions & 0 deletions server/api/v1/enter.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package v1

import (
"github.com/flipped-aurora/gin-vue-admin/server/api/v1/cloud"
"github.com/flipped-aurora/gin-vue-admin/server/api/v1/example"
"github.com/flipped-aurora/gin-vue-admin/server/api/v1/system"
)

type ApiGroup struct {
SystemApiGroup system.ApiGroup
ExampleApiGroup example.ApiGroup
CloudApiGroup cloud.ApiGroup
}

var ApiGroupApp = new(ApiGroup)
2 changes: 2 additions & 0 deletions server/initialize/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Routers() *gin.Engine {
InstallPlugin(Router) // 安装插件
systemRouter := router.RouterGroupApp.System
exampleRouter := router.RouterGroupApp.Example
cloudRouter := router.RouterGroupApp.Cloud
// 如果想要不使用nginx代理前端网页,可以修改 web/.env.production 下的
// VUE_APP_BASE_API = /
// VUE_APP_BASE_PATH = http://localhost
Expand Down Expand Up @@ -90,6 +91,7 @@ func Routers() *gin.Engine {
systemRouter.InitSysExportTemplateRouter(PrivateGroup) // 导出模板
exampleRouter.InitCustomerRouter(PrivateGroup) // 客户路由
exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup) // 文件上传下载功能路由
cloudRouter.InitServerRouter(PublicGroup) // 服务器相关路由

}

Expand Down
36 changes: 36 additions & 0 deletions server/model/cloud/request/ssh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package request

import (
"context"
"github.com/flipped-aurora/gin-vue-admin/server/model/cloud"
"golang.org/x/crypto/ssh"
"time"
)

type SSHRequest struct {
Host string `json:"host"`
Port string `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
}

func (s *SSHRequest) Connection(ctx context.Context) (*cloud.Context, error) {
var cloudCtx cloud.Context
ctx, cancel := context.WithCancel(ctx)
config := &ssh.ClientConfig{
User: s.Username,
Auth: []ssh.AuthMethod{
ssh.Password(s.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 30 * time.Second,
}
cloudCtx.CTX = ctx
cloudCtx.Cancel = cancel
client, err := ssh.Dial("tcp", s.Host+":"+s.Port, config)
if err != nil {
return nil, err
}
cloudCtx.Client = client
return &cloudCtx, nil
}
16 changes: 16 additions & 0 deletions server/model/cloud/ssh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cloud

import (
"context"
"golang.org/x/crypto/ssh"
)

type SSH interface {
Connection(ctx context.Context) (*Context, error)
}

type Context struct {
Client *ssh.Client
CTX context.Context
Cancel context.CancelFunc
}
5 changes: 5 additions & 0 deletions server/router/cloud/enter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cloud

type RouterGroup struct {
ServerRouter
}
17 changes: 17 additions & 0 deletions server/router/cloud/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cloud

import (
v1 "github.com/flipped-aurora/gin-vue-admin/server/api/v1"
"github.com/flipped-aurora/gin-vue-admin/server/middleware"
"github.com/gin-gonic/gin"
)

type ServerRouter struct{}

func (e *ServerRouter) InitServerRouter(Router *gin.RouterGroup) {
customerRouter := Router.Group("server").Use(middleware.OperationRecord())
serverApi := v1.ApiGroupApp.CloudApiGroup.ServerApi
{
customerRouter.GET("check", serverApi.Check) // 创建客户
}
}
2 changes: 2 additions & 0 deletions server/router/enter.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package router

import (
"github.com/flipped-aurora/gin-vue-admin/server/router/cloud"
"github.com/flipped-aurora/gin-vue-admin/server/router/example"
"github.com/flipped-aurora/gin-vue-admin/server/router/system"
)

type RouterGroup struct {
System system.RouterGroup
Example example.RouterGroup
Cloud cloud.RouterGroup
}

var RouterGroupApp = new(RouterGroup)
5 changes: 5 additions & 0 deletions server/service/cloud/enter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cloud

type ServiceGroup struct {
ServerService
}
40 changes: 40 additions & 0 deletions server/service/cloud/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cloud

import (
"bufio"
"github.com/flipped-aurora/gin-vue-admin/server/model/cloud"
)

type ServerService struct{}

// 环境检测
func (s *ServerService) Cmd(cmd string, msg chan string, connect cloud.Context) error {
client := connect.Client
defer client.Close()

session, err := client.NewSession()
if err != nil {
return err
}
defer session.Close()
stdout, err := session.StdoutPipe()
if err != nil {
return err
}
reader := bufio.NewReader(stdout)

err = session.Start(cmd)

if err != nil {
return err
}
for {
line, err := reader.ReadString('\n')
if err != nil {
connect.Cancel()
break
}
msg <- line
}
return nil
}
2 changes: 2 additions & 0 deletions server/service/enter.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package service

import (
"github.com/flipped-aurora/gin-vue-admin/server/service/cloud"
"github.com/flipped-aurora/gin-vue-admin/server/service/example"
"github.com/flipped-aurora/gin-vue-admin/server/service/system"
)

type ServiceGroup struct {
SystemServiceGroup system.ServiceGroup
ExampleServiceGroup example.ServiceGroup
CloudServiceGroup cloud.ServiceGroup
}

var ServiceGroupApp = new(ServiceGroup)

0 comments on commit 1f74774

Please sign in to comment.