2019-01-06 06:44:33 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2020-06-01 07:12:36 +08:00
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/runner"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
WSWriteDeadline = 10 * time.Second
|
2019-01-06 06:44:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
cmdNotAllowed = []byte("Command not allowed.")
|
|
|
|
)
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
func wsErr(ws *websocket.Conn, r *http.Request, status int, err error) { //nolint:unparam
|
2019-01-06 06:44:33 +08:00
|
|
|
txt := http.StatusText(status)
|
|
|
|
if err != nil || status >= 400 {
|
|
|
|
log.Printf("%s: %v %s %v", r.URL.Path, status, r.RemoteAddr, err)
|
|
|
|
}
|
2020-06-01 07:12:36 +08:00
|
|
|
if err := ws.WriteControl(websocket.CloseInternalServerErr, []byte(txt), time.Now().Add(WSWriteDeadline)); err != nil { //nolint:shadow
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var commandsHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
var raw string
|
|
|
|
|
|
|
|
for {
|
2020-06-01 07:12:36 +08:00
|
|
|
_, msg, err := conn.ReadMessage() //nolint:shadow
|
2019-01-06 06:44:33 +08:00
|
|
|
if err != nil {
|
|
|
|
wsErr(conn, r, http.StatusInternalServerError, err)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw = strings.TrimSpace(string(msg))
|
|
|
|
if raw != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !d.user.CanExecute(strings.Split(raw, " ")[0]) {
|
2020-06-01 07:12:36 +08:00
|
|
|
if err := conn.WriteMessage(websocket.TextMessage, cmdNotAllowed); err != nil { //nolint:shadow
|
2019-01-06 06:44:33 +08:00
|
|
|
wsErr(conn, r, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
command, err := runner.ParseCommand(d.settings, raw)
|
|
|
|
if err != nil {
|
2020-06-01 07:12:36 +08:00
|
|
|
if err := conn.WriteMessage(websocket.TextMessage, []byte(err.Error())); err != nil { //nolint:shadow
|
2019-01-06 06:44:33 +08:00
|
|
|
wsErr(conn, r, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
cmd := exec.Command(command[0], command[1:]...) //nolint:gosec
|
2019-01-06 06:44:33 +08:00
|
|
|
cmd.Dir = d.user.FullPath(r.URL.Path)
|
|
|
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
wsErr(conn, r, http.StatusInternalServerError, err)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
wsErr(conn, r, http.StatusInternalServerError, err)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
wsErr(conn, r, http.StatusInternalServerError, err)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s := bufio.NewScanner(io.MultiReader(stdout, stderr))
|
|
|
|
for s.Scan() {
|
2020-06-01 07:12:36 +08:00
|
|
|
if err := conn.WriteMessage(websocket.TextMessage, s.Bytes()); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
wsErr(conn, r, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
})
|