witty/main.go

261 lines
5.4 KiB
Go
Raw Normal View History

2022-01-04 21:41:41 +01:00
package main
import (
2022-01-16 22:46:01 +01:00
"encoding/json"
"io/ioutil"
"log"
2022-01-04 21:41:41 +01:00
"net/http"
2022-01-10 23:16:22 +01:00
"net/url"
2022-01-05 18:38:52 +01:00
"os"
2022-01-16 22:46:01 +01:00
"strconv"
"strings"
2022-01-05 18:38:52 +01:00
2022-01-14 19:35:10 +01:00
"github.com/dchest/uniuri"
2022-01-05 21:08:27 +01:00
"github.com/gin-gonic/gin"
"github.com/syssecfsu/witty/term_conn"
2022-01-04 21:41:41 +01:00
)
2022-01-09 14:56:43 +01:00
// command line options
2022-01-07 02:53:36 +01:00
var cmdToExec = []string{"bash"}
2022-01-07 02:21:11 +01:00
2022-01-10 23:16:22 +01:00
var host *string = nil
// simple function to check origin
func checkOrigin(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)
}
2022-01-12 03:18:19 +01:00
type InteractiveSession struct {
2022-01-14 19:35:10 +01:00
Ip string
Cmd string
Id string
2022-01-12 03:18:19 +01:00
}
2022-01-16 22:46:01 +01:00
type RecordedSession struct {
Fname string
Fsize string
Duration string
Time string
}
// how many seconds of the session
func getDuration(fname string) int64 {
fp, err := os.Open("./records/" + fname)
if err != nil {
log.Println("Failed to open record file", err)
return 0
}
decoder := json.NewDecoder(fp)
if decoder == nil {
log.Println("Failed to create JSON decoder")
return 0
}
// To work with javascript decoder, we organize the file as
// an array of writeRecord. golang decode instead decode
// as individual record. Call decoder.Token to skip opening [
decoder.Token()
var dur int64 = 0
for decoder.More() {
var record term_conn.WriteRecord
if err := decoder.Decode(&record); err != nil {
log.Println("Failed to decode record", err)
continue
}
dur += record.Dur.Milliseconds()
}
return dur/1000 + 1
}
2022-01-17 21:20:18 +01:00
func collectTabData(c *gin.Context) (players []InteractiveSession, records []RecordedSession) {
2022-01-12 03:18:19 +01:00
term_conn.ForEachSession(func(tc *term_conn.TermConn) {
players = append(players, InteractiveSession{
2022-01-14 19:35:10 +01:00
Id: tc.Name,
Ip: tc.Ip,
Cmd: cmdToExec[0],
2022-01-12 03:18:19 +01:00
})
})
2022-01-16 22:46:01 +01:00
files, err := ioutil.ReadDir("./records/")
2022-01-17 21:20:18 +01:00
2022-01-16 22:46:01 +01:00
if err == nil {
for _, finfo := range files {
fname := finfo.Name()
if !strings.HasSuffix(fname, ".rec") {
continue
}
2022-01-17 12:55:58 +01:00
fsize := finfo.Size() / 1024
2022-01-16 22:46:01 +01:00
duration := getDuration(fname)
records = append(records,
RecordedSession{
Fname: fname,
Fsize: strconv.FormatInt(fsize, 10),
Duration: strconv.FormatInt(duration, 10),
Time: finfo.ModTime().Format("Jan/2/2006, 15:04:05"),
})
}
}
2022-01-17 21:20:18 +01:00
return
2022-01-12 03:18:19 +01:00
}
2022-01-05 03:21:21 +01:00
func main() {
2022-01-12 04:46:56 +01:00
fp, err := os.OpenFile("witty.log", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err == nil {
defer fp.Close()
log.SetOutput(fp)
gin.DefaultWriter = fp
}
2022-01-07 02:21:11 +01:00
// parse the arguments. User can pass the command to execute
// by default, we use bash, but macos users might want to use zsh
// you can also run single program, such as pstree, htop...
// but program might misbehave (htop seems to be fine)
args := os.Args
if len(args) > 1 {
2022-01-07 02:53:36 +01:00
cmdToExec = args[1:]
2022-01-07 02:21:11 +01:00
log.Println(cmdToExec)
}
2022-01-04 21:41:41 +01:00
rt := gin.Default()
rt.SetTrustedProxies(nil)
2022-01-17 21:20:18 +01:00
rt.LoadHTMLGlob("./assets/template/*")
2022-01-04 21:41:41 +01:00
2022-01-14 19:35:10 +01:00
// Fill in the index page
rt.GET("/", func(c *gin.Context) {
host = &c.Request.Host
2022-01-17 21:20:18 +01:00
players, records := collectTabData(c)
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "interactive terminal",
"players": players,
"records": records,
})
})
2022-01-18 19:06:25 +01:00
rt.GET("/favicon.ico", func(c *gin.Context) {
c.File("./assets/img/favicon.ico")
})
2022-01-17 21:20:18 +01:00
// to update the tabs of current interactive and saved sessions
rt.GET("/update/:active", func(c *gin.Context) {
var active0, active1 string
// setup which tab is active, it is hard to do in javascript at
// client side due to timing issues.
which := c.Param("active")
if which == "0" {
active0 = "active"
active1 = ""
} else {
active0 = ""
active1 = "active"
}
host = &c.Request.Host
players, records := collectTabData(c)
c.HTML(http.StatusOK, "tab.html", gin.H{
"players": players,
"records": records,
"active0": active0,
"active1": active1,
})
2022-01-09 14:56:43 +01:00
})
2022-01-14 19:35:10 +01:00
// create a new interactive session
2022-01-11 21:22:24 +01:00
rt.GET("/new", func(c *gin.Context) {
if host == nil {
host = &c.Request.Host
}
2022-01-14 19:35:10 +01:00
id := uniuri.New()
2022-01-11 21:22:24 +01:00
c.HTML(http.StatusOK, "term.html", gin.H{
2022-01-12 03:59:58 +01:00
"title": "interactive terminal",
2022-01-14 19:35:10 +01:00
"path": "/ws_new/" + id,
"id": id,
2022-01-17 12:55:58 +01:00
"logo": "keyboard",
2022-01-11 21:22:24 +01:00
})
})
2022-01-14 19:35:10 +01:00
rt.GET("/ws_new/:id", func(c *gin.Context) {
id := c.Param("id")
term_conn.ConnectTerm(c.Writer, c.Request, false, id, cmdToExec)
2022-01-09 14:56:43 +01:00
})
2022-01-04 21:41:41 +01:00
2022-01-14 19:35:10 +01:00
// create a viewer of an interactive session
rt.GET("/view/:id", func(c *gin.Context) {
id := c.Param("id")
c.HTML(http.StatusOK, "term.html", gin.H{
"title": "viewer terminal",
"path": "/ws_view/" + id,
2022-01-17 12:55:58 +01:00
"id": id,
"logo": "view",
2022-01-14 19:35:10 +01:00
})
2022-01-09 14:56:43 +01:00
})
2022-01-14 19:35:10 +01:00
rt.GET("/ws_view/:id", func(c *gin.Context) {
id := c.Param("id")
term_conn.ConnectTerm(c.Writer, c.Request, true, id, nil)
})
2022-01-09 14:56:43 +01:00
2022-01-14 19:35:10 +01:00
// start/stop recording the session
rt.GET("/record/:id", func(c *gin.Context) {
id := c.Param("id")
term_conn.StartRecord(id)
2022-01-04 21:41:41 +01:00
})
2022-01-14 19:35:10 +01:00
rt.GET("/stop/:id", func(c *gin.Context) {
id := c.Param("id")
term_conn.StopRecord(id)
})
2022-01-15 13:13:42 +01:00
// create a viewer of an interactive session
2022-01-16 22:46:01 +01:00
rt.GET("/replay/:id", func(c *gin.Context) {
2022-01-15 13:13:42 +01:00
id := c.Param("id")
log.Println("replay/ called with", id)
2022-01-16 22:46:01 +01:00
c.HTML(http.StatusOK, "replay.html", gin.H{
"fname": id,
})
})
rt.GET("/delete/:fname", func(c *gin.Context) {
fname := c.Param("fname")
if err := os.Remove("./records/" + fname); err != nil {
log.Println("Failed to delete file,", err)
}
2022-01-15 13:13:42 +01:00
})
2022-01-14 19:35:10 +01:00
// handle static files
rt.Static("/assets", "./assets")
2022-01-16 00:54:15 +01:00
rt.Static("/records", "./records")
2022-01-14 19:35:10 +01:00
2022-01-10 23:16:22 +01:00
term_conn.Init(checkOrigin)
2022-01-07 15:00:44 +01:00
rt.RunTLS(":8080", "./tls/cert.pem", "./tls/private-key.pem")
2022-01-05 03:21:21 +01:00
}