Merge pull request #54 from jeeinn/master

fix jump server
This commit is contained in:
zhenorzz 2023-05-24 13:43:48 +08:00 committed by GitHub
commit 800ab8663b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 23 deletions

View File

@ -98,7 +98,7 @@ go build -o goploy cmd/server/main.go
2. go mod required 2. go mod required
3. edit goploy.toml `cp goploy.example.toml goploy.toml` 3. edit goploy.toml `cp goploy.example.toml goploy.toml`
4. build [Frontend](#Frontend) 4. build [Frontend](#Frontend)
5. run `go run main.go --asset-dir=./` 5. run `cd cmd/server && go run main.go --asset-dir=../../`
6. use gin (hot reload) 6. use gin (hot reload)
## Frontend ## Frontend

View File

@ -102,7 +102,7 @@ go build -o goploy cmd/server/main.go
2. 项目使用 go mod 管理 2. 项目使用 go mod 管理
3. 修改 goploy.toml `cp goploy.example.toml goploy.toml` 3. 修改 goploy.toml `cp goploy.example.toml goploy.toml`
4. 需要编译一次前端 [前端开发说明](#前端开发说明) 4. 需要编译一次前端 [前端开发说明](#前端开发说明)
5. 运行 `go run main.go --asset-dir=./` 5. 运行 `cd cmd/server && go run main.go --asset-dir=../../`
6. 或者使用 gin可以热更新代码改变就生效 6. 或者使用 gin可以热更新代码改变就生效
## 前端开发说明 ## 前端开发说明

View File

@ -151,12 +151,12 @@ func (ps ProjectServer) ToSSHOption() string {
if ps.ServerJumpIP != "" { if ps.ServerJumpIP != "" {
if ps.ServerJumpPath != "" { if ps.ServerJumpPath != "" {
if ps.ServerJumpPassword != "" { if ps.ServerJumpPassword != "" {
proxyCommand = fmt.Sprintf("-o ProxyCommand='sshpass -p %s -P assphrase ssh -o StrictHostKeyChecking=no -W %%h:%%p -i %s -p %d %s@%s' ", ps.ServerPassword, ps.ServerJumpPath, ps.ServerJumpPort, ps.ServerJumpOwner, ps.ServerJumpIP) proxyCommand = fmt.Sprintf("-o ProxyCommand='sshpass -p %s -P assphrase ssh -o StrictHostKeyChecking=no -W %%h:%%p -i %s -p %d %s@%s' ", ps.ServerJumpPassword, ps.ServerJumpPath, ps.ServerJumpPort, ps.ServerJumpOwner, ps.ServerJumpIP)
} else { } else {
proxyCommand = fmt.Sprintf("-o ProxyCommand='ssh -o StrictHostKeyChecking=no -W %%h:%%p -i %s -p %d %s@%s' ", ps.ServerJumpPath, ps.ServerJumpPort, ps.ServerJumpOwner, ps.ServerJumpIP) proxyCommand = fmt.Sprintf("-o ProxyCommand='ssh -o StrictHostKeyChecking=no -W %%h:%%p -i %s -p %d %s@%s' ", ps.ServerJumpPath, ps.ServerJumpPort, ps.ServerJumpOwner, ps.ServerJumpIP)
} }
} else { } else {
proxyCommand = fmt.Sprintf("-o ProxyCommand='sshpass -p %s ssh -o StrictHostKeyChecking=no -W %%h:%%p -p %d %s@%s' ", ps.ServerPassword, ps.ServerJumpPort, ps.ServerJumpOwner, ps.ServerJumpIP) proxyCommand = fmt.Sprintf("-o ProxyCommand='sshpass -p %s ssh -o StrictHostKeyChecking=no -W %%h:%%p -p %d %s@%s' ", ps.ServerJumpPassword, ps.ServerJumpPort, ps.ServerJumpOwner, ps.ServerJumpIP)
} }
} }
if ps.ServerPath != "" { if ps.ServerPath != "" {

View File

@ -31,6 +31,34 @@ type SSHConfig struct {
} }
func (sshConfig SSHConfig) Dial() (*ssh.Client, error) { func (sshConfig SSHConfig) Dial() (*ssh.Client, error) {
if sshConfig.JumpHost != "" {
// 连接跳板机
clientConfig, err := sshConfig.getConfig(sshConfig.JumpUser, sshConfig.JumpPassword, sshConfig.JumpPath)
if err != nil {
return nil, err
}
sshClient, err := ssh.Dial("tcp", sshConfig.jumpAddr(), clientConfig)
if err != nil {
return nil, err
}
// 连接目标机
conn, err := sshClient.Dial("tcp", sshConfig.addr())
if err != nil {
return nil, err
}
targetConfig, err := sshConfig.getConfig(sshConfig.User, sshConfig.Password, sshConfig.Path)
if err != nil {
return nil, err
}
ncc, chans, reqs, err := ssh.NewClientConn(conn, sshConfig.addr(), targetConfig)
if err != nil {
return nil, err
}
sshClient = ssh.NewClient(ncc, chans, reqs)
return sshClient, err
} else {
clientConfig, err := sshConfig.getConfig(sshConfig.User, sshConfig.Password, sshConfig.Path) clientConfig, err := sshConfig.getConfig(sshConfig.User, sshConfig.Password, sshConfig.Path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -40,27 +68,9 @@ func (sshConfig SSHConfig) Dial() (*ssh.Client, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if sshConfig.JumpHost != "" {
conn, err := sshClient.Dial("tcp", sshConfig.jumpAddr())
if err != nil {
return nil, err
}
targetConfig, err := sshConfig.getConfig(sshConfig.JumpUser, sshConfig.JumpPassword, sshConfig.JumpPath)
if err != nil {
return nil, err
}
ncc, chans, reqs, err := ssh.NewClientConn(conn, sshConfig.jumpAddr(), targetConfig)
if err != nil {
return nil, err
}
sshClient = ssh.NewClient(ncc, chans, reqs)
}
return sshClient, err return sshClient, err
} }
}
// version|cpu cores|mem // version|cpu cores|mem

View File

@ -264,6 +264,8 @@
"importCSV": "导入csv", "importCSV": "导入csv",
"installAgent": "安装agent", "installAgent": "安装agent",
"installAgentTips": "Agent正在安装, 请密切关注日志输出", "installAgentTips": "Agent正在安装, 请密切关注日志输出",
"jumpHost": "跳板机Host",
"jumpPort": "跳板机Port",
"loginType": "登录方式", "loginType": "登录方式",
"sshKeyOwner": "SSH-Key 所有者", "sshKeyOwner": "SSH-Key 所有者",
"sshKeyPath": "SSH-Key 路径", "sshKeyPath": "SSH-Key 路径",
@ -284,7 +286,7 @@
"item": "条目", "item": "条目",
"formula": "公式", "formula": "公式",
"cycle": "周期", "cycle": "周期",
"vaildPeriod": "有效时段", "validPeriod": "有效时段",
"silentCycle": "通道沉默周期", "silentCycle": "通道沉默周期",
"advance": "高级选项", "advance": "高级选项",
"transferFile": "传输文件", "transferFile": "传输文件",

View File

@ -161,7 +161,7 @@
</el-col> </el-col>
</el-row> </el-row>
</el-form-item> </el-form-item>
<el-form-item :label="$t('serverPage.vaildPeriod')"> <el-form-item :label="$t('serverPage.validPeriod')">
<el-row> <el-row>
<el-col :span="12"> <el-col :span="12">
<el-time-select <el-time-select

View File

@ -310,10 +310,10 @@
</el-button> </el-button>
</el-form-item> </el-form-item>
<template v-if="formProps.showAdvance"> <template v-if="formProps.showAdvance">
<el-form-item label="Jump host"> <el-form-item :label="$t('serverPage.jumpHost')">
<el-input v-model="formData.jumpIP" autocomplete="off" /> <el-input v-model="formData.jumpIP" autocomplete="off" />
</el-form-item> </el-form-item>
<el-form-item label="Jump port"> <el-form-item :label="$t('serverPage.jumpPort')">
<el-input v-model.number="formData.jumpPort" autocomplete="off" /> <el-input v-model.number="formData.jumpPort" autocomplete="off" />
</el-form-item> </el-form-item>
<el-form-item :label="$t('serverPage.loginType')"> <el-form-item :label="$t('serverPage.loginType')">