general improvements; updates on #32
This commit is contained in:
@@ -2,7 +2,6 @@ package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -18,167 +17,120 @@ var upgrader = websocket.Upgrader{
|
||||
WriteBufferSize: 1024,
|
||||
}
|
||||
|
||||
var (
|
||||
cmdNotImplemented = []byte("Command not implemented.")
|
||||
cmdNotAllowed = []byte("Command not allowed.")
|
||||
)
|
||||
|
||||
// Command handles the requests for VCS related commands: git, svn and mercurial
|
||||
func Command(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User) (int, error) {
|
||||
// Upgrades the connection to a websocket and checks for errors.
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return 0, nil
|
||||
return 0, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
var (
|
||||
message []byte
|
||||
command []string
|
||||
)
|
||||
|
||||
// Starts an infinite loop until a valid command is captured.
|
||||
for {
|
||||
_, message, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
fmt.Println("read:", err)
|
||||
break
|
||||
}
|
||||
|
||||
command := strings.Split(string(message), " ")
|
||||
|
||||
if len(command) == 0 {
|
||||
continue
|
||||
}
|
||||
// Check if the command is allowed
|
||||
mayContinue := false
|
||||
|
||||
for _, cmd := range u.Commands {
|
||||
if cmd == command[0] {
|
||||
mayContinue = true
|
||||
}
|
||||
}
|
||||
|
||||
if !mayContinue {
|
||||
err = conn.WriteMessage(websocket.BinaryMessage, []byte("FORBIDDEN"))
|
||||
if err != nil {
|
||||
fmt.Println("write:", err)
|
||||
break
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// Check if the program is talled is installed on the computer
|
||||
if _, err = exec.LookPath(command[0]); err != nil {
|
||||
err = conn.WriteMessage(websocket.BinaryMessage, []byte("Command not implemented."))
|
||||
if err != nil {
|
||||
fmt.Println("write:", err)
|
||||
break
|
||||
}
|
||||
|
||||
return http.StatusNotImplemented, nil
|
||||
}
|
||||
|
||||
path := strings.Replace(r.URL.Path, c.BaseURL, c.Scope, 1)
|
||||
path = filepath.Clean(path)
|
||||
|
||||
buff := new(bytes.Buffer)
|
||||
|
||||
cmd := exec.Command(command[0], command[1:len(command)]...)
|
||||
cmd.Dir = path
|
||||
cmd.Stderr = buff
|
||||
cmd.Stdout = buff
|
||||
err = cmd.Start()
|
||||
_, message, err = conn.ReadMessage()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
done := false
|
||||
go func() {
|
||||
err = cmd.Wait()
|
||||
done = true
|
||||
}()
|
||||
|
||||
for !done {
|
||||
by := buff.Bytes()
|
||||
if len(by) > 0 {
|
||||
err = conn.WriteMessage(websocket.TextMessage, by)
|
||||
if err != nil {
|
||||
fmt.Println("write:", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
command = strings.Split(string(message), " ")
|
||||
if len(command) != 0 {
|
||||
break
|
||||
}
|
||||
|
||||
by := buff.Bytes()
|
||||
if len(by) > 0 {
|
||||
err = conn.WriteMessage(websocket.TextMessage, by)
|
||||
if err != nil {
|
||||
fmt.Println("write:", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
/* command := strings.Split(r.Header.Get("command"), " ")
|
||||
|
||||
// Check if the command is allowed
|
||||
mayContinue := false
|
||||
allowed := false
|
||||
|
||||
for _, cmd := range u.Commands {
|
||||
if cmd == command[0] {
|
||||
mayContinue = true
|
||||
allowed = true
|
||||
}
|
||||
}
|
||||
|
||||
if !mayContinue {
|
||||
return http.StatusForbidden, nil
|
||||
if !allowed {
|
||||
err = conn.WriteMessage(websocket.BinaryMessage, cmdNotAllowed)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// Check if the program is talled is installed on the computer
|
||||
if _, err := exec.LookPath(command[0]); err != nil {
|
||||
// Check if the program is talled is installed on the computer.
|
||||
if _, err = exec.LookPath(command[0]); err != nil {
|
||||
err = conn.WriteMessage(websocket.BinaryMessage, cmdNotImplemented)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return http.StatusNotImplemented, nil
|
||||
}
|
||||
|
||||
// Gets the path and initializes a buffer.
|
||||
path := strings.Replace(r.URL.Path, c.BaseURL, c.Scope, 1)
|
||||
path = filepath.Clean(path)
|
||||
buff := new(bytes.Buffer)
|
||||
|
||||
cmd := exec.Command(command[0], command[1:len(command)]...)
|
||||
// Sets up the command executation.
|
||||
cmd := exec.Command(command[0], command[1:]...)
|
||||
cmd.Dir = path
|
||||
cmd.Stderr = w
|
||||
cmd.Stdout = w
|
||||
cmd.Start()
|
||||
cmd.Stderr = buff
|
||||
cmd.Stdout = buff
|
||||
|
||||
/*cmd.Stderr = b
|
||||
cmd.Stdout = b
|
||||
|
||||
// Starts the comamnd
|
||||
err := cmd.Start()
|
||||
// Starts the command and checks for errors.
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
// Set a 'done' variable to check whetever the command has already finished
|
||||
// running or not. This verification is done using a goroutine that uses the
|
||||
// method .Wait() from the command.
|
||||
done := false
|
||||
go func() {
|
||||
err = cmd.Wait()
|
||||
done = true
|
||||
}()
|
||||
|
||||
for !done {
|
||||
by := b.Bytes()
|
||||
// Function to print the current information on the buffer to the connection.
|
||||
print := func() error {
|
||||
by := buff.Bytes()
|
||||
if len(by) > 0 {
|
||||
fmt.Println(string(by))
|
||||
err = conn.WriteMessage(websocket.TextMessage, by)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
//w.Write(by)
|
||||
return nil
|
||||
}
|
||||
|
||||
}*/
|
||||
// While the command hasn't finished running, continue sending the output
|
||||
// to the client in intervals of 100 milliseconds.
|
||||
for !done {
|
||||
if err = print(); err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
//out, err := cmd.CombinedOutput()
|
||||
//fmt.Println(string(out))
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
//if err != nil {
|
||||
// return http.StatusInternalServerError, err
|
||||
//}
|
||||
// After the command is done executing, send the output one more time to the
|
||||
// browser to make sure it gets the latest information.
|
||||
if err = print(); err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
/* cmd.Wait()
|
||||
|
||||
//p := &page.Page{Info: &page.Info{Data: string(output)}} */
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user