restructure the code

This commit is contained in:
Zhi Wang 2022-01-09 22:04:23 -05:00
parent f4a15a06b0
commit 0f76aeef8d
4 changed files with 171 additions and 85 deletions

View File

@ -27,7 +27,7 @@
<div id="terminal_view"></div> <div id="terminal_view"></div>
</div> </div>
<script> <script>
term = createTerminal({{.path}}); term = createTerminal("{{.path}}");
// print something to test output and scroll // print something to test output and scroll
var str = [ var str = [
' ┌────────────────────────────────────────────────────────────────────────────┐', ' ┌────────────────────────────────────────────────────────────────────────────┐',

12
main.go
View File

@ -31,23 +31,25 @@ func main() {
log.Println(cmdToExec) log.Println(cmdToExec)
} }
registry.init()
rt := gin.Default() rt := gin.Default()
rt.SetTrustedProxies(nil) rt.SetTrustedProxies(nil)
rt.LoadHTMLGlob("./assets/*.html") rt.LoadHTMLGlob("./assets/*.html")
rt.GET("/watch/*sname", func(c *gin.Context) { rt.GET("/view/*sname", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{ c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Watcher terminal", "title": "Watcher terminal",
"path": "/ws_watch", "path": "/ws_view",
}) })
}) })
rt.GET("/ws_run", func(c *gin.Context) { rt.GET("/ws_do", func(c *gin.Context) {
wsHandler(c.Writer, c.Request, false) wsHandler(c.Writer, c.Request, false)
}) })
rt.GET("/ws_watch", func(c *gin.Context) { rt.GET("/ws_view", func(c *gin.Context) {
wsHandler(c.Writer, c.Request, true) wsHandler(c.Writer, c.Request, true)
}) })
@ -57,7 +59,7 @@ func main() {
rt.GET("/", func(c *gin.Context) { rt.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{ c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Master terminal", "title": "Master terminal",
"path": "/ws_run ", "path": "/ws_do",
}) })
host = &c.Request.Host host = &c.Request.Host
}) })

58
reg.go Normal file
View File

@ -0,0 +1,58 @@
package main
import (
"errors"
"log"
"sync"
"github.com/gorilla/websocket"
)
// a simple registry for actors and their channels. It is possible to
// design this using channels, but it is simple enough with mutex
type Registry struct {
mtx sync.Mutex
doers map[string]*TermConn
}
var registry Registry
func (reg *Registry) init() {
reg.doers = make(map[string]*TermConn)
}
func (d *Registry) addDoer(name string, tc *TermConn) {
d.mtx.Lock()
if val, ok := d.doers[name]; ok {
log.Printf(name, "already exist in the dispatcher")
val.release()
}
d.doers[name] = tc
d.mtx.Unlock()
}
func (d *Registry) delDoer(name string) error {
d.mtx.Lock()
var err error = errors.New("not found")
if _, ok := d.doers[name]; ok {
delete(d.doers, name)
err = nil
}
d.mtx.Unlock()
return err
}
// we do not want to return the channel to viewer so it won't be used out of the critical section
func (d *Registry) sendToDoer(name string, ws *websocket.Conn) bool {
d.mtx.Lock()
tc, ok := d.doers[name]
if ok {
tc.vchan <- ws
}
d.mtx.Unlock()
return ok
}

180
relay.go
View File

