From 45f65bfc297890e1b68dd72dea52337a45ae25ba Mon Sep 17 00:00:00 2001 From: Iwasaki Yudai Date: Sun, 23 Aug 2015 22:55:06 +0900 Subject: [PATCH] Support profile files to customize hterm --- Makefile | 3 --- README.md | 16 ++++++++++++++++ app/app.go | 36 +++++++++++++++++++++++++++++++++++- app/client_context.go | 10 ++++++++++ app/resource.go | 4 ++-- main.go | 7 +++++++ resources/gotty.js | 9 ++++++++- 7 files changed, 78 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 3f2e201..5e80abd 100644 --- a/Makefile +++ b/Makefile @@ -13,9 +13,6 @@ bindata: bindata/static: bindata mkdir bindata/static -bindata/templates: bindata - mkdir bindata/templates - bindata/static/hterm.js: bindata/static libapps/hterm/js/*.js cd libapps && \ LIBDOT_SEARCH_PATH=`pwd` ./libdot/bin/concat.sh -i ./hterm/concat/hterm_all.concat -o ../bindata/static/hterm.js diff --git a/README.md b/README.md index ff61d69..eee5941 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,28 @@ By default, gotty starts a web server at port 8080. Open the URL on your web bro --permit-write, -w Permit clients to write to the TTY (BE CAREFUL) [$GOTTY_PERMIT_WRITE] --credential, -c Credential for Basic Authentication (ex: user:pass) [$GOTTY_CREDENTIAL] --random-url, -r Add a random string to the URL [$GOTTY_RANDOM_URL] +--profile-file, -f "~/.gotty" Path to profile file [$GOTTY_PROFILE_FILE] --title-format "GoTTY - {{ .Command }} ({{ .Hostname }})" Title format of browser window [$GOTTY_TITLE_FORMAT] --version, -v print the version ``` By default, gotty doesn't allow clients to send any keystrokes or commands except terminal window resizing. When you want to permit clients to write input to the PTY, add the `-w` option. However, accepting input from remote clients is dangerous for most commands. Make sure that only trusted clients can connect to your gotty server when you activate this option. If you need interaction with the PTY, consider starting gotty with tmux or GNU Screen and run your main command on it. +### Profile File + +You can customize your terminal (hterm) by providing a profile file to the `gotty` command, which is a JSON file that has a map of preference keys and values. Gotty loads a profile file at `~/.gotty` by default when it exists. + +The following example makes the font size smaller and the background color a little bit blue. + +```json +{ + "font-size": 5, + "background-color": "rgb(16, 16, 32)" +} +``` + +Available preferences are listed in [the hterm source code](https://chromium.googlesource.com/apps/libapps/+/master/hterm/js/hterm_preference_manager.js) + ## Sharing with Multiple Clients Gotty starts a new process when a new client connects to the server. This means users cannot share a single terminal with others by default. However, you can use terminal multiplexers for sharing a single process with multiple clients. diff --git a/app/app.go b/app/app.go index 9482bf3..439a7c8 100644 --- a/app/app.go +++ b/app/app.go @@ -3,12 +3,16 @@ package app import ( "crypto/rand" "encoding/base64" + "encoding/json" "errors" + "io/ioutil" "log" "math/big" "net" "net/http" + "os" "os/exec" + "os/user" "strconv" "strings" "text/template" @@ -21,7 +25,9 @@ import ( type App struct { options Options - upgrader *websocket.Upgrader + upgrader *websocket.Upgrader + + preferences map[string]interface{} titleTemplate *template.Template } @@ -31,16 +37,42 @@ type Options struct { PermitWrite bool Credential string RandomUrl bool + ProfileFile string TitleFormat string Command []string } +const DefaultProfileFilePath = "~/.gotty" + func New(options Options) (*App, error) { titleTemplate, err := template.New("title").Parse(options.TitleFormat) if err != nil { return nil, errors.New("Title format string syntax error") } + prefString := []byte{} + prefPath := options.ProfileFile + if options.ProfileFile == DefaultProfileFilePath { + usr, _ := user.Current() + prefPath = usr.HomeDir + "/.gotty" + } + if _, err = os.Stat(prefPath); os.IsNotExist(err) { + if options.ProfileFile != DefaultProfileFilePath { + return nil, err + } + } else { + log.Printf("Loading profile path: %s", prefPath) + prefString, _ = ioutil.ReadFile(prefPath) + } + if len(prefString) == 0 { + prefString = []byte(("{}")) + } + var prefMap map[string]interface{} + err = json.Unmarshal(prefString, &prefMap) + if err != nil { + return nil, err + } + return &App{ options: options, @@ -49,6 +81,8 @@ func New(options Options) (*App, error) { WriteBufferSize: 1024, Subprotocols: []string{"gotty"}, }, + + preferences: prefMap, titleTemplate: titleTemplate, }, nil } diff --git a/app/client_context.go b/app/client_context.go index 6825000..e5e2045 100644 --- a/app/client_context.go +++ b/app/client_context.go @@ -30,6 +30,7 @@ const ( const ( Output = '0' SetWindowTitle = '1' + SetPreferences = '2' ) type argResizeTerminal struct { @@ -114,6 +115,15 @@ func (context *clientContext) sendInitialize() error { } writer.Close() + prefs, _ := json.Marshal(context.app.preferences) + writer, err = context.connection.NextWriter(websocket.TextMessage) + if err != nil { + return err + } + writer.Write([]byte{SetPreferences}) + writer.Write(prefs) + writer.Close() + return nil } diff --git a/app/resource.go b/app/resource.go index 1a259f3..a655e46 100644 --- a/app/resource.go +++ b/app/resource.go @@ -71,7 +71,7 @@ func (fi bindataFileInfo) Sys() interface{} { return nil } -var _staticGottyJs = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\x8c\x54\xc1\x8e\xda\x30\x10\xbd\xf3\x15\x56\x2e\x38\x2a\xf2\xc2\xa5\x87\x20\xd4\xc3\xaa\x3d\xb4\x55\x57\x2a\xb4\x7b\x58\xed\xc1\x89\x07\x62\x61\x3c\xc8\x76\x88\xd2\x2a\xff\x5e\x3b\x24\x10\x20\xa0\xce\x21\x72\x66\xde\x7b\xf3\x3c\x76\x42\xd7\x85\xce\x9c\x44\x4d\x63\xf2\x77\x44\x7c\x1c\xb8\x21\xb9\x73\x7b\xfb\x59\xf3\x54\x81\x20\x0b\x52\x4a\x2d\xb0\x64\x0a\x33\x1e\xa0\x6c\x6f\xd0\x61\x86\x8a\x2c\x16\x24\x6a\xb0\x49\x34\x3f\x91\x0b\xe3\x0b\x84\x5e\x68\x7c\x22\xe3\xd2\xda\xe4\xe9\x69\x4c\x92\xb0\x0c\xab\x98\x7c\xb8\x51\xce\xd1\xba\x81\xf4\x9e\xbb\x5c\xf3\x1d\xf8\x92\x27\x8f\xcf\xbd\x3a\x27\xd6\x77\x7c\x8b\x36\xe8\x5c\x15\xbd\x9f\xcb\x65\xc8\x6b\x28\xc9\x2b\xa4\x4b\xcc\xb6\xe0\xa8\x77\x37\x39\xd3\xe2\xf9\xe8\x04\x76\x60\x76\xed\x6b\x69\x19\x6a\xdc\x83\xf6\xf4\xd3\x80\xe0\x00\xda\x75\x53\x0a\x91\x07\x06\x13\xb0\xe6\x85\x72\x4b\x87\x86\x6f\xa0\xed\xa7\x64\xca\xda\x0c\xfb\xee\x77\xa1\x68\x3c\x3a\xf1\x02\xad\xc5\x1d\x25\x56\xfe\x21\x75\x00\xcd\x2f\x51\xde\x45\x57\xfb\x09\x5c\x54\x7d\x3b\x7d\x27\xdd\x16\x24\x7a\x44\x43\x94\xc8\xf6\x85\xcd\x2f\x14\x43\xf8\x3c\xea\xdf\xab\x6f\x50\x59\x67\x70\x0b\x7d\x45\x9f\xb9\x16\x6d\x87\x61\x41\x0b\x1a\x4d\x23\x3f\xff\x00\x9a\x5f\x60\xea\xdb\x16\x01\xbf\x74\x46\xea\x8d\xd7\xbf\x6e\x39\xe4\xe8\xbc\x4b\x2b\xff\x5c\x98\xf2\xa7\x54\xec\xb4\x9d\x10\x83\xa5\x7d\x64\xef\xa6\x10\x22\x9a\x05\xcf\x5f\x97\x2f\x3f\x98\x6d\xfc\xc8\x75\x35\x8c\x0c\x71\x2b\xde\x8f\xd6\x49\xd2\x2d\x26\x0f\xd1\xc1\x6e\xd2\x3c\xef\xe3\xea\xc1\x4a\x7c\x93\x8d\x1f\xce\xfb\x78\xde\xda\x3a\xae\x94\x1f\x72\x8a\xdc\x08\xda\x3b\xa3\xfa\xfa\x52\x09\xc8\xfc\xcd\x74\x40\x05\x66\xc5\xce\x5f\x6a\x96\xa2\xa8\x5a\x46\xdd\xff\x04\x76\x60\xed\xf1\x52\xdf\xff\x0a\x04\x77\xdc\x03\x9a\x3c\x0b\x2f\xcc\x2a\x99\x01\x9d\xf5\x2c\xd8\x52\xba\x2c\xa7\x67\xcc\xdb\xf4\xbd\xaf\x91\x71\x0b\x64\x3c\x1d\x27\x03\x1b\x43\x56\x1a\xe9\xe0\xd7\xea\xcb\xec\x23\x0d\xdc\xab\xeb\x97\x1a\xe0\xdb\xf9\x95\xd4\x6c\x48\xca\x82\x7b\x6d\x7e\x2c\x2b\xe9\x14\xfc\x87\xd6\xf1\x7c\xea\xde\x40\x32\x85\xf6\xf1\x38\x3a\xd3\x36\xc7\xf2\xe5\x00\x46\xf1\x8a\x46\xcf\xa8\x35\x34\x04\xf2\x1c\x14\x44\x34\x21\xba\x50\xaa\xd7\xbe\xe1\x15\xfa\xce\x39\xd6\xa3\x3a\xf6\xbf\x90\x7f\x01\x00\x00\xff\xff\x5d\xa5\x6c\xd7\xae\x05\x00\x00") +var _staticGottyJs = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\x8c\x54\xc1\x8e\x9b\x30\x10\xbd\xe7\x2b\x2c\x2e\x31\x6a\xe4\xdd\xf4\xd0\x43\xa2\x55\x0f\xab\xed\xa1\xad\xba\x55\x93\x76\x0f\xab\x3d\x18\x33\x09\x6e\x1c\x1b\xd9\x66\x51\x5a\xf1\xef\x1d\x13\x48\x08\x61\x51\xe7\x80\x60\xfc\xde\xf3\xf3\xcc\x18\xba\x29\xb4\xf0\xd2\x68\x1a\x93\xbf\x13\x82\xf1\xca\x2d\xc9\xbc\xcf\xdd\x83\xe6\x89\x82\x94\xdc\x91\x52\xea\xd4\x94\x4c\x19\xc1\x03\x94\xe5\xd6\x78\x23\x8c\x22\x77\x77\x24\xaa\xb1\x8b\x68\x79\x22\x17\x16\x17\x08\xbd\xd0\xf8\x48\xa6\xa5\x73\x8b\x9b\x9b\x29\x59\x84\xd7\xf0\x16\x93\x77\x57\xca\x99\x71\x7e\x20\x9d\x73\x9f\x69\xbe\x07\x5c\x42\xf2\xf4\xbc\x57\xeb\xc4\xe1\x8e\xcf\xd1\xd6\x78\x7f\x88\x5e\xce\xcb\x65\xc8\x6b\x28\xc9\x13\x24\x2b\x23\x76\xe0\x29\xba\x9b\x9d\x69\xf1\x72\x72\x02\x7b\xb0\xfb\xe6\xb3\x74\xcc\x68\x93\x83\x46\xfa\xa9\x40\xf0\x0a\xda\xb7\x55\x0a\x91\x05\x06\x4b\x61\xc3\x0b\xe5\x57\xde\x58\xbe\x85\x66\x3f\x25\x13\xd6\x64\xd8\x57\x3c\x85\xa2\xf1\x72\x94\xc7\x84\x02\x6e\x69\xeb\x27\x44\x40\x35\x72\x47\xc6\x1a\x1f\x52\x1f\xb5\x2e\x50\x68\xb6\x5d\xfb\x01\x3c\x3d\x74\x5d\x77\x0d\xb7\x27\x95\x06\x11\x35\x51\x1a\x96\x17\x2e\xbb\x50\x0c\x81\x79\xa3\x7f\xad\xbf\xc0\xc1\x79\x6b\x76\xd0\x55\xc4\x4c\x5f\xb4\xa9\x99\x03\x9d\xd2\xe8\x36\xc2\x36\x05\xd0\xf2\x02\x53\x5d\x6f\x11\xf0\x2b\x6f\xa5\xde\xa2\x7e\x7f\xcb\x21\x47\xe7\x53\x3a\xf9\xe7\xc2\x14\x36\xb3\xd8\x6b\x37\x23\xd6\x94\x6e\xcc\xde\xd5\x42\x88\x68\x1e\x3c\x7f\x5e\x3d\x7e\x63\xae\xf6\x23\x37\x87\x61\x64\x88\x6b\xf1\x6e\x34\x4e\x16\xed\xcb\x6c\x14\x1d\xec\x2e\xea\xe7\xdb\xb8\x6a\x70\x25\xbe\xca\xc6\xa3\xf5\x3e\xf6\x5b\x3b\xcf\x95\xc2\x22\x27\x86\xdb\xb4\x3b\x95\x55\x7f\xa8\x52\x10\x38\x9a\x1e\x68\x6a\x44\xb1\xc7\xd9\x67\x89\x49\x0f\x0d\xa3\xea\xde\x94\x3d\x38\x77\x9c\xfd\xb7\x2f\x4b\xca\x3d\x47\x40\x9d\x67\xe1\x83\x39\x25\x05\xd0\x79\xc7\x82\x2b\xa5\x17\x19\x3d\x63\x9e\x6f\x5f\xba\x1a\x82\x3b\x20\xd3\xdb\xe9\x62\xe0\x60\x86\x95\x56\x7a\xf8\xb9\xfe\x34\xff\x40\x03\xb7\x37\x7e\x89\x05\xbe\x5b\xf6\xa4\xe6\x43\x52\x0e\xfc\x53\xfd\xff\x59\x4b\xaf\xe0\xbf\xb5\xde\xf7\xb4\x72\x0b\x1b\xb0\xa0\x05\x84\x9f\x50\x3d\x5b\x39\xb7\x6e\x50\xf0\x31\xf9\x0d\xc2\xb3\x1d\xce\x3e\xed\xf0\x62\xb6\x31\xf6\x81\x63\x49\x4e\x75\x45\xc8\xd0\x74\xd7\xc6\xb7\xe0\xbf\x23\xd9\xd1\x38\x9c\x21\x40\x67\x5d\x17\xcf\x98\x78\xe9\x5f\xca\xf1\x93\x1d\x27\xaf\xea\xb4\x5a\x28\xe3\xc6\x1b\xdd\xb6\xc3\x65\xa6\x7c\x7c\x05\xab\xf8\x81\x46\xf7\x46\x6b\xa8\x09\xe4\x3e\x28\xa4\xd1\x8c\xe8\x42\xa9\xce\xf6\x35\xaf\xd0\x6f\x4c\x68\x35\xa9\x62\x1a\x4f\xfe\x05\x00\x00\xff\xff\x0d\xc1\x26\x9b\xaf\x06\x00\x00") func staticGottyJsBytes() ([]byte, error) { return bindataRead( @@ -86,7 +86,7 @@ func staticGottyJs() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "static/gotty.js", size: 1454, mode: os.FileMode(436), modTime: time.Unix(1440329892, 0)} + info := bindataFileInfo{name: "static/gotty.js", size: 1711, mode: os.FileMode(436), modTime: time.Unix(1440336972, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/main.go b/main.go index 49ba6af..33e1909 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,12 @@ func main() { Usage: "Add a random string to the URL", EnvVar: "GOTTY_RANDOM_URL", }, + cli.StringFlag{ + Name: "profile-file, f", + Value: app.DefaultProfileFilePath, + Usage: "Path to profile file", + EnvVar: "GOTTY_PROFILE_FILE", + }, cli.StringFlag{ Name: "title-format", Value: "GoTTY - {{ .Command }} ({{ .Hostname }})", @@ -64,6 +70,7 @@ func main() { c.Bool("permit-write"), c.String("credential"), c.Bool("random-url"), + c.String("profile-file"), c.String("title-format"), c.Args(), }, diff --git a/resources/gotty.js b/resources/gotty.js index 915f89c..bdca523 100644 --- a/resources/gotty.js +++ b/resources/gotty.js @@ -7,7 +7,8 @@ var term; ws.onopen = function(event) { - hterm.defaultStorage = new lib.Storage.Local() + hterm.defaultStorage = new lib.Storage.Local(); + hterm.defaultStorage.clear(); term = new hterm.Terminal(); @@ -46,6 +47,12 @@ case '1': term.setWindowTitle(data); break; + case '2': + preferences = JSON.parse(data); + Object.keys(preferences).forEach(function(key) { + term.getPrefs().set(key, preferences[key]); + }); + break; } }