- Nova pty_session.go: RingBuffer, consolePTYSession, spawnConsolePTY - Nova ws.go: WebSocket handler za PTY bidirekcioni I/O - console.go: koristi consolePTYSession umesto starih pipe-ova - console.html: xterm.js 5.5.0 CDN, FitAddon, WebLinksAddon - Podrška za resize, binarni podaci, replay buffer (1MB) - 8 novih testova (RingBuffer + xterm konzola) — ukupno 179 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
147 lines
3.2 KiB
Go
147 lines
3.2 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
var wsUpgrader = websocket.Upgrader{
|
|
ReadBufferSize: 4096,
|
|
WriteBufferSize: 4096,
|
|
CheckOrigin: func(r *http.Request) bool { return true },
|
|
}
|
|
|
|
// wsResizeMsg is sent from the browser when the terminal size changes.
|
|
type wsResizeMsg struct {
|
|
Type string `json:"type"`
|
|
Cols uint16 `json:"cols"`
|
|
Rows uint16 `json:"rows"`
|
|
}
|
|
|
|
// handleConsoleWS handles WebSocket connections for console PTY sessions.
|
|
func (s *Server) handleConsoleWS(c *gin.Context) {
|
|
sessionNum := c.Param("session")
|
|
if sessionNum != "1" && sessionNum != "2" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "sesija mora biti 1 ili 2"})
|
|
return
|
|
}
|
|
|
|
conn, err := wsUpgrader.Upgrade(c.Writer, c.Request, nil)
|
|
if err != nil {
|
|
log.Printf("WebSocket upgrade error: %v", err)
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
idx := 0
|
|
if sessionNum == "2" {
|
|
idx = 1
|
|
}
|
|
|
|
session := s.console.getSession(idx)
|
|
|
|
// Wait for PTY session to be available (it gets set when a command is executed)
|
|
session.mu.Lock()
|
|
ptySess := session.ptySess
|
|
session.mu.Unlock()
|
|
|
|
if ptySess == nil {
|
|
// No active PTY — send message and wait
|
|
conn.WriteMessage(websocket.TextMessage, []byte("\r\n\033[33m[Nema aktivne sesije. Pokrenite komandu.]\033[0m\r\n"))
|
|
|
|
// Poll for session to start
|
|
ticker := time.NewTicker(500 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
session.mu.Lock()
|
|
ptySess = session.ptySess
|
|
session.mu.Unlock()
|
|
if ptySess != nil {
|
|
goto connected
|
|
}
|
|
case <-c.Request.Context().Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
connected:
|
|
subID := fmt.Sprintf("ws-%d", time.Now().UnixNano())
|
|
outputCh := ptySess.Subscribe(subID)
|
|
defer ptySess.Unsubscribe(subID)
|
|
|
|
// Send buffered output for reconnect
|
|
buffered := ptySess.GetBuffer()
|
|
if len(buffered) > 0 {
|
|
conn.WriteMessage(websocket.BinaryMessage, buffered)
|
|
}
|
|
|
|
// Serialized write channel
|
|
writeCh := make(chan []byte, 256)
|
|
writeDone := make(chan struct{})
|
|
go func() {
|
|
defer close(writeDone)
|
|
for data := range writeCh {
|
|
if err := conn.WriteMessage(websocket.BinaryMessage, data); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
// PTY output → WebSocket
|
|
go func() {
|
|
for data := range outputCh {
|
|
select {
|
|
case writeCh <- data:
|
|
default:
|
|
}
|
|
}
|
|
}()
|
|
|
|
// Watch for process exit
|
|
go func() {
|
|
<-ptySess.Done()
|
|
select {
|
|
case writeCh <- []byte("\r\n\033[33m[Sesija završena]\033[0m\r\n"):
|
|
default:
|
|
}
|
|
}()
|
|
|
|
// WebSocket → PTY (read pump)
|
|
for {
|
|
_, msg, err := conn.ReadMessage()
|
|
if err != nil {
|
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
|
|
log.Printf("WebSocket read error: %v", err)
|
|
}
|
|
break
|
|
}
|
|
|
|
// Check for resize message
|
|
var resize wsResizeMsg
|
|
if json.Unmarshal(msg, &resize) == nil && resize.Type == "resize" && resize.Cols > 0 && resize.Rows > 0 {
|
|
if err := ptySess.Resize(resize.Rows, resize.Cols); err != nil {
|
|
log.Printf("PTY resize error: %v", err)
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Regular keyboard input → PTY
|
|
if _, err := ptySess.WriteInput(msg); err != nil {
|
|
log.Printf("PTY write error: %v", err)
|
|
break
|
|
}
|
|
}
|
|
|
|
close(writeCh)
|
|
<-writeDone
|
|
}
|