diff --git a/go.mod b/go.mod index c4a75d3..d22c96e 100644 --- a/go.mod +++ b/go.mod @@ -7,3 +7,5 @@ require ( github.com/umpc/go-sortedmap v0.0.0-20180422175548-64ab94c482f4 golang.org/x/crypto v0.29.0 ) + +require github.com/gorilla/websocket v1.5.3 // indirect diff --git a/go.sum b/go.sum index 2d8b795..c4b8858 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/elliotchance/orderedmap/v2 v2.4.0 h1:6tUmMwD9F998FNpwFxA5E6NQvSpk2PVw7RKsVq3+2Cw= github.com/elliotchance/orderedmap/v2 v2.4.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= diff --git a/main.go b/main.go index 5c8e298..49102d4 100644 --- a/main.go +++ b/main.go @@ -4,14 +4,17 @@ import ( "crypto/rand" "database/sql" "fmt" + "io" "log" "net/http" "os" + "os/exec" "strconv" "text/template" "time" "wargh/db" + "github.com/gorilla/websocket" "golang.org/x/crypto/bcrypt" ) @@ -41,6 +44,20 @@ var ERROR_TEXT = []string{ "Unathorized", } +var wsUpgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, +} + +type Job struct { + Command *exec.Cmd + StdoutPipe io.ReadCloser + StderrPipe io.ReadCloser + Socket []*websocket.Conn +} + +var jobs map[string]Job = make(map[string]Job) + func errorHandler(w http.ResponseWriter, r *http.Request) { errorParam := r.URL.Query().Get("error") var errorCode int @@ -226,6 +243,50 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { } } +func createJob() (string, error) { + cmd := exec.Command("/usr/bin/sh", "-c", "while true; do echo $(date) \"test\"") + + stdout, err := cmd.StdoutPipe() + if err != nil { + return "", err + } + + stderr, err := cmd.StderrPipe() + if err != nil { + return "", err + } + + err = cmd.Start() + + jobId := randString(64) + jobs[jobId] = Job{ + Command: cmd, + StdoutPipe: stdout, + StderrPipe: stderr, + Socket: make([]*websocket.Conn, 0), + } + return jobId, err +} + +func wsHandler(w http.ResponseWriter, r *http.Request) { + if !checkSession(w, r) { + return + } + + // get job id and attach + + ws, err := wsUpgrader.Upgrade(w, r, nil) + if err != nil { + log.Print(err) + + redirectError(w, r, ERROR_UNKNOWN) + return + } + + ws.WriteMessage(websocket.TextMessage, ([]byte)("test")) + ws.Close() +} + func main() { db.Init(&db.DBConfig{ DBPath: "wargh.db", @@ -252,6 +313,7 @@ func main() { http.HandleFunc("/", indexHandler) http.HandleFunc("/error", errorHandler) http.HandleFunc("/login", loginHandler) + http.HandleFunc("/ws", wsHandler) log.Fatal(http.ListenAndServe(":8080", nil)) }