From a4e77b2b76a7b3d3c0380bbd4302035770c407d1 Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Mon, 5 Oct 2015 09:50:13 +0200 Subject: [PATCH] =?UTF-8?q?Added=20handling=20of=20=E2=80=94permit-argumen?= =?UTF-8?q?ts=20option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + app/app.go | 41 ++++++++++++++++++++++++++++++++++++++--- main.go | 1 + resources/gotty.js | 4 ++-- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 468205c..de41195 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/app/app.go b/app/app.go index 61b60dd..aaab79c 100644 --- a/app/app.go +++ b/app/app.go @@ -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") diff --git a/main.go b/main.go index cfffb8c..50b8141 100644 --- a/main.go +++ b/main.go @@ -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{ diff --git a/resources/gotty.js b/resources/gotty.js index 1decd5d..35aada1 100644 --- a/resources/gotty.js +++ b/resources/gotty.js @@ -1,5 +1,6 @@ (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();