@ -30,7 +30,20 @@ const (
closeGracePeriod = 10 * time.Second closeGracePeriod = 10 * time.Second
) )
func createPty(cmdline []string) (*os.File, *exec.Cmd, error) { // 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 {
// Create a shell command. // Create a shell command.
cmd := exec.Command(cmdline[0], cmdline[1:]...) cmd := exec.Command(cmdline[0], cmdline[1:]...)
@ -38,19 +51,22 @@ func createPty(cmdline []string) (*os.File, *exec.Cmd, error) {
ptmx, err := pty.Start(cmd) ptmx, err := pty.Start(cmd)
if err != nil { if err != nil {
return nil, nil, err return err
} }
// Use fixed size, the xterm is initalized as 122x37, // Use fixed size, the xterm is initalized as 122x37,
// But we set pty to 120x36. Using fullsize will lead // But we set pty to 120x36. Using fullsize will lead
// some program to misbehaive. // some program to misbehave.
pty.Setsize(ptmx, &pty.Winsize{ pty.Setsize(ptmx, &pty.Winsize{
Cols: 120, Cols: 120,
Rows: 36, Rows: 36,
}) })
tc.ptmx = ptmx
tc.cmd = cmd
log.Printf("Create shell process %v (%v)", cmdline, cmd.Process.Pid) log.Printf("Create shell process %v (%v)", cmdline, cmd.Process.Pid)
return ptmx, cmd, nil return nil
} }
var host *string = nil var host *string = nil
@ -75,20 +91,20 @@ var upgrader = websocket.Upgrader{
} }
// Periodically send ping message to detect the status of the ws // Periodically send ping message to detect the status of the ws
func ping(ws *websocket.Conn, done chan struct{}) { func (tc *TermConn) ping() {
ticker := time.NewTicker(pingPeriod) ticker := time.NewTicker(pingPeriod)
defer ticker.Stop() defer ticker.Stop()
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
err := ws.WriteControl(websocket.PingMessage, err := tc.ws.WriteControl(websocket.PingMessage,
[]byte{}, time.Now().Add(writeWait)) []byte{}, time.Now().Add(writeWait))
if err != nil { if err != nil {
log.Println("Failed to write ping message:", err) log.Println("Failed to write ping message:", err)
} }
case <-done: case <-tc.done:
log.Println("Exit ping routine as stdout is going away") log.Println("Exit ping routine as stdout is going away")
return return
} }
@ -96,28 +112,29 @@ func ping(ws *websocket.Conn, done chan struct{}) {
} }
// shovel data from websocket to pty stdin // shovel data from websocket to pty stdin
func toPtyStdin(ws *websocket.Conn, ptmx *os.File) { func (tc *TermConn) wsToPtyStdin() {
ws.SetReadLimit(maxMessageSize) tc.ws.SetReadLimit(maxMessageSize)
// set the readdeadline. The idea here is simple, // set the readdeadline. The idea here is simple,
// as long as we keep receiving pong message, // as long as we keep receiving pong message,
// the readdeadline will keep updating. Otherwise // the readdeadline will keep updating. Otherwise
// read will timeout. // read will timeout.
ws.SetReadDeadline(time.Now().Add(pongWait)) tc.ws.SetReadDeadline(time.Now().Add(pongWait))
ws.SetPongHandler(func(string) error { tc.ws.SetPongHandler(func(string) error {
ws.SetReadDeadline(time.Now().Add(pongWait)) tc.ws.SetReadDeadline(time.Now().Add(pongWait))
return nil return nil
}) })
// we do not need to forward user input to viewers, only the stdout
for { for {
_, buf, err := ws.ReadMessage() _, buf, err := tc.ws.ReadMessage()
if err != nil { if err != nil {
log.Println("Failed to receive data from ws:", err) log.Println("Failed to receive data from ws:", err)
break break
} }
_, err = ptmx.Write(buf) _, err = tc.ptmx.Write(buf)
if err != nil { if err != nil {
log.Println("Failed to send data to pty stdin: ", err) log.Println("Failed to send data to pty stdin: ", err)
@ -127,100 +144,73 @@ func toPtyStdin(ws *websocket.Conn, ptmx *os.File) {
} }
// shovel data from pty Stdout to WS // shovel data from pty Stdout to WS
func fromPtyStdout(ws *websocket.Conn, ptmx *os.File, done chan struct{}) { func (tc *TermConn) ptyStdoutToWs() {
var watchers []*websocket.Conn var viewers []*websocket.Conn
readBuf := make([]byte, 4096) readBuf := make([]byte, 4096)
for { for {
n, err := ptmx.Read(readBuf) n, err := tc.ptmx.Read(readBuf)
if err != nil { if err != nil {
log.Println("Failed to read from pty stdout: ", err) log.Println("Failed to read from pty stdout: ", err)
break break
} }
// handle watchers, we want to use non-blocking receive // handle viewers, we want to use non-blocking receive
select { select {
case watcher := <-watcherChan: case watcher := <-tc.vchan:
log.Println("Received watcher", watcher) log.Println("Received watcher", watcher)
watchers = append(watchers, watcher) viewers = append(viewers, watcher)
default: default:
log.Println("no watcher received") log.Println("no watcher received")
} }
// We could add ws to watchers as well, but we want to handle it // We could add ws to viewers as well (then we can use io.MultiWriter),
// differently if there is an error // but we want to handle errors differently
ws.SetWriteDeadline(time.Now().Add(writeWait)) tc.ws.SetWriteDeadline(time.Now().Add(writeWait))
if ws.WriteMessage(websocket.BinaryMessage, readBuf[0:n]); err != nil { if tc.ws.WriteMessage(websocket.BinaryMessage, readBuf[0:n]); err != nil {
log.Println("Failed to write message: ", err) log.Println("Failed to write message: ", err)
break break
} }
for i, w := range watchers { for i, w := range viewers {
if w == nil { if w == nil {
continue continue
} }
// if the viewer exits, we will just ignore the error
if err = w.WriteMessage(websocket.BinaryMessage, readBuf[0:n]); err != nil { if err = w.WriteMessage(websocket.BinaryMessage, readBuf[0:n]); err != nil {
log.Println("Failed to write message to watcher: ", err) log.Println("Failed to write message to watcher: ", err)
watchers[i] = nil viewers[i] = nil
w.Close() w.Close()
} }
} }
} }
close(done)
close(watcherChan)
watcherChan = nil
// close the watcher // close the watcher
for _, w := range watchers { for _, w := range viewers {
if w != nil { if w != nil {
w.Close() w.Close()
} }
} }
ws.SetWriteDeadline(time.Now().Add(writeWait)) tc.ws.SetWriteDeadline(time.Now().Add(writeWait))
ws.WriteMessage(websocket.CloseMessage, tc.ws.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Pty closed")) websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Pty closed"))
time.Sleep(closeGracePeriod) time.Sleep(closeGracePeriod)
} }
var watcherChan chan *websocket.Conn func (tc *TermConn) release() {
log.Println("releasing", tc.name)
// handle websockets registry.delDoer(tc.name)
func wsHandleRun(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Failed to create websocket: ", err)
return
}
defer ws.Close()
log.Println("\n\nCreated the websocket")
ptmx, cmd, err := createPty(cmdToExec)
if err != nil {
log.Println("Failed to create PTY: ", err)
return
}
done := make(chan struct{})
watcherChan = make(chan *websocket.Conn)
go fromPtyStdout(ws, ptmx, done)
go ping(ws, done)
toPtyStdin(ws, ptmx)
if tc.ptmx != nil {
// cleanup the pty and its related process // cleanup the pty and its related process
ptmx.Close() tc.ptmx.Close()
proc := cmd.Process
// terminate the command line process
proc := tc.cmd.Process
// send an interrupt, this will cause the shell process to // send an interrupt, this will cause the shell process to
// return from syscalls if any is pending // return from syscalls if any is pending
@ -240,10 +230,51 @@ func wsHandleRun(w http.ResponseWriter, r *http.Request) {
if _, err := proc.Wait(); err != nil { if _, err := proc.Wait(); err != nil {
log.Printf("Failed to wait for shell process(%v): %v", proc.Pid, err) log.Printf("Failed to wait for shell process(%v): %v", proc.Pid, err)
} }
close(tc.done)
close(tc.vchan)
}
tc.ws.Close()
} }
// handle websockets // handle websockets
func wsHandleWatcher(w http.ResponseWriter, r *http.Request) { func wsHandleDoer(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Failed to create websocket: ", err)
return
}
tc := TermConn{
ws: ws,
name: "main",
}
defer tc.release()
log.Println("\n\nCreated the websocket")
if err := tc.createPty(cmdToExec); err != nil {
log.Println("Failed to create PTY: ", err)
return
}
tc.done = make(chan struct{})
tc.vchan = make(chan *websocket.Conn)
registry.addDoer("main", &tc)
// main event loop to shovel data between ws and pty
go tc.ping()
go tc.wsToPtyStdin()
tc.ptyStdoutToWs()
}
// handle websockets
func wsHandleViewer(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil) ws, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
@ -252,21 +283,16 @@ func wsHandleWatcher(w http.ResponseWriter, r *http.Request) {
} }
log.Println("\n\nCreated the websocket") log.Println("\n\nCreated the websocket")
if !registry.sendToDoer("main", ws) {
if watcherChan == nil { log.Println("Failed to send websocket to doer, close it")
log.Println("No active runner, create a runner first")
ws.Close() ws.Close()
return
} }
// hand the websocket to runner
watcherChan <- ws
} }
func wsHandler(w http.ResponseWriter, r *http.Request, isWatcher bool) { func wsHandler(w http.ResponseWriter, r *http.Request, isViewer bool) {
if !isWatcher { if !isViewer {
wsHandleRun(w, r) wsHandleDoer(w, r)
} else { } else {
wsHandleWatcher(w, r) wsHandleViewer(w, r)
} }
} }