witty/main.go

123 lines
2.4 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-05 21:08:27 +01:00
"github.com/gin-gonic/gin"
2022-01-10 23:16:22 +01:00
"github.com/syssecfsu/web_terminal/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 {
Ip string
Cmd string
Name string
}
func fillIndex(c *gin.Context) {
var players []InteractiveSession
term_conn.ForEachSession(func(tc *term_conn.TermConn) {
players = append(players, InteractiveSession{
Name: tc.Name,
Ip: tc.Ip,
Cmd: cmdToExec[0],
})
})
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
"path": "/ws_do",
"players": players,
})
}
2022-01-05 03:21:21 +01:00
func main() {
2022-01-06 21:55:29 +01:00
fp, err := os.OpenFile("web_term.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-12 03:18:19 +01:00
rt.GET("/view/:sname", func(c *gin.Context) {
sname := c.Param("sname")
2022-01-11 21:22:24 +01:00
c.HTML(http.StatusOK, "term.html", gin.H{
2022-01-12 03:58:43 +01:00
"title": "viewer terminal",
2022-01-12 03:18:19 +01:00
"path": "/ws_view/" + sname,
2022-01-09 14:56:43 +01:00
})
})
2022-01-11 21:22:24 +01:00
rt.GET("/new", func(c *gin.Context) {
if host == nil {
host = &c.Request.Host
}
c.HTML(http.StatusOK, "term.html", gin.H{
2022-01-12 03:59:58 +01:00
"title": "interactive terminal",
2022-01-11 21:22:24 +01:00
"path": "/ws_do",
})
})
2022-01-10 04:04:23 +01:00
rt.GET("/ws_do", func(c *gin.Context) {
2022-01-12 03:18:19 +01:00
term_conn.ConnectTerm(c.Writer, c.Request, false, "", cmdToExec)
2022-01-09 14:56:43 +01:00
})
2022-01-04 21:41:41 +01:00
2022-01-12 03:18:19 +01:00
rt.GET("/ws_view/:sname", func(c *gin.Context) {
path := c.Param("sname")
term_conn.ConnectTerm(c.Writer, c.Request, true, path, nil)
2022-01-09 14:56:43 +01:00
})
// handle static files
rt.Static("/assets", "./assets")
rt.GET("/", func(c *gin.Context) {
2022-01-11 21:22:24 +01:00
host = &c.Request.Host
2022-01-12 03:18:19 +01:00
fillIndex(c)
2022-01-04 21:41:41 +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
}