-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
47 lines (40 loc) · 955 Bytes
/
term.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"context"
"log"
"net/http"
"os/exec"
"github.com/coder/websocket"
"github.com/linuxexam/webrun/run"
)
func (c *Client) ServeTerm(w http.ResponseWriter, r *http.Request) {
defer func() {
if c.ptmx != nil {
c.ptmx.Close()
}
DelClient(c.id)
}()
conn, err := websocket.Accept(w, r, nil)
if err != nil {
log.Fatal(err)
}
c.termConn = conn
log.Println("ws-term websocket connected.")
defer func() {
conn.CloseNow()
log.Println("ws-term websocket disconnected.")
}()
// Run the command
ctx, _ := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, c.commandName, c.commandArgs...)
ptmx, err := run.StartCommand(conn, cmd)
if err != nil {
log.Printf("start command error: %v\n", err)
}
c.ptmx = ptmx
log.Printf("%v\n", c)
if err := cmd.Wait(); err != nil {
log.Printf("cmd %s failed: %v\n", c.commandName, err)
}
log.Printf("%s:%s finished.\n", c.id, c.commandName)
}