2015-10-18 23:32:14 +03:00
|
|
|
// Copyright 2015 The Gorilla WebSocket Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-16 04:01:07 +03:00
|
|
|
addr = flag.String("addr", "127.0.0.1:8080", "http service address")
|
|
|
|
cmdPath string
|
2015-10-18 23:32:14 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Time allowed to write a message to the peer.
|
|
|
|
writeWait = 10 * time.Second
|
|
|
|
|
|
|
|
// Maximum message size allowed from peer.
|
|
|
|
maxMessageSize = 8192
|
2015-10-19 04:17:26 +03:00
|
|
|
|
|
|
|
// Time allowed to read the next pong message from the peer.
|
|
|
|
pongWait = 60 * time.Second
|
|
|
|
|
|
|
|
// Send pings to peer with this period. Must be less than pongWait.
|
|
|
|
pingPeriod = (pongWait * 9) / 10
|
2016-10-28 07:05:23 +03:00
|
|
|
|
|
|
|
// Time to wait before force close on connection.
|
|
|
|
closeGracePeriod = 10 * time.Second
|
2015-10-18 23:32:14 +03:00
|
|
|
)
|
|
|
|
|
2015-10-19 01:32:45 +03:00
|
|
|
func pumpStdin(ws *websocket.Conn, w io.Writer) {
|
|
|
|
defer ws.Close()
|
|
|
|
ws.SetReadLimit(maxMessageSize)
|
2015-10-19 04:17:26 +03:00
|
|
|
ws.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
2015-10-18 23:32:14 +03:00
|
|
|
for {
|
2015-10-19 01:32:45 +03:00
|
|
|
_, message, err := ws.ReadMessage()
|
2015-10-18 23:32:14 +03:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
message = append(message, '\n')
|
2015-10-19 01:32:45 +03:00
|
|
|
if _, err := w.Write(message); err != nil {
|
2015-10-18 23:32:14 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 01:32:45 +03:00
|
|
|
func pumpStdout(ws *websocket.Conn, r io.Reader, done chan struct{}) {
|
2024-06-14 05:53:52 +03:00
|
|
|
defer func() {
|
|
|
|
}()
|
2015-10-19 01:32:45 +03:00
|
|
|
s := bufio.NewScanner(r)
|
2015-10-18 23:32:14 +03:00
|
|
|
for s.Scan() {
|
2015-10-19 01:32:45 +03:00
|
|
|
ws.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
if err := ws.WriteMessage(websocket.TextMessage, s.Bytes()); err != nil {
|
2016-10-28 07:05:23 +03:00
|
|
|
ws.Close()
|
2015-10-18 23:32:14 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.Err() != nil {
|
|
|
|
log.Println("scan:", s.Err())
|
|
|
|
}
|
2016-10-28 07:05:23 +03:00
|
|
|
close(done)
|
|
|
|
|
|
|
|
ws.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
|
|
|
time.Sleep(closeGracePeriod)
|
|
|
|
ws.Close()
|
2015-10-19 04:17:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func ping(ws *websocket.Conn, done chan struct{}) {
|
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(writeWait)); err != nil {
|
|
|
|
log.Println("ping:", err)
|
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-10-18 23:32:14 +03:00
|
|
|
}
|
|
|
|
|
2015-10-19 01:32:45 +03:00
|
|
|
func internalError(ws *websocket.Conn, msg string, err error) {
|
|
|
|
log.Println(msg, err)
|
2015-10-18 23:32:14 +03:00
|
|
|
ws.WriteMessage(websocket.TextMessage, []byte("Internal server error."))
|
|
|
|
}
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{}
|
|
|
|
|
|
|
|
func serveWs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ws, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
2015-10-19 01:32:45 +03:00
|
|
|
log.Println("upgrade:", err)
|
2015-10-18 23:32:14 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-19 01:32:45 +03:00
|
|
|
defer ws.Close()
|
2015-10-18 23:32:14 +03:00
|
|
|
|
2015-10-19 01:32:45 +03:00
|
|
|
outr, outw, err := os.Pipe()
|
2015-10-18 23:32:14 +03:00
|
|
|
if err != nil {
|
2015-10-19 01:32:45 +03:00
|
|
|
internalError(ws, "stdout:", err)
|
2015-10-18 23:32:14 +03:00
|
|
|
return
|
|
|
|
}
|
2015-10-19 01:32:45 +03:00
|
|
|
defer outr.Close()
|
|
|
|
defer outw.Close()
|
2015-10-18 23:32:14 +03:00
|
|
|
|
2015-10-19 01:32:45 +03:00
|
|
|
inr, inw, err := os.Pipe()
|
2015-10-18 23:32:14 +03:00
|
|
|
if err != nil {
|
2015-10-19 01:32:45 +03:00
|
|
|
internalError(ws, "stdin:", err)
|
2015-10-18 23:32:14 +03:00
|
|
|
return
|
|
|
|
}
|
2015-10-19 01:32:45 +03:00
|
|
|
defer inr.Close()
|
|
|
|
defer inw.Close()
|
2015-10-18 23:32:14 +03:00
|
|
|
|
2015-10-19 01:32:45 +03:00
|
|
|
proc, err := os.StartProcess(cmdPath, flag.Args(), &os.ProcAttr{
|
|
|
|
Files: []*os.File{inr, outw, outw},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
internalError(ws, "start:", err)
|
2015-10-18 23:32:14 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-19 01:32:45 +03:00
|
|
|
inr.Close()
|
|
|
|
outw.Close()
|
|
|
|
|
2015-10-19 04:17:26 +03:00
|
|
|
stdoutDone := make(chan struct{})
|
|
|
|
go pumpStdout(ws, outr, stdoutDone)
|
|
|
|
go ping(ws, stdoutDone)
|
2015-10-19 01:32:45 +03:00
|
|
|
|
|
|
|
pumpStdin(ws, inw)
|
|
|
|
|
|
|
|
// Some commands will exit when stdin is closed.
|
|
|
|
inw.Close()
|
2015-10-18 23:32:14 +03:00
|
|
|
|
2015-10-19 04:17:26 +03:00
|
|
|
// Other commands need a bonk on the head.
|
2015-10-19 01:32:45 +03:00
|
|
|
if err := proc.Signal(os.Interrupt); err != nil {
|
|
|
|
log.Println("inter:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-10-19 04:17:26 +03:00
|
|
|
case <-stdoutDone:
|
2015-10-19 01:32:45 +03:00
|
|
|
case <-time.After(time.Second):
|
|
|
|
// A bigger bonk on the head.
|
|
|
|
if err := proc.Signal(os.Kill); err != nil {
|
|
|
|
log.Println("term:", err)
|
|
|
|
}
|
2015-10-19 04:17:26 +03:00
|
|
|
<-stdoutDone
|
2015-10-19 01:32:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := proc.Wait(); err != nil {
|
2015-10-18 23:32:14 +03:00
|
|
|
log.Println("wait:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveHome(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/" {
|
2018-02-26 06:30:15 +03:00
|
|
|
http.Error(w, "Not found", http.StatusNotFound)
|
2015-10-18 23:32:14 +03:00
|
|
|
return
|
|
|
|
}
|
2021-12-19 19:21:45 +03:00
|
|
|
if r.Method != http.MethodGet {
|
2018-02-26 06:30:15 +03:00
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
2015-10-18 23:32:14 +03:00
|
|
|
return
|
|
|
|
}
|
2017-02-16 04:01:07 +03:00
|
|
|
http.ServeFile(w, r, "home.html")
|
2015-10-18 23:32:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
if len(flag.Args()) < 1 {
|
|
|
|
log.Fatal("must specify at least one argument")
|
|
|
|
}
|
2015-10-19 01:32:45 +03:00
|
|
|
var err error
|
|
|
|
cmdPath, err = exec.LookPath(flag.Args()[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-10-18 23:32:14 +03:00
|
|
|
http.HandleFunc("/", serveHome)
|
|
|
|
http.HandleFunc("/ws", serveWs)
|
2024-04-04 16:32:47 +03:00
|
|
|
log.Fatal(http.ListenAndServe(*addr, nil))
|
2015-10-18 23:32:14 +03:00
|
|
|
}
|