Added handling of —permit-arguments option

This commit is contained in:
Quentin Perez 2015-10-05 09:50:13 +02:00
parent 7715f93517
commit a4e77b2b76
4 changed files with 42 additions and 5 deletions

View File

@ -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] --once Accept only one client and exit on disconnection [$GOTTY_ONCE]
--config "~/.gotty" Config file path [$GOTTY_CONFIG] --config "~/.gotty" Config file path [$GOTTY_CONFIG]
--version, -v print the version --version, -v print the version
--permit-arguments Allow to send arguments like this http://exemple.com:8080/?arg=AAA&arg=BBB
``` ```

View File

@ -5,6 +5,7 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"encoding/base64" "encoding/base64"
"encoding/json"
"errors" "errors"
"io/ioutil" "io/ioutil"
"log" "log"
@ -26,6 +27,11 @@ import (
"github.com/kr/pty" "github.com/kr/pty"
) )
type InitMessage struct {
Arguments string `json:"Arguments,omitempty"`
AuthToken string `json:"AuthToken,omitempty"`
}
type App struct { type App struct {
command []string command []string
options *Options options *Options
@ -54,6 +60,7 @@ type Options struct {
EnableReconnect bool `hcl:"enable_reconnect"` EnableReconnect bool `hcl:"enable_reconnect"`
ReconnectTime int `hcl:"reconnect_time"` ReconnectTime int `hcl:"reconnect_time"`
Once bool `hcl:"once"` Once bool `hcl:"once"`
PermitArguments bool `hcl:"permit_arguments"`
Preferences map[string]interface{} `hcl:"preferences"` Preferences map[string]interface{} `hcl:"preferences"`
} }
@ -272,14 +279,42 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
return return
} }
_, initMessage, err := conn.ReadMessage() _, stream, err := conn.ReadMessage()
if err != nil || string(initMessage) != app.options.Credential { if err != nil {
log.Print("Failed to authenticate websocket connection") log.Print("Failed to authenticate websocket connection")
conn.Close() conn.Close()
return 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) ptyIo, err := pty.Start(cmd)
if err != nil { if err != nil {
log.Print("Failed to execute command") log.Print("Failed to execute command")

View File

@ -34,6 +34,7 @@ func main() {
flag{"reconnect", "", "Enable reconnection"}, flag{"reconnect", "", "Enable reconnection"},
flag{"reconnect-time", "", "Time to reconnect"}, flag{"reconnect-time", "", "Time to reconnect"},
flag{"once", "", "Accept only one client and exit on disconnection"}, 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{ mappingHint := map[string]string{

View File

@ -1,5 +1,6 @@
(function() { (function() {
var httpsEnabled = window.location.protocol == "https:"; var httpsEnabled = window.location.protocol == "https:";
var args = window.location.search;
var url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws'; var url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws';
var protocols = ["gotty"]; var protocols = ["gotty"];
var autoReconnect = -1; var autoReconnect = -1;
@ -12,8 +13,7 @@
var pingTimer; var pingTimer;
ws.onopen = function(event) { 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); pingTimer = setInterval(sendPing, 30 * 1000, ws);
hterm.defaultStorage = new lib.Storage.Local(); hterm.defaultStorage = new lib.Storage.Local();