Compare commits

...
2 Commits
Author SHA1 Message Date
naiba d667ce8afe feat: WebSSH 上传下载文件 (by trzsz) 2024-08-13 00:01:39 +08:00
naiba 6abc0fce51 fix: 流量统计异常 2024-08-12 10:06:55 +08:00
6 changed files with 22 additions and 5 deletions
+2
View File
@@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -384,6 +385,7 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
} else { } else {
s.Host = &model.Host{} s.Host = &model.Host{}
s.State = &model.HostState{} s.State = &model.HostState{}
s.TaskCloseLock = new(sync.Mutex)
singleton.ServerLock.Lock() singleton.ServerLock.Lock()
singleton.SecretToID[s.Secret] = s.ID singleton.SecretToID[s.Secret] = s.ID
singleton.ServerList[s.ID] = &s singleton.ServerList[s.ID] = &s
+3
View File
@@ -3,6 +3,7 @@ package model
import ( import (
"fmt" "fmt"
"html/template" "html/template"
"sync"
"time" "time"
"github.com/naiba/nezha/pkg/utils" "github.com/naiba/nezha/pkg/utils"
@@ -28,6 +29,7 @@ type Server struct {
LastActive time.Time `gorm:"-"` LastActive time.Time `gorm:"-"`
TaskClose chan error `gorm:"-" json:"-"` TaskClose chan error `gorm:"-" json:"-"`
TaskCloseLock *sync.Mutex `gorm:"-" json:"-"`
TaskStream pb.NezhaService_RequestTaskServer `gorm:"-" json:"-"` TaskStream pb.NezhaService_RequestTaskServer `gorm:"-" json:"-"`
PrevTransferInSnapshot int64 `gorm:"-" json:"-"` // 上次数据点时的入站使用量 PrevTransferInSnapshot int64 `gorm:"-" json:"-"` // 上次数据点时的入站使用量
@@ -39,6 +41,7 @@ func (s *Server) CopyFromRunningServer(old *Server) {
s.State = old.State s.State = old.State
s.LastActive = old.LastActive s.LastActive = old.LastActive
s.TaskClose = old.TaskClose s.TaskClose = old.TaskClose
s.TaskCloseLock = old.TaskCloseLock
s.TaskStream = old.TaskStream s.TaskStream = old.TaskStream
s.PrevTransferInSnapshot = old.PrevTransferInSnapshot s.PrevTransferInSnapshot = old.PrevTransferInSnapshot
s.PrevTransferOutSnapshot = old.PrevTransferOutSnapshot s.PrevTransferOutSnapshot = old.PrevTransferOutSnapshot
+3
View File
@@ -81,5 +81,8 @@ func Uint64SubInt64(a uint64, b int64) uint64 {
if b < 0 { if b < 0 {
return a + uint64(-b) return a + uint64(-b)
} }
if a < uint64(b) {
return 0
}
return a - uint64(b) return a - uint64(b)
} }
+9 -3
View File
@@ -1,6 +1,7 @@
{{define "dashboard-default/terminal"}} {{define "dashboard-default/terminal"}}
<!DOCTYPE html> <!DOCTYPE html>
<html lang="{{.Conf.Language}}"> <html lang="{{.Conf.Language}}">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
@@ -23,16 +24,19 @@
background-color: black; background-color: black;
} }
</style> </style>
<body onresize="onResize()"> <body onresize="onResize()">
<div id="terminal-container"></div> <div id="terminal-container"></div>
<script src="https://unpkg.com/[email protected]/lib/xterm.js"></script> <script src="https://unpkg.com/[email protected]/lib/xterm.js"></script>
<script src="https://unpkg.com/@xterm/[email protected]/lib/addon-attach.js"></script>
<script src="https://unpkg.com/@xterm/[email protected]/lib/addon-fit.js"></script> <script src="https://unpkg.com/@xterm/[email protected]/lib/addon-fit.js"></script>
<script src="https://unpkg.com/@xterm/[email protected]/lib/addon-web-links.js"></script>
<script src="https://unpkg.com/[email protected]/lib/trzsz.js"></script>
<script> <script>
let sendResizing = false; let sendResizing = false;
function doResize() { function doResize() {
fitAddon.fit() fitAddon.fit()
const w = fitAddon.proposeDimensions(); const w = fitAddon.proposeDimensions();
const prefix = new Int8Array([1]); const prefix = new Int8Array([1]);
const resizeMessage = new TextEncoder().encode(JSON.stringify({ const resizeMessage = new TextEncoder().encode(JSON.stringify({
@@ -70,10 +74,11 @@
cursorBlink: true, cursorBlink: true,
}); });
const socket = new WebSocket((window.location.protocol == 'https:' ? 'wss' : 'ws') + '://' + window.location.host + '/terminal/' + '{{.SessionID}}'); const socket = new WebSocket((window.location.protocol == 'https:' ? 'wss' : 'ws') + '://' + window.location.host + '/terminal/' + '{{.SessionID}}');
const attachAddon = new AttachAddon.AttachAddon(socket); const trzszAddon = new TrzszAddon(socket);
term.loadAddon(trzszAddon);
const fitAddon = new FitAddon.FitAddon(); const fitAddon = new FitAddon.FitAddon();
term.loadAddon(attachAddon);
term.loadAddon(fitAddon); term.loadAddon(fitAddon);
term.open(document.getElementById('terminal-container')); term.open(document.getElementById('terminal-container'));
socket.onopen = () => { socket.onopen = () => {
@@ -90,5 +95,6 @@
} }
</script> </script>
</body> </body>
</html> </html>
{{end}} {{end}}
+2
View File
@@ -90,12 +90,14 @@ func (s *NezhaHandler) RequestTask(h *pb.Host, stream pb.NezhaService_RequestTas
} }
closeCh := make(chan error) closeCh := make(chan error)
singleton.ServerLock.RLock() singleton.ServerLock.RLock()
singleton.ServerList[clientID].TaskCloseLock.Lock()
// 修复不断的请求 task 但是没有 return 导致内存泄漏 // 修复不断的请求 task 但是没有 return 导致内存泄漏
if singleton.ServerList[clientID].TaskClose != nil { if singleton.ServerList[clientID].TaskClose != nil {
close(singleton.ServerList[clientID].TaskClose) close(singleton.ServerList[clientID].TaskClose)
} }
singleton.ServerList[clientID].TaskStream = stream singleton.ServerList[clientID].TaskStream = stream
singleton.ServerList[clientID].TaskClose = closeCh singleton.ServerList[clientID].TaskClose = closeCh
singleton.ServerList[clientID].TaskCloseLock.Unlock()
singleton.ServerLock.RUnlock() singleton.ServerLock.RUnlock()
return <-closeCh return <-closeCh
} }
+1
View File
@@ -34,6 +34,7 @@ func loadServers() {
innerS := s innerS := s
innerS.Host = &model.Host{} innerS.Host = &model.Host{}
innerS.State = &model.HostState{} innerS.State = &model.HostState{}
innerS.TaskCloseLock = new(sync.Mutex)
ServerList[innerS.ID] = &innerS ServerList[innerS.ID] = &innerS
SecretToID[innerS.Secret] = innerS.ID SecretToID[innerS.Secret] = innerS.ID
ServerTagToIDList[innerS.Tag] = append(ServerTagToIDList[innerS.Tag], innerS.ID) ServerTagToIDList[innerS.Tag] = append(ServerTagToIDList[innerS.Tag], innerS.ID)