witty/main.go

256 lines
5.3 KiB
Go
Raw Normal View History

2022-01-04 21:41:41 +01:00
package main
import (
"fmt"
"log"
2022-01-04 21:41:41 +01:00
"net/http"
"net/url"
2022-01-05 18:38:52 +01:00
"os"
"os/exec"
2022-01-05 21:08:27 +01:00
"strings"
"time"
2022-01-05 18:38:52 +01:00
"github.com/creack/pty"
2022-01-05 21:08:27 +01:00
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
2022-01-04 21:41:41 +01:00
)
const (
// Time allowed to write a message to the peer.
writeWait = 5 * time.Second
// Time allowed to read the next pong message from the peer.
pongWait = 30 * time.Second
// 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
)
func createPty(cmdline string) (*os.File, *exec.Cmd, error) {
2022-01-05 18:38:52 +01:00
// Create a shell command.
cmd := exec.Command(cmdline)
// Start the command with a pty.
ptmx, err := pty.Start(cmd)
if err != nil {
return nil, nil, err
}
// Use fixed size, the xterm is initalized as 122x37,
// But we set pty to 120x36. Using fullsize will lead
// some program to misbehaive.
pty.Setsize(ptmx, &pty.Winsize{
Cols: 120,
Rows: 36,
})
return ptmx, cmd, nil
2022-01-05 18:38:52 +01:00
}
2022-01-04 21:41:41 +01:00
var host *string = nil
var upgrader = websocket.Upgrader{
2022-01-05 03:21:21 +01:00
ReadBufferSize: 4096,
2022-01-04 21:41:41 +01:00
WriteBufferSize: 4096,
CheckOrigin: func(r *http.Request) bool {
org := r.Header.Get("Origin")
2022-01-05 03:21:21 +01:00
h, err := url.Parse(org)
2022-01-04 21:41:41 +01:00
if err != nil {
return false
}
if (host == nil) || (*host != h.Host) {
log.Println("Failed origin check of ", org)
2022-01-04 21:41:41 +01:00
}
return (host != nil) && (*host == h.Host)
},
}
// Periodically send ping message to detect the status of the ws
func ping(ws *websocket.Conn, done chan struct{}) {
ticker := time.NewTicker(pingPeriod)
defer ticker.Stop()
for {
select {
case <-ticker.C:
err := ws.WriteControl(websocket.PingMessage,
[]byte{}, time.Now().Add(writeWait))
if err != nil {
log.Println("Failed to write ping message:", err)
}
case <-done:
log.Println("Exit ping routine as stdout is going away")
return
}
}
}
// shovel data from websocket to pty stdin
func toPtyStdin(ws *websocket.Conn, ptmx *os.File) {
ws.SetReadLimit(maxMessageSize)
// 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.
ws.SetReadDeadline(time.Now().Add(pongWait))
ws.SetPongHandler(func(string) error {
ws.SetReadDeadline(time.Now().Add(pongWait))
return nil
})
for {
_, buf, err := ws.ReadMessage()
if err != nil {
log.Println("Failed to receive data from ws:", err)
break
}
_, err = ptmx.Write(buf)
if err != nil {
log.Println("Failed to send data to pty stdin: ", err)
break
}
}
}
// shovel data from websocket to pty stdin
func fromPtyStdout(ws *websocket.Conn, ptmx *os.File, done chan struct{}) {
readBuf := make([]byte, 4096)
for {
n, err := ptmx.Read(readBuf)
if err != nil {
log.Println("Failed to read from pty stdout: ", err)
break
}
ws.SetWriteDeadline(time.Now().Add(writeWait))
if err = ws.WriteMessage(websocket.BinaryMessage, readBuf[:n]); err != nil {
log.Println("Failed to write message: ", err)
break
}
}
close(done)
ws.SetWriteDeadline(time.Now().Add(writeWait))
ws.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Pty closed"))
time.Sleep(closeGracePeriod)
}
2022-01-04 21:41:41 +01:00
// handle websockets
func wsHandler(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
2022-01-04 21:41:41 +01:00
2022-01-05 03:21:21 +01:00
if err != nil {
log.Println("Failed to create websocket: ", err)
2022-01-04 21:41:41 +01:00
return
}
defer ws.Close()
2022-01-04 21:41:41 +01:00
log.Println("\n\nCreated the websocket")
2022-01-05 18:38:52 +01:00
ptmx, cmd, err := createPty("bash")
2022-01-05 18:38:52 +01:00
if err != nil {
log.Println("Failed to create PTY: ", err)
2022-01-05 18:38:52 +01:00
return
}
done := make(chan struct{})
2022-01-05 18:38:52 +01:00
go fromPtyStdout(ws, ptmx, done)
go ping(ws, done)
2022-01-05 18:38:52 +01:00
toPtyStdin(ws, ptmx)
2022-01-05 18:38:52 +01:00
// cleanup the pty and its related process
ptmx.Close()
proc := cmd.Process
2022-01-05 18:38:52 +01:00
// 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.Println("Failed to send Interrupt to shell process: ", err)
}
2022-01-05 18:38:52 +01:00
// Wait for a second for shell process to interrupt before kill it
time.Sleep(time.Second)
2022-01-04 21:41:41 +01:00
log.Printf("Try to kill the shell process")
2022-01-04 21:41:41 +01:00
if err := proc.Signal(os.Kill); err != nil {
log.Println("Failed to send KILL to shell process: ", err)
}
if _, err := proc.Wait(); err != nil {
log.Println("Failed to wait for shell process: ", err)
2022-01-04 21:41:41 +01:00
}
}
// return files
2022-01-05 03:21:21 +01:00
func fileHandler(c *gin.Context, fname string) {
2022-01-04 21:41:41 +01:00
// if the URL has no fname, c.Param returns "/"
if fname == "/" {
fname = "/index.html"
host = &c.Request.Host
}
fname = fname[1:] //fname always starts with /
log.Println("Sending ", fname)
2022-01-04 21:41:41 +01:00
if strings.HasSuffix(fname, "html") {
c.HTML(200, fname, nil)
} else {
//c.HTML interprets the file as HTML file
//we do not need that for regular files
c.File(fmt.Sprint("assets/", fname))
}
}
2022-01-05 03:21:21 +01:00
func main() {
fp, err := os.OpenFile("web_term.log", os.O_RDWR|os.O_CREATE, 0644)
if err == nil {
defer fp.Close()
log.SetOutput(fp)
gin.DefaultWriter = fp
}
2022-01-04 21:41:41 +01:00
rt := gin.Default()
rt.SetTrustedProxies(nil)
rt.LoadHTMLGlob("assets/*.html")
2022-01-05 03:21:21 +01:00
rt.GET("/*fname", func(c *gin.Context) {
2022-01-04 21:41:41 +01:00
fname := c.Param("fname")
// ws is a special case to create a new websocket
switch fname {
2022-01-05 03:21:21 +01:00
case "/ws":
2022-01-04 21:41:41 +01:00
wsHandler(c.Writer, c.Request)
default:
fileHandler(c, fname)
}
})
rt.Run(":8080")
2022-01-05 03:21:21 +01:00
}