Commit 6edf5b45 by Iwasaki Yudai

Merge pull request #64 from QuentinPerez/handle_arguments

Added handling of —permit-arguments option
2 parents 7715f935 a4e77b2b
......@@ -66,6 +66,7 @@ By default, GoTTY starts a web server at port 8080. Open the URL on your web bro
--once Accept only one client and exit on disconnection [$GOTTY_ONCE]
--config "~/.gotty" Config file path [$GOTTY_CONFIG]
--version, -v print the version
--permit-arguments Allow to send arguments like this http://exemple.com:8080/?arg=AAA&arg=BBB
```
......
......@@ -5,6 +5,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"log"
......@@ -26,6 +27,11 @@ import (
"github.com/kr/pty"
)
type InitMessage struct {
Arguments string `json:"Arguments,omitempty"`
AuthToken string `json:"AuthToken,omitempty"`
}
type App struct {
command []string
options *Options
......@@ -54,6 +60,7 @@ type Options struct {
EnableReconnect bool `hcl:"enable_reconnect"`
ReconnectTime int `hcl:"reconnect_time"`
Once bool `hcl:"once"`
PermitArguments bool `hcl:"permit_arguments"`
Preferences map[string]interface{} `hcl:"preferences"`
}
......@@ -272,14 +279,42 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
return
}
_, initMessage, err := conn.ReadMessage()
if err != nil || string(initMessage) != app.options.Credential {
_, stream, err := conn.ReadMessage()
if err != nil {
log.Print("Failed to authenticate websocket connection")
conn.Close()
return
}
var init InitMessage
cmd := exec.Command(app.command[0], app.command[1:]...)
err = json.Unmarshal(stream, &init)
if err != nil {
log.Printf("Failed to parse init message %v", err)
conn.Close()
return
}
if init.AuthToken != app.options.Credential {
log.Print("Failed to authenticate websocket connection")
conn.Close()
return
}
argv := app.command[1:]
if app.options.PermitArguments {
if init.Arguments == "" {
init.Arguments = "?"
}
query, err := url.Parse(init.Arguments)
if err != nil {
log.Print("Failed to parse arguments")
conn.Close()
return
}
params := query.Query()["arg"]
if len(params) != 0 {
argv = append(argv, params...)
}
}
cmd := exec.Command(app.command[0], argv...)
ptyIo, err := pty.Start(cmd)
if err != nil {
log.Print("Failed to execute command")
......
......@@ -34,6 +34,7 @@ func main() {
flag{"reconnect", "", "Enable reconnection"},
flag{"reconnect-time", "", "Time to reconnect"},
flag{"once", "", "Accept only one client and exit on disconnection"},
flag{"permit-arguments", "", "Allow to send arguments like this http://exemple.com:8080/?arg=AAA&arg=BBB"},
}
mappingHint := map[string]string{
......
(function() {
var httpsEnabled = window.location.protocol == "https:";
var args = window.location.search;
var url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws';
var protocols = ["gotty"];
var autoReconnect = -1;
......@@ -12,8 +13,7 @@
var pingTimer;
ws.onopen = function(event) {
ws.send(gotty_auth_token);
ws.send(JSON.stringify({ Arguments: args, AuthToken: gotty_auth_token,}));
pingTimer = setInterval(sendPing, 30 * 1000, ws);
hterm.defaultStorage = new lib.Storage.Local();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!