2015-08-16 09:47:23 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/elazarl/go-bindata-assetfs"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/kr/pty"
|
2015-08-17 06:46:35 +00:00
|
|
|
"github.com/yudai/utf8reader"
|
2015-08-16 09:47:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
|
|
|
Address string
|
|
|
|
Port string
|
|
|
|
PermitWrite bool
|
|
|
|
Command []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(address string, port string, permitWrite bool, command []string) *App {
|
|
|
|
return &App{
|
|
|
|
Address: address,
|
|
|
|
Port: port,
|
|
|
|
PermitWrite: permitWrite,
|
|
|
|
Command: command,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Run() error {
|
|
|
|
http.Handle("/",
|
|
|
|
http.FileServer(
|
|
|
|
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "bindata"},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
http.HandleFunc("/ws", app.generateHandler())
|
|
|
|
|
|
|
|
url := app.Address + ":" + app.Port
|
|
|
|
log.Printf("Sever is running at %s, command: %s", url, strings.Join(app.Command, " "))
|
|
|
|
err := http.ListenAndServe(url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Printf("New client connected: %s", r.RemoteAddr)
|
|
|
|
|
|
|
|
upgrader := websocket.Upgrader{
|
2015-08-17 02:00:09 +00:00
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
2015-08-16 09:47:23 +00:00
|
|
|
Subprotocols: []string{"gotty"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "Method not allowed", 405)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Failed to upgrade connection")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(app.Command[0], app.Command[1:]...)
|
|
|
|
fio, err := pty.Start(cmd)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Failed to execute command")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
exit := make(chan bool, 2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() { exit <- true }()
|
|
|
|
|
2015-08-17 02:00:09 +00:00
|
|
|
buf := make([]byte, 1024)
|
2015-08-17 06:46:35 +00:00
|
|
|
utf8f := utf8reader.New(fio)
|
2015-08-17 02:00:09 +00:00
|
|
|
|
2015-08-17 06:46:35 +00:00
|
|
|
for {
|
|
|
|
size, err := utf8f.Read(buf)
|
2015-08-16 09:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("command exited for: %s", r.RemoteAddr)
|
|
|
|
return
|
|
|
|
}
|
2015-08-17 02:00:09 +00:00
|
|
|
|
|
|
|
writer, err := conn.NextWriter(websocket.TextMessage)
|
2015-08-16 09:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-08-17 02:00:09 +00:00
|
|
|
|
2015-08-17 06:46:35 +00:00
|
|
|
writer.Write(buf[:size])
|
2015-08-16 09:47:23 +00:00
|
|
|
writer.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() { exit <- true }()
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, data, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch data[0] {
|
|
|
|
case '0':
|
|
|
|
if !app.PermitWrite {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := fio.Write(data[1:])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case '1':
|
|
|
|
var remoteCmd command
|
|
|
|
err = json.Unmarshal(data[1:], &remoteCmd)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Malformed remote command")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch remoteCmd.Name {
|
|
|
|
case "resize_terminal":
|
|
|
|
|
|
|
|
rows := remoteCmd.Arguments["rows"]
|
|
|
|
switch rows.(type) {
|
|
|
|
case float64:
|
|
|
|
default:
|
|
|
|
log.Print("Malformed remote command")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cols := remoteCmd.Arguments["columns"]
|
|
|
|
switch cols.(type) {
|
|
|
|
case float64:
|
|
|
|
default:
|
|
|
|
log.Print("Malformed remote command")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
window := struct {
|
|
|
|
row uint16
|
|
|
|
col uint16
|
|
|
|
x uint16
|
|
|
|
y uint16
|
|
|
|
}{
|
|
|
|
uint16(rows.(float64)),
|
|
|
|
uint16(cols.(float64)),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
}
|
|
|
|
syscall.Syscall(
|
|
|
|
syscall.SYS_IOCTL,
|
|
|
|
fio.Fd(),
|
|
|
|
syscall.TIOCSWINSZ,
|
|
|
|
uintptr(unsafe.Pointer(&window)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Print("Unknown message type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-exit
|
2015-08-17 05:24:24 +00:00
|
|
|
fio.Write([]byte{4})
|
2015-08-16 09:47:23 +00:00
|
|
|
fio.Close()
|
|
|
|
conn.Close()
|
|
|
|
log.Printf("Connection closed: %s", r.RemoteAddr)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type command struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Arguments map[string]interface{} `json:"arguments"`
|
|
|
|
}
|