dev (#239)
* 优化图标和LOGO * 修改登录页面动画的速度为3 * 增加对websocket的异常处理 * 修复了用户组和用户名唯一判断错误的问题 * 提示版本号 * 修复readme错别字 * 修复单词拼写错误的问题 * 修复代码格式 * 修改Windows资产属性名称 * Docker 打包流程增加 upx 压缩 * 升级依赖文件,修改sqlite驱动为 github.com/glebarez/sqlite * 修复第一次查询「授权令牌」的错误 * 移除无关代码 * 修改docker打包脚本 * 增加打包脚本 * 增加微信群 * 修复单词拼写错误的问题 * 修复代码格式 * 修改Windows资产属性名称 * Docker 打包流程增加 upx 压缩 * 修改docker打包脚本 * - 替换 sqlite 驱动为 github.com/glebarez/sqlite - 修复数据库锁定的问题 - 修复部分代码不完善的问题 - 修复策略显示不完整的问题 - 修复编辑文件换行符的问题 - 优化guacd连接
This commit is contained in:
@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"next-terminal/server/guacd"
|
||||
"next-terminal/server/log"
|
||||
@ -12,73 +11,47 @@ import (
|
||||
)
|
||||
|
||||
type GuacamoleHandler struct {
|
||||
ws *websocket.Conn
|
||||
tunnel *guacd.Tunnel
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
dataChan chan []byte
|
||||
tick *time.Ticker
|
||||
ws *websocket.Conn
|
||||
tunnel *guacd.Tunnel
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func NewGuacamoleHandler(ws *websocket.Conn, tunnel *guacd.Tunnel) *GuacamoleHandler {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
tick := time.NewTicker(time.Millisecond * time.Duration(60))
|
||||
return &GuacamoleHandler{
|
||||
ws: ws,
|
||||
tunnel: tunnel,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
dataChan: make(chan []byte),
|
||||
tick: tick,
|
||||
ws: ws,
|
||||
tunnel: tunnel,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
func (r GuacamoleHandler) Start() {
|
||||
go r.readFormTunnel()
|
||||
go r.writeToWebsocket()
|
||||
}
|
||||
|
||||
func (r GuacamoleHandler) Stop() {
|
||||
r.tick.Stop()
|
||||
r.cancel()
|
||||
}
|
||||
|
||||
func (r GuacamoleHandler) readFormTunnel() {
|
||||
for {
|
||||
select {
|
||||
case <-r.ctx.Done():
|
||||
return
|
||||
default:
|
||||
instruction, err := r.tunnel.Read()
|
||||
if err != nil {
|
||||
utils.Disconnect(r.ws, TunnelClosed, "远程连接已关闭")
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-r.ctx.Done():
|
||||
return
|
||||
}
|
||||
if len(instruction) == 0 {
|
||||
continue
|
||||
}
|
||||
r.dataChan <- instruction
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r GuacamoleHandler) writeToWebsocket() {
|
||||
var buf []byte
|
||||
for {
|
||||
select {
|
||||
case <-r.ctx.Done():
|
||||
return
|
||||
case <-r.tick.C:
|
||||
if len(buf) > 0 {
|
||||
err := r.ws.WriteMessage(websocket.TextMessage, buf)
|
||||
default:
|
||||
instruction, err := r.tunnel.Read()
|
||||
if err != nil {
|
||||
utils.Disconnect(r.ws, TunnelClosed, "远程连接已关闭")
|
||||
return
|
||||
}
|
||||
if len(instruction) == 0 {
|
||||
continue
|
||||
}
|
||||
err = r.ws.WriteMessage(websocket.TextMessage, instruction)
|
||||
if err != nil {
|
||||
log.Debugf("WebSocket写入失败,即将关闭Guacd连接...")
|
||||
return
|
||||
}
|
||||
buf = []byte{}
|
||||
}
|
||||
case data := <-r.dataChan:
|
||||
buf = append(buf, data...)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (r GuacamoleHandler) Stop() {
|
||||
r.cancel()
|
||||
}
|
||||
|
Reference in New Issue
Block a user