witty/main.go

141 lines
2.8 KiB
Go
Raw Normal View History

2022-01-04 21:41:41 +01:00
package main
import (
"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-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
}
func fillIndex(c *gin.Context) {
var players []InteractiveSession
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
})
})
c.HTML(http.StatusOK, "index.html", gin.H{
2022-01-12 03:58:43 +01:00
"title": "interactive terminal",
2022-01-12 03:18:19 +01:00
"players": players,
})
}
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-07 15:00:44 +01:00
rt.LoadHTMLGlob("./assets/*.html")
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
fillIndex(c)
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-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-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)
})
// handle static files
rt.Static("/assets", "./assets")
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
}