🚸 v0.9.29 WebSSH ping-pong 保活

This commit is contained in:
naiba 2021-08-20 10:45:10 +08:00
parent 9878699b3e
commit fb7e54a3af
4 changed files with 59 additions and 26 deletions

View File

@ -4,7 +4,7 @@
<br> <br>
<small><i>LOGO designed by <a href="https://xio.ng" target="_blank">熊大</a> .</i></small> <small><i>LOGO designed by <a href="https://xio.ng" target="_blank">熊大</a> .</i></small>
<br><br> <br><br>
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.9.28&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.7.0-brightgreen?style=for-the-badge&logo=linux"> <img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.9.29&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.7.0-brightgreen?style=for-the-badge&logo=linux">
<br> <br>
<br> <br>
<p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,命令批量执行和计划任务。</p> <p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,命令批量执行和计划任务。</p>

View File

@ -294,11 +294,22 @@ func (cp *commonPage) terminal(c *gin.Context) {
deadlineCh := make(chan interface{}) deadlineCh := make(chan interface{})
go func() { go func() {
// 对方连接超时
connectDeadline := time.NewTimer(time.Second * 15) connectDeadline := time.NewTimer(time.Second * 15)
<-connectDeadline.C <-connectDeadline.C
deadlineCh <- struct{}{} deadlineCh <- struct{}{}
}() }()
go func() {
// PING 保活
for {
if err = conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
return
}
time.Sleep(time.Second * 10)
}
}()
dataCh := make(chan []byte) dataCh := make(chan []byte)
errorCh := make(chan error) errorCh := make(chan error)
go func() { go func() {
@ -318,10 +329,20 @@ func (cp *commonPage) terminal(c *gin.Context) {
var dataBuffer [][]byte var dataBuffer [][]byte
var distConn *websocket.Conn var distConn *websocket.Conn
checkDistConn := func() {
if distConn == nil {
if isAgent {
distConn = terminal.userConn
} else {
distConn = terminal.agentConn
}
}
}
for { for {
select { select {
case <-deadlineCh: case <-deadlineCh:
checkDistConn()
if distConn == nil { if distConn == nil {
return return
} }
@ -329,14 +350,7 @@ func (cp *commonPage) terminal(c *gin.Context) {
return return
case data := <-dataCh: case data := <-dataCh:
dataBuffer = append(dataBuffer, data) dataBuffer = append(dataBuffer, data)
if distConn == nil { checkDistConn()
// 传递给对方
if isAgent {
distConn = terminal.userConn
} else {
distConn = terminal.agentConn
}
}
if distConn != nil { if distConn != nil {
for i := 0; i < len(dataBuffer); i++ { for i := 0; i < len(dataBuffer); i++ {
err = distConn.WriteMessage(websocket.BinaryMessage, dataBuffer[i]) err = distConn.WriteMessage(websocket.BinaryMessage, dataBuffer[i])

View File

@ -32,6 +32,41 @@
<script src="https://cdn.jsdelivr.net/npm/xterm-addon-attach@0.6.0/lib/xterm-addon-attach.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/xterm-addon-attach@0.6.0/lib/xterm-addon-attach.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm-addon-fit@0.5.0/lib/xterm-addon-fit.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/xterm-addon-fit@0.5.0/lib/xterm-addon-fit.min.js"></script>
<script> <script>
let sendResizing = false;
function doResize() {
fitAddon.fit()
const w = fitAddon.proposeDimensions();
const prefix = new Int8Array([1]);
const resizeMessage = new TextEncoder().encode(JSON.stringify({
Rows: w.rows,
Cols: w.cols,
}));
var msg = new Int8Array(prefix.length + resizeMessage.length);
msg.set(prefix);
msg.set(resizeMessage, prefix.length);
socket.send(msg)
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function onResize() {
if (sendResizing) return;
sendResizing = true;
try {
await sleep(1500);
doResize();
} catch (error) {
console.log('resize', error);
} finally {
sendResizing = false
}
}
const term = new Terminal({ const term = new Terminal({
screenKeys: true, screenKeys: true,
useStyle: true, useStyle: true,
@ -56,22 +91,6 @@
socket.onerror = () => { socket.onerror = () => {
alert('Terminal 连接失败,请检查 /terminal/* 的 WebSocket 反代情况') alert('Terminal 连接失败,请检查 /terminal/* 的 WebSocket 反代情况')
} }
function onResize() {
fitAddon.fit()
const w = fitAddon.proposeDimensions();
const prefix = new Int8Array([1]);
const resizeMessage = new TextEncoder().encode(JSON.stringify({
Rows: w.rows,
Cols: w.cols,
}));
var msg = new Int8Array(prefix.length + resizeMessage.length);
msg.set(prefix);
msg.set(resizeMessage, prefix.length);
socket.send(msg)
}
</script> </script>
</body> </body>

View File

@ -13,7 +13,7 @@ import (
pb "github.com/naiba/nezha/proto" pb "github.com/naiba/nezha/proto"
) )
var Version = "v0.9.28" // !!记得修改 README 中的 badge 版本!! var Version = "v0.9.29" // !!记得修改 README 中的 badge 版本!!
var ( var (
Conf *model.Config Conf *model.Config