move relay.go and reg.go to a module

This commit is contained in:
Zhi Wang 2022-01-10 17:16:22 -05:00
parent dd85834715
commit c8d0a05087
3 changed files with 41 additions and 29 deletions

33
main.go
View File

@ -3,14 +3,34 @@ package main
import ( import (
"log" "log"
"net/http" "net/http"
"net/url"
"os" "os"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/syssecfsu/web_terminal/term_conn"
) )
// command line options // command line options
var cmdToExec = []string{"bash"} var cmdToExec = []string{"bash"}
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)
}
func main() { func main() {
fp, err := os.OpenFile("web_term.log", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) fp, err := os.OpenFile("web_term.log", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
@ -31,8 +51,6 @@ func main() {
log.Println(cmdToExec) log.Println(cmdToExec)
} }
registry.init()
rt := gin.Default() rt := gin.Default()
rt.SetTrustedProxies(nil) rt.SetTrustedProxies(nil)
@ -40,17 +58,17 @@ func main() {
rt.GET("/view/*sname", func(c *gin.Context) { rt.GET("/view/*sname", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{ c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Watcher terminal", "title": "Viewer terminal",
"path": "/ws_view", "path": "/ws_view",
}) })
}) })
rt.GET("/ws_do", func(c *gin.Context) { rt.GET("/ws_do", func(c *gin.Context) {
wsHandler(c.Writer, c.Request, false) term_conn.ConnectTerm(c.Writer, c.Request, false, cmdToExec)
}) })
rt.GET("/ws_view", func(c *gin.Context) { rt.GET("/ws_view", func(c *gin.Context) {
wsHandler(c.Writer, c.Request, true) term_conn.ConnectTerm(c.Writer, c.Request, true, nil)
}) })
// handle static files // handle static files
@ -58,11 +76,14 @@ func main() {
rt.GET("/", func(c *gin.Context) { rt.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{ c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Master terminal", "title": "Interactive terminal",
"path": "/ws_do", "path": "/ws_do",
}) })
host = &c.Request.Host host = &c.Request.Host
}) })
term_conn.Init(checkOrigin)
rt.RunTLS(":8080", "./tls/cert.pem", "./tls/private-key.pem") rt.RunTLS(":8080", "./tls/cert.pem", "./tls/private-key.pem")
} }

View File

@ -1,4 +1,4 @@
package main package term_conn
import ( import (
"errors" "errors"

View File

@ -1,10 +1,9 @@
//This file contains code to relay traffic between websocket and pty //This file contains code to relay traffic between websocket and pty
package main package term_conn
import ( import (
"log" "log"
"net/http" "net/http"
"net/url"
"os" "os"
"os/exec" "os/exec"
"sync" "sync"
@ -35,24 +34,11 @@ const (
closeGracePeriod = 10 * time.Second closeGracePeriod = 10 * time.Second
) )
var host *string = nil
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
ReadBufferSize: readBufferSize, ReadBufferSize: readBufferSize,
WriteBufferSize: WriteBufferSize, WriteBufferSize: WriteBufferSize,
CheckOrigin: func(r *http.Request) bool { CheckOrigin: func(r *http.Request) bool {
org := r.Header.Get("Origin") return true
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)
}, },
} }
@ -310,7 +296,7 @@ func (tc *TermConn) release() {
} }
// handle websockets // handle websockets
func wsHandlePlayer(w http.ResponseWriter, r *http.Request) { func handlePlayer(w http.ResponseWriter, r *http.Request, cmdline []string) {
ws, err := upgrader.Upgrade(w, r, nil) ws, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
@ -330,7 +316,7 @@ func wsHandlePlayer(w http.ResponseWriter, r *http.Request) {
tc.pty_done = make(chan struct{}) tc.pty_done = make(chan struct{})
tc.vchan = make(chan *websocket.Conn) tc.vchan = make(chan *websocket.Conn)
if err := tc.createPty(cmdToExec); err != nil { if err := tc.createPty(cmdline); err != nil {
log.Println("Failed to create PTY: ", err) log.Println("Failed to create PTY: ", err)
return return
} }
@ -353,7 +339,7 @@ func wsHandlePlayer(w http.ResponseWriter, r *http.Request) {
} }
// handle websockets // handle websockets
func wsHandleViewer(w http.ResponseWriter, r *http.Request) { func handleViewer(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil) ws, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
@ -368,10 +354,15 @@ func wsHandleViewer(w http.ResponseWriter, r *http.Request) {
} }
} }
func wsHandler(w http.ResponseWriter, r *http.Request, isViewer bool) { func ConnectTerm(w http.ResponseWriter, r *http.Request, isViewer bool, cmdline []string) {
if !isViewer { if !isViewer {
wsHandlePlayer(w, r) handlePlayer(w, r, cmdline)
} else { } else {
wsHandleViewer(w, r) handleViewer(w, r)
} }
} }
func Init(checkOrigin func(r *http.Request) bool) {
upgrader.CheckOrigin = checkOrigin
registry.init()
}