2022-01-09 14:56:43 +01:00
|
|
|
//This file contains code to relay traffic between websocket and pty
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/creack/pty"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Time allowed to write a message to the peer.
|
2022-01-10 15:35:05 +01:00
|
|
|
readWait = 10 * time.Second
|
|
|
|
writeWait = 10 * time.Second
|
|
|
|
viewWait = 3 * time.Second
|
2022-01-09 14:56:43 +01:00
|
|
|
|
|
|
|
// Time allowed to read the next pong message from the peer.
|
2022-01-10 15:35:05 +01:00
|
|
|
pongWait = 10 * time.Second
|
2022-01-09 14:56:43 +01:00
|
|
|
|
|
|
|
// Maximum message size allowed from peer.
|
|
|
|
maxMessageSize = 8192
|
|
|
|
|
|
|
|
// Send pings to peer with this period. Must be less than pongWait.
|
|
|
|
pingPeriod = (pongWait * 9) / 10
|
|
|
|
|
|
|
|
// Time to wait before force close on connection.
|
|
|
|
closeGracePeriod = 10 * time.Second
|
|
|
|
)
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
// TermConn represents the connected websocket and pty.
|
|
|
|
// if isViewer is true
|
|
|
|
type TermConn struct {
|
|
|
|
ws *websocket.Conn
|
|
|
|
name string
|
|
|
|
|
|
|
|
// only valid for doers
|
|
|
|
ptmx *os.File // the pty that runs the command
|
|
|
|
cmd *exec.Cmd // represents the process, we need it to terminate the process
|
|
|
|
vchan chan *websocket.Conn // channel to receive viewers
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *TermConn) createPty(cmdline []string) error {
|
2022-01-09 14:56:43 +01:00
|
|
|
// Create a shell command.
|
|
|
|
cmd := exec.Command(cmdline[0], cmdline[1:]...)
|
|
|
|
|
|
|
|
// Start the command with a pty.
|
|
|
|
ptmx, err := pty.Start(cmd)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-01-10 04:04:23 +01:00
|
|
|
return err
|
2022-01-09 14:56:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use fixed size, the xterm is initalized as 122x37,
|
|
|
|
// But we set pty to 120x36. Using fullsize will lead
|
2022-01-10 04:04:23 +01:00
|
|
|
// some program to misbehave.
|
2022-01-09 14:56:43 +01:00
|
|
|
pty.Setsize(ptmx, &pty.Winsize{
|
|
|
|
Cols: 120,
|
|
|
|
Rows: 36,
|
|
|
|
})
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
tc.ptmx = ptmx
|
|
|
|
tc.cmd = cmd
|
|
|
|
|
2022-01-09 14:56:43 +01:00
|
|
|
log.Printf("Create shell process %v (%v)", cmdline, cmd.Process.Pid)
|
2022-01-10 04:04:23 +01:00
|
|
|
return nil
|
2022-01-09 14:56:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var host *string = nil
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 4096,
|
|
|
|
WriteBufferSize: 4096,
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
org := r.Header.Get("Origin")
|
|
|
|
h, err := url.Parse(org)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host == nil) || (*host != h.Host) {
|
|
|
|
log.Println("Failed origin check of ", org)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (host != nil) && (*host == h.Host)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Periodically send ping message to detect the status of the ws
|
2022-01-10 04:04:23 +01:00
|
|
|
func (tc *TermConn) ping() {
|
2022-01-09 14:56:43 +01:00
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2022-01-10 04:04:23 +01:00
|
|
|
err := tc.ws.WriteControl(websocket.PingMessage,
|
2022-01-09 14:56:43 +01:00
|
|
|
[]byte{}, time.Now().Add(writeWait))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to write ping message:", err)
|
2022-01-10 15:35:05 +01:00
|
|
|
return
|
2022-01-09 14:56:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
case <-tc.done:
|
2022-01-10 15:35:05 +01:00
|
|
|
log.Println("Exit ping routine as pty/ws is going away")
|
2022-01-09 14:56:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// shovel data from websocket to pty stdin
|
2022-01-10 04:04:23 +01:00
|
|
|
func (tc *TermConn) wsToPtyStdin() {
|
|
|
|
tc.ws.SetReadLimit(maxMessageSize)
|
2022-01-09 14:56:43 +01:00
|
|
|
|
|
|
|
// set the readdeadline. The idea here is simple,
|
|
|
|
// as long as we keep receiving pong message,
|
|
|
|
// the readdeadline will keep updating. Otherwise
|
|
|
|
// read will timeout.
|
2022-01-10 04:04:23 +01:00
|
|
|
tc.ws.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
tc.ws.SetPongHandler(func(string) error {
|
|
|
|
tc.ws.SetReadDeadline(time.Now().Add(pongWait))
|
2022-01-09 14:56:43 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
// we do not need to forward user input to viewers, only the stdout
|
2022-01-09 14:56:43 +01:00
|
|
|
for {
|
2022-01-10 04:04:23 +01:00
|
|
|
_, buf, err := tc.ws.ReadMessage()
|
2022-01-09 14:56:43 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to receive data from ws:", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
_, err = tc.ptmx.Write(buf)
|
2022-01-09 14:56:43 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to send data to pty stdin: ", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// shovel data from pty Stdout to WS
|
2022-01-10 04:04:23 +01:00
|
|
|
func (tc *TermConn) ptyStdoutToWs() {
|
|
|
|
var viewers []*websocket.Conn
|
2022-01-09 14:56:43 +01:00
|
|
|
readBuf := make([]byte, 4096)
|
2022-01-10 15:35:05 +01:00
|
|
|
closed := false
|
2022-01-09 14:56:43 +01:00
|
|
|
|
2022-01-10 15:35:05 +01:00
|
|
|
out:
|
2022-01-09 14:56:43 +01:00
|
|
|
for {
|
2022-01-10 04:04:23 +01:00
|
|
|
n, err := tc.ptmx.Read(readBuf)
|
2022-01-09 14:56:43 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to read from pty stdout: ", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
// handle viewers, we want to use non-blocking receive
|
2022-01-09 14:56:43 +01:00
|
|
|
select {
|
2022-01-10 15:35:05 +01:00
|
|
|
case viewer := <-tc.vchan:
|
|
|
|
log.Println("Received viewer")
|
|
|
|
viewers = append(viewers, viewer)
|
|
|
|
|
|
|
|
case <-tc.done:
|
|
|
|
log.Println("Websocket is closed by main goroutine")
|
|
|
|
closed = true
|
|
|
|
break out
|
|
|
|
|
|
|
|
default: // do not block on these two channels
|
2022-01-09 14:56:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
// We could add ws to viewers as well (then we can use io.MultiWriter),
|
|
|
|
// but we want to handle errors differently
|
|
|
|
tc.ws.SetWriteDeadline(time.Now().Add(writeWait))
|
2022-01-10 15:35:05 +01:00
|
|
|
if err = tc.ws.WriteMessage(websocket.BinaryMessage, readBuf[0:n]); err != nil {
|
2022-01-09 14:56:43 +01:00
|
|
|
log.Println("Failed to write message: ", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
for i, w := range viewers {
|
2022-01-09 14:56:43 +01:00
|
|
|
if w == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
// if the viewer exits, we will just ignore the error
|
2022-01-10 15:35:05 +01:00
|
|
|
w.SetWriteDeadline(time.Now().Add(viewWait))
|
2022-01-09 14:56:43 +01:00
|
|
|
if err = w.WriteMessage(websocket.BinaryMessage, readBuf[0:n]); err != nil {
|
2022-01-10 15:35:05 +01:00
|
|
|
log.Println("Failed to write message to viewer: ", err)
|
2022-01-09 14:56:43 +01:00
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
viewers[i] = nil
|
2022-01-10 15:35:05 +01:00
|
|
|
w.Close() // we own the socket and need to close it
|
2022-01-09 14:56:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the watcher
|
2022-01-10 04:04:23 +01:00
|
|
|
for _, w := range viewers {
|
2022-01-09 14:56:43 +01:00
|
|
|
if w != nil {
|
|
|
|
w.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 15:35:05 +01:00
|
|
|
if !closed { // If the error is caused by pty, try to close the socket
|
|
|
|
tc.ws.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
tc.ws.WriteMessage(websocket.CloseMessage,
|
|
|
|
websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Pty closed"))
|
|
|
|
time.Sleep(closeGracePeriod)
|
|
|
|
}
|
2022-01-09 14:56:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-10 15:35:05 +01:00
|
|
|
// this function should be executed by the main goroutine for the connection
|
|
|
|
func (tc *TermConn) release() {
|
|
|
|
log.Println("Releasing terminal connection", tc.name)
|
2022-01-10 04:23:52 +01:00
|
|
|
|
2022-01-10 15:35:05 +01:00
|
|
|
registry.removePlayer(tc.name)
|
2022-01-10 04:04:23 +01:00
|
|
|
|
|
|
|
if tc.ptmx != nil {
|
|
|
|
// cleanup the pty and its related process
|
|
|
|
tc.ptmx.Close()
|
|
|
|
|
|
|
|
// terminate the command line process
|
|
|
|
proc := tc.cmd.Process
|
|
|
|
|
|
|
|
// send an interrupt, this will cause the shell process to
|
|
|
|
// return from syscalls if any is pending
|
|
|
|
if err := proc.Signal(os.Interrupt); err != nil {
|
|
|
|
log.Printf("Failed to send Interrupt to shell process(%v): %v ", proc.Pid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for a second for shell process to interrupt before kill it
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
log.Printf("Try to kill the shell process(%v)", proc.Pid)
|
|
|
|
|
|
|
|
if err := proc.Signal(os.Kill); err != nil {
|
|
|
|
log.Printf("Failed to send KILL to shell process(%v): %v", proc.Pid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := proc.Wait(); err != nil {
|
|
|
|
log.Printf("Failed to wait for shell process(%v): %v", proc.Pid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
close(tc.vchan)
|
2022-01-10 15:35:05 +01:00
|
|
|
close(tc.done)
|
2022-01-10 04:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tc.ws.Close()
|
|
|
|
}
|
2022-01-09 14:56:43 +01:00
|
|
|
|
|
|
|
// handle websockets
|
2022-01-10 15:35:05 +01:00
|
|
|
func wsHandlePlayer(w http.ResponseWriter, r *http.Request) {
|
2022-01-09 14:56:43 +01:00
|
|
|
ws, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to create websocket: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
tc := TermConn{
|
|
|
|
ws: ws,
|
|
|
|
name: "main",
|
|
|
|
}
|
2022-01-09 14:56:43 +01:00
|
|
|
|
2022-01-10 15:35:05 +01:00
|
|
|
defer tc.release()
|
2022-01-09 14:56:43 +01:00
|
|
|
log.Println("\n\nCreated the websocket")
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
if err := tc.createPty(cmdToExec); err != nil {
|
2022-01-09 14:56:43 +01:00
|
|
|
log.Println("Failed to create PTY: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
tc.done = make(chan struct{})
|
|
|
|
tc.vchan = make(chan *websocket.Conn)
|
2022-01-09 14:56:43 +01:00
|
|
|
|
2022-01-10 15:35:05 +01:00
|
|
|
registry.addPlayer("main", &tc)
|
2022-01-09 14:56:43 +01:00
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
// main event loop to shovel data between ws and pty
|
2022-01-10 15:35:05 +01:00
|
|
|
// do not call ptyStdoutToWs in this goroutine, otherwise
|
|
|
|
// the websocket will not close. This is because ptyStdoutToWs
|
|
|
|
// is usually blocked in the pty.Read
|
2022-01-10 04:04:23 +01:00
|
|
|
go tc.ping()
|
2022-01-10 15:35:05 +01:00
|
|
|
go tc.ptyStdoutToWs()
|
2022-01-09 14:56:43 +01:00
|
|
|
|
2022-01-10 15:35:05 +01:00
|
|
|
tc.wsToPtyStdin()
|
2022-01-09 14:56:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle websockets
|
2022-01-10 04:04:23 +01:00
|
|
|
func wsHandleViewer(w http.ResponseWriter, r *http.Request) {
|
2022-01-09 14:56:43 +01:00
|
|
|
ws, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to create websocket: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("\n\nCreated the websocket")
|
2022-01-10 15:35:05 +01:00
|
|
|
if !registry.sendToPlayer("main", ws) {
|
|
|
|
log.Println("Failed to send websocket to player, close it")
|
2022-01-09 14:56:43 +01:00
|
|
|
ws.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:04:23 +01:00
|
|
|
func wsHandler(w http.ResponseWriter, r *http.Request, isViewer bool) {
|
|
|
|
if !isViewer {
|
2022-01-10 15:35:05 +01:00
|
|
|
wsHandlePlayer(w, r)
|
2022-01-09 14:56:43 +01:00
|
|
|
} else {
|
2022-01-10 04:04:23 +01:00
|
|
|
wsHandleViewer(w, r)
|
2022-01-09 14:56:43 +01:00
|
|
|
}
|
|
|
|
}
|