Skip to content

Commit

Permalink
修复「接入网关」+「guacd」无法使用监控和文件管理功能的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
dushixiang committed Apr 20, 2022
1 parent a608b84 commit 03b59d6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ echo "clean build history"

echo "build web..."
cd web || exit
yarn build
yarn build || exit
cp -r build ../server/resource/
echo "build web success"

Expand Down
23 changes: 12 additions & 11 deletions server/api/guacamole.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
AccessGatewayUnAvailable int = 803
AccessGatewayCreateError int = 804
AssetNotActive int = 805
NewSshClientError int = 806
)

var UpGrader = websocket.Upgrader{
Expand Down Expand Up @@ -69,10 +70,7 @@ func (api GuacamoleApi) Guacamole(c echo.Context) error {
return err
}
api.setConfig(propertyMap, s, configuration)
var (
ip = s.IP
port = s.Port
)

if s.AccessGatewayId != "" && s.AccessGatewayId != "-" {
g, err := service.GatewayService.GetGatewayAndReconnectById(s.AccessGatewayId)
if err != nil {
Expand All @@ -83,18 +81,18 @@ func (api GuacamoleApi) Guacamole(c echo.Context) error {
utils.Disconnect(ws, AccessGatewayUnAvailable, "接入网关不可用:"+g.Message)
return nil
}
exposedIP, exposedPort, err := g.OpenSshTunnel(s.ID, ip, port)
exposedIP, exposedPort, err := g.OpenSshTunnel(s.ID, s.IP, s.Port)
if err != nil {
utils.Disconnect(ws, AccessGatewayCreateError, "创建SSH隧道失败:"+err.Error())
return nil
}
ip = exposedIP
port = exposedPort
s.IP = exposedIP
s.Port = exposedPort
defer g.CloseSshTunnel(s.ID)
}

configuration.SetParameter("hostname", ip)
configuration.SetParameter("port", strconv.Itoa(port))
configuration.SetParameter("hostname", s.IP)
configuration.SetParameter("port", strconv.Itoa(s.Port))

// 加载资产配置的属性,优先级比全局配置的高,因此最后加载,覆盖掉全局配置
attributes, err := repository.AssetRepository.FindAssetAttrMapByAssetId(ctx, s.AssetId)
Expand Down Expand Up @@ -132,9 +130,12 @@ func (api GuacamoleApi) Guacamole(c echo.Context) error {

if configuration.Protocol == constant.SSH {
nextTerminal, err := CreateNextTerminalBySession(s)
if err == nil {
nextSession.NextTerminal = nextTerminal
if err != nil {
utils.Disconnect(ws, NewSshClientError, "建立SSH客户端失败: "+err.Error())
log.Printf("[%v] 建立 ssh 客户端失败: %v", sessionId, err.Error())
return err
}
nextSession.NextTerminal = nextTerminal
}

nextSession.Observer = session.NewObserver(sessionId)
Expand Down
2 changes: 1 addition & 1 deletion server/constant/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

const (
AppVersion = "v1.2.6-beta"
AppVersion = "v1.2.7"
AppName = "Next Terminal"
AppBanner = `
_______ __ ___________ .__ .__
Expand Down
51 changes: 27 additions & 24 deletions server/global/gateway/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,36 @@ type Tunnel struct {

func (r *Tunnel) Open() {
localAddr := fmt.Sprintf("%s:%d", r.LocalHost, r.LocalPort)
for {
select {
case <-r.ctx.Done():
_ = r.listener.Close()
log.Debugf("SSH 隧道 %v 关闭", localAddr)
return
default:
log.Debugf("等待客户端访问 %v", localAddr)
localConn, err := r.listener.Accept()
if err != nil {
log.Debugf("接受连接失败 %v", err.Error())
continue
}

go func() {
<-r.ctx.Done()
_ = r.listener.Close()
log.Debugf("SSH 隧道 %v 关闭", localAddr)
}()
log.Debugf("等待客户端访问 %v", localAddr)
localConn, err := r.listener.Accept()
if err != nil {
r.err = err
return
}
log.Debugf("客户端 %v 连接至 %v", localConn.RemoteAddr().String(), localAddr)
remoteAddr := fmt.Sprintf("%s:%d", r.RemoteHost, r.RemotePort)
log.Debugf("连接远程主机 %v ...", remoteAddr)
remoteConn, err := r.Gateway.SshClient.Dial("tcp", remoteAddr)
if err != nil {
log.Debugf("连接远程主机 %v 失败", remoteAddr)
r.err = err
return
}

log.Debugf("客户端 %v 连接至 %v", localConn.RemoteAddr().String(), localAddr)
remoteAddr := fmt.Sprintf("%s:%d", r.RemoteHost, r.RemotePort)
log.Debugf("连接远程主机 %v ...", remoteAddr)
remoteConn, err := r.Gateway.SshClient.Dial("tcp", remoteAddr)
if err != nil {
log.Debugf("连接远程主机 %v 失败", remoteAddr)
r.err = err
return
log.Debugf("连接远程主机 %v 成功", remoteAddr)
go copyConn(localConn, remoteConn)
go copyConn(remoteConn, localConn)
log.Debugf("转发数据 [%v]->[%v]", localAddr, remoteAddr)
}
}

log.Debugf("连接远程主机 %v 成功", remoteAddr)
go copyConn(localConn, remoteConn)
go copyConn(remoteConn, localConn)
log.Debugf("转发数据 [%v]->[%v]", localAddr, remoteAddr)
}

func (r Tunnel) Close() {
Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-terminal",
"version": "1.2.6-beta",
"version": "1.2.7",
"private": true,
"dependencies": {
"@ant-design/charts": "^1.2.13",
Expand Down

0 comments on commit 03b59d6

Please sign in to comment